Advertisement
digemall

MS Chart tooltip on mouse hover a point

May 18th, 2012
9,444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. // This code sample describes how to show a tooltip owhen the mouse cursor hovers a data.point.
  2. // How To Run:
  3. // 1 - Create a WinForms project called WinFormTests
  4. // 2 - Double-click on Form1, add a MS chart (Dock = Fill)
  5. // 3 - Open Form1.cs and copy and paste the following code
  6. // 4 - Build and run
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Windows.Forms;
  14. using System.Windows.Forms.DataVisualization.Charting;
  15.  
  16. namespace WinFormTests
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         internal class Item
  21.         {
  22.             public double X { get; private set; }
  23.             public double Y { get; private set; }
  24.             public Item(double x, double y)
  25.             {
  26.                 this.X = x;
  27.                 this.Y = y;
  28.             }
  29.         }
  30.  
  31.         public Form1()
  32.         {
  33.             InitializeComponent();
  34.             this.chart1.MouseMove += new MouseEventHandler(chart1_MouseMove);
  35.             this.tooltip.AutomaticDelay = 10;
  36.             FillChart();
  37.         }
  38.  
  39.         private void FillChart()
  40.         {
  41.             var rand = new Random(123);
  42.             var items = Enumerable.Range(0, 20).Select(x => new Item(x, rand.Next(1, 100) / 2.0)).ToList();
  43.  
  44.             this.chart1.Series.Clear();
  45.  
  46.             var seriesLines = this.chart1.Series.Add("Line");
  47.             seriesLines.ChartType = SeriesChartType.Line; //System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
  48.             seriesLines.XValueMember = "X";
  49.             seriesLines.YValueMembers = "Y";
  50.             seriesLines.Color = Color.Red;
  51.  
  52.             var seriesPoints = this.chart1.Series.Add("Points");
  53.             seriesPoints.ChartType = SeriesChartType.Point; //System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
  54.             seriesPoints.XValueMember = "X";
  55.             seriesPoints.YValueMembers = "Y";
  56.  
  57.             this.chart1.DataSource = items;
  58.         }
  59.  
  60.  
  61.         Point? prevPosition = null;
  62.         ToolTip tooltip = new ToolTip();
  63.  
  64.         void chart1_MouseMove(object sender, MouseEventArgs e)
  65.         {
  66.             var pos = e.Location;
  67.             if (prevPosition.HasValue && pos == prevPosition.Value)
  68.                 return;
  69.             tooltip.RemoveAll();
  70.             prevPosition = pos;
  71.             var results = chart1.HitTest(pos.X, pos.Y, false,
  72.                                             ChartElementType.DataPoint);
  73.             foreach (var result in results)
  74.             {
  75.                 if (result.ChartElementType == ChartElementType.DataPoint)
  76.                 {
  77.                     var prop = result.Object as DataPoint;
  78.                     if (prop != null)
  79.                     {
  80.                         var pointXPixel = result.ChartArea.AxisX.ValueToPixelPosition(prop.XValue);
  81.                         var pointYPixel = result.ChartArea.AxisY.ValueToPixelPosition(prop.YValues[0]);
  82.  
  83.                         // check if the cursor is really close to the point (2 pixels around)
  84.                         if (Math.Abs(pos.X - pointXPixel) < 2 &&
  85.                             Math.Abs(pos.Y - pointYPixel) < 2)
  86.                         {
  87.                             tooltip.Show("X=" + prop.XValue + ", Y=" + prop.YValues[0], this.chart1,
  88.                                             pos.X, pos.Y - 15);
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement