Adding line chart series and data points dynamically using ASPOSE.slides for .Net

Hi,
I googled quite a bit and only found out a certain way to add a new line series using IChartDataCellFactory. But I am not able to find this interface in my ASPOSE.slides dll. I am trying to add line series to a blank chart in a power point presentation. I am using a licensed version of aspose slides dll. Please help!!

Thanks,
Naeem

@naeem.gittham,

Thanks for inquiring Aspose.Slides.

I have observed the requirements shared by you and like to share that you can create MSO charts using Aspose.Slides. I suggest you to please visit this documentation link for creating a line series chart. Please share, if I may help you further in this regard.

Many Thanks,

Mudassir Fayyaz

Hi Fayyaz,
The example you have given doesn’t relate to my problem at all. I have a powerpoint presentation with an empty line chart object already in place. I am trying to get an instance of the chart object series and add datapoints dynamically from a json data source. Here is the code snippet. I am able to add a series dynamically as shown below but not sure how to add datapoints for each series. I have a max of 5 series. Thanks for your help!!

//Iterate through the shapes and set a reference to the table found
foreach (IShape shp in sld.Shapes)
{
if (shp is IChart)
{
chrt = (IChart)shp;

                        if (chrt.Name.ToLower().Equals("yield curves"))
                        {
                            //Setting the index of chart data sheet
                            int defaultWorksheetIndex = 0;

                            chrt.ChartData.Series.Clear();  //clear all default generated series

                            var seriesCount = 0;
                            IChartDataWorkbook fact = chrt.ChartData.ChartDataWorkbook;
                            
                            foreach (var sy in curves.First().shockedYields)
                            {
                                //chrt.ChartData.Series.Add(ChartType.Line);
                                chrt.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 3, "Series 3"), chrt.Type);
                                var datapointCount = 0;
                                foreach (var dp in sy)
                                {
                                    IChartSeries series = chrt.ChartData.Series[seriesCount];

                                    IChartDataPoint point = series.DataPoints.AddDataPointForLineSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, (double)4.5));

                                    //series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, datapointCount+1, 3, (double)dp));
                                    



                                    datapointCount++;
                                }
                                seriesCount++;
                            }

                            break;
                        }
                    }
                }

@naeem.gittham,

I have tried to understand your requirements after observing your sample code and have generated a workable sample code for your convenience to populate the chart data points for chart categories inside chart series. As per your requirement, I have used an empty chart with only one series and no categories data. In sample code, you will be adding category and its respective chart series data point value on run time. I hope this will be useful on your end.

public static void AddLineChart()
{
    string path = @"C:\Aspose Data\";

    Presentation pres = new Presentation(path + "LineChart.pptx");

    ISlide slide = pres.Slides[0];
    IChart chrt = (IChart)slide.Shapes[0];

    //Setting the index of chart data sheet
    int defaultWorksheetIndex = 0;


    var seriesCount = 0;
    IChartDataWorkbook fact = chrt.ChartData.ChartDataWorkbook;


    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.Add("Category", typeof(string));
    dt.Columns.Add("Value", typeof(int));

    // Create a DataRow, add Name and Age data, and add to the DataTable
    System.Data.DataRow dr = dt.NewRow();
    dr["Category"] = "Cat 1";
    dr["Value"] = 24; 
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["Category"] = "Cat 2";
    dr["Value"] = 50; // 
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["Category"] = "Cat 3"; 
    dr["Value"] = 80; 
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["Category"] = "Cat 4"; 
    dr["Value"] = 35; 
    dt.Rows.Add(dr);

    for (int i = 0; i < dt.Rows.Count;i++ )
    {
        dr = dt.Rows[i];

        chrt.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, i + 1, 0, dr["Category"].ToString()));

    }
    //only one series in line chart
    IChartSeries series = chrt.ChartData.Series[0];
    series.DataPoints.Clear();

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        dr = dt.Rows[i];

        chrt.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, i + 1, 0, dr["Category"].ToString()));
        IChartDataPoint point = series.DataPoints.AddDataPointForLineSeries(fact.GetCell(defaultWorksheetIndex, i+1, 1, Convert.ToDouble(dr["Value"].ToString()) ));

    }

    pres.Save(path + "LineChart2.pptx",SaveFormat.Pptx);


}

Many Thanks,

Mudassir Fayyaz

LineChart.zip (65.7 KB)

Hi Fayyaz,
Your solution works for me. Thanks a lot!!

Naeem