Advertisement
digemall

MS Chart with data point click handling

Jul 22nd, 2011
2,804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. // This code sample shows how to handle clicks in MS chart to show more info.
  2. // In particular here we handle the datapoint click to show the ROW of the datatable
  3. // corresponding to the point.
  4. // How To Run:
  5. // 1 - Create a WinForms project called WinFormTests
  6. // 2 - Dbl click on Form1, add a SplitContainer with Dock = Fill and Orientation = Horizontal
  7. // 3 - Add a MS chart to the top panel of the split container and set Dock = Fill
  8. // 4 - Add a DataGridView to the bottom panel of the splitContainer and set Dock = Fill
  9. // 5 - Open Form1.cs and copy and paste the following code
  10. // 6 - Build and run
  11.  
  12. using System;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Data;
  16. using System.Drawing;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Windows.Forms;
  20. using System.Windows.Forms.DataVisualization.Charting;
  21.  
  22. namespace WinFormTests
  23. {
  24.     public partial class Form1 : Form
  25.     {
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.  
  30.             // register the hit test event
  31.             this.chart1.MouseClick += new MouseEventHandler(chart1_MouseClick);
  32.  
  33.             // fill the data table
  34.             DataTable dt = new DataTable();
  35.             dt.Columns.Add("Month No.", typeof(int));
  36.             dt.Columns.Add("Month", typeof(string));
  37.             dt.Columns.Add("Month Description", typeof(string));
  38.             dt.Columns.Add("Value", typeof(double));
  39.  
  40.             dt.Rows.Add("Jan", "January", 1, 10);
  41.             dt.Rows.Add("Feb", "February", 2, 30);
  42.             dt.Rows.Add("Mar", "March", 3, 50);
  43.             dt.Rows.Add("Apr", "April", 4, 20);
  44.             dt.Rows.Add("May", "May", 5, 40);
  45.             dt.Rows.Add("Jun", "June", 6, 25);
  46.             dt.Rows.Add("Jul", "July", 7, 35);
  47.             dt.Rows.Add("Aug", "August", 8, 70);
  48.             dt.Rows.Add("Sep", "September", 9, 15);
  49.             dt.Rows.Add("Oct", "October", 10, 30);
  50.             dt.Rows.Add("Nov", "November", 11, 5);
  51.             dt.Rows.Add("Dec", "December", 12, 100);
  52.  
  53.             // bind the data table to chart
  54.             this.chart1.Series.Clear();
  55.  
  56.             var series = this.chart1.Series.Add("Series 1");
  57.             series.XValueMember = "Month No.";
  58.             series.YValueMembers = "Value";
  59.             series.ChartType = SeriesChartType.Line;
  60.             this.chart1.DataSource = dt;
  61.             this.chart1.DataBind();
  62.  
  63.             // bint the data table to datagridview
  64.             this.dataGridView1.DataSource = dt;
  65.  
  66.         }
  67.  
  68.         void chart1_MouseClick(object sender, MouseEventArgs e)
  69.         {
  70.             var pos = e.Location;
  71.             var results = chart1.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
  72.             foreach (var result in results)
  73.             {
  74.                 if (result.ChartElementType == ChartElementType.DataPoint)
  75.                 {
  76.                     var point = result.Object as DataPoint;
  77.                     var month = (int)point.XValue;
  78.                    
  79.                     this.dataGridView1.ClearSelection();
  80.  
  81.                     foreach (DataGridViewRow row in this.dataGridView1.Rows)
  82.                     {
  83.                         if ((int)row.Cells["Month No."].Value == month)
  84.                         {
  85.                             row.Selected = true;
  86.                             break;
  87.                         }
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement