Posted By

jimfred on 12/22/09


Tagged


Versions (?)

DataVisualization.Charting.Chart simple example


 / Published in: C#
 

For error... 'The type or namespace name 'DataVisualization' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?) ...Here's how to install: * Search for: download "Microsoft Chart Controls for Microsoft .NET Framework 3.5" and expect to find MSChart.exe. Download and install. * Make sure the proj has a reference to System.Windows.Forms.DataVisualization.

For an example (.SLN) that demonstrates usage of mschart, see http://code.msdn.microsoft.com/mschart or, search for "Samples Environment for Microsoft Chart Controls" on MSDN

  1. // Add a chart to a WinForm. Expect this in the x.Designer.cs file:
  2. private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
  3.  
  4.  
  5. // For an X-Y plot, add X-Y coordinates for each point.
  6. // This assume that dat is a double[] or a List<double>.
  7. // Series["MyGraph"] is a series added in the form designer.
  8. for (int i = 0; i < dat.Length; i++ )
  9. {
  10. // Add X and Y values for a point.
  11. chart1.Series["MyGraph"].Points.AddXY(i,dat[i]);
  12. }
  13.  
  14. // This is the corresponding code added by the form designer, found in x.Designer.cs. It shows the code that creates the series referenced above.
  15. //
  16. // chart1
  17. //
  18. chartArea1.Name = "ChartArea1";
  19. this.chart1.ChartAreas.Add(chartArea1);
  20. this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
  21. legend1.Enabled = false;
  22. legend1.Name = "Legend1";
  23. this.chart1.Legends.Add(legend1);
  24. this.chart1.Location = new System.Drawing.Point(3, 3);
  25. this.chart1.Name = "chart1";
  26. this.chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Fire;
  27. series1.ChartArea = "ChartArea1";
  28. series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
  29. series1.Legend = "Legend1";
  30. series1.Name = "MyGraph";
  31. series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
  32. series2.ChartArea = "ChartArea1";
  33. series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
  34. series2.Legend = "Legend1";
  35. series2.Name = "Gap";
  36. series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
  37. series2.YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
  38. series2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
  39. this.chart1.Series.Add(series1);
  40. this.chart1.Series.Add(series2);
  41. this.chart1.Size = new System.Drawing.Size(797, 267);
  42. this.chart1.TabIndex = 0;
  43. this.chart1.Text = "chart1";
  44. this.chart1.Click += new System.EventHandler(this.chart1_Click);
  45.  
  46. // Add this to enable zoom and scroll.
  47. // See http://stackoverflow.com/questions/1212914/enable-scrolling-on-the-microsoft-chart-control-for-windows-forms
  48. chartArea1.CursorX.IsUserEnabled = true;
  49. chartArea1.CursorX.IsUserSelectionEnabled = true;
  50. chartArea1.CursorY.IsUserEnabled = true;
  51. chartArea1.CursorY.IsUserSelectionEnabled = true;
  52.  
  53. // Render a tooltip showing x and y values when mousing-over a point.
  54. // Two additions:
  55. // [1] in x.designer.cs file
  56. this.chart1.GetToolTipText += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs>(this.Chart1_GetToolTipText);
  57. ...
  58. // [2] in x.cs file.
  59. private void Chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
  60. {
  61.  
  62. // Check selevted chart element and set tooltip text
  63. if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
  64. {
  65. int i = e.HitTestResult.PointIndex;
  66. DataPoint dp = e.HitTestResult.Series.Points[i];
  67. e.Text = string.Format("{0:F1}, {1:F1}", dp.XValue, dp.YValues[0] );
  68. }
  69. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Girevik on June 24, 2010

I have done everything that is written here, but the compiler can not find DataVisualization? Why? Should I do something else?

Posted By: jimfred on January 3, 2012

For error... 'The type or namespace name 'DataVisualization' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?) ...Here's how to install: * Search for: download "Microsoft Chart Controls for Microsoft .NET Framework 3.5" and expect to find MSChart.exe. Download and install. * Make sure the proj has a reference to System.Windows.Forms.DataVisualization.

You need to login to post a comment.