Skip to content

Commit

Permalink
automatic tooltips strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
beto-rodriguez committed Jun 1, 2021
1 parent 7e0e35c commit 3852a4f
Show file tree
Hide file tree
Showing 28 changed files with 126 additions and 89 deletions.
14 changes: 14 additions & 0 deletions docs/1.overview/1.10.tooltips.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="text-right edit-article">
<a class="btn btn-light" href="https://github.com/beto-rodriguez/LiveCharts2/blob/master/docs/1.overview/1.8.series.md">
<div class="d-flex flex-row align-items-center">
<div class="me-3">
<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="35px" viewBox="0 0 24 24" width="35px" fill="#404040">
<rect fill="none" height="24" width="24" /><path d="M3,10h11v2H3V10z M3,8h11V6H3V8z M3,16h7v-2H3V16z M18.01,12.87l0.71-0.71c0.39-0.39,1.02-0.39,1.41,0l0.71,0.71 c0.39,0.39,0.39,1.02,0,1.41l-0.71,0.71L18.01,12.87z M17.3,13.58l-5.3,5.3V21h2.12l5.3-5.3L17.3,13.58z" /></svg>
</div>
<span>Edit this article</span>
</div>
</a>
</div>

# Tooltips

File renamed without changes.
File renamed without changes.
Empty file removed docs/1.overview/1.12.tooltips.md
Empty file.
19 changes: 18 additions & 1 deletion src/LiveChartsCore/CartesianChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,24 @@ public override void Update(ChartUpdateParams? chartUpdateParams = null)
/// <returns></returns>
public override IEnumerable<TooltipPoint> FindPointsNearTo(PointF pointerPosition)
{
return _chartView.Series.SelectMany(series => series.FindPointsNearTo(this, pointerPosition));
var actualStrategy = TooltipFindingStrategy;
if (TooltipFindingStrategy == TooltipFindingStrategy.Automatic)
{
var areAllX = true;
var areAllY = true;

foreach (var series in Series)
{
areAllX = areAllX && (series.SeriesProperties & SeriesProperties.PrefersXStrategyTooltips) != 0;
areAllY = areAllY && (series.SeriesProperties & SeriesProperties.PrefersYStrategyTooltips) != 0;
}

actualStrategy = areAllX
? TooltipFindingStrategy.CompareOnlyX
: (areAllY ? TooltipFindingStrategy.CompareOnlyY : TooltipFindingStrategy.CompareAll);
}

return _chartView.Series.SelectMany(series => series.FindPointsNearTo(this, pointerPosition, actualStrategy));
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/LiveChartsCore/ColumnSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public abstract class ColumnSeries<TModel, TVisual, TLabel, TDrawingContext> : B
/// Initializes a new instance of the <see cref="ColumnSeries{TModel, TVisual, TLabel, TDrawingContext}"/> class.
/// </summary>
public ColumnSeries()
: base(SeriesProperties.Bar | SeriesProperties.PrimaryAxisVerticalOrientation | SeriesProperties.Solid)
: base(
SeriesProperties.Bar | SeriesProperties.PrimaryAxisVerticalOrientation |
SeriesProperties.Solid | SeriesProperties.PrefersXStrategyTooltips)
{
DataPadding = new PointF(0, 1);
}
Expand Down
2 changes: 2 additions & 0 deletions src/LiveChartsCore/Drawing/MotionCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public void DrawFrame(TDrawingContext context)

foreach (var geometry in task.GetGeometries(this))
{
if (geometry == null) continue;

geometry.IsValid = true;
geometry.CurrentTime = frameTime;
if (!task.IsPaused) geometry.Draw(context);
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/ISeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ public interface ISeries : IDisposable
/// </summary>
/// <param name="chart">the chart</param>
/// <param name="pointerPosition">the pointer position</param>
/// <param name="automaticStategy">the already resolved strategy when strategy is set to automatic.</param>
/// <returns></returns>
IEnumerable<TooltipPoint> FindPointsNearTo(IChart chart, PointF pointerPosition);
IEnumerable<TooltipPoint> FindPointsNearTo(IChart chart, PointF pointerPosition, TooltipFindingStrategy automaticStategy);

/// <summary>
/// Marks a given point as a given state.
Expand Down
9 changes: 9 additions & 0 deletions src/LiveChartsCore/Kernel/ICartesianChartView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public interface ICartesianChartView<TDrawingContext> : IChartView<TDrawingConte
/// </value>
ZoomAndPanMode ZoomMode { get; set; }


/// <summary>
/// Gets or sets the tool tip finding strategy.
/// </summary>
/// <value>
/// The tool tip finding strategy.
/// </value>
TooltipFindingStrategy TooltipFindingStrategy { get; set; }

/// <summary>
/// Gets or sets the zooming speed from 0 to 1, where 0 is the fastest and 1 the slowest.
/// </summary>
Expand Down
8 changes: 0 additions & 8 deletions src/LiveChartsCore/Kernel/IChartView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ public interface IChartView
/// The tooltip position.
/// </value>
TooltipPosition TooltipPosition { get; set; }

/// <summary>
/// Gets or sets the tooltip finding strategy.
/// </summary>
/// <value>
/// The tooltip finding strategy.
/// </value>
TooltipFindingStrategy TooltipFindingStrategy { get; set; }
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/LiveChartsCore/Kernel/LiveChartsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public class LiveChartsSettings
/// <value>
/// The default tooltip finding strategy.
/// </value>
public TooltipFindingStrategy DefaultTooltipFindingStrategy { get; set; } = TooltipFindingStrategy.CompareOnlyX;
public TooltipFindingStrategy DefaultTooltipFindingStrategy { get; set; } = TooltipFindingStrategy.Automatic;

/// <summary>
/// Gets the theme identifier.
Expand Down
7 changes: 5 additions & 2 deletions src/LiveChartsCore/Kernel/RectangleHoverArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

using LiveChartsCore.Measure;
using System;
using System.Diagnostics;
using System.Drawing;

namespace LiveChartsCore.Kernel
Expand Down Expand Up @@ -88,12 +89,14 @@ public override float GetDistanceToPoint(PointF point, TooltipFindingStrategy st
var dx = point.X - (X + Width * 0.5f);
var dy = point.Y - (Y + Height * 0.5f);

//Trace.WriteLine($"{Y:N2} {point.Y:N2}");

return strategy switch
{
TooltipFindingStrategy.CompareAll => (float)Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dx, 2)),
TooltipFindingStrategy.CompareAll => (float)Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)),
TooltipFindingStrategy.CompareOnlyX => Math.Abs(dx),
TooltipFindingStrategy.CompareOnlyY => Math.Abs(dy),
_ => throw new Exception($"The strategy {strategy} is not supported."),
TooltipFindingStrategy.Automatic or _ => throw new Exception($"The strategy {strategy} is not supported.")
};
}

