Free Support Forum - aspose.com

How to create a chart?

Hi I am evaluating Aspose.Excel. I’d like to create a chart that
draws data point through API. For instance, a pseudo code like:

Chart chart = excel.worksheet[0].charts.add();
ASeries series = chart.NSeries.Add();
DataPoint point1 = new DataPoint(X, Y);
series.add(point1);

Could someone show me an example?

Please check http://www.aspose.com/wiki/default.aspx/Aspose.Excel/SettingCharts.html for reference.

Reference page shows how to setup a chart. But I want to know the way
to add datapoints through API

When you add the source data to the chart, the datapoints are automatically generated.

The example only shows add data from columns. But the data I need to
draw on the chart is consolidated from the columns. Do you have example
for that?

Please create an Excel file with chart to show your need. Then I can provide related sample code.

ok, for example, if I have a column contains the following data

0
1
0
3
2
0

I need to create a chart to display there are 3 zeros, 1 one, 1 two and 1 three.
So, the Y axis is the count, X axis is value (0, 1, 2, 3)

thanks,

Still not sure about what kind of chart you want to create. Please manually create it in MS Excel. Then I can provide sample code to create it.

the attached is the chart I want to create

Thanks

Please try the following sample code with the attached fix:

Excel excel = new Excel();

int chartIndex = excel.Worksheets[0].Charts.Add(ChartType.Scatter, 10, 3, 25, 10);
Chart chart = excel.Worksheets[0].Charts[chartIndex];
chart.NSeries.Add("{15}", true);
chart.NSeries.Add("{17}", true);
chart.NSeries.Add("{2}", true);

chart.NSeries[0].Name = "coupon 0";
chart.NSeries[1].Name = "coupon 1";
chart.NSeries[2].Name = "coupon 2";

chart.NSeries[0].XValues = "{0}";
chart.NSeries[1].XValues = "{1}";
chart.NSeries[2].XValues = "{2}";

excel.Save("d:\\test\\abc.xls");