We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Evaluation Version Limitations (only 4 data points?)

The code below creates an chart with 4 data points. Notice that two of my data points in series0 are commented out. When I uncomment these, my program crashes because it says my index is out of bounds (which i'm certain it's not). Is there a limitation on the number of data points that can be added to a series in the free/trial version?


ISlide slide = pres.Slides[slideNum];
IChart chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 100, 100, 500, 400);

chart.ChartData.Series[0].DataPoints[0].Value.Data = dataTable.Rows[0][1];
chart.ChartData.Series[0].DataPoints[1].Value.Data = dataTable.Rows[1][1];
chart.ChartData.Series[0].DataPoints[2].Value.Data = dataTable.Rows[2][1];
chart.ChartData.Series[0].DataPoints[3].Value.Data = dataTable.Rows[3][1];
//chart.ChartData.Series[0].DataPoints[4].Value.Data = dataTable.Rows[4][1];
//chart.ChartData.Series[0].DataPoints[5].Value.Data = dataTable.Rows[5][1];

Hi Caleb,


Thank you for posting.

I have observed your comments and like to share with you that out of bounds error is appearing because when a chart is added, 4 categories are added by default. Now when you are setting value for 5th category then out of bound error appears. To avoid this error, you may add categories as in the code below.

//Setting
the index of chart data sheet
<o:p></o:p>

int defaultWorksheetIndex = 0;

//Getting the chart data worksheet

IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

//Adding new categories

chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 5, 0, "Category 5"));

chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 6, 0, "Category 6"));

chart.ChartData.Series[0].DataPoints[4].Value.Data = dataTable.Rows[4][1];

chart.ChartData.Series[0].DataPoints[5].Value.Data = dataTable.Rows[5][1];


I hope this will clarify the concept. Please share if I may help you further in this regard.

Best Regards,

Hi Muhammad,


I’m still getting a index out of bounds error even when I add 5 categories before setting the data points. The following code fails on the final line. Can you offer any help?

chart.ChartData.Categories.Add(dataTable.Rows[0][0]);
chart.ChartData.Categories.Add(dataTable.Rows[1][0]);
chart.ChartData.Categories.Add(dataTable.Rows[2][0]);
chart.ChartData.Categories.Add(dataTable.Rows[3][0]);
chart.ChartData.Categories.Add(dataTable.Rows[4][0]);

chart.ChartData.Series.Add(Convert.ToString(dataTable.Rows[0]), ChartType.Line);
chart.ChartData.Series[0].DataPoints[0].Value.Data = dataTable.Rows[0][1];
chart.ChartData.Series[0].DataPoints[1].Value.Data = dataTable.Rows[1][1];
chart.ChartData.Series[0].DataPoints[2].Value.Data = dataTable.Rows[2][1];
chart.ChartData.Series[0].DataPoints[3].Value.Data = dataTable.Rows[3][1];
chart.ChartData.Series[0].DataPoints[4].Value.Data = dataTable.Rows[4][1];

SOme more information: if i delete the categories and series using the following:


chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int s = chart.ChartData.Series.Count;
s = chart.ChartData.Categories.Count;

it appears as if I cannot create any categories myself, the code will fault whenever I attempt to add a data point. Do I NEED to add the categories using IChartDataWorkbook?

Hi,


I have observed your comments and like to request you to please try using below sample code which clears the series and categories, then it adds them along with sample data while clarifying the way to add series and categories.

//Instantiate
Presentation class that represents PPTX file
<o:p></o:p>

Presentation pres = new Presentation();

//Access first slide

ISlide sld = pres.Slides[0];

// Add chart with default data

IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);

//Setting chart Title

chart.ChartTitle.AddTextFrameForOverriding("Sample Title");

chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;

chart.ChartTitle.Height = 20;

chart.HasTitle = true;

//Set first series to Show Values

chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;

//Setting the index of chart data sheet

int defaultWorksheetIndex = 0;

//Getting the chart data worksheet

IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

//Delete default generated series and categories

chart.ChartData.Series.Clear();

chart.ChartData.Categories.Clear();

int s = chart.ChartData.Series.Count;

s = chart.ChartData.Categories.Count;

//Adding new series

chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);

chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);

chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 3, "Series 3"), chart.Type);

//Adding new categories

chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));

chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));

chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));

chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 4, 0, "Caetegoty 4"));

chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 5, 0, "Caetegoty 5"));

chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 6, 0, "Caetegoty 6"));

//Take first chart series

IChartSeries series = chart.ChartData.Series[0];

//Now populating series data

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 1, 40));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 1, 60));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 1, 70));

//Setting fill color for series

series.Format.Fill.FillType = FillType.Solid;

series.Format.Fill.SolidFillColor.Color = Color.Red;

//Take second chart series

series = chart.ChartData.Series[1];

//Now populating series data

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 2, 21));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 2, 17));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 2, 15));

//Setting fill color for series

series.Format.Fill.FillType = FillType.Solid;

series.Format.Fill.SolidFillColor.Color = Color.Green;

//Take third chart series

series = chart.ChartData.Series[2];

//Now populating series data

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 3, 7));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 3, 11));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 3, 15));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 4, 3, 13));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 5, 3, 26));

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 6, 3, 35));

//Setting fill color for series

series.Format.Fill.FillType = FillType.Solid;

series.Format.Fill.SolidFillColor.Color = Color.Blue;

//Save presentation with chart

pres.Save(@"D:\ Chart_16.3.0.pptx", SaveFormat.Pptx);


I hope this will clarify the concept. Please share if I may help you further in this regard.

Best Regards,