Expand Down
17 changes: 16 additions & 1 deletion src/LiveChartsCore/Kernel/SeriesProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ public enum SeriesProperties
/// <summary>
/// The solid
/// </summary>
Solid = 1 << 13
Solid = 1 << 13,

/// <summary>
/// The prefers x tool tips
/// </summary>
PrefersXStrategyTooltips = 1 << 14,

/// <summary>
/// The prefers y tool tips
/// </summary>
PrefersYStrategyTooltips = 1 << 15,

/// <summary>
/// The prefers xy tool tips
/// </summary>
PrefersXYStrategyTooltips = 1 << 16
}
}
4 changes: 3 additions & 1 deletion src/LiveChartsCore/LineSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public class LineSeries<TModel, TVisual, TLabel, TDrawingContext, TPathGeometry,
/// </summary>
/// <param name="isStacked">if set to <c>true</c> [is stacked].</param>
public LineSeries(bool isStacked = false)
: base(SeriesProperties.Line | SeriesProperties.PrimaryAxisVerticalOrientation | (isStacked ? SeriesProperties.Stacked : 0) | SeriesProperties.Sketch)
: base(
SeriesProperties.Line | SeriesProperties.PrimaryAxisVerticalOrientation |
(isStacked ? SeriesProperties.Stacked : 0) | SeriesProperties.Sketch | SeriesProperties.PrefersXStrategyTooltips)
{
DataPadding = new PointF(0.5f, 1f);
HoverState = LiveCharts.LineSeriesHoverKey;
Expand Down
7 changes: 6 additions & 1 deletion src/LiveChartsCore/Measure/TooltipFindingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
namespace LiveChartsCore.Measure
{
/// <summary>
/// Defines the tooltip finding strategy.
/// Defines the tool tip finding strategy.
/// </summary>
public enum TooltipFindingStrategy
{
/// <summary>
/// The automatic mode, it will be calculated based on the series in the chart.
/// </summary>
Automatic,

/// <summary>
/// Compares X and Y coordinates.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/LiveChartsCore/PieChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ public PieChart(
/// <returns></returns>
public override IEnumerable<TooltipPoint> FindPointsNearTo(PointF pointerPosition)
{
return _chartView.Series.SelectMany(series => series.FindPointsNearTo(this, pointerPosition));
return _chartView.Series.SelectMany(
series => series.FindPointsNearTo(this, pointerPosition, TooltipFindingStrategy.CompareAll));
}

/// <inheritdoc cref="IChart.Update(ChartUpdateParams?)" />
Expand Down Expand Up @@ -175,7 +176,6 @@ protected override void Measure()
legend = _chartView.Legend;

tooltipPosition = _chartView.TooltipPosition;
tooltipFindingStrategy = _chartView.TooltipFindingStrategy;
tooltip = _chartView.Tooltip;

animationsSpeed = _chartView.AnimationsSpeed;
Expand Down
10 changes: 6 additions & 4 deletions src/LiveChartsCore/RowSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public class RowSeries<TModel, TVisual, TLabel, TDrawingContext> : BarSeries<TMo
/// Initializes a new instance of the <see cref="RowSeries{TModel, TVisual, TLabel, TDrawingContext}"/> class.
/// </summary>
public RowSeries()
: base(SeriesProperties.Bar | SeriesProperties.PrimaryAxisHorizontalOrientation | SeriesProperties.Solid) { }
: base(
SeriesProperties.Bar | SeriesProperties.PrimaryAxisHorizontalOrientation
| SeriesProperties.Solid | SeriesProperties.PrefersYStrategyTooltips) { }

/// <inheritdoc cref="CartesianSeries{TModel, TVisual, TLabel, TDrawingContext}.Measure"/>
public override void Measure(
Expand All @@ -59,7 +61,7 @@ public override void Measure(
primaryAxis.PreviousDataBounds == null ? null : new Scaler(drawLocation, drawMarginSize, primaryAxis);
var primaryScale = new Scaler(drawLocation, drawMarginSize, secondaryAxis);

var uw = secondaryScale.ToPixels((float)primaryAxis.UnitWidth) - secondaryScale.ToPixels(0f);
var uw = secondaryScale.ToPixels(0f) - secondaryScale.ToPixels((float)primaryAxis.UnitWidth);
var uwm = 0.5f * uw;

uw -= (float)GroupPadding;
Expand All @@ -81,7 +83,7 @@ public override void Measure(

if (uw > MaxBarWidth)
{
uw = (float)MaxBarWidth * -1;
uw = (float)MaxBarWidth;
uwm = uw / 2f;
}

Expand Down Expand Up @@ -171,7 +173,7 @@ public override void Measure(
sizedGeometry.Ry = ry;
sizedGeometry.RemoveOnCompleted = false;

point.Context.HoverArea = new RectangleHoverArea().SetDimensions(primary, secondary - uwm + cp, b, uw);
point.Context.HoverArea = new RectangleHoverArea().SetDimensions(cx, y, b, uw);

OnPointMeasured(point);
_ = toDeletePoints.Remove(point);
Expand Down
2 changes: 1 addition & 1 deletion src/LiveChartsCore/ScatterSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ScatterSeries<TModel, TVisual, TLabel, TDrawingContext> : Cartesian
/// Initializes a new instance of the <see cref="ScatterSeries{TModel, TVisual, TLabel, TDrawingContext}"/> class.
/// </summary>
public ScatterSeries()
: base(SeriesProperties.Scatter | SeriesProperties.Solid)
: base(SeriesProperties.Scatter | SeriesProperties.Solid | SeriesProperties.PrefersXYStrategyTooltips)
{
DataPadding = new PointF(1, 1);

Expand Down
16 changes: 10 additions & 6 deletions src/LiveChartsCore/Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Diagnostics;

namespace LiveChartsCore
{
Expand Down Expand Up @@ -223,9 +224,9 @@ IEnumerable<ChartPoint> ISeries.Fetch(IChart chart)
return Fetch(chart);
}

IEnumerable<TooltipPoint> ISeries.FindPointsNearTo(IChart chart, PointF pointerPosition)
IEnumerable<TooltipPoint> ISeries.FindPointsNearTo(IChart chart, PointF pointerPosition, TooltipFindingStrategy automaticStategy)
{
return FilterTooltipPoints(Fetch(chart), chart, pointerPosition);
return FilterTooltipPoints(Fetch(chart), chart, pointerPosition, automaticStategy);
}

/// <inheritdoc />
Expand Down Expand Up @@ -383,7 +384,7 @@ protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
}

private IEnumerable<TooltipPoint> FilterTooltipPoints(
IEnumerable<ChartPoint>? points, IChart chart, PointF pointerPosition)
IEnumerable<ChartPoint>? points, IChart chart, PointF pointerPosition, TooltipFindingStrategy automaticStategy)
{
if (points == null) return Enumerable.Empty<TooltipPoint>();
var tolerance = float.MaxValue;
Expand All @@ -406,11 +407,12 @@ private IEnumerable<TooltipPoint> FilterTooltipPoints(
tolerance = (float)Math.Sqrt(Math.Pow(uwx, 2) + Math.Pow(uwy, 2));
break;
case TooltipFindingStrategy.CompareOnlyX:
tolerance = uwx;
tolerance = Math.Abs(uwx);
break;
case TooltipFindingStrategy.CompareOnlyY:
tolerance = uwy;
tolerance = Math.Abs(uwy);
break;
case TooltipFindingStrategy.Automatic:
default:
break;
}
Expand All @@ -421,7 +423,7 @@ private IEnumerable<TooltipPoint> FilterTooltipPoints(
foreach (var point in points)
{
if (point == null || point.Context.HoverArea == null) continue;
var d = point.Context.HoverArea.GetDistanceToPoint(pointerPosition, chart.TooltipFindingStrategy);
var d = point.Context.HoverArea.GetDistanceToPoint(pointerPosition, automaticStategy); //chart.TooltipFindingStrategy
if (d > tolerance || d > minD.Item1) continue;

if (minD.Item1 == d)
Expand All @@ -434,6 +436,8 @@ private IEnumerable<TooltipPoint> FilterTooltipPoints(
minD = new Tuple<float, List<TooltipPoint>>(d, new List<TooltipPoint> { new TooltipPoint(this, point) });
}

Trace.WriteLine(minD.Item2.Count);

return minD.Item2;
}

Expand Down
4 changes: 3 additions & 1 deletion src/LiveChartsCore/StackedColumnSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public class StackedColumnSeries<TModel, TVisual, TLabel, TDrawingContext> : Sta
/// Initializes a new instance of the <see cref="StackedColumnSeries{TModel, TVisual, TLabel, TDrawingContext}"/> class.
/// </summary>
public StackedColumnSeries()
: base(SeriesProperties.Bar | SeriesProperties.PrimaryAxisVerticalOrientation | SeriesProperties.Stacked | SeriesProperties.Solid)
: base(
SeriesProperties.Bar | SeriesProperties.PrimaryAxisVerticalOrientation | SeriesProperties.Stacked |
SeriesProperties.Solid | SeriesProperties.PrefersXStrategyTooltips)
{
DataPadding = new PointF(0, 1);
}
Expand Down
4 changes: 3 additions & 1 deletion src/LiveChartsCore/StackedRowSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public class StackedRowSeries<TModel, TVisual, TLabel, TDrawingContext> : Stacke
/// Initializes a new instance of the <see cref="StackedRowSeries{TModel, TVisual, TLabel, TDrawingContext}"/> class.
/// </summary>
public StackedRowSeries()
: base(SeriesProperties.Bar | SeriesProperties.PrimaryAxisHorizontalOrientation | SeriesProperties.Stacked | SeriesProperties.Solid)
: base(
SeriesProperties.Bar | SeriesProperties.PrimaryAxisHorizontalOrientation | SeriesProperties.Stacked |
SeriesProperties.Solid | SeriesProperties.PrefersXStrategyTooltips)
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public TooltipPosition TooltipPosition
set => SetValue(TooltipPositionProperty, value);
}

/// <inheritdoc cref="IChartView.TooltipFindingStrategy" />
/// <inheritdoc cref="ICartesianChartView{TDrawingContext}.TooltipFindingStrategy" />
public TooltipFindingStrategy TooltipFindingStrategy
{
get => (TooltipFindingStrategy)GetValue(TooltipFindingStrategyProperty);
Expand Down
14 changes: 0 additions & 14 deletions src/skiasharp/LiveChartsCore.SkiaSharp.Avalonia/PieChart.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,6 @@ public PieChart()
AvaloniaProperty.Register<CartesianChart, TooltipPosition>(
nameof(TooltipPosition), LiveCharts.CurrentSettings.DefaultTooltipPosition, inherits: true);

/// <summary>
/// The tool tip finding strategy property
/// </summary>
public static readonly AvaloniaProperty<TooltipFindingStrategy> TooltipFindingStrategyProperty =
AvaloniaProperty.Register<CartesianChart, TooltipFindingStrategy>(
nameof(LegendPosition), LiveCharts.CurrentSettings.DefaultTooltipFindingStrategy, inherits: true);

/// <summary>
/// The tool tip font family property
/// </summary>
Expand Down Expand Up @@ -375,13 +368,6 @@ public TooltipPosition TooltipPosition
set => SetValue(TooltipPositionProperty, value);
}

/// <inheritdoc cref="IChartView.TooltipFindingStrategy" />
public TooltipFindingStrategy TooltipFindingStrategy
{
get => (TooltipFindingStrategy)GetValue(TooltipFindingStrategyProperty);
set => SetValue(TooltipFindingStrategyProperty, value);
}

/// <summary>
/// Gets or sets the tool data tip template.
/// </summary>
Expand Down
Loading

0 comments on commit 3852a4f

Please sign in to comment.