Skip to content

Commit

Permalink
add tool tip support for annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
iniceice88 committed May 15, 2023
1 parent 8bfed30 commit 39b6141
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,53 @@ public static PlotModel EllipseAnnotation()
var model = new PlotModel { Title = "EllipseAnnotation" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left });
model.Annotations.Add(new EllipseAnnotation { X = 20, Y = 60, Width = 20, Height = 15, Text = "EllipseAnnotation", TextRotation = 10, Fill = OxyColor.FromAColor(99, OxyColors.Green), Stroke = OxyColors.Black, StrokeThickness = 2 });
model.Annotations.Add(new EllipseAnnotation
{
X = 20,
Y = 60,
Width = 20,
Height = 15,
Text = "EllipseAnnotation",
ToolTip = "EllipseAnnotation",
TextRotation = 10,
Fill = OxyColor.FromAColor(99, OxyColors.Green),
Stroke = OxyColors.Black,
StrokeThickness = 2
});

model.Annotations.Add(new EllipseAnnotation { X = 20, Y = 20, Width = 20, Height = 20, Fill = OxyColor.FromAColor(99, OxyColors.Green), Stroke = OxyColors.Black, StrokeThickness = 2 });
model.Annotations.Add(new EllipseAnnotation { X = 30, Y = 20, Width = 20, Height = 20, Fill = OxyColor.FromAColor(99, OxyColors.Red), Stroke = OxyColors.Black, StrokeThickness = 2 });
model.Annotations.Add(new EllipseAnnotation { X = 25, Y = 30, Width = 20, Height = 20, Fill = OxyColor.FromAColor(99, OxyColors.Blue), Stroke = OxyColors.Black, StrokeThickness = 2 });
model.Annotations.Add(new EllipseAnnotation
{
X = 20,
Y = 20,
Width = 20,
Height = 20,
Fill = OxyColor.FromAColor(99, OxyColors.Green),
ToolTip = "Green",
Stroke = OxyColors.Black,
StrokeThickness = 2
});
model.Annotations.Add(new EllipseAnnotation
{
X = 30,
Y = 20,
Width = 20,
Height = 20,
Fill = OxyColor.FromAColor(99, OxyColors.Red),
ToolTip = "Red",
Stroke = OxyColors.Black,
StrokeThickness = 2
});
model.Annotations.Add(new EllipseAnnotation
{
X = 25,
Y = 30,
Width = 20,
Height = 20,
Fill = OxyColor.FromAColor(99, OxyColors.Blue),
ToolTip = "Blue",
Stroke = OxyColors.Black,
StrokeThickness = 2
});
return model;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Examples/ExampleLibrary/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Example(PlotModel model, IPlotController controller = null)
/// <value>
/// The controller.
/// </value>
public IPlotController Controller { get; private set; }
public IPlotController Controller { get; set; }

/// <summary>
/// Gets the model.
Expand Down
2 changes: 1 addition & 1 deletion Source/Examples/ExampleLibrary/ExampleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ExampleInfo
/// <summary>
/// The un-modified example.
/// </summary>
private Example Example;
public Example Example { get; private set; }

/// <summary>
/// The examples for this example info.
Expand Down
39 changes: 39 additions & 0 deletions Source/OxyPlot.Maui.Skia/Manipulators/TouchTrackerManipulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public TouchTrackerManipulator(IPlotView plotView)
/// <remarks>This parameter is ignored if <see cref="PointsOnly"/> is equal to <c>False</c>.</remarks>
public bool CheckDistanceBetweenPoints { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to track annotations.
/// </summary>
public bool IsTrackAnnotations { get; set; } = false;

/// <summary>
/// Occurs when a manipulation is complete.
/// </summary>
Expand Down Expand Up @@ -151,6 +156,10 @@ private void UpdateTracker(ScreenPoint position)
this.PlotView.HideTracker();
}

if (IsTrackAnnotations)
{
TrackAnnotations(position);
}
return;
}

Expand All @@ -174,5 +183,35 @@ private void UpdateTracker(ScreenPoint position)
actualModel.RaiseTrackerChanged(result);
}
}

/// <summary>
/// Track Annotations
/// </summary>
/// <param name="sp"></param>
private void TrackAnnotations(ScreenPoint sp)
{
foreach (var annotation in PlotView.ActualModel.Annotations
.Where(x => !string.IsNullOrEmpty(x.ToolTip))
.Reverse())
{
var args = new HitTestArguments(sp, FiresDistance);
var res = annotation.HitTest(args);

if (res == null)
continue;

var dp = annotation.InverseTransform(sp);
var result = new TrackerHitResult
{
Position = sp,
DataPoint = dp,
Text = annotation.ToolTip,
PlotModel = this.PlotView.ActualModel
};
this.PlotView.ShowTracker(result);
this.PlotView.ActualModel.RaiseTrackerChanged(result);
break;
}
}
}
}
35 changes: 35 additions & 0 deletions Source/OxyplotMauiSample/Pages/ExampleBrowser.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using ExampleLibrary;
using System.Collections.ObjectModel;
using System.Windows.Input;
using OxyPlot;
using OxyPlot.Maui.Skia;
using OxyPlot.Maui.Skia.Core;

namespace OxyplotMauiSample
{
Expand Down Expand Up @@ -29,14 +32,46 @@ private async void ListView_OnItemSelected(object sender, SelectedItemChangedEve
return;

var exampleInfo = e.SelectedItem as ExampleInfo;
if (exampleInfo == null)
return;

var page = new PlotViewPage
{
ExampleInfo = exampleInfo
};

// try add tracker support for annotations
if (exampleInfo.Tags.Contains("Annotations") && exampleInfo.PlotController == null)
{
exampleInfo.Example.Controller = CreateAnnotationTrackController();
}
await Navigation.PushAsync(page);

LvExamples.SelectedItem = null;
}

private IPlotController CreateAnnotationTrackController()
{
var controller = new OxyPlot.Maui.Skia.PlotController();
controller.UnbindTouchDown();

var snapTrackTouch = new DelegatePlotCommand<OxyTouchEventArgs>((view, c, args) =>
c.AddTouchManipulator(view, new OxyPlot.Maui.Skia.Manipulators.TouchTrackerManipulator(view)
{
Snap = true,
PointsOnly = true,
LockToInitialSeries = false,
IsTrackAnnotations = true
}, args));

var cmd = new CompositeDelegateViewCommand<OxyTouchEventArgs>(
snapTrackTouch,
OxyPlot.Maui.Skia.PlotCommands.PanZoomByTouch
);
controller.BindTouchDown(cmd);

return controller;
}
}

public class ExampleBrowserViewModel : ViewModelBase
Expand Down

0 comments on commit 39b6141

Please sign in to comment.