Change the color of a line segment based on the value

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
WarmBooter
Newbie
Newbie
Posts: 33
Joined: Thu Mar 18, 2004 5:00 am
Location: Brasil
Contact:

Change the color of a line segment based on the value

Post by WarmBooter » Wed Feb 18, 2015 7:27 pm

I have a TLineSeries with positive and negative values in the array.

I would like to change the color of the line to Red when the value is negative. I tried something like this, but all the line segments are being drawn in red:

Code: Select all

  with myDBChart.series[0] do
  begin
    for i := 0 to Count - 1 do
      if YValue[i] >= 0 then
        ValueColor[i] := clLime
      else
        ValueColor[i] := clRed;
  end;
Is there a way to do what I want?

Carlos

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

Re: Change the color of a line segment based on the value

Post by Yeray » Thu Feb 19, 2015 10:28 am

Hi Carlos,

I've made a simple example that seems to works fine for me here.
I'm using random values, so I've calculated a threshold between the minimum and maximum values in the series (t) and I'm using it instead of your zero.
I'm also using a TColorLineTool to highlight that threshold. This the code I used:

Code: Select all

uses Series, TeeTools;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
    t: Double;
begin
  Chart1.View3D:=false;

  with Chart1.AddSeries(TLineSeries) do
  begin
    FillSampleValues;
    t:=YValues.MinValue+(YValues.MaxValue-YValues.MinValue)/5;
    for i:=0 to Count-1 do
      if YValue[i] >= t then
        ValueColor[i]:=clLime
      else
        ValueColor[i]:=clRed;
  end;

  with Chart1.Tools.Add(TColorLineTool) as TColorLineTool do
  begin
    Axis:=Chart1.Axes.Left;
    Value:=t;
  end;
end;
And this is what I get:
2015-02-19_1121.png
2015-02-19_1121.png (35.55 KiB) Viewed 7003 times
Note that TeeChart draws the line segments in the color defined by the ValueColor on the index of the point in the right of the segment.

Also note the TLineSeries has to be set to use dsSegments DrawStyle; dsAll and dsCurve use the series Color property to draw all the series at once. dsSegments is the default DrawStyle so I didn't set it in the example above but I found it was worth mentioning it:

Code: Select all

(Chart1[0] as TLineSeries).DrawStyle:=dsSegments;
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

WarmBooter
Newbie
Newbie
Posts: 33
Joined: Thu Mar 18, 2004 5:00 am
Location: Brasil
Contact:

Re: Change the color of a line segment based on the value

Post by WarmBooter » Thu Feb 19, 2015 5:14 pm

Thanks... after I posted the original message, I was able to make it work turning the Pointer property visible (and setting the pointer transparency as 100%, since I didn't want any point appearing in the graphics).

After reading your reply, I decide to try with Pointer disabled, and for my surprise, it still works.

Thanks,

Carlos

Post Reply