Page 1 of 1

Change the color of a line segment based on the value

Posted: Wed Feb 18, 2015 7:27 pm
by 9231501
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

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

Posted: Thu Feb 19, 2015 10:28 am
by yeray
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 7483 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;

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

Posted: Thu Feb 19, 2015 5:14 pm
by 9231501
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