none
VB.net Excel - Chart - Color of the bars in a chart RRS feed

  • Question

  • Hi,<o:p></o:p>

    I have created a simple barchart in Excel using VS2013 Pro (using Visual Basic). 
    Everything works and appears fine.<o:p></o:p>

    A fragment is given below.  In the "With chart_Format" section each line such as ".ChartTitle.Characters.Font.Name" comes up
    with Intellisense prompts.  However after ".SeriesCollection(1)." there is no Intellisense and I have added "Interior.Color" in a similar way that I used in VB6.<o:p></o:p>

    I have found that this works BUT I am concerned that this is not the correct way to set a barchart color in Visual Studio.<o:p></o:p>

    Is this an acceptable way or is it a VBA method that happens to work? 

    Is there a better VS way to set the color of a barchart??

    Also I have looked at "ChartColor" as an alternative method.  There is very little information regarding this Method - MSDN only ststes that the color is "set by an integer".  The "Interior.Color" method seems to be preferred.<o:p></o:p>

    All comments are very welcome<o:p>!</o:p>

    Thanks for your time<o:p></o:p>

    Margarita<o:p></o:p>

    Dim xlCharts As Excel.ChartObjects

    Dim chart_Investigate As Excel.ChartObject

    Dim chart_Format As Excel.Chart

    Dim chart_Range As Excel.Range

    xlCharts = oSheet.ChartObjects

    chart_Investigate = xlCharts.Add(320, 160, 400, 400)

    chart_Range = oSheet.Range("C2" , "D20")

    chart_Format = chart_Investigate.Chart

    With chart_Format

         .SetSourceData(Source:=chart_Range)

         .ChartType = Excel.XlChartType.xlColumnClustered

         .HasTitle = True

         .ChartTitle.Characters.Text = "xxxxx"

         .ChartTitle.Characters.Font.Name = "Arial"

         .ChartTitle.Characters.Font.Bold = True

         .ChartTitle.Characters.Font.Size = 14

         .HasLegend = False

         .SeriesCollection(1).Interior.Color = bar_color

    End With<o:p></o:p>



    Winchestermili

    Tuesday, April 7, 2015 8:08 AM

Answers

  • I would say if it works it works:)

    Not sure if it's a better way but for setting chart element format properties look at the Format object, eg

    mySeries.Format.Fill.RGB = lngColor

    You'd also need to use the Format Fill or Line object to apply a palette schemecolor, with .ObjectThemeColor, .TintAndShade, & .Brightness 

    In passing since 2007 you can add charts with sheet.Shapes.AddChart and io 2013 with AddChart2 which includes as its first argument a Style property

    • Marked as answer by L.HlModerator Thursday, April 16, 2015 2:11 AM
    Tuesday, April 7, 2015 10:22 AM
    Moderator