More controls over bar gradient

TeeChart for ActiveX, COM and ASP
Post Reply
Raymond
Newbie
Newbie
Posts: 27
Joined: Tue Dec 02, 2008 12:00 am

More controls over bar gradient

Post by Raymond » Tue Oct 27, 2009 10:44 pm

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?

Yeray
Site Admin
Site Admin
Posts: 9552
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: More controls over bar gradient

Post by Yeray » Wed Oct 28, 2009 10:46 am

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
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Raymond
Newbie
Newbie
Posts: 27
Joined: Tue Dec 02, 2008 12:00 am

Re: More controls over bar gradient

Post by Raymond » Wed Oct 28, 2009 3:28 pm

The values of the bar can vary. I attach an image of the bar graph.
BarSeries.jpg
BarSeries.jpg (288.69 KiB) Viewed 12512 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.

Yeray
Site Admin
Site Admin
Posts: 9552
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: More controls over bar gradient

Post by Yeray » Fri Oct 30, 2009 3:29 pm

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?
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Raymond
Newbie
Newbie
Posts: 27
Joined: Tue Dec 02, 2008 12:00 am

Re: More controls over bar gradient

Post by Raymond » Fri Oct 30, 2009 6:34 pm

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.]
BarSeries1.jpg
Original
BarSeries1.jpg (64.61 KiB) Viewed 12462 times
BarSeries2.jpg
After scroll
BarSeries2.jpg (58.5 KiB) Viewed 12429 times

Yeray
Site Admin
Site Admin
Posts: 9552
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: More controls over bar gradient

Post by Yeray » Mon Nov 02, 2009 9:29 am

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
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Raymond
Newbie
Newbie
Posts: 27
Joined: Tue Dec 02, 2008 12:00 am

Re: More controls over bar gradient

Post by Raymond » Mon Nov 02, 2009 5:42 pm

It's working! Thanks!

Raymond
Newbie
Newbie
Posts: 27
Joined: Tue Dec 02, 2008 12:00 am

Re: More controls over bar gradient

Post by Raymond » Mon Nov 02, 2009 8:57 pm

Hi,

The bar fill color works. Is there a way to change the border color to match?

Yeray
Site Admin
Site Admin
Posts: 9552
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: More controls over bar gradient

Post by Yeray » Tue Nov 03, 2009 8:57 am

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
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply