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

Harsha picture Harsha · Nov 14, 2012 · Viewed 21.3k times · Source

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.

Answer

hridya pv picture hridya pv · Nov 14, 2012

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);
}