More controls over bar gradient
More controls over bar gradient
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?
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
Hi Raymond,
Here is an example doing it:
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
Best Regards,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
Re: More controls over bar gradient
The values of the bar can vary. I attach an image of the bar graph.
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
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?
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?
Best Regards,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
Re: More controls over bar gradient
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.]
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.]
Re: More controls over bar gradient
Hi Raymond,
Yes, I think that the following code works better. Note that the colours assignment should be recalculated if your data changes:
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
Best Regards,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
Re: More controls over bar gradient
It's working! Thanks!
Re: More controls over bar gradient
Hi,
The bar fill color works. Is there a way to change the border color to match?
The bar fill color works. Is there a way to change the border color to match?
Re: More controls over bar gradient
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.
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
Best Regards,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |