none
Setting chart line color RRS feed

  • Question

  • How could I set the color of the line in line chart (presentation)? I take the template and change data for the chart, I found a "Marker" property in LineChartSeries so I can change a marker of the line, but how can I change the color (it would be provided to the generating function)?


    UPD

    Analyzing PowerPoint-generated XML after changing line color and my presentation XML I managed it. Solution looks like this:

    lineChartSeries.ChartShapeProperties = new ChartShapeProperties();
    var outline = lineChartSeries.ChartShapeProperties.AppendChild<DocumentFormat.OpenXml.Drawing.Outline>(new DocumentFormat.OpenXml.Drawing.Outline());
    var solid = outline.AppendChild<DocumentFormat.OpenXml.Drawing.SolidFill>(new DocumentFormat.OpenXml.Drawing.SolidFill());              
    solid.AppendChild<Drawing.RgbColorModelHex>(new Drawing.RgbColorModelHex() { Val = new HexBinaryValue() { Value = "ff0000" } });

    I wonder if they could make changing the color as easy as changing line marker. Just for information it can be done like this:

    lineChartSeries.Marker = new Marker() { Symbol = new Drawing.Charts.Symbol() { Val = MarkerStyleValues.Diamond } };




    • Edited by Melamori Monday, October 6, 2014 12:46 PM
    Monday, October 6, 2014 9:29 AM

Answers

  • Hi Melamori,

    Thanks for sharing with us the solution about changing line color.

    >>I wonder if they could make changing the color as easy as changing line marker. Just for information it can be done like this:<<

    We can change the color manually and compare the difference between the file changed before and after. And here is the difference:

    And here is the code to change the color:

     // Creates an Marker instance and adds its children.
            public Marker GenerateMarker()
            {
                Marker marker1 = new Marker();
                Symbol symbol1 = new Symbol(){ Val = MarkerStyleValues.Circle };
                Size size1 = new Size(){ Val = 5 };
    
                ChartShapeProperties chartShapeProperties1 = new ChartShapeProperties();
    
                A.SolidFill solidFill1 = new A.SolidFill();
                A.SchemeColor schemeColor1 = new A.SchemeColor(){ Val = A.SchemeColorValues.Accent2 };
    
                solidFill1.Append(schemeColor1);
    
                A.Outline outline1 = new A.Outline(){ Width = 9525 };
    
                A.SolidFill solidFill2 = new A.SolidFill();
                A.SchemeColor schemeColor2 = new A.SchemeColor(){ Val = A.SchemeColorValues.Accent1 };
    
                solidFill2.Append(schemeColor2);
    
                outline1.Append(solidFill2);
                A.EffectList effectList1 = new A.EffectList();
    
                chartShapeProperties1.Append(solidFill1);
                chartShapeProperties1.Append(outline1);
                chartShapeProperties1.Append(effectList1);
    
                marker1.Append(symbol1);
                marker1.Append(size1);
                marker1.Append(chartShapeProperties1);
                return marker1;
            }
    

    The code is for changing the line color for the line chart. And to make the code suitable for linechart, we need to find the chart first. And then we can modify the attibute for the marker via Marker Class.

    Here is a sample that modify the color of line chart of first slide to red:

      class ChangeChartColor
        {
            public void Main()
            {
                string docName = @"C:\Users\UserName\Desktop\Presentation3.pptx";              
                using (PresentationDocument document = PresentationDocument.Open(docName, true))
                {
                    document.GetIdOfPart(document.PresentationPart);
                    
    
                   SlidePart slide1= document.PresentationPart.GetPartsOfType<SlidePart>().First();
    
                   ChartPart chart1=slide1.GetPartsOfType<ChartPart>().First();
    
                   LineChart lineChart1= chart1.ChartSpace.Descendants<LineChart>().First();
                   LineChartSeries LineChartSeries1 = lineChart1.Descendants<LineChartSeries>().First();
                   Outline outline1=LineChartSeries1.Marker.ChartShapeProperties.Descendants<Outline>().First();
                   //outline1.Descendants<SolidFill>().First().SchemeColor = new SchemeColor(){ Val = SchemeColorValues.Accent2 };               
                   outline1.Descendants<SolidFill>().First().RgbColorModelHex = new RgbColorModelHex() { Val = "FF0000" };
                
                }
            }
        }

    Hope it is helpful.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 7, 2014 7:14 AM
    Moderator