Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Data Display - WPF - Need to add text to canvas - C#

I am using the dynamic data display WPF chart. I have a requirement to display a label next to every point on the curves plotted on the chart.

The exact functionality is as follows:

  1. Every curve has a an object that holds its data and a description that inculdes color, marker shape etc. It also tell me whether the labels must be visible for that particular curve.

  2. There is also an option using a checkbox to hide/show the labels for all points on all the curves on the plot.

  3. There is a third option where a user can left click on the marker and see a label next to it.

Now, I previously implemented it by adding labels along with the ElementMarkerPointGraph for each point and setting the visibility of the labels. I know there is a massive performance hit with this approach.

I am now looking to create a solution where I can render text directly to the canvas at a location that I provide. I also need help with the removing the text from the canvas.

Is there a way of adding text natively to the canvas? What is the most efficient way to do so?

EDIT: I need to move the text around as the plotter zooms. I already know when the plotter zooms, I need to be able to move the text to the appropriate location.

like image 887
Harsha Avatar asked Nov 14 '12 06:11

Harsha


2 Answers

I'm not sure whether this will give you the zooming purpose but the code below can be used to add text inside a canvas..I got it from a site while googling.

private void Text(double x, double y, string text, Color color) 
{
    TextBlock textBlock = new TextBlock();
    textBlock.Text = text;
    textBlock.Foreground = new SolidColorBrush(color);
    Canvas.SetLeft(textBlock, x);
    Canvas.SetTop(textBlock, y);
    canvasObj.Children.Add(textBlock);
}
like image 167
hridya pv Avatar answered Oct 21 '22 15:10

hridya pv


I figured it out myself. I'll be overriding the OnRender method to handle this. I can draw text using the drawing context.

like image 21
Harsha Avatar answered Oct 21 '22 16:10

Harsha