Skip to content

Commit

Permalink
legend docs
Browse files Browse the repository at this point in the history
  • Loading branch information
beto-rodriguez committed Jun 3, 2021
1 parent 2d39ba1 commit ba07209
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 95 deletions.
188 changes: 94 additions & 94 deletions docs/1.overview/1.10.tooltips.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,128 +214,128 @@ If you need to customize more, you can also use the create your own template:
You can create your own tooltip control, the key is that your control must implement `IChartTooltip<SkiaSharpDrawingContext>` and then
you have to create a new instance of that control when your chart initializes.

Add a new form to your app, then change the code as follows:
Add a new form to your app named `CustomTooltip`, then change the code as follows:

```
public partial class CustomTooltip : Form, IChartTooltip<SkiaSharpDrawingContext>, IDisposable
{
private readonly Dictionary<ChartPoint, object> activePoints = new();
public CustomTooltip()
{
private readonly Dictionary<ChartPoint, object> activePoints = new();
InitializeComponent();
}
public CustomTooltip()
{
InitializeComponent();
}
public void Show(IEnumerable<TooltipPoint> tooltipPoints, Chart<SkiaSharpDrawingContext> chart)
{
var wfChart = (Chart)chart.View;
public void Show(IEnumerable<TooltipPoint> tooltipPoints, Chart<SkiaSharpDrawingContext> chart)
if (!tooltipPoints.Any())
{
var wfChart = (Chart)chart.View;
if (!tooltipPoints.Any())
foreach (var key in activePoints.Keys.ToArray())
{
foreach (var key in activePoints.Keys.ToArray())
{
key.RemoveFromHoverState();
_ = activePoints.Remove(key);
}
return;
key.RemoveFromHoverState();
_ = activePoints.Remove(key);
}
if (activePoints.Count > 0 && tooltipPoints.All(x => activePoints.ContainsKey(x.Point))) return;
return;
}
var size = DrawAndMesure(tooltipPoints, wfChart);
PointF? location = null;
if (activePoints.Count > 0 && tooltipPoints.All(x => activePoints.ContainsKey(x.Point))) return;
if (chart is CartesianChart<SkiaSharpDrawingContext>)
{
location = tooltipPoints.GetCartesianTooltipLocation(
chart.TooltipPosition, new SizeF((float)size.Width, (float)size.Height), chart.ControlSize);
}
if (chart is PieChart<SkiaSharpDrawingContext>)
{
location = tooltipPoints.GetPieTooltipLocation(
chart.TooltipPosition, new SizeF((float)size.Width, (float)size.Height));
}
var size = DrawAndMesure(tooltipPoints, wfChart);
PointF? location = null;
BackColor = Color.FromArgb(255, 30, 30, 30);
Height = (int)size.Height;
Width = (int)size.Width;
if (chart is CartesianChart<SkiaSharpDrawingContext>)
{
location = tooltipPoints.GetCartesianTooltipLocation(
chart.TooltipPosition, new SizeF((float)size.Width, (float)size.Height), chart.ControlSize);
}
if (chart is PieChart<SkiaSharpDrawingContext>)
{
location = tooltipPoints.GetPieTooltipLocation(
chart.TooltipPosition, new SizeF((float)size.Width, (float)size.Height));
}
var l = wfChart.PointToScreen(Point.Empty);
var x = l.X + (int)location.Value.X;
var y = l.Y + (int)location.Value.Y;
Location = new Point(x, y);
Show();
BackColor = Color.FromArgb(255, 30, 30, 30);
Height = (int)size.Height;
Width = (int)size.Width;
var o = new object();
foreach (var tooltipPoint in tooltipPoints)
{
tooltipPoint.Point.AddToHoverState();
activePoints[tooltipPoint.Point] = o;
}
var l = wfChart.PointToScreen(Point.Empty);
var x = l.X + (int)location.Value.X;
var y = l.Y + (int)location.Value.Y;
Location = new Point(x, y);
Show();
foreach (var key in activePoints.Keys.ToArray())
{
if (activePoints[key] == o) continue;
key.RemoveFromHoverState();
_ = activePoints.Remove(key);
}
var o = new object();
foreach (var tooltipPoint in tooltipPoints)
{
tooltipPoint.Point.AddToHoverState();
activePoints[tooltipPoint.Point] = o;
}
wfChart.CoreCanvas.Invalidate();
foreach (var key in activePoints.Keys.ToArray())
{
if (activePoints[key] == o) continue;
key.RemoveFromHoverState();
_ = activePoints.Remove(key);
}
private SizeF DrawAndMesure(IEnumerable<TooltipPoint> tooltipPoints, Chart chart)
wfChart.CoreCanvas.Invalidate();
}
private SizeF DrawAndMesure(IEnumerable<TooltipPoint> tooltipPoints, Chart chart)
{
SuspendLayout();
Controls.Clear();
var h = 0f;
var w = 0f;
foreach (var point in tooltipPoints)
{
SuspendLayout();
Controls.Clear();
using var g = CreateGraphics();
var text = point.Point.AsTooltipString;
var size = g.MeasureString(text, chart.TooltipFont);
var h = 0f;
var w = 0f;
foreach (var point in tooltipPoints)
var drawableSeries = (IDrawableSeries<SkiaSharpDrawingContext>)point.Series;
Controls.Add(new MotionCanvas
{
using var g = CreateGraphics();
var text = point.Point.AsTooltipString;
var size = g.MeasureString(text, chart.TooltipFont);
var drawableSeries = (IDrawableSeries<SkiaSharpDrawingContext>)point.Series;
Controls.Add(new MotionCanvas
{
Location = new Point(6, (int)h + 6),
PaintTasks = drawableSeries.CanvasSchedule.PaintSchedules,
Width = (int)drawableSeries.CanvasSchedule.Width,
Height = (int)drawableSeries.CanvasSchedule.Height
});
Controls.Add(new Label
{
Text = text,
ForeColor = Color.FromArgb(255, 250, 250, 250),
Font = chart.TooltipFont,
Location = new Point(6 + (int)drawableSeries.CanvasSchedule.Width + 6, (int)h + 6),
AutoSize = true
});
var thisW = size.Width + 18 + (int)drawableSeries.CanvasSchedule.Width;
h += size.Height + 6;
w = thisW > w ? thisW : w;
}
Location = new Point(6, (int)h + 6),
PaintTasks = drawableSeries.CanvasSchedule.PaintSchedules,
Width = (int)drawableSeries.CanvasSchedule.Width,
Height = (int)drawableSeries.CanvasSchedule.Height
});
Controls.Add(new Label
{
Text = text,
ForeColor = Color.FromArgb(255, 250, 250, 250),
Font = chart.TooltipFont,
Location = new Point(6 + (int)drawableSeries.CanvasSchedule.Width + 6, (int)h + 6),
AutoSize = true
});
var thisW = size.Width + 18 + (int)drawableSeries.CanvasSchedule.Width;
h += size.Height + 6;
w = thisW > w ? thisW : w;
}
h += 6;
h += 6;
ResumeLayout();
return new SizeF(w, h);
}
ResumeLayout();
return new SizeF(w, h);
}
protected override void Dispose(bool disposing)
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
components.Dispose();
}
base.Dispose(disposing);
}
}
```

Your tooltip is ready to be used, now when you create a chart, we have to pass a new instance of the tooltip we just created.
Expand Down
Loading

0 comments on commit ba07209

Please sign in to comment.