Free Support Forum - aspose.com

Chartex issues

I have a chart on the slide which is being updated. the current chart is a clustered column chart with two columns on the chart… is it possible for me to add another column though the code and the chart will show this addition… as currently i have the chart and the data appears within the chart but the extra column is not being displayed on the chart any ideas how this could be done

Hi Harish,


I have observed your requirements and feel that we have a relevant sample in our online documentation to serve the purpose. Please visit this documentation link for your kind reference and share with us if any further help is needed in this regard.

Many Thanks,
the code which I have been using is below and this writes the number of rows stight into the chart. Currently in the chart there is a area of 9 row selected of the data being displayed under the series. say i wanted to add another row into this series the system fails and give this error message "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index""

as the selection of the chart area is constant is there a method to overcome this OR a method tom allow me to re-size the table in excel file connected to the chart



ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;
ChartSeriesEx series = chart.ChartData.Series[0];

for (int i = 0; i < noOfRows; i++)
{
string insertedValue = extractedData.Rows[i][chartinedx].ToString();
string[] catName = extractedData.Rows[i][valuesUsed[0]].ToString().Split('[', ']');
string cat = catName[1];
if (insertedValue.Equals("-") || insertedValue.Equals(""))
{
insertedValue = "0";
}
fact.GetCell(0, catIndex, 0, cat);
catIndex++;
fact.GetCell(0, 0, 1, "Index");
series.Values[i].Value = Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero);
}
catIndex = 0;

Hi Harish,


I have observed the sample code shared by you and it is only some part of code. Can you please share the source presentation, generated faulty presentation and desired output presentation for reference. Please share a complete sample application that I may use on my end to quickly help you out.

Many Thanks,

this is the entire code in the method that changes the chart data… all the other part is finding and passing it through that works that fine… if the number of rows changes from 9 to 10 then i get the flowing error message


“Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index”"

So my main query is there any way to change the selection made for the original chart in the excel file, so that more data can be added…

I have tried using this to add the values into the chat it changes the chart but add two bars instead of one bar and does not display the chart bar name

series.Values.Add(fact.GetCell(0, noOfRows, 0, “test”));

Hi Harish,


I have observed the code sample shared but it does not reflect the requirement shared by you. If you want to add a new column in your chart as mentioned in very first post of this thread then you need to add a new series in chart. Each column exhibit a series and three or 4 values added for series mean they belong to 3 or 4 categories. So, if you want to add the new value to series even you need to add a category for that. In order to add new series that will add a new column you need to use the following sample code as shared by me in my first post of this thread.

//Now, Adding a new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 3, “Series 3”), chart.Type);

//Take 3rd chart series
series = chart.ChartData.Series[2];

//Now populating series data
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 1, 3, 20));
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 2, 3, 50));
series.Values.Add(fact.GetCell(defaultWorksheetIndex, 3, 3, 30));

Please share the source chart and also share the output chart in form of PowerPoint presentation that you need to generate using Aspose.Slides.

Many Thanks,

this is the perfered output.


I am not trying to add data to a new series but trying to add it to the a series already defined. How would i check the number of values that in the index series as well

I have got the value to be added onto presentation in the chart but the label is the edit data but is not diplayed on the chart the below is the new code and added another presentation with the out that i get form the code whihc i have

int noOfRows = rows;

if (noOfRows == 0 || noOfRows > extractedData.Rows.Count)
{
noOfRows = extractedData.Rows.Count;
}

int catIndex = 1;
chart.ChartDataTable.Format.Equals(noOfRows);
ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;
ChartSeriesEx series = chart.ChartData.Series[0];

//series.Values.Add(fact.GetCell(0, noOfRows, 0, “test”));
//series.Values.Add(fact.GetCell(0, noOfRows, 1, 0));
if (series.Values.Count<noOfRows)
{
int valuesToAdd = noOfRows - series.Values.Count;

for (int i = 0; i < valuesToAdd; i++)
{
series.Values.Add(fact.GetCell(0,noOfRows,1,0));
}
}
for (int i = 0; i < noOfRows; i++)
{
string insertedValue = extractedData.Rows[i][chartinedx].ToString();
string[] catName = extractedData.Rows[i][valuesUsed[0]].ToString().Split(’[’, ‘]’);
string cat = catName[1];
if (insertedValue.Equals("-") || insertedValue.Equals(""))
{
insertedValue = “0”;
}
fact.GetCell(0, catIndex, 0, cat);
catIndex++;
fact.GetCell(0, 0, 1, “Index”);
series.Values[i].Value = Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero);
}
catIndex = 0;


