@@ -214,128 +214,128 @@ If you need to customize more, you can also use the create your own template:
214
214
You can create your own tooltip control, the key is that your control must implement ` IChartTooltip<SkiaSharpDrawingContext> ` and then
215
215
you have to create a new instance of that control when your chart initializes.
216
216
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:
218
218
219
219
```
220
220
public partial class CustomTooltip : Form, IChartTooltip<SkiaSharpDrawingContext>, IDisposable
221
+ {
222
+ private readonly Dictionary<ChartPoint, object> activePoints = new();
223
+
224
+ public CustomTooltip()
221
225
{
222
- private readonly Dictionary<ChartPoint, object> activePoints = new();
226
+ InitializeComponent();
227
+ }
223
228
224
- public CustomTooltip()
225
- {
226
- InitializeComponent();
227
- }
229
+ public void Show(IEnumerable<TooltipPoint> tooltipPoints, Chart<SkiaSharpDrawingContext> chart)
230
+ {
231
+ var wfChart = (Chart)chart.View;
228
232
229
- public void Show(IEnumerable<TooltipPoint> tooltipPoints, Chart<SkiaSharpDrawingContext> chart )
233
+ if (! tooltipPoints.Any() )
230
234
{
231
- var wfChart = (Chart)chart.View;
232
-
233
- if (!tooltipPoints.Any())
235
+ foreach (var key in activePoints.Keys.ToArray())
234
236
{
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);
242
239
}
243
240
244
- if (activePoints.Count > 0 && tooltipPoints.All(x => activePoints.ContainsKey(x.Point))) return;
241
+ return;
242
+ }
245
243
246
- var size = DrawAndMesure(tooltipPoints, wfChart);
247
- PointF? location = null;
244
+ if (activePoints.Count > 0 && tooltipPoints.All(x => activePoints.ContainsKey(x.Point))) return;
248
245
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;
259
248
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
+ }
263
259
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;
269
263
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();
276
269
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
+ }
283
276
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);
285
282
}
286
283
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)
288
295
{
289
- SuspendLayout();
290
- Controls.Clear();
296
+ using var g = CreateGraphics();
297
+ var text = point.Point.AsTooltipString;
298
+ var size = g.MeasureString(text, chart.TooltipFont);
291
299
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
295
303
{
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
+ }
322
322
323
- h += 6;
323
+ h += 6;
324
324
325
- ResumeLayout();
326
- return new SizeF(w, h);
327
- }
325
+ ResumeLayout();
326
+ return new SizeF(w, h);
327
+ }
328
328
329
- protected override void Dispose(bool disposing)
329
+ protected override void Dispose(bool disposing)
330
+ {
331
+ if (disposing && (components != null))
330
332
{
331
- if (disposing && (components != null))
332
- {
333
- components.Dispose();
334
- }
335
-
336
- base.Dispose(disposing);
333
+ components.Dispose();
337
334
}
335
+
336
+ base.Dispose(disposing);
338
337
}
338
+ }
339
339
```
340
340
341
341
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