Jump to content
Software FX Community

Making a crosshair on the chart?


Recommended Posts

When the user moves the mouse over the chart I would like a crosshair to appear and track with the mouse. (vertical and horizonal line that intersect at the current mouse location) This should only be on the chart portion and not on the legend or titles etc..

I assume I would use annotation's to accomplish this? Do you have a sample of this anywhere?


Link to comment
Share on other sites

Nevermind got it working.

Heres what I did:

In the XAML I added two annotations for the horizontal and vertical lines. (note the binding, to span to the size of the container)


<cfxAnnotation:Annotations x:Name="annotations">

<Line x:Name="lineH" Visibility="Visible" Fill="Black" Stroke="Black" X1="0" X2="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}},Path=Width}" />

<Line x:Name="lineV" Visibility="Visible" Fill="Black" Stroke="Black" Y1="0" Y2="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}},Path=Height}" />



C# Code

private void OnChartMouseMove(object sender, MouseEventArgs e)


HitTestResults hitTest = chart1.HitTest(e.GetPosition(chart1));

if (!hitTest.IsInsidePlotArea) return;

Axis axisX = chart1.AxisX;

Axis axisY = chart1.AxisY;

FrameworkElement parent = lineH.Parent as FrameworkElement;

lineH.Y1 = e.GetPosition(parent).Y;

lineH.Y2 = e.GetPosition(parent).Y;

lineV.X1 = e.GetPosition(parent).X;

lineV.X2 = e.GetPosition(parent).X;


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.

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...