none
Vary Chart Bar Graphs Color RRS feed

  • Question

  • Hello folks!

    I am creating an Excel chart using Excel Interop (please find below code and output for the code given below). I need to vary the colors of same-series chart bar graphs (The graphs only now in orange). This line of code doesn't do the trick:

     series1.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
     How can I achieve my goal? Any advice will be appreciated!
                Excel.ChartObjects xlCharts = (Excel.ChartObjects)_sheet.ChartObjects(Type.Missing);
                Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(236, 241, 224, 268);
                Excel.Chart chartPage = myChart.Chart;
    
                chartPage.HasLegend = false;
    
                chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
    
                chartPage.ChartStyle = 4;
    
                Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
    
                Excel.Series series1 = seriesCollection.NewSeries();
                series1.Name = "My Title";
    
                series1.XValues = xValues;
                series1.Values = yValues;
    
               series1.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
    
                Excel.Axis axis = (Excel.Axis)chartPage.Axes(
                                           Excel.XlAxisType.xlValue,
                                          Excel.XlAxisGroup.xlPrimary);
    
                axis.HasTitle = true;
                axis.AxisTitle.Text = "My Chart";
    
                series1.ApplyDataLabels(
                                              Excel.XlDataLabelsType.xlDataLabelsShowLabel,
                                               true, true, true, false, false, true, true,
                                               true, true);
    

                

    Thursday, January 29, 2015 11:39 AM

Answers

  • Hi hokushin,

    The series1.Interior.ColorIndex property can only set the color for the whole series. If you want to vary the color of the bar graphs in the same series, one way is to vary the color by the Point.  For example:

    int pointsCount = series1.Points().Count;
                for (int i = 1; i <= pointsCount; i++)
                {
                    series1.Points(i).Interior.Color = Color.FromArgb(0, 30*i, 0);
                }

    You can set the color as you want for every Point of the series, the series1.Points([point number]) method will return the whole Points collection or just one Point of them.


    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.


    • Edited by CaillenModerator Friday, January 30, 2015 5:43 AM
    • Marked as answer by hokushin Friday, January 30, 2015 6:30 AM
    Friday, January 30, 2015 5:39 AM
    Moderator

All replies

  • Hi hokushin,

    The series1.Interior.ColorIndex property can only set the color for the whole series. If you want to vary the color of the bar graphs in the same series, one way is to vary the color by the Point.  For example:

    int pointsCount = series1.Points().Count;
                for (int i = 1; i <= pointsCount; i++)
                {
                    series1.Points(i).Interior.Color = Color.FromArgb(0, 30*i, 0);
                }

    You can set the color as you want for every Point of the series, the series1.Points([point number]) method will return the whole Points collection or just one Point of them.


    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.


    • Edited by CaillenModerator Friday, January 30, 2015 5:43 AM
    • Marked as answer by hokushin Friday, January 30, 2015 6:30 AM
    Friday, January 30, 2015 5:39 AM
    Moderator
  • Caillen, thanks a lot for resolving the problem with detailed explanation. Your workaround did the trick :-) Appreciate it.
    Friday, January 30, 2015 6:30 AM