Page 1 of 1
More controls over bar gradient
Posted: Tue Oct 27, 2009 10:44 pm
by 15051059
Hi,
I have a situation where I have 2 bar series. If the value of the 1st bar is greater than the value of the 2nd bar, I want the 2nd bar to be colored red; if it is the other way around, I want the 2nd bar to be colored green. The problem is I cannot apply gradient (From lighter red to darker red or from lighter green to darker green) if I choose to color each bar separately. Is it possible to give more controls over the bar gradient?
Re: More controls over bar gradient
Posted: Wed Oct 28, 2009 10:46 am
by yeray
Hi Raymond,
Here is an example doing it:
Code: Select all
Private Sub Form_Load()
TChart1.Aspect.View3D = False
TChart1.AddSeries scBar
TChart1.AddSeries scBar
TChart1.Series(0).FillSampleValues 5
TChart1.Series(1).FillSampleValues 5
TChart1.Series(0).Color = vbBlue
TChart1.Series(0).asBar.Gradient.Visible = True
TChart1.Series(0).asBar.Gradient.StartColor = RGB(150, 150, 255)
TChart1.Series(1).asBar.Gradient.Visible = True
TChart1.Environment.InternalRepaint
End Sub
Private Sub TChart1_OnGetSeriesBarStyle(ByVal SeriesIndex As Long, ByVal ValueIndex As Long, BarStyle As TeeChart.EBarStyle)
BarStyle = bsRectGradient
If SeriesIndex = 1 Then
If TChart1.Series(0).YValues.Value(ValueIndex) > TChart1.Series(1).YValues.Value(ValueIndex) Then
TChart1.Series(1).asBar.Gradient.StartColor = RGB(255, 150, 150)
Else
TChart1.Series(1).asBar.Gradient.StartColor = RGB(140, 200, 140)
End If
If TChart1.Series(0).YValues.Value((ValueIndex + 1) Mod TChart1.Series(0).Count) > TChart1.Series(1).YValues.Value((ValueIndex + 1) Mod TChart1.Series(1).Count) Then
TChart1.Series(1).Color = vbRed
Else
TChart1.Series(1).Color = RGB(0, 128, 0)
End If
End If
End Sub
Re: More controls over bar gradient
Posted: Wed Oct 28, 2009 3:28 pm
by 15051059
The values of the bar can vary. I attach an image of the bar graph.
- BarSeries.jpg (288.69 KiB) Viewed 13136 times
What I want to do is to put gradient on the red series from darker red to lighter red and on the green series from darker green to lighter green. I already tweaked around with adding 2 separate series stacked on top of each other (Red and green), but when I showed the marks, the marks show for both of them.
Re: More controls over bar gradient
Posted: Fri Oct 30, 2009 3:29 pm
by yeray
Hi Raymond,
In the example I posted above, the values can vary. The colours will be updated each time the chart will be repainted.
Could you please tell us where is the example above failing? Or could you please send us a simple example project we can run as-is to reproduce the problem here?
Re: More controls over bar gradient
Posted: Fri Oct 30, 2009 6:34 pm
by 15051059
Hi,
Oh ... I am sorry. I did not realize OnGetSeriesBarStyle is an event. There is another problem, though. If I scroll horizontally, if the color of the first value is green and the color of the second value is red, and the bar is right on the left edge, the gradient will change from red to green.]
- Original
- BarSeries1.jpg (64.61 KiB) Viewed 13086 times
- After scroll
- BarSeries2.jpg (58.5 KiB) Viewed 13053 times
Re: More controls over bar gradient
Posted: Mon Nov 02, 2009 9:29 am
by yeray
Hi Raymond,
Yes, I think that the following code works better. Note that the colours assignment should be recalculated if your data changes:
Code: Select all
Private Sub Form_Load()
TChart1.Aspect.View3D = False
TChart1.AddSeries scBar
TChart1.AddSeries scBar
TChart1.Series(0).FillSampleValues 5
TChart1.Series(1).FillSampleValues 5
TChart1.Series(0).Color = vbBlue
TChart1.Series(0).asBar.Gradient.Visible = True
TChart1.Series(0).asBar.Gradient.StartColor = RGB(150, 150, 255)
TChart1.Series(1).asBar.Gradient.Visible = True
TChart1.Environment.InternalRepaint
' Colours assignment
Dim i As Integer
For i = 0 To TChart1.Series(0).Count - 1
If TChart1.Series(0).YValues.Value(i Mod TChart1.Series(0).Count) > TChart1.Series(1).YValues.Value(i Mod TChart1.Series(1).Count) Then
TChart1.Series(1).PointColor(i) = vbRed
Else
TChart1.Series(1).PointColor(i) = RGB(0, 128, 0)
End If
Next i
End Sub
Private Sub TChart1_OnGetSeriesBarStyle(ByVal SeriesIndex As Long, ByVal ValueIndex As Long, BarStyle As TeeChart.EBarStyle)
BarStyle = bsRectGradient
If SeriesIndex = 1 Then
If TChart1.Series(0).YValues.Value(ValueIndex) > TChart1.Series(1).YValues.Value(ValueIndex) Then
TChart1.Series(1).asBar.Gradient.StartColor = RGB(255, 150, 150)
Else
TChart1.Series(1).asBar.Gradient.StartColor = RGB(140, 200, 140)
End If
End If
End Sub
Re: More controls over bar gradient
Posted: Mon Nov 02, 2009 5:42 pm
by 15051059
It's working! Thanks!
Re: More controls over bar gradient
Posted: Mon Nov 02, 2009 8:57 pm
by 15051059
Hi,
The bar fill color works. Is there a way to change the border color to match?
Re: More controls over bar gradient
Posted: Tue Nov 03, 2009 8:57 am
by yeray
Hi Raymond,
The problem is that the border colour is a property from the series so you won't be able to set a border colour for each bar. And this means that if we change the series' pen colour for the whole series at OnGetBarStyle event, as with my first suggestion for the gradient, we will have the same problem: we can use this event to change the series' pen colour when each bar is going to be drawn but this will be applied for the next bar, so we need to set a colour or another depending on the values at ValueIndex+1. And then, there is a problem when the first bar isn't drawn.
So another possibility would be simply deactivating the series' pen.
Code: Select all
TChart1.Series(0).asBar.BarPen.Visible = False
TChart1.Series(1).asBar.BarPen.Visible = False
TChart1.Series(1).asBar.OffsetPercent = 4