.NET Framework Developer Center >
.NET Development Forums
>
Chart Controls for .NET Framework
>
Different Color For Bar in Chart (What am I doing wrong?)
Different Color For Bar in Chart (What am I doing wrong?)
- I'm trying to change the color of the bar in a chart but its just not working, can anyone shed any light on why?
Dim chart1 As Chart = chtTest chart1.Series.Add("Series1") chart1.Series.Add("Series2") chart1.PaletteCustomColors = New Color() {Color.Red, Color.Blue} For Each r As DataRow In dt.Rows chart1.Series("Series1").Points.AddXY(r.Item("AcceptedBy"), r.Item("PercentCommited")) Select Case r.Item("PercentCommited") Case 100 chart1.Series("Series2").Points.AddY(0) Case Else diff = 100 - r.Item("PercentCommited") chart1.Series("Series2").Points.AddY(diff) End Select Next Dim fp As Integer = 0 For Each Point As DataPoint In chart1.Series("Series1").Points If fp = 0 Then Point.Color = Color.Blue fp = 1 End If Next chart1.Series("Series1").ChartType = SeriesChartType.StackedBar chart1.Series("Series2").ChartType = SeriesChartType.StackedBarIts possibly something simple but I just need to change on color on series1 to identify something. Data comes from a sql backend and it binds fine using the above.
Answers
- If you want PaletteCustomColors to work, you have to set Chart.Palette to ChartColorPalette.None.
The color setting for the point should work nevertheless. Could it be you just missed the color change since the default color for your "Series1" is also a shade of blue?
I ran this piece of code and it worked just as expected:
Private Sub ExampleForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Init chart Chart1.ChartAreas.Clear() Chart1.Series.Clear() Chart1.PaletteCustomColors = New Color() {Color.Red, Color.Blue} Chart1.Palette = ChartColorPalette.None 'Set the Chart.Palette to None so that the CustomColors work. 'Create a chartarea Chart1.ChartAreas.Add("AREA") 'Create a series Chart1.Series.Add("SERIES") Chart1.Series("SERIES").ChartType = SeriesChartType.StackedBar 'Add data to series Chart1.Series("SERIES").Points.AddXY(1, 50) Chart1.Series("SERIES").Points.AddXY(2, 25) Chart1.Series("SERIES").Points.AddXY(3, 70) Chart1.Series("SERIES").Points.AddXY(4, 20) 'Create another series Chart1.Series.Add("SERIES2") Chart1.Series("SERIES2").ChartType = SeriesChartType.StackedBar 'Add data to series Chart1.Series("SERIES2").Points.AddXY(1, 30) Chart1.Series("SERIES2").Points.AddXY(2, 15) Chart1.Series("SERIES2").Points.AddXY(3, 40) Chart1.Series("SERIES2").Points.AddXY(4, 5) 'Sets the color of the first point in SERIES to Color.Blue '(Will look like the whole bar is blue, since SERIES2 is blue too) Dim fp As Integer = 0 For Each Point As DataPoint In Chart1.Series("SERIES").Points If fp = 0 Then Point.Color = Color.Blue fp = 1 End If Next End Sub
- Marked As Answer byMat Johnson Monday, November 09, 2009 10:29 AM
All Replies
- I think you might have to either do an if...else on color assignment or you may have to applycustomcolors before changing the dp color...
Please don't forget (and feel free to remind me) to post if you got the answer you wanted, and select who really answered your post when you do so future visitors will know too! Remember, this is .NET 4.0 in a .NET 3.5 world, you're a pioneer right now. - Hmm, just looked at that doing an if else. Nothing happened, So i've just looked at the Point.Color in the imidiate window and it just lists the colors so it doesn't look like I can set the property.
Would I need to do something like Point.Color.PalletCustomColors = New Color() {Color.Red, Color.Blue} or is that just rubbish? - If you want PaletteCustomColors to work, you have to set Chart.Palette to ChartColorPalette.None.
The color setting for the point should work nevertheless. Could it be you just missed the color change since the default color for your "Series1" is also a shade of blue?
I ran this piece of code and it worked just as expected:
Private Sub ExampleForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Init chart Chart1.ChartAreas.Clear() Chart1.Series.Clear() Chart1.PaletteCustomColors = New Color() {Color.Red, Color.Blue} Chart1.Palette = ChartColorPalette.None 'Set the Chart.Palette to None so that the CustomColors work. 'Create a chartarea Chart1.ChartAreas.Add("AREA") 'Create a series Chart1.Series.Add("SERIES") Chart1.Series("SERIES").ChartType = SeriesChartType.StackedBar 'Add data to series Chart1.Series("SERIES").Points.AddXY(1, 50) Chart1.Series("SERIES").Points.AddXY(2, 25) Chart1.Series("SERIES").Points.AddXY(3, 70) Chart1.Series("SERIES").Points.AddXY(4, 20) 'Create another series Chart1.Series.Add("SERIES2") Chart1.Series("SERIES2").ChartType = SeriesChartType.StackedBar 'Add data to series Chart1.Series("SERIES2").Points.AddXY(1, 30) Chart1.Series("SERIES2").Points.AddXY(2, 15) Chart1.Series("SERIES2").Points.AddXY(3, 40) Chart1.Series("SERIES2").Points.AddXY(4, 5) 'Sets the color of the first point in SERIES to Color.Blue '(Will look like the whole bar is blue, since SERIES2 is blue too) Dim fp As Integer = 0 For Each Point As DataPoint In Chart1.Series("SERIES").Points If fp = 0 Then Point.Color = Color.Blue fp = 1 End If Next End Sub
- Marked As Answer byMat Johnson Monday, November 09, 2009 10:29 AM
- K, just to complete the solution, I've changed the details to add the items into the column
For Each r As DataRow In dt.Rows Dim point As New DataPoint point.SetValueXY(r.Item("AcceptedBy"), r.Item("PercentCommited")) 'chart1.Series("Series1").Points.AddXY(r.Item("AcceptedBy"), r.Item("PercentCommited")) Select Case r.Item("PercentCommited") Case 100 chart1.Series("Series2").Points.AddY(0) Case Else diff = 100 - r.Item("PercentCommited") chart1.Series("Series2").Points.AddY(diff) End Select If CBool(r.Item("isActualLimit")) Then point.Color = Color.Blue End If chart1.Series("Series1").Points.Add(point) Next


