14

How to use System.Windows.Forms.DataVisualization.Charting.Chart inside console application to draw an graph and save it to file?

ch_100
  • 191
  • 1
  • 1
  • 9

2 Answers2

20
        //populate dataset with some demo data..
        DataSet dataSet = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Counter", typeof(int));
        DataRow r1 = dt.NewRow();
        r1[0] = "Demo";
        r1[1] = 8;
        dt.Rows.Add(r1);
        DataRow r2 = dt.NewRow();
        r2[0] = "Second";
        r2[1] = 15;
        dt.Rows.Add(r2);
        dataSet.Tables.Add(dt);


        //prepare chart control...
        Chart chart = new Chart();
        chart.DataSource = dataSet.Tables[0];
        chart.Width = 600;
        chart.Height = 350;
        //create serie...
        Series serie1 = new Series();
        serie1.Name = "Serie1";
        serie1.Color = Color.FromArgb(112, 255, 200);
        serie1.BorderColor = Color.FromArgb(164, 164, 164);
        serie1.ChartType = SeriesChartType.Column;
        serie1.BorderDashStyle = ChartDashStyle.Solid;
        serie1.BorderWidth = 1;
        serie1.ShadowColor = Color.FromArgb(128, 128, 128);
        serie1.ShadowOffset = 1;
        serie1.IsValueShownAsLabel = true;
        serie1.XValueMember = "Name";
        serie1.YValueMembers = "Counter";
        serie1.Font = new Font("Tahoma", 8.0f);
        serie1.BackSecondaryColor = Color.FromArgb(0, 102, 153);
        serie1.LabelForeColor = Color.FromArgb(100, 100, 100);
        chart.Series.Add(serie1);
        //create chartareas...
        ChartArea ca = new ChartArea();
        ca.Name = "ChartArea1";
        ca.BackColor = Color.White;
        ca.BorderColor = Color.FromArgb(26, 59, 105);
        ca.BorderWidth = 0;
        ca.BorderDashStyle = ChartDashStyle.Solid;
        ca.AxisX = new Axis();
        ca.AxisY = new Axis();
        chart.ChartAreas.Add(ca);
        //databind...
        chart.DataBind();
        //save result...
        chart.SaveImage(@"c:\myChart.png", ChartImageFormat.Png);

Add this declaration on top of your class:

       using System.Windows.Forms.DataVisualization.Charting;
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46
  • Great answer. Being a noob I had a little trouble with it at first (I'm using VB.net). When I put `Imports System.Windows.Forms.DataVisualization.Charting` it would complain that there were no exportable members. I had to add a Reference in my project and found it under the Assemblies->Extensions. Once I had that reference in there I could add the Import/Using statement without trouble. – Brett Nov 06 '14 at 16:30
5

Plot sin(x) between 0 and 2pi.

You have to add "System.Windows.Forms.DataVisualization.dll" and "System.Drawing.dll" to your project.

Good Luck :)

        // 
        // chart
        //
        System.Windows.Forms.DataVisualization.Charting.Chart chart = new System.Windows.Forms.DataVisualization.Charting.Chart(); 
        chart.Size = new System.Drawing.Size(640, 320);
        chart.ChartAreas.Add("ChartArea1");
        chart.Legends.Add("legend1");

        // Plot {sin(x), 0, 2pi} 
        chart.Series.Add("sin");
        chart.Series["sin"].LegendText = "Sin(x)";
        chart.Series["sin"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;

        for (double x = 0; x < 2 * Math.PI; x += 0.01)
        {
            chart.Series["sin"].Points.AddXY(x, Math.Sin(x));
        }

        // Save sin_0_2pi.png image file
        chart.SaveImage("sin_0_2pi.png", System.Drawing.Imaging.ImageFormat.Png);
Yas
  • 4,957
  • 2
  • 41
  • 24
  • I also needed to import `System.Windows.Forms`. – mlhDev Sep 21 '18 at 20:00
  • @mlhDev you can add `System.Windows.Forms.dll` to your project, then use `System.Windows.Forms` namespace! – Yas Sep 23 '18 at 05:26
  • Nice self contained example. – AnthonyVO Nov 29 '19 at 18:36
  • I paste this into a Console app, but it does not display the image on the screen... how would I do that? I try .Visible = true and .Show but I dont see the image as I do in the windows forms project – Markus Apr 18 '23 at 20:15