Pre loader

How can I get mouse cursor position (Coordinates) from a SciChartSurface 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

1
1

Hi all,

As the title indicates, I am trying to get mouse cursor position (Coordinates) from a SciChartSurface in MVVM manner.

I have lots of data bindings to my chart properties in my View Model in the background. This following snipped is a copy of my XAML code in which I have all the data bindings set:

<sci:SciChartSurface Grid.Row="0" Grid.Column="0"
                         RenderableSeries="{Binding SciChartSeriesViewModels}"     
                         Padding="0,8,0,2"                                         
                         sci:ThemeManager.Theme="BrightSpark"                                       
                         YAxes="{Binding SciChartYAxesCollection,Mode=TwoWay}"                                         
                         AutoRangeOnStartup="True"                                       
                         Annotations="{Binding ChartAnnotations}"                                     
                         Name="ApplicationSciChart"                                       
                         ChartModifier ="{Binding ChartModifierGroup}"
                         BorderBrush="LightSlateGray"
                         BorderThickness="1"
                         Visibility="{Binding ChartVisibility}">

        <sci:SciChartSurface.XAxis>
            <sci:TimeSpanAxis AxisTitle="Time"
                              AutoRange="Once" GrowBy="0.03,0.001"
                              TextFormatting=""/>
        </sci:SciChartSurface.XAxis>

  </sci:SciChartSurface>

As can be seen from the code above, the ChartModifier property is one of the items that has Data binding to an instance of a ModiferGroup I have created in my View-Model in the background. The following code is a method that adds members to that ModiferGroup (called ChartModiferGroup in my View-Model):

 private void CreateChartModifiers()
    {
        var rubberBandXyZoomModifier = new RubberBandXyZoomModifier
        {
            ExecuteOn = SciChart.Charting.ChartModifiers.ExecuteOn.MouseLeftButton,
            RubberBandFill = (SolidColorBrush)(new BrushConverter().ConvertFrom("#33FFFFFF")),
            RubberBandStroke = Brushes.SteelBlue, 
            RubberBandStrokeDashArray = new DoubleCollection {2.0, 2.0}
        };

        var zoomPanModifier = new ZoomPanModifier
        {
            ExecuteOn = SciChart.Charting.ChartModifiers.ExecuteOn.MouseMiddleButton,
            ClipModeX = SciChart.Charting.ClipMode.None
        };

        var yAxisDragModifier = new YAxisDragModifier
        {
            DragMode = SciChart.Charting.AxisDragModes.Scale
        };

        var xAxisDragModifier = new XAxisDragModifier
        {
            DragMode = SciChart.Charting.AxisDragModes.Pan
        };

        var mouseWheelZoomModifier = new MouseWheelZoomModifier();

        var zoomExtentsModifier = new ZoomExtentsModifier
        {
            ExecuteOn = SciChart.Charting.ChartModifiers.ExecuteOn.MouseDoubleClick
        };

        var cursorModifier = new CursorModifier
        {
            ReceiveHandledEvents = true,
            ShowAxisLabels = false
        };


        var probeline = new VerticalLineAnnotation()
        {
            Stroke = Brushes.LightSlateGray,
            IsEditable = true,
            ShowLabel = true,
            LabelPlacement = LabelPlacement.Axis,
            Visibility = Visibility.Visible,
            IsHidden = false,
            IsEnabled = true,
            X1 = TimeSpan.FromMilliseconds(-100),
            YAxisId = "Triggered",
            StrokeDashArray = new DoubleCollection { 2.0,2.0},
            StrokeThickness = 1
        };

        var verticalSliceModifier = new VerticalSliceModifier
        {
            IsEnabled = true,
            ShowAxisLabels = true,
            Style = probeline.Style
        };
        verticalSliceModifier.VerticalLines.Add(probeline);


        ChartModifierGroup.ChildModifiers.Add(rubberBandXyZoomModifier);
        ChartModifierGroup.ChildModifiers.Add(rubberBandXyZoomModifier);
        ChartModifierGroup.ChildModifiers.Add(zoomPanModifier);
        ChartModifierGroup.ChildModifiers.Add(yAxisDragModifier);
        ChartModifierGroup.ChildModifiers.Add(xAxisDragModifier);
        ChartModifierGroup.ChildModifiers.Add(mouseWheelZoomModifier);
        ChartModifierGroup.ChildModifiers.Add(zoomExtentsModifier);
        ChartModifierGroup.ChildModifiers.Add(verticalSliceModifier);
        ChartModifierGroup.ChildModifiers.Add(cursorModifier);
    }

What I am trying to do next, is to add functionality in my application, such that I can add more VerticalSliceModifiers to my ChartModifierGroup when a button is pressed. In order to do that, I will require having access to the Mouse cursor position of my SciChartSurface to set the X1 property of the VerticalLineAnnotation, that would be used for the VerticalSliceModifiers (to be created).

In simple words, every time I press a button, I need the position of the mouse pointer to get captured and used as the location of the new VerticalSliceModifer that I am going to create.

I have already read many of the examples and documentation of Sci-Chart such as: Vertical Slice Tooltips Example. However, in this example, the mouse pointer location is accessed is under the xaml.cs and not inside an actual View-Model.

I was wondering if anybody could let me know how I can get access to my SciChartSurface mouse pointer position using true MVVM model.

Version
4
  • Marcus Krauße
    I would need this too. Is there any solution for that right now?
  • Andrew Burnett-Thompson
    I suggested below using ChartModifier API, overriding ChartModifierBase.OnModifierMouseDown/Move/Up and simply passing to the viewmodel.
  • You must to post comments
0
0

Hi there, funny we didn’t see this or forgot about it.

The API we have designed to interact directly with the chart is the ChartModifier API.

This allows you to create classes which inherit from ChartModifierBase and override methods OnModifierMouseDown, OnModifierMouseMove, OnModifierMouseUp, to interact directly with the chart, its series, annotations, properties and settings.

What I would suggest you do, is create a class which inherits ChartModifierBase, override OnModifierMouseDown and simply pass this coordinate to your Viewmodel.

Some tutorials / examples on custom chart modifiers can be found here:

Best regards
Andrew

  • You must to post comments
Showing 1 result
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