Hello! I'm evaluating xamDataChart for my company and have one question.
How to get 'real' coordinates of the point in MouseLeftButtonDown?
If I write something like
e.GetPosition(this.xmChart)
(as shown in sample project) I get double coordinates assosiated with current WindowRect. But I have DateTime X axis and want to get DateTime closest to the point I have clicked.
Is there a solution for my problem?
Thanks, Igor.
This sample should show you the pieces you can use to put this together:
The xaml:
<UserControl.Resources> <local:TestData x:Key="data" /> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <igChart:XamDataChart x:Name="theChart" MouseLeftButtonDown="theChart_MouseLeftButtonDown"> <igChart:XamDataChart.Axes> <igChart:NumericYAxis x:Name="yAxis" /> <igChart:CategoryDateTimeXAxis x:Name="xAxis" ItemsSource="{StaticResource data}" DateTimeMemberPath="Date" Label="{}{Date}"/> </igChart:XamDataChart.Axes> <igChart:XamDataChart.Series> <igChart:LineSeries x:Name="series" ItemsSource="{StaticResource data}" XAxis="{Binding ElementName=xAxis}" YAxis="{Binding ElementName=yAxis}" ValueMemberPath="Value" /> </igChart:XamDataChart.Series> </igChart:XamDataChart> </Grid>
And the code behind:
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void theChart_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var s = theChart.Series.FirstOrDefault(); if (s == null) { return; } var position = e.GetPosition(s); //we are only accounting for CategoryDateTimeXAxis in the following logic. if (!s.Chart.Axes.OfType<CategoryDateTimeXAxis>().Any()) { return; } var x = s.Chart.Axes.OfType<CategoryDateTimeXAxis>().First(); var y = s.Chart.Axes.OfType<NumericYAxis>().First(); var viewport = new Rect(0, 0, x.ActualWidth, y.ActualHeight); var window = s.Chart.WindowRect; var unscaledX = x.GetUnscaledValue(position.X, window, viewport); var unscaledY = y.GetUnscaledValue(position.Y, window, viewport); MessageBox.Show("x: " + new DateTime((long)unscaledX) + ", y:" + unscaledY); } } public class TestData : ObservableCollection<TestDataItem> { public TestData() { Add(new TestDataItem() { Date = DateTime.Now.AddDays(-4), Value = 1 }); Add(new TestDataItem() { Date = DateTime.Now.AddDays(-3), Value = 2 }); Add(new TestDataItem() { Date = DateTime.Now.AddDays(-2), Value = 3 }); Add(new TestDataItem() { Date = DateTime.Now.AddDays(-1), Value = 4 }); } } public class TestDataItem { public DateTime Date { get; set; } public double Value { get; set; } }
Let me know if you have any questions. Hope this helps!
-Graham
Thank you, that's what i wanted.
How can I do it for the CategoryXAxis?
Hi,
The approach would be roughly the same, but change any references to CategoryDateTimeXAxis to CategoryXAxis.
What you would get from GetUnscaledValue would be the index into the itemsSource of the item closest to where you clicked. So if you want to display its category label you would have to use that index to get the item out of the collection and grab the property value that is bound to the labels.