Hi,
I want to create a chart with values coming through database. Right now from your examples it seems it can only be done through excel sheet and i have to populate excel sheet first then do the chart stuff.
-Majid
//initialize an instance of Presentation class
Presentation ppt = new Presentation();
RectangleF rect = new RectangleF(40, 100, 550, 320);
IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);
//clear the default dummy data
chart.ChartData.Clear(0, 0, 5, 5);
chart.Series.Clear();
chart.Categories.Clear();
using (OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand))
{
DataTable dt = new DataTable();
da.Fill(dt);
InitChartData(chart, dt);
ppt.SaveToFile("14355.pptx",FileFormat.Ppsx2010);
}
private void InitChartData(IChart chart, DataTable dataTable)
{
//set series name
ChartSeriesFormatCollection ctf = chart.Series;
for (int c = 0; c < dataTable.Columns.Count; c++)
{
chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;
if (c > 0)
{
ctf.Append(chart.ChartData[0, c]);
}
}
ChartCategoryCollection catg = chart.Categories;
for (int r = 0; r < dataTable.Rows.Count; r++)
{
//set category
catg.Append(chart.ChartData[r + 1, 0]);
//set chartData value
object[] data = dataTable.Rows[r].ItemArray;
for (int c = 0; c < data.Length; c++)
{
chart.ChartData[r + 1, c].Value = data[c];
}
//set series value
for (int i = 0; i < ctf.Count; i++)
{
chart.Series[i].Values.Add(chart.ChartData[r + 1, i + 1]);
}
}
}
#region Chart1
RectangleF rec = new RectangleF(25, 40, 325, 225);
IChart chart1 = ppt.Slides[0].Shapes.AppendChart(Spire.Presentation.Charts.ChartType.BarClustered, rec);
chart1.ChartTitle.TextProperties.Text = V2.ToString();
chart1.ChartTitle.TextProperties.IsCentered = false;
chart1.ChartTitle.Height = 30;
chart1.HasTitle = true;
// clear the default dummy data
chart1.ChartData.Clear(0, 0, 5, 5);
chart1.Series.Clear();
chart1.Categories.Clear();
// Lines of Code by Location
SqlCommand sqlcmdLOC = new SqlCommand(sqlQueryLOC, sqlconn);
using (SqlDataAdapter da1 = new SqlDataAdapter(sqlcmdLOC))
{
DataTable dt1 = new DataTable();
da1.Fill(dt1);
InitChartData(chart1, dt1);
sqlconn.Close();
}
#endregion
#region InitChartData Helper
private static void InitChartData(IChart chart1, DataTable dt1)
{
//set series name
ChartSeriesFormatCollection ctf = chart1.Series;
for (int c = 0; c < dt1.Columns.Count; c++)
{
chart1.ChartData[0, c].Text = dt1.Columns[c].Caption;
if (c > 0)
{
ctf.Append(chart1.ChartData[0, c]);
}
}
ChartCategoryCollection catg = chart1.Categories;
for (int r = 0; r < dt1.Rows.Count; r++)
{
//set category
catg.Append(chart1.ChartData[r + 1, 0]);
//set chartData value
object[] data = dt1.Rows[r].ItemArray;
for (int c = 0; c < data.Length; c++)
{
chart1.ChartData[r + 1, c].Value = data[c];
}
//set series value
for (int i = 0; i < ctf.Count; i++)
{
chart1.Series[i].Values.Add(chart1.ChartData[r + 1, i + 1]);
}
}
}
#endregion
static void Main(string[] args)
{
Presentation presentation = new Presentation();
RectangleF rect = new RectangleF(40, 100, 550, 320);
IChart chart1 = presentation.Slides[0].Shapes.AppendChart(ChartType.BarClustered, rect);
chart1.ChartTitle.TextProperties.Text = "sales";
chart1.ChartTitle.TextProperties.IsCentered = true;
chart1.ChartTitle.Height = 30;
chart1.HasTitle = true;
chart1.ChartData.Clear(0, 0, 5, 5);
chart1.Series.Clear();
chart1.Categories.Clear();
//Create a datetable
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Country", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Jun", Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("Aug", Type.GetType("System.Int32")));
dt.Rows.Add("Cuba", "6000", "3200");
dt.Rows.Add("Mexico", "8000", "2000");
dt.Rows.Add("France", "9000", "4000");
dt.Rows.Add("German", "8500", "2300");
InitChartData(chart1, dt);
presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);
}
private static void InitChartData(IChart chart1, DataTable dt1)
{
//set series name
ChartSeriesFormatCollection ctf = chart1.Series;
for (int c = 0; c < dt1.Columns.Count; c++)
{
chart1.ChartData[0, c].Text = dt1.Columns[c].Caption;
if (c > 0)
{
ctf.Append(chart1.ChartData[0, c]);
}
}
ChartCategoryCollection catg = chart1.Categories;
for (int r = 0; r < dt1.Rows.Count; r++)
{
//set category
catg.Append(chart1.ChartData[r + 1, 0]);
//set chartData value
object[] data = dt1.Rows[r].ItemArray;
for (int c = 0; c < data.Length; c++)
{
chart1.ChartData[r + 1, c].Value = data[c];
}
//set series value
for (int i = 0; i < ctf.Count; i++)
{
chart1.Series[i].Values.Add(chart1.ChartData[r + 1, i + 1]);
}
}
}