Search code examples
c#.netvstopowerpoint

How to change color of individual chart elements color in powerpoint


I have an issue, suppose a chart (with all data required) is already inserted in powerpoint slide .Now what i want to do is if a person clicks on chart title or bars/columns or legend or chart background, the back color of that element should change to, say, black color. In word app I am able to do same with

application.Selection.InlineShapes[1].Fill.ForeColor.RGB = setColor;

How to do same in powerpoint.

What I tried in powerpoint is:

application.ActiveWindow.Selection.ShapeRange[1].Fill.BackColor.RGB = 0;

But this always changes color of chart background and Selection.ShapeRange[1].Name is always "placeholder".

Only want to change color of single element. If user click on 1 bar, the color of that bar should change to black and no other element should change similarly if chart title is clicked background of that only should change. I am new to .net and help on this. And I think I am able to explain my concern . Last thing what i tried is

ColorStyle colorstyle2 = MyAddin.AddinModule.CurrentInstance.SelectedColorStyle;
int setColor = ColorTranslator.ToOle(System.Drawing.Color.FromArgb(colorstyle2.R, colorstyle2.G, colorstyle2.B));
int position = app.ActiveWindow.Selection.ShapeRange.ZOrderPosition;
Slide slide2 = (Slide)app.ActiveWindow.View.Slide;// SELECTS CHART FROM SHAPE RANGE AS SELECTION HAS SHAPERANGE not shape
powerPoint.Shape s = slide2.Shapes[position];
if (s.HasChart==MsoTriState.msoTrue)
{
    object cht = s.GetType().InvokeMember("Chart", BindingFlags.GetProperty, null, (object)s, null);
    object chtFormat = cht.GetType().InvokeMember("Format", BindingFlags.GetProperty, null, (object)cht, null);
    object chtFill = cht.GetType().InvokeMember("Fill", BindingFlags.GetProperty, null, (object)chtFormat, null);
    object chtBackcolor = chtFill.GetType().InvokeMember("Backcolor", BindingFlags.GetProperty, null, (object)chtFill, null);
    object[] args = { 0 };//also used {0} here
    object chtRGB = chtBackcolor.GetType().InvokeMember("RGB", BindingFlags.SetProperty, null, (object)chtBackcolor, args);
}

Solution

  • I have posted this questions on many forums but seems like there is no solution.Although I have a work around there is a property chart.GetChartElement use it like

    int ElementId = -1, arg1 = -1, arg2 = -1;    
    chart.GetChartElement(X, Y, ref ElementId, ref arg1, ref arg2) 
    //in which 
    //X is relative position of element wrt to chart 
    // similarly Y
    var chartitem = (powerPoint.XlChartItem)ElementId;
    switch (chartitem)
    {
     case powerPoint.XlChartItem.xlDataLabel:break;
     case powerPoint.XlChartItem.xlChartArea:break;
     case powerPoint.XlChartItem.xlSeries:break;
     case powerPoint.XlChartItem.xlChartTitle:break;
     case powerPoint.XlChartItem.xlDataTable:break;
     case powerPoint.XlChartItem.xlMajorGridlines:break;
     case powerPoint.XlChartItem.xlMinorGridlines:break;
     case powerPoint.XlChartItem.xlAxisTitle:break;
     case powerPoint.XlChartItem.xlPlotArea:break;
     case powerPoint.XlChartItem.xlAxis:break;
     case powerPoint.XlChartItem.xlLegendEntry:break;
     case powerPoint.XlChartItem.xlLegendKey:break;
     case powerPoint.XlChartItem.xlLegend:break;
     default: break;
    }
    

    agr1 and arg2 gives extra info like which series/data label is clicked and which point .The result is 90 percent accurate as it depends on position which is clicked.