Skip to content

Commit ba07209

Browse files
legend docs
1 parent 2d39ba1 commit ba07209

File tree

3 files changed

+307
-95
lines changed

3 files changed

+307
-95
lines changed

docs/1.overview/1.10.tooltips.md

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -214,128 +214,128 @@ If you need to customize more, you can also use the create your own template:
214214
You can create your own tooltip control, the key is that your control must implement `IChartTooltip<SkiaSharpDrawingContext>` and then
215215
you have to create a new instance of that control when your chart initializes.
216216

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

219219
```
220220
public partial class CustomTooltip : Form, IChartTooltip<SkiaSharpDrawingContext>, IDisposable
221+
{
222+
private readonly Dictionary<ChartPoint, object> activePoints = new();
223+
224+
public CustomTooltip()
221225
{
222-
private readonly Dictionary<ChartPoint, object> activePoints = new();
226+
InitializeComponent();
227+
}
223228
224-
public CustomTooltip()
225-
{
226-
InitializeComponent();
227-
}
229+
public void Show(IEnumerable<TooltipPoint> tooltipPoints, Chart<SkiaSharpDrawingContext> chart)
230+
{
231+
var wfChart = (Chart)chart.View;
228232
229-
public void Show(IEnumerable<TooltipPoint> tooltipPoints, Chart<SkiaSharpDrawingContext> chart)
233+
if (!tooltipPoints.Any())
230234
{
231-
var wfChart = (Chart)chart.View;
232-
233-
if (!tooltipPoints.Any())
235+
foreach (var key in activePoints.Keys.ToArray())
234236
{
235-
foreach (var key in activePoints.Keys.ToArray())
236-
{
237-
key.RemoveFromHoverState();
238-
_ = activePoints.Remove(key);
239-
}
240-
241-
return;
237+
key.RemoveFromHoverState();
238+
_ = activePoints.Remove(key);
242239
}
243240
244-
if (activePoints.Count > 0 && tooltipPoints.All(x => activePoints.ContainsKey(x.Point))) return;
241+
return;
242+
}
245243
246-
var size = DrawAndMesure(tooltipPoints, wfChart);
247-
PointF? location = null;
244+
if (activePoints.Count > 0 && tooltipPoints.All(x => activePoints.ContainsKey(x.Point))) return;
248245
249-
if (chart is CartesianChart<SkiaSharpDrawingContext>)
250-
{
251-
location = tooltipPoints.GetCartesianTooltipLocation(
252-
chart.TooltipPosition, new SizeF((float)size.Width, (float)size.Height), chart.ControlSize);
253-
}
254-
if (chart is PieChart<SkiaSharpDrawingContext>)
255-
{
256-
location = tooltipPoints.GetPieTooltipLocation(
257-
chart.TooltipPosition, new SizeF((float)size.Width, (float)size.Height));
258-
}
246+
var size = DrawAndMesure(tooltipPoints, wfChart);
247+
PointF? location = null;
259248
260-
BackColor = Color.FromArgb(255, 30, 30, 30);
261-
Height = (int)size.Height;
262-
Width = (int)size.Width;
249+
if (chart is CartesianChart<SkiaSharpDrawingContext>)
250+
{
251+
location = tooltipPoints.GetCartesianTooltipLocation(
252+
chart.TooltipPosition, new SizeF((float)size.Width, (float)size.Height), chart.ControlSize);
253+
}
254+
if (chart is PieChart<SkiaSharpDrawingContext>)
255+
{
256+
location = tooltipPoints.GetPieTooltipLocation(
257+
chart.TooltipPosition, new SizeF((float)size.Width, (float)size.Height));
258+
}
263259
264-
var l = wfChart.PointToScreen(Point.Empty);
265-
var x = l.X + (int)location.Value.X;
266-
var y = l.Y + (int)location.Value.Y;
267-
Location = new Point(x, y);
268-
Show();
260+
BackColor = Color.FromArgb(255, 30, 30, 30);
261+
Height = (int)size.Height;
262+
Width = (int)size.Width;
269263
270-
var o = new object();
271-
foreach (var tooltipPoint in tooltipPoints)
272-
{
273-
tooltipPoint.Point.AddToHoverState();
274-
activePoints[tooltipPoint.Point] = o;
275-
}
264+
var l = wfChart.PointToScreen(Point.Empty);
265+
var x = l.X + (int)location.Value.X;
266+
var y = l.Y + (int)location.Value.Y;
267+
Location = new Point(x, y);
268+
Show();
276269
277-
foreach (var key in activePoints.Keys.ToArray())
278-
{
279-
if (activePoints[key] == o) continue;
280-
key.RemoveFromHoverState();
281-
_ = activePoints.Remove(key);
282-
}
270+
var o = new object();
271+
foreach (var tooltipPoint in tooltipPoints)
272+
{
273+
tooltipPoint.Point.AddToHoverState();
274+
activePoints[tooltipPoint.Point] = o;
275+
}
283276
284-
wfChart.CoreCanvas.Invalidate();
277+
foreach (var key in activePoints.Keys.ToArray())
278+
{
279+
if (activePoints[key] == o) continue;
280+
key.RemoveFromHoverState();
281+
_ = activePoints.Remove(key);
285282
}
286283
287-
private SizeF DrawAndMesure(IEnumerable<TooltipPoint> tooltipPoints, Chart chart)
284+
wfChart.CoreCanvas.Invalidate();
285+
}
286+
287+
private SizeF DrawAndMesure(IEnumerable<TooltipPoint> tooltipPoints, Chart chart)
288+
{
289+
SuspendLayout();
290+
Controls.Clear();
291+
292+
var h = 0f;
293+
var w = 0f;
294+
foreach (var point in tooltipPoints)
288295
{
289-
SuspendLayout();
290-
Controls.Clear();
296+
using var g = CreateGraphics();
297+
var text = point.Point.AsTooltipString;
298+
var size = g.MeasureString(text, chart.TooltipFont);
291299
292-
var h = 0f;
293-
var w = 0f;
294-
foreach (var point in tooltipPoints)
300+
var drawableSeries = (IDrawableSeries<SkiaSharpDrawingContext>)point.Series;
301+
302+
Controls.Add(new MotionCanvas
295303
{
296-
using var g = CreateGraphics();
297-
var text = point.Point.AsTooltipString;
298-
var size = g.MeasureString(text, chart.TooltipFont);
299-
300-
var drawableSeries = (IDrawableSeries<SkiaSharpDrawingContext>)point.Series;
301-
302-
Controls.Add(new MotionCanvas
303-
{
304-
Location = new Point(6, (int)h + 6),
305-
PaintTasks = drawableSeries.CanvasSchedule.PaintSchedules,
306-
Width = (int)drawableSeries.CanvasSchedule.Width,
307-
Height = (int)drawableSeries.CanvasSchedule.Height
308-
});
309-
Controls.Add(new Label
310-
{
311-
Text = text,
312-
ForeColor = Color.FromArgb(255, 250, 250, 250),
313-
Font = chart.TooltipFont,
314-
Location = new Point(6 + (int)drawableSeries.CanvasSchedule.Width + 6, (int)h + 6),
315-
AutoSize = true
316-
});
317-
318-
var thisW = size.Width + 18 + (int)drawableSeries.CanvasSchedule.Width;
319-
h += size.Height + 6;
320-
w = thisW > w ? thisW : w;
321-
}
304+
Location = new Point(6, (int)h + 6),
305+
PaintTasks = drawableSeries.CanvasSchedule.PaintSchedules,
306+
Width = (int)drawableSeries.CanvasSchedule.Width,
307+
Height = (int)drawableSeries.CanvasSchedule.Height
308+
});
309+
Controls.Add(new Label
310+
{
311+
Text = text,
312+
ForeColor = Color.FromArgb(255, 250, 250, 250),
313+
Font = chart.TooltipFont,
314+
Location = new Point(6 + (int)drawableSeries.CanvasSchedule.Width + 6, (int)h + 6),
315+
AutoSize = true
316+
});
317+
318+
var thisW = size.Width + 18 + (int)drawableSeries.CanvasSchedule.Width;
319+
h += size.Height + 6;
320+
w = thisW > w ? thisW : w;
321+
}
322322
323-
h += 6;
323+
h += 6;
324324
325-
ResumeLayout();
326-
return new SizeF(w, h);
327-
}
325+
ResumeLayout();
326+
return new SizeF(w, h);
327+
}
328328
329-
protected override void Dispose(bool disposing)
329+
protected override void Dispose(bool disposing)
330+
{
331+
if (disposing && (components != null))
330332
{
331-
if (disposing && (components != null))
332-
{
333-
components.Dispose();
334-
}
335-
336-
base.Dispose(disposing);
333+
components.Dispose();
337334
}
335+
336+
base.Dispose(disposing);
338337
}
338+
}
339339
```
340340

341341
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.

0 commit comments

Comments
 (0)