Wednesday 11 April 2012

Creating a chart programmatically in C# using DataVisualization.Charting

Means you have to reference System.Windows.Forms and System.Windows.Forms.DataVisualization, even though you might be doing this in a console application. That said, it also includes a set of data manipulation functions, some of which are even finance-specific:

using System;
using System.Drawing;
using System.Windows.Forms.DataVisualization.Charting;

namespace Sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            // set up some data
            var xvals = new[]
                {
                    new DateTime(2012, 4, 4), 
                    new DateTime(2012, 4, 5), 
                    new DateTime(2012, 4, 6), 
                    new DateTime(2012, 4, 7)
                };
            var yvals = new[] { 1,3,7,12 };

            // create the chart
            var chart = new Chart();
            chart.Size = new Size(600, 250);

            var chartArea = new ChartArea();
            chartArea.AxisX.LabelStyle.Format = "dd/MMM\nhh:mm";
            chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
            chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
            chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 8);
            chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 8);
            chart.ChartAreas.Add(chartArea);

            var series = new Series();
            series.Name = "Series1";
            series.ChartType = SeriesChartType.FastLine;
            series.XValueType = ChartValueType.DateTime;
            chart.Series.Add(series);

            // bind the datapoints
            chart.Series["Series1"].Points.DataBindXY(xvals, yvals);

            // copy the series and manipulate the copy
            chart.DataManipulator.CopySeriesValues("Series1", "Series2");
            chart.DataManipulator.FinancialFormula(
                FinancialFormula.WeightedMovingAverage, 
                "Series2"
            );
            chart.Series["Series2"].ChartType = SeriesChartType.FastLine;

            // draw!
            chart.Invalidate();

            // write out a file
            chart.SaveImage("chart.png", ChartImageFormat.Png);
        }
    }
}

Gives the following result:

chart

11 comments:

Chris O'Connor said...

Thanks so much for this - we're starting to design some pages to show report data - and need to also save out the chart to include in a Word Doc.

This is the approach we'll look to use - exactly what we need - cheers !

Chris O'Connor said...

Thanks so much for this - we're starting to design some pages to show graphs with data from a SQL Server proc - we need to also save out the same chart to include in a Word Doc.

This is the approach we'll look to use - exactly what we need - cheers !

Chris O'Connor said...

Thanks again - I made use of your example - and got it working within ASP.NET - some minor differences.

I did a blog post about it - and linked to your post - thanks again !

http://sharepointroot.com/2013/12/06/datavisualization-charting-using-asp-net-and-c/

Unknown said...

Hi!

I get an error named: " The name 'Color' does not exist in the current context " and " The type or namespace name 'Size' could not be found (are you missing a using directive or an assembly reference?) "

What can I do?
Thanks

Unknown said...

Hi

I get an error "The type or namespace name 'Size' could not be found (are you missing a using directive or an assembly reference?)" and "The name 'Color' does not exist in the current context"
What can I do? Thanks

Unknown said...

Great article! I was looking for something simple, it was hard to find any clear way to use these libraries. Thanks for sharing.

Unknown said...

Great article! I was looking for something simple and clear to understand the use of these libraries. Thanks for sharing!

Unknown said...

Thanx!
It's very helpful.

Unknown said...

nice example, do you know how to fill a serie with a lot of data, a year maybe. and put interval of 1 week or 1 day, without zoom need, only scroll bar

Unknown said...

nice example, do you know how to fill a serie with a lot of data, a year maybe. and put interval of 1 week or 1 day, without zoom need, only scroll bar

Tim Barrass said...

Goodness, something like 28k views for this post! Apologies to all for not responding to your comments and thanks for getting in touch -- in truth I've put all these posts out there fully expecting no-one to read them, so actually having some interest is -- wonderfully surprising.