Free Support Forum - aspose.com

Aspose Slides - Chart Creation using JSON

Hi Team,

Recently started exploring Aspose Slides.net for Chart creation in PPT. Already license holder for Aspose.cells.

Need to create a multi Line Chart in PPT from my JSON.
Series1 : Metric1
Key : “Date Period” (Valid Date Ranges as per data Ex: 01/01/2000 to 31/12/2000)
Values : above each date range Values for Charts
X-Axis - Key
Y-Axis - Values

Series2 : Metric2
Key : “Date Period” (Valid Date Ranges as per data Ex: 01/01/2000 to 31/12/2000)
Values : above each date range Values for Charts
X-Axis - Key
Y-Axis - Values

Can you guide me some one to create multi line chart from JSON data as input using Aspose.Slides.Net (c#).

Regards,
Venkat Raman

@villan328,
Welcome to our community! Thank you for the query. It will take me a while to help you. I will answer you as soon as possible.

@villan328,
You should parse your JSON data first, and then you can create the multi-line chart as shown below:

string seriesName0 = "Metric1";
string seriesName1 = "Metric2";
double[] series0Values = new double[] { 1, 2, 3, 4, 5 };
double[] series1Values = new double[] { 5, 4, 3, 2, 1 };

DateTime[] categories = new DateTime[]
{
    new DateTime(2021, 1, 1),
    new DateTime(2021, 2, 1),
    new DateTime(2021, 3, 1),
    new DateTime(2021, 4, 1),
    new DateTime(2021, 5, 1)
};

using (Presentation pres = new Presentation())
{
    IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.Line, 100, 100, 500, 400, false);

    //fill categories
    IChartDataWorkbook wb = chart.ChartData.ChartDataWorkbook;
    for (int i = 0; i < categories.Length; i++)
    {
        chart.ChartData.Categories.Add(wb.GetCell(0, i + 1, 0, categories[i].ToOADate()));
    }

    //fill first series data
    IChartSeries series = chart.ChartData.Series.Add(ChartType.Line);
    series.Name.AsCells.Add(wb.GetCell(0, 0, 1, seriesName0));
    for (int i = 0; i < series0Values.Length; i++)
    {
        series.DataPoints.AddDataPointForLineSeries(wb.GetCell(0, i + 1, 1, series0Values[i]));
    }

    //fill second series data
    series = chart.ChartData.Series.Add(ChartType.Line);
    series.Name.AsCells.Add(wb.GetCell(0, 0, 2, seriesName1));
    for (int i = 0; i < series1Values.Length; i++)
    {
        series.DataPoints.AddDataPointForLineSeries(wb.GetCell(0, i + 1, 2, series1Values[i]));
    }

    //setup axes
    chart.Axes.HorizontalAxis.CategoryAxisType = CategoryAxisType.Date;
    chart.Axes.HorizontalAxis.NumberFormat = "dd/mm/yyyy";
    chart.Axes.HorizontalAxis.IsNumberFormatLinkedToSource = false;
    chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
    chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;

    chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;

    //save result
    pres.Save(dataPath + "LineChart.pptx", SaveFormat.Pptx);
}

More examples: Create Chart
API Reference: IChart Interface

@Andrey_Potapov: Thanks for sharing the code.
Let me try from my end and update you shortly…