Hi Harish,


I have tried to use your sample code and have observed the issue. As I shared with you earlier that the missing label in value axis for added series values is due to fact that you have not added respective category for that. When you add a new series value then you need to first add the category for that and then add the series value for that. I have shared the sample application for your kind reference along with generated presentation.

public static System.Data.DataTable GetDataTable()
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add(“CatName”, typeof(string));
table.Columns.Add(“Value”, typeof(double));

table.Rows.Add(“London”, 200);
table.Rows.Add(“Financial”, 92);
table.Rows.Add(“The Ind”, 82);
table.Rows.Add(“Evening Mail”, 65);
table.Rows.Add(“Metro”, 161);
table.Rows.Add(“Independent”, 48);
table.Rows.Add(“Daily Star”, 133);
table.Rows.Add(“Manchester”, 116);
table.Rows.Add(“Sun”, 15);
table.Rows.Add(“Guardian”, 115);

return table;
}


public static void ModifyColumnChart()
{

String path = @“D:\Aspose Data”;
PresentationEx pres = new PresentationEx(path + “Presentation2.pptx”);
SlideEx slide = pres.Slides[0];
ChartEx chart = (ChartEx)slide.Shapes[0];
ChartDataEx data = chart.ChartData;
System.Data.DataTable extractedData = GetDataTable();

int noOfRows = extractedData.Rows.Count;

if (noOfRows == 0 || noOfRows > extractedData.Rows.Count)
{
noOfRows = extractedData.Rows.Count;
}

int catIndex = 1;
chart.ChartDataTable.Format.Equals(noOfRows);
ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;
ChartSeriesEx series = chart.ChartData.Series[0];

//series.Values.Add(fact.GetCell(0, noOfRows, 0, “test”));
//series.Values.Add(fact.GetCell(0, noOfRows, 1, 0));
//Added by Mudassir
if (chart.ChartData.Categories.Count< noOfRows)
{
int valuesToAdd = noOfRows - chart.ChartData.Categories.Count;
int indToAdd = noOfRows;

for (; indToAdd > chart.ChartData.Categories.Count; indToAdd–)
{
chart.ChartData.Categories.Add(fact.GetCell(0, indToAdd, 0, 0));

// series.Values.Add(fact.GetCell(0, indToAdd, 0, 0));

}
}

if (series.Values.Count < noOfRows)
{
int valuesToAdd = noOfRows - series.Values.Count;

for (int i = 0; i < valuesToAdd; i++)
{

series.Values.Add(fact.GetCell(0, noOfRows, 1, 0));

}
}

for (int i = 0; i < noOfRows; i++)
{
string insertedValue = extractedData.Rows[i][“Value”].ToString();
string catName = extractedData.Rows[i][“CatName”].ToString();//.Split(’[’, ‘]’);
string cat = catName;
if (insertedValue.Equals("-") || insertedValue.Equals(""))
{
insertedValue = “0”;
}
fact.GetCell(0, catIndex, 0, cat);
catIndex++;
fact.GetCell(0, 0, 1, “Index”);
series.Values[i].Value = Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero);


}
pres.Save(path + “GenPres.pptx”, Aspose.Slides.Export.SaveFormat.Pptx);
}

Since your chart has base value of 100 so any series value lying below 100 is considered as negative value. There is option Invert if Negative for rendering fill colors for negative values. If the Invert if Negative is unchecked then it renders with same fill color with which other positive series values are rendered. You need to open the generated presentation and set this value using PowerPoint in case your series values are negative. The feature is currently unavailable in Aspose.Slides for .NET and an issue with ID SLIDESNET-33821 has already been added to provide the feature support for checking/un-checking Invert if Negative for chart series values. We will share the information with you as soon as the feature will be available.

Many Thanks,

The issues you have found earlier (filed as SLIDESNET-33821) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(3)