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

Free Support Forum - aspose.com

Percent bar chart out error


i am using the code below and it give the output on the second slide of the pptx but i would like it to give the output that is on the first slide the code is placed below if any ideas why this would be

int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;
ChartSeriesEx series = chart.ChartData.Series[0];

fact.GetCell(0, 0, 1, "Newspapers");
fact.GetCell(0, 0, 2, "Magazines");
fact.GetCell(0, 0, 3, "Television");
fact.GetCell(0, 0, 4, "Internet");
fact.GetCell(0, 0, 5, "Outdoor Media");
fact.GetCell(0, 0, 6, "Cinema");
fact.GetCell(0, 0, 7, "Radio");

for (int i = 0; i < extractedData.Rows.Count; i++)
{
string insertedValue = extractedData.Rows[i][valuesUsed[5]].ToString();

if (i<6)
{
series.Values.Add(fact.GetCell(0, i + 1, 1, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero)));
}
else if (i < 12)
{
series.Values.Add(fact.GetCell(0, count + 1, 2, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero)));
count++;
}
else if ( i < 18)
{
series.Values.Add(fact.GetCell(0, count1 + 1, 3, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero)));
count1++;
}
else if ( i < 24)
{
series.Values.Add(fact.GetCell(0, count2 + 1, 4, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero)));
count2++;
}
else if ( i < 30)
{
series.Values.Add(fact.GetCell(0, count3 + 1, 5, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero)));
count3++;
}
else if (i < 36)
{
series.Values.Add(fact.GetCell(0, count4 + 1, 6, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero)));
count4++;
}
else if (i <42)
{
series.Values.Add(fact.GetCell(0, count5 + 1, 7, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero)));
count5++;
}
}

Hi Jimmy,


I have observed the issue in the code sample shared by you. You are actually adding new series value. This is wrong. You need to either modify the value in series values collection as shared by me. I have shared the generated presentation as well for your reference.

public static System.Data.DataTable GetBarTable()
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add(“BarValue”, typeof(double));
// table.Columns.Add(“NormalValue”, typeof(double));
//First series data
table.Rows.Add(12);
table.Rows.Add(23);
table.Rows.Add(16);
table.Rows.Add(28);
table.Rows.Add(17);
table.Rows.Add(43);
table.Rows.Add(11);
//2nd series data
table.Rows.Add(55);
table.Rows.Add(31);
table.Rows.Add(27);
table.Rows.Add(35);
table.Rows.Add(53);
table.Rows.Add(14);
table.Rows.Add(44);
//3rd series data
table.Rows.Add(75);
table.Rows.Add(63);
table.Rows.Add(87);
table.Rows.Add(65);
table.Rows.Add(45);
table.Rows.Add(34);
table.Rows.Add(23);
//4TH series data
table.Rows.Add(35);
table.Rows.Add(71);
table.Rows.Add(77);
table.Rows.Add(25);
table.Rows.Add(13);
table.Rows.Add(64);
table.Rows.Add(53);
//5TH series data
table.Rows.Add(35);
table.Rows.Add(61);
table.Rows.Add(77);
table.Rows.Add(35);
table.Rows.Add(83);
table.Rows.Add(44);
table.Rows.Add(23);
//6TH series data
table.Rows.Add(15);
table.Rows.Add(21);
table.Rows.Add(47);
table.Rows.Add(39);
table.Rows.Add(23);
table.Rows.Add(44);
table.Rows.Add(34);


return table;
}

public static void ModifyBarChart()
{

String path = @“D:\Aspose Data”;
PresentationEx pres = new PresentationEx(path + “Help2.pptx”);
SlideEx slide = pres.Slides[0];
ChartEx chart = (ChartEx)slide.Shapes[0];
ChartDataEx data = chart.ChartData;
// series.XValues.RemoveAt(0);
// series.YValues.RemoveAt(0);
System.Data.DataTable extractedData = GetBarTable();

ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;
ChartSeriesEx series = chart.ChartData.Series[0];
fact.GetCell(0, 0, 1, “Newspapers”);
fact.GetCell(0, 0, 2, “Magazines”);
fact.GetCell(0, 0, 3, “Television”);
fact.GetCell(0, 0, 4, “Internet”);
fact.GetCell(0, 0, 5, “Outdoor Media”);
fact.GetCell(0, 0, 6, “Cinema”);
fact.GetCell(0, 0, 7, “Radio”);

//Setting series names
fact.GetCell(0, 1, 0, “Quintile 1 (Highest)”);
fact.GetCell(0, 2, 0, “Quintile 2”);
fact.GetCell(0, 3, 0, “Quintile 3”);
fact.GetCell(0, 4, 0, “Quintile 4”);
fact.GetCell(0, 5, 0, “Quintile 5 (Lowest)”);
fact.GetCell(0, 6, 0, “None”);

int i = 0;
string insertedValue = “”;
//for (int i = 0; i < extractedData.Rows.Count; i++)
for (int a = 0; a < chart.ChartData.Series.Count;a++)
{
series = chart.ChartData.Series[a];
for (int j = 0; j < series.Values.Count; j++)
{
insertedValue = extractedData.Rows[i][0].ToString();
i++;
series.Values[j].Value = Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero);
}
}
pres.Write(path + “ModifiedBarChart.pptx”);
}


The other option that you can use in your code sample is to use the following method for setting values in cells as shown below.

