Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is it possible to generate images (jpeg, png, etc) using the Microsoft Chart Controls library without instantiating a WinForm or ASP.NET Control class? All the examples I have seen utilize a control component. I need to create a library which contains simple methods that take data to be plotted and returns a new chart image. Examples:

public byte[] GeneratePlot(IList<SeriesData> series)
{
    // generate and return JPEG
}
public void GeneratePlot(IList<SeriesData> series, Stream outputStream)
{
    // generate JPEG and write to stream
}

If it is not possible:

  1. would you recommend creating/disposing a new chart control each time the user calls the GeneratePlot() method?
  2. is there another .NET library (preferably free) that you would recommend?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
266 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes, that's possible:

using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
...
    public void GeneratePlot(IList<DataPoint> series, Stream outputStream) {
      using (var ch = new Chart()) {
        ch.ChartAreas.Add(new ChartArea());
        var s = new Series();
        foreach (var pnt in series) s.Points.Add(pnt);
        ch.Series.Add(s);
        ch.SaveImage(outputStream, ChartImageFormat.Jpeg);
      }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...