PDA

View Full Version : Chart.WritePNG



jkenyon
08-06-2007, 12:29 PM
I'm just getting started with Imsl.Chart2D, in the context of a C# console application that needs to produce a few graphs as files (PNG is fine, so is JPG, GIF, etc.) I copied the vast majority of the following code from the IMSL example for Chart, and put my code that (I thought) would write a PNG at the end.

The code works, but the resulting PNG file has only the Legend box in the upper left corner.

Also, have to say that, IMHO, the documentation for Chart2D is not up to the standard of the rest of the package. Lots of restating argument names, but little useful explanation. For example, in the doc for WritePNG, "width" and "height" are pretty obvious arguments, but specifying the units would be nice (pixels? points? inches? cm?).

Anyone have any experience producing a PNG from a console app?



Chart chart = new Chart();
AxisXY axis = new AxisXY(chart);

int npoints = 20;
double dx = .5 * System.Math.PI / (npoints - 1);
double[] x = new double[npoints];
double[] y1 = new double[npoints];
double[] y2 = new double[npoints];
double[] y3 = new double[npoints];

// Generate some data
for (int i = 0; i < npoints; i++)
{
x[i] = i * dx;
y1[i] = System.Math.Sin(x[i]);
y2[i] = System.Math.Cos(x[i]);
y3[i] = System.Math.Atan(x[i]);
}
Data d1 = new Data(axis, x, y1);
Data d2 = new Data(axis, x, y2);
Data d3 = new Data(axis, x, y3);

// Set Data Type to Fill Area
axis.DataType = Imsl.Chart2D.Data.DATA_TYPE_FILL;

// Set Line Colors
d1.LineColor = System.Drawing.Color.Red;
d2.LineColor = System.Drawing.Color.Black;
d3.LineColor = System.Drawing.Color.Blue;

// Set Fill Colors
d1.FillColor = System.Drawing.Color.Red;
d2.FillColor = System.Drawing.Color.Black;
d3.FillColor = System.Drawing.Color.Blue;

// Set Data Labels
d1.SetTitle("Sine");
d2.SetTitle("Cosine");
d3.SetTitle("ArcTangent");

// Add a Legend
Legend legend = chart.Legend;
legend.SetTitle(new Text("Legend"));
legend.IsVisible = true;

// Create chart and write the PNG to the output dir
FileStream fs = new FileStream(this.outputDir + "/Opt_E_vs_SD.png", FileMode.OpenOrCreate);
chart.WritePNG(fs,500,500);
fs.Close();


Thanks!
Jeff

ed
08-06-2007, 03:01 PM
Jeff,

I think the problem may be in the first line of code. Generally, you don't want to create a new Chart instance, but instead grab the Chart property of the container. In custom applications, this will usually be a PanelChart, but in smaller examples it could be a FrameChart.

In your case, I suspect you see just the Legend because your 'chart' object isn't being displayed anywhere at all, and yet you may have set the Chart property inadvertently with "Legend legend = Chart.legend" (not upper case 'C').

I included a more complete example below that extends FrameChart and the output png works OK. If you're using a PanelChart in your own frame, the top line would be "Chart chart = panelChart.Chart" instead.



using Imsl.Chart2D;
using System.IO;
using System;

public class SampleArea : FrameChart {

public SampleArea() {
Chart chart = this.Chart;
AxisXY axis = new AxisXY(chart);

int npoints = 20;
double dx = .5 * System.Math.PI / (npoints - 1);
double[] x = new double[npoints];
double[] y1 = new double[npoints];
double[] y2 = new double[npoints];
double[] y3 = new double[npoints];

// Generate some data
for (int i = 0; i < npoints; i++)
{
x[i] = i * dx;
y1[i] = System.Math.Sin(x[i]);
y2[i] = System.Math.Cos(x[i]);
y3[i] = System.Math.Atan(x[i]);
}
Data d1 = new Data(axis, x, y1);
Data d2 = new Data(axis, x, y2);
Data d3 = new Data(axis, x, y3);

// Set Data Type to Fill Area
axis.DataType = Imsl.Chart2D.Data.DATA_TYPE_FILL;

// Set Line Colors
d1.LineColor = System.Drawing.Color.Red;
d2.LineColor = System.Drawing.Color.Black;
d3.LineColor = System.Drawing.Color.Blue;

// Set Fill Colors
d1.FillColor = System.Drawing.Color.Red;
d2.FillColor = System.Drawing.Color.Black;
d3.FillColor = System.Drawing.Color.Blue;

// Set Data Labels
d1.SetTitle("Sine");
d2.SetTitle("Cosine");
d3.SetTitle("ArcTangent");

// Add a Legend
Legend legend = chart.Legend;
legend.SetTitle(new Text("Legend"));
legend.IsVisible = true;

// Create chart and write the PNG to the output dir
FileStream fs = new FileStream("AreaFill.png", FileMode.OpenOrCreate);
chart.WritePNG(fs, 500, 500);
fs.Close();
}

public static void Main(string[] argv) {
System.Windows.Forms.Application.Run(new SampleArea());
}
}

On the documentation side, we hope to remedy this shortcoming with the next release. We plan to include a Chart Programmer's Guide like we have for the Java product to supplement the standard docs.

jkenyon
08-06-2007, 03:16 PM
Yep, that did the trick. Thanks very much!

Jeff