if (i < 6)
{
fact.GetCell(0, i + 1, 1, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
}

else if (i < 12)
{
fact.GetCell(0, count + 1, 2, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count++;
}
else if (i < 18)
{
fact.GetCell(0, count1 + 1, 3, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count1++;
}
else if (i < 24)
{
fact.GetCell(0, count2 + 1, 4, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count2++;
}
else if (i < 30)
{
fact.GetCell(0, count3 + 1, 5, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count3++;
}
else if (i < 36)
{
fact.GetCell(0, count4 + 1, 6, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count4++;
}
else if (i < 42)
{
fact.GetCell(0, count5 + 1, 7, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count5++;
}

Please share, if I may help you further in this regard.

Many Thanks,

the garph is working fine but the values are being put in the wrong place… the file i have attached is showing the results program should create and on the second slide the putput that i am getting form the code that has been provided… as can be seen on the second slide that is in the first bar is supposed to be in the bottom line and not going up in the upwoulds

Hi Jimmy,


I like to share that I actually shared the mechanism for updating chart series with dummy data and its up to you to populate the data accordingly in the chart series. The logical way is to populate the data by traversing through every series and setting the values of respective categories in the series in order. I have devised the workaround that you are looking for by modifying the code sample that you have shared earlier. But, I would like to add here is that prescribed way to modify chart data is to modify that according to series. If you follow the approach that you have shared earlier then you have to manage the order of data and its verification by your self.

public static System.Data.DataTable GetBarTable2()
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add(“BarValue”, typeof(double));
// table.Columns.Add(“NormalValue”, typeof(double));
//First series data
table.Rows.Add(12);
table.Rows.Add(23);
table.Rows.Add(16);
table.Rows.Add(28);
table.Rows.Add(17);
table.Rows.Add(43);



//2nd series data
table.Rows.Add(11);
table.Rows.Add(44);
table.Rows.Add(23);
table.Rows.Add(53);
table.Rows.Add(23);
table.Rows.Add(34);

table.Rows.Add(55);
table.Rows.Add(31);
table.Rows.Add(27);
table.Rows.Add(35);
table.Rows.Add(53);
table.Rows.Add(14);

//3rd series data
table.Rows.Add(75);
table.Rows.Add(63);
table.Rows.Add(87);
table.Rows.Add(65);
table.Rows.Add(45);
table.Rows.Add(34);

//4TH series data
table.Rows.Add(35);
table.Rows.Add(71);
table.Rows.Add(77);
table.Rows.Add(25);
table.Rows.Add(13);
table.Rows.Add(64);

//5TH series data
table.Rows.Add(35);
table.Rows.Add(61);
table.Rows.Add(77);
table.Rows.Add(35);
table.Rows.Add(83);
table.Rows.Add(44);

//6TH series data
table.Rows.Add(15);
table.Rows.Add(21);
table.Rows.Add(47);
table.Rows.Add(39);
table.Rows.Add(23);
table.Rows.Add(44);


return table;
}

public static void ModifyBarChart()
{

String path = @“D:\Aspose Data”;
PresentationEx pres = new PresentationEx(path + “Help2.pptx”);
SlideEx slide = pres.Slides[0];
ChartEx chart = (ChartEx)slide.Shapes[0];
ChartDataEx data = chart.ChartData;
// series.XValues.RemoveAt(0);
// series.YValues.RemoveAt(0);
System.Data.DataTable extractedData = GetBarTable2();

int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
ChartDataCellFactory fact = chart.ChartData.ChartDataCellFactory;
ChartSeriesEx series = chart.ChartData.Series[0];
fact.GetCell(0, 0, 1, “Newspapers”);
fact.GetCell(0, 0, 2, “Magazines”);
fact.GetCell(0, 0, 3, “Television”);
fact.GetCell(0, 0, 4, “Internet”);
fact.GetCell(0, 0, 5, “Outdoor Media”);
fact.GetCell(0, 0, 6, “Cinema”);
fact.GetCell(0, 0, 7, “Radio”);

//Setting series names
fact.GetCell(0, 1, 0, “Quintile 1 (Highest)”);
fact.GetCell(0, 2, 0, “Quintile 2”);
fact.GetCell(0, 3, 0, “Quintile 3”);
fact.GetCell(0, 4, 0, “Quintile 4”);
fact.GetCell(0, 5, 0, “Quintile 5 (Lowest)”);
fact.GetCell(0, 6, 0, “None”);

// int i = 0;
string insertedValue = “”;


for (int i = 0; i < extractedData.Rows.Count; i++)
{
insertedValue = extractedData.Rows[i][0].ToString();

if (i < 6)
{
fact.GetCell(0, i + 1, 1, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
}

else if (i < 12)
{
fact.GetCell(0, count + 1, 2, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count++;
}
else if (i < 18)
{
fact.GetCell(0, count1 + 1, 3, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count1++;
}
else if (i < 24)
{
fact.GetCell(0, count2 + 1, 4, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count2++;
}
else if (i < 30)
{
fact.GetCell(0, count3 + 1, 5, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count3++;
}
else if (i < 36)
{
fact.GetCell(0, count4 + 1, 6, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count4++;
}
else if (i < 42)
{
fact.GetCell(0, count5 + 1, 7, Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero));
count5++;
}

}
/* for (int a = 0; a < chart.ChartData.Series.Count;a++)
{
series = chart.ChartData.Series[a];
for (int j = 0; j < series.Values.Count; j++)
{
insertedValue = extractedData.Rows[i][0].ToString();
i++;
series.Values[j].Value = Math.Round(Convert.ToDouble(insertedValue), MidpointRounding.AwayFromZero);
}
}*/
pres.Write(path + “ModifiedBarChart.pptx”);
}

Many Thanks,