Jump to content
Software FX Community

Problems using Annotations in the code behind


Shanez

Recommended Posts

I'm trying to figure out how to use the Annotations extension in the code behind for WPF.

My scenario is that my user would right click on a chart to access a Context Menu option to "Add an Annotation" and I would respond to the event and add a new annotation at the users mouse location. I have been reviewing other forum posts and API Reference documentation from the Software FX Resource Center, but up to this point I can only get Annotations to work by directly putting them in the XAML - and then only if I use the Canvas properties to set the location. Setting the AttachX and AttachY in XAML does not work either. (At least from my experience).

I am using the ChartFx for WPF Trial Download for my testing.

Here is what I have put together in the code behind, but doesn't work...

Annotations annots = new Annotations();chart1.Extensions.Add(annots);Rectangle rectangle = new Rectangle();rectangle.Fill = Brushes.Red;rectangle.Stroke = Brushes.Black;Annotations.SetAttachX(rectangle, 2);Annotations.SetAttachY(rectangle, 5);Annotations.SetAttachHeight(rectangle, 2);Annotations.SetAttachWidth(rectangle, 4);annots.Children.Add(rectangle);

I am using the sample application (PassingData) as my test project. I updated the CLRObjectPage page with these changes in the code behind. No luck.

Any ideas?

Link to comment
Share on other sites

 Hi,

 

Try to set the heigth and width property to the rectangle.

 

  Annotations annot = new Annotations();
  chart1.Extensions.Add(annot);

  Rectangle rectangle = new Rectangle();
  rectangle.Width = 20;
  rectangle.Height = 20;

  rectangle.Fill = new LinearGradientBrush(Color.FromRgb(0, 255, 0),Color.FromRgb(0, 255, 0),45.00);
  rectangle.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));

  Annotations.SetAttachX(rectangle, 2);
  Annotations.SetAttachY(rectangle, 40);
  annot.Children.Add(rectangle);

 

This worked for me.

Link to comment
Share on other sites

I tested your code "as is" and it worked for me. Please note that if you use AttachWidth or AttachHeight, the dimensions of these properties are expressed in axis units, if your Y axis goes from 0 to 1 million and you use AttachHeight 2, we will ask the axis to translate 2 into pixels and that might result in a very very small rectangle.

When I tested your code (using AttachWidth and AttachHeight) on a chart with 12 points and a Y axis from 0 to 100, I can see the red rectangle.

Note that the same applies to AttachX and AttachY, they will be converted from axis units to pixels.

Regards,

JuanC

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...