10

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.

Harsha
  • 661
  • 1
  • 8
  • 21
  • Hard to tell with no code. How do you display the curves? Could we have the piece of XAML that actually drw the curves ? – Kek Nov 14 '12 at 07:30
  • It uses the extension methods in Plotter2DExtensions class. You can have a look at the D3 source code on codeplex. Anyway, I have decided to use the OnRender method to achieve this. Thanks! [link]http://dynamicdatadisplay.codeplex.com – Harsha Nov 14 '12 at 08:46

3 Answers3

22

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);
}
Jason Higgins
  • 1,516
  • 1
  • 17
  • 37
hridya pv
  • 1,039
  • 1
  • 7
  • 17
  • Thanks! But I already knew to add textblock but I was looking to avoid using more controls. :) – Harsha Nov 14 '12 at 08:44
6

OK. My exact implementation can't be put up here. But I can provide some idea of how to do it.

So create a simple user control that derives from Canvas.

class CustomCanvas : Canvas
{
    protected override void OnRender(DrawingContext dc)
    {
        FormattedText someFormattedText = new FormattedText(someText, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                someTypeFace, someFontSize, someColor);
        dc.DrawText(someFormattedText, new Point(15, 15));
    }
}

You can seal the class, if you do not want it subclassed/overriden further.

That's about it. You can check out the other methods available with the drawing context to do some other stuff. :)

Harsha
  • 661
  • 1
  • 8
  • 21
2

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

Harsha
  • 661
  • 1
  • 8
  • 21