Return to Snippet

Revision: 21831
at December 22, 2009 14:40 by jimfred


Updated Code
// Add a chart to a WinForm. Expect this in the x.Designer.cs file:
private System.Windows.Forms.DataVisualization.Charting.Chart chart1;


// For an X-Y plot, add X-Y coordinates for each point.
// This assume that dat is a double[] or a List<double>.
// Series["MyGraph"] is a series added in the form designer.
for (int i = 0; i < dat.Length; i++ )
{
   // Add X and Y values for a point. 
   chart1.Series["MyGraph"].Points.AddXY(i,dat[i]);
}

// 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.
         // 
         // chart1
         // 
         chartArea1.Name = "ChartArea1";
         this.chart1.ChartAreas.Add(chartArea1);
         this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
         legend1.Enabled = false;
         legend1.Name = "Legend1";
         this.chart1.Legends.Add(legend1);
         this.chart1.Location = new System.Drawing.Point(3, 3);
         this.chart1.Name = "chart1";
         this.chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Fire;
         series1.ChartArea = "ChartArea1";
         series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
         series1.Legend = "Legend1";
         series1.Name = "MyGraph";
         series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
         series2.ChartArea = "ChartArea1";
         series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
         series2.Legend = "Legend1";
         series2.Name = "Gap";
         series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
         series2.YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
         series2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
         this.chart1.Series.Add(series1);
         this.chart1.Series.Add(series2);
         this.chart1.Size = new System.Drawing.Size(797, 267);
         this.chart1.TabIndex = 0;
         this.chart1.Text = "chart1";
         this.chart1.Click += new System.EventHandler(this.chart1_Click);

// Add this to enable zoom and scroll.
// See http://stackoverflow.com/questions/1212914/enable-scrolling-on-the-microsoft-chart-control-for-windows-forms
         chartArea1.CursorX.IsUserEnabled = true;
         chartArea1.CursorX.IsUserSelectionEnabled = true;
         chartArea1.CursorY.IsUserEnabled = true;
         chartArea1.CursorY.IsUserSelectionEnabled = true;

// Render a tooltip showing x and y values when mousing-over a point.
// Two additions:
// [1] in x.designer.cs file
this.chart1.GetToolTipText += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs>(this.Chart1_GetToolTipText);
...
// [2] in x.cs file.
private void Chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
{

   // Check selevted chart element and set tooltip text
   if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
   {
      int i = e.HitTestResult.PointIndex;
      DataPoint dp = e.HitTestResult.Series.Points[i];
      e.Text = string.Format("{0:F1}, {1:F1}", dp.XValue, dp.YValues[0] );
   }
}

Revision: 21830
at December 22, 2009 12:17 by jimfred


Updated Code
// Add a chart to a WinForm. Expect this in the x.Designer.cs file:
private System.Windows.Forms.DataVisualization.Charting.Chart chart1;


// For an X-Y plot, add X-Y coordinates for each point.
// This assume that dat is a double[] or a List<double>.
// Series["MyGraph"] is a series added in the form designer.
for (int i = 0; i < dat.Length; i++ )
{
   // Add X and Y values for a point. 
   chart1.Series["MyGraph"].Points.AddXY(i,dat[i]);
}

// 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.
         // 
         // chart1
         // 
         chartArea1.Name = "ChartArea1";
         this.chart1.ChartAreas.Add(chartArea1);
         this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
         legend1.Enabled = false;
         legend1.Name = "Legend1";
         this.chart1.Legends.Add(legend1);
         this.chart1.Location = new System.Drawing.Point(3, 3);
         this.chart1.Name = "chart1";
         this.chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Fire;
         series1.ChartArea = "ChartArea1";
         series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
         series1.Legend = "Legend1";
         series1.Name = "MyGraph";
         series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
         series2.ChartArea = "ChartArea1";
         series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
         series2.Legend = "Legend1";
         series2.Name = "Gap";
         series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
         series2.YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
         series2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
         this.chart1.Series.Add(series1);
         this.chart1.Series.Add(series2);
         this.chart1.Size = new System.Drawing.Size(797, 267);
         this.chart1.TabIndex = 0;
         this.chart1.Text = "chart1";
         this.chart1.Click += new System.EventHandler(this.chart1_Click);

// Add this to enable zoom and scroll.
// See http://stackoverflow.com/questions/1212914/enable-scrolling-on-the-microsoft-chart-control-for-windows-forms
         chartArea1.CursorX.IsUserEnabled = true;
         chartArea1.CursorX.IsUserSelectionEnabled = true;
         chartArea1.CursorY.IsUserEnabled = true;
         chartArea1.CursorY.IsUserSelectionEnabled = true;

Revision: 21829
at December 22, 2009 11:27 by jimfred


Initial Code
// Add a chart to a WinForm. Expect this in the x.Designer.cs file:
private System.Windows.Forms.DataVisualization.Charting.Chart chart1;


// For an X-Y plot, add X-Y coordinates for each point.
// This assume that dat is a double[] or a List<double>.
// Series["MyGraph"] is a series added in the form designer.
for (int i = 0; i < dat.Length; i++ )
{
   // Add X and Y values for a point. 
   chart1.Series["MyGraph"].Points.AddXY(i,dat[i]);
}

// 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.
         // 
         // chart1
         // 
         chartArea1.Name = "ChartArea1";
         this.chart1.ChartAreas.Add(chartArea1);
         this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
         legend1.Enabled = false;
         legend1.Name = "Legend1";
         this.chart1.Legends.Add(legend1);
         this.chart1.Location = new System.Drawing.Point(3, 3);
         this.chart1.Name = "chart1";
         this.chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Fire;
         series1.ChartArea = "ChartArea1";
         series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
         series1.Legend = "Legend1";
         series1.Name = "MyGraph";
         series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
         series2.ChartArea = "ChartArea1";
         series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
         series2.Legend = "Legend1";
         series2.Name = "Gap";
         series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
         series2.YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
         series2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
         this.chart1.Series.Add(series1);
         this.chart1.Series.Add(series2);
         this.chart1.Size = new System.Drawing.Size(797, 267);
         this.chart1.TabIndex = 0;
         this.chart1.Text = "chart1";
         this.chart1.Click += new System.EventHandler(this.chart1_Click);

Initial URL

                                

Initial Description
For error...
<code>
 'The type or namespace name 'DataVisualization' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)
</code>
...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

Initial Title
DataVisualization.Charting.Chart simple example

Initial Tags

                                

Initial Language
C#