5ORBEX
5ORBEX

Reputation: 129

OxyPlot: Customize Tooltip

I use OxyPlot in my work to show some information. And I need to change the default tooltip which you can see after clicking on some point in the graph.

Currently, I have the test WPF window with a simple linear series. I've changed the template for the tooltip to display some text and button.

My controllers:

public class PlotViewTest : PlotView
{ }

public class TestTracker : TrackerControl
{
    public TestTracker()
    {
        CanCenterHorizontally = false;
        CanCenterVertically = false;
    }
}

My WPF window code:

<controlers:PlotViewTest Model="{Binding MyModel}">
    <controlers:PlotViewTest.DefaultTrackerTemplate>
        <ControlTemplate>
            <controlers:TestTracker Position="{Binding Position}">
                <Grid Margin="15">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <TextBlock Margin="5" Text="Hello world!"/>
                    <Button Grid.Row="1" Margin="5" Content="Start"/>
                </Grid>
            </controlers:TestTracker>
        </ControlTemplate>
    </controlers:PlotViewTest.DefaultTrackerTemplate>
</controlers:PlotViewTest>

My WPF window:

TestWindow.png

But there are a few behaviors that I want to change.

  1. I can see the tooltip only when I hold down the left mouse button. When I up it the tooltip will be hidden. But I need to show tooltip always after 1 click of left mouse button on red point. When I click on the second red point another tooltip will be shown. But when I click in some empty space tooltip should be hidden.
  2. The tooltip also can be shown when I hold down the left mouse button on the blue line. But I want to prevent it. It should be shown only when I click on red points.

How can change these two behaviors??

Upvotes: 0

Views: 2180

Answers (1)

Anu Viswan
Anu Viswan

Reputation: 18153

You could achieve the objectives by writing a Custom TrackerManipulator which overrides the Completed action of the Tracker. For example

public class StaysOpenTrackerManipulator : TrackerManipulator
{
        public StaysOpenTrackerManipulator(IPlotView plotView) : base(plotView)
        {
            Snap = true;
            PointsOnly = true;
        }
        public override void Completed(OxyMouseEventArgs e)
        {
            // Do nothing
        }
}

By setting Snap and PointsOnly properties to true, you ensure that the Tracker is opened only when selecting the points and not elsewhere (line/outside).

You could bind the Custom TrackerManipulator to PlotView using a PlotController.

// Property
public PlotController CustomPlotController { get; set; }

// Assign Value for CustomPlotController 
var customController = new PlotController();
customController.UnbindAll();
customController.BindMouseDown(OxyMouseButton.Left, new DelegatePlotCommand<OxyMouseDownEventArgs>((view, controller, args) =>
                controller.AddMouseManipulator(view, new StaysOpenTrackerManipulator(view), args)));
CustomPlotController = customController;

And in Xaml

<controlers:PlotViewTest Model="{Binding MyModel}" Controller="{Binding CustomPlotController}">

Upvotes: 3

Related Questions