Zeus82
Zeus82

Reputation: 6375

Render a Line Graph to Attach to an Email

I'm trying to render a line graph, as an image of some sort that I can attach to an email. All my searching only turns up libraries that help with rendering controls. My application has no UI or Web component which will not work with the libs I have found.

Does anyone know of any lib/tutorial that can help me with this. My line graph doesn't need to be fancy at all, all I need is to show a few series with different colors. I don't need any labels, and I can even get away without a legend.

Upvotes: 0

Views: 436

Answers (1)

TaW
TaW

Reputation: 54433

Here is an example of a console application that uses the WinForm Chart control to create a bitmap. It saves it to disk, but you can attatch it to your mail as well..

First include references to these Assemblies: System.Drawing, System.Windows.Forms and System.Windows.Forms.DataVisualization to your project.

Then add these using clauses:

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

Now you can use the Chart Control:

class Program
{
    static void Main(string[] args)
    {
        Bitmap bmp = getChartImage();
        bmp.Save("yourpathandfilename.png");
        bmp.Dispose();
    }

    static Bitmap getChartImage()
    {
        Chart chart = new Chart();
        ChartArea chartArea = new ChartArea();
        chart.ChartAreas.Add(chartArea);
        Legend legend = new Legend(); // optional
        chart.Legends.Add(legend);    // optional
        chart.ClientSize = new Size(800, 500);

        // a few test data 
        for (int s = 0; s < 5; s++)
        {
            Series series = chart.Series.Add(s.ToString("series 0"));
            series.ChartType = SeriesChartType.Line;
        }
        for (int s = 0; s < 5; s++)
            for (int p = 0; p < 50; p++)
                chart.Series[s].Points.AddXY(p, s * p);

        Bitmap bmp = new Bitmap(chart.ClientSize.Width, chart.ClientSize.Height);
        chart.DrawToBitmap(bmp, chart.ClientRectangle);

        return bmp;
    }
}

I advise you to create a Winforms application to play around with the many options the Control offers. They can then all be applied to the console version.. The same is true for the many things you can do in the designer!

Upvotes: 2

Related Questions