How to create Scatter chart in pptx using OpenXML C#

Vineeta Shukla 1 Reputation point
2021-02-25T06:57:36.31+00:00

I am trying to create Scatter chart in PPT using OpenXML C#. but the chart series not showing on graph. though I can see the attached excel file data after click on edit data of graph.

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,656 Reputation points
    2021-02-26T05:41:38.96+00:00

    Hi VineetaShukla-4156,
    I suggest you can try to use Spire.Presentation to create scatter chart in PowerPoint.
    Compared to OpenXML, it's relatively simple and straightforward.
    Here is a code example you can refer to.

    Presentation presentation = new Presentation();  
    //Add a "Scatter with Smooth Lines and Markers" chart to the first slide, and set the chart title.  
    IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ScatterSmoothLinesAndMarkers, new RectangleF(40, 80, 550, 320), false);  
    chart.ChartTitle.TextProperties.Text = "Scatter Chart";  
    chart.ChartTitle.TextProperties.IsCentered = true;  
    chart.ChartTitle.Height = 20;  
    chart.HasTitle = true;  
    //Write data to the chart data.  
    Double[] xdata = new Double[] { 1.0, 2.4, 5.0, 8.9 };  
    Double[] ydata = new Double[] { 5.3, 15.2, 6.7, 8 };  
    chart.ChartData[0, 0].Text = "X-Values";  
    chart.ChartData[0, 1].Text = "Y-Values";  
    for (Int32 i = 0; i < xdata.Length; ++i)  
    {  
        chart.ChartData[i + 1, 0].Value = xdata[i];  
        chart.ChartData[i + 1, 1].Value = ydata[i];  
    }  
    //Set up the data source of the X values, the Y values, and the series label.  
    chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];  
    chart.Series[0].XValues = chart.ChartData["A2", "A5"];  
    chart.Series[0].YValues = chart.ChartData["B2", "B5"];  
    //Add and display the data labels in the chart.  
    for (int i = 0; i < 4; i++)  
    {  
        ChartDataLabel dataLabel = chart.Series[0].DataLabels.Add();  
        dataLabel.LabelValueVisible = true;  
    }  
      
    chart.PrimaryValueAxis.HasTitle = true;  
    chart.PrimaryValueAxis.Title.TextProperties.Text = "X-Axis Title";  
    chart.SecondaryValueAxis.HasTitle = true;  
    chart.SecondaryValueAxis.Title.TextProperties.Text = "Y-Axis Title";  
    //Format the gridlines.  
    chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.Solid;  
    chart.SecondaryValueAxis.MajorGridTextLines.Style = TextLineStyle.ThinThin;  
    chart.SecondaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.Gray;  
    chart.PrimaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None;  
    //Format the outline.  
    chart.Series[0].Line.FillType = FillFormatType.Solid;  
    chart.Series[0].Line.Width = 0.1f;  
    chart.Series[0].Line.SolidFillColor.Color = Color.RoyalBlue;  
    //Save the file.  
    presentation.SaveToFile(@"C:\Users\Desktop\ScatterChart.pptx", FileFormat.Pptx2010);  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.