10

Is it possible to plot data on to a ZedGraph graph and save it as a file without showing / generating a graph that is visible to the user? I'm looking to process a lot of datasets and generate a graph and saving it to a file for viewing outside of the application.

If this can't be done, would it be possible show the graph on a hidden/minimized form, save the graph, close the window, and repeat for each graph?

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176

2 Answers2

7

It is possible.

You create and manipulate the ZedGraph control as usual, but just don't add it to the Form.Controls list, for example, in the InitializeComponent() method, comment out something that looks like the below

this.Controls.Add(this.zedGraphControl);

There are a couple of ways to save the graph

  • If you want a SaveAs dialog to appear, call SaveAs() on the graph control.
  • If you don't want the dialog, you can write out the image using GetImage() on the MasterPane, and then save that:

    zedGraphControl.MasterPane.GetImage().Save("test.bmp");

PeskyGnat
  • 2,454
  • 19
  • 22
  • 1
    I have a BackgroundWorker that runs the code to generate a graph, as well as other tasks, asynchronously. I've determined that generating a ZedGraphControl on a background thread will cause the thread to not Complete (via TaskComplete event), even though the DoWork method executes. Is there any other method to do this? – Stealth Rabbi Jul 08 '11 at 13:28
  • I just tried this and it worked for me, I registered with worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( backgroundWorker1_RunWorkerCompleted); – PeskyGnat Jul 08 '11 at 13:58
  • Sorry, I actually have an event that is fired sometime after a backgroundworker completes. The event handler generates the graph, and then kicks off that same BackgroundWorker again. The second time the BGWorker runs, it never gets to TaskComplete. I had to put the graph generation in a seperate BGWorker for some reason. It's quite strange. – Stealth Rabbi Jul 08 '11 at 16:18
5

Here is a code snippet to create and save the Bitmaps without any WinForms infrastructure necessary:

var zedGraph = new ZedGraphControl();

// configure ZedGraphControl here

using (var g = zedGraph.CreateGraphics())
{
    zedGraph.MasterPane.ReSize(g, new RectangleF(0, 0, widthPx, heightPx));
}
zedGraph.MasterPane.GetImage().Save(Path.Combine(destinationDir, "test.bmp"));

This should even be able to run as service without any desktop. The only downside is that you need to reference System.Windows.Forms and System.Drawing to use it.

David Schmitt
  • 58,259
  • 26
  • 121
  • 165
  • This must be an old version of *ZedGraph*. **MasterPane** is not defined here. **GraphPane** is, but it does not have the **GetImage()** method. Can you update this code, please? –  Dec 05 '18 at 12:17
  • 1
    This answer is from 2012. I haven't touched ZedGraph since 2013. – David Schmitt Dec 07 '18 at 12:58