Pre loader

CategoryDateTimeAxis in MVVM

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

0
0

I have a WPF app using the MVVM pattern. I am using an XY line chart with date-based data. I was previously using a DateTimeAxis to display the data and had the VisibleRange property bound to a DateRange property in my view model. The app has a feature where the user can click buttons to quickly select a visible range i.e. 1 day, 1 month, 6 months, etc. I was just setting the value of the visible range property in my view model to update the chart. That was all working great.

But the chart is showing financial data, so I needed to switch over to the CategoryDateTimeAxis to remove gaps in the data. Now my VisibleRange binding no longer works. I get an error saying “Object must be of type DateTime”. I have read other posts and understand that this is because the CategoryDateTimeAxis uses an IndexRange instead of a DateRange for its VisibleRange.

My question is: is there a way for me to still set the desired visible range using date values in my view model and have it converted to an IndexRange so the chart will show the correct data?

Thanks

  • You must to post comments
0
0

Hi there,

There is, but it’s a little bit tricky in MVVM. You need to use the API to convert between pixel and data coordinates on an Axis.

Here’s an excerpt from our docs:

// Get coordinate from CategoryDateTimeAxis
CategoryDateTimeAxis catAxis = new CategoryDateTimeAxis();
ICategoryCoordinateCalculator coordCalc = catAxis.GetCurrentCoordinateCalculator() as ICategoryCoordinateCalculator;
int index = coordCalc.TransformDataToIndex(new DateTime(2015, 01, 01));
double pixelCoord = coordCalc.GetCoordinate(index);

Specifically, take a look at the method TransformdataToIndex. This, well, transforms an DateTime value to Index. There is also an inverse method, TransformIndexToData.

Getting this into the viewmodel is the hard part as you need an axis instance. I would suggest instead either (easy) handling the click events to change time-scale in code-behind, or, (slightly harder) pass the SciChartSurface.XAxis as a command parameter in your button command to ViewModel.

Hope this helps,

Best regards,
Andrew

  • You must to post comments
0
0

I am passing the axis as a command parameter to my view model and that is working. Thanks!

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies