Hello! I have a problem with the column's color of a chart with a single serie.
If the value is negative, the color of the column is transparent. And i set that all the columns must be Blue. If the value is positive, the column is blue.
Here is my code.
createPowerpoint()
{
presentation.Slides.Append();
//insert chart
RectangleF rect = new RectangleF(50, 50, 600, 400);
IChart chart = presentation.Slides[5].Shapes.AppendChart(ChartType.ColumnClustered, rect);
//add chart Title
chart.ChartTitle.Height = 30;
chart.HasTitle = true;
//load data
DataTable dataTable = LoadData();
//load data from datatable to chart
InitChartData(chart, dataTable);
chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];
chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];
chart.Series[0].Values = chart.ChartData["B2", "B5"];
chart.Series[0].Fill.SolidColor.Color =Color.Blue;
chart.Series[0].Fill.FillType = FillFormatType.Solid;
chart.HasLegend = false;
chart.PrimaryValueAxis.HasTitle = true;
chart.PrimaryValueAxis.Title.TextProperties.Text = "Title";
}
//function to load data from XML file to DataTable
private DataTable LoadData()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Categoria"));
dt.Columns.Add(new DataColumn("Valor"));
DataRow dr1 = dt.NewRow();
dr1["Categoria"] = "Category 1";
dr1["Valor"] = 0;
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["Categoria"] = "Category 2";
dr2["Valor"] = -1;
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3["Categoria"] = "Category 3";
dr3["Valor"] = -4;
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4["Categoria"] = "Category 4";
dr4["Valor"] = 32;
dt.Rows.Add(dr4);
return dt;
}
//function to load data from DataTable to IChart
private void InitChartData(IChart chart, DataTable dataTable)
{
for (int c = 0; c<dataTable.Columns.Count; c++)
{
chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;
}
for (int r = 0; r<dataTable.Rows.Count; r++)
{
object[] data = dataTable.Rows[r].ItemArray;
for (int c = 0; c<data.Length; c++)
{
if (c == 0)
chart.ChartData[r + 1, c].Value = data[c];
else
{
chart.ChartData[r + 1, c].Value = decimal.Parse(data[c].ToString());
}
}
}
}