Page 1 of 1

Assign a different color to a specific pointers

Posted: Sat Jan 21, 2006 1:28 pm
by 9341477
Hi,

I have TPointSeries. The background color of each pointer is given by the value of SeriesColor property. At runtime, I'd like to be able to change the background pointer color of just a few specific points. I've tried playing with ColorEachPoint property but that one sets the pointer colors deliberately. I don't want to change the background color of all pointers, just only a few.

Is there a way how to achieve this in TeeChart VCL 7 Pro?

Thanks in advance,
Ivo

Posted: Mon Jan 23, 2006 8:51 am
by narcis
Hi Ivo,

Yes, this can be easily done using the series OnGetPointerStyle event as shown here:

Code: Select all

function TForm1.Series1GetPointerStyle(Sender: TChartSeries;
  ValueIndex: Integer): TSeriesPointerStyle;
begin
  if (Series1.YValue[ValueIndex] > 100) then Series1.Pointer.Color:=clBlue
  else Series1.Pointer.Color:=clRed;

  result:=psRectangle;
end;

Posted: Mon Jan 23, 2006 9:20 am
by 9341477
Hi, NarcĂ­s,

I've tried your code (exactly) and created the following points at runtime:

Code: Select all

Series1.AddXY(10, 50);
  Series1.AddXY(20, 80);
  Series1.AddXY(30, 111);
  Series1.AddXY(40, 101);
  Series1.AddXY(50, 80);
  Series1.AddXY(60, 60);
The expected order of colors would be:

Red
Red
Blue
Blue
Red
Red

However, the actual order of colors I get is:

Red
Red
Red
Blue
Blue
Red

Could you please tell me why? What am I doing wrong?

Thanks!

Posted: Mon Jan 23, 2006 5:22 pm
by Pep
Hi Ivo,

yes, you're correct, to get correct results you should use similar code to the following :

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
Series1.AddXY(10, 50);
  Series1.AddXY(20, 80);
  Series1.AddXY(30, 111);
  Series1.AddXY(40, 101);
  Series1.AddXY(50, 80);
  Series1.AddXY(60, 60);
  Chart1.draw();
end;

function TForm1.Series1GetPointerStyle(Sender: TChartSeries;
  ValueIndex: Integer): TSeriesPointerStyle;
begin
  if (Series1.YValue[ValueIndex] > 100) then
    Series1.ValueColor[ValueIndex]:=clBlue
  else
    Series1.Valuecolor[ValueIndex]:=clRed;

  result:=psRectangle;
end;

Posted: Mon Jan 23, 2006 9:10 pm
by 9341477
Hi, Pep!

Series1.ValueColor[] array property is the correct way how to change the data point color at runtime, you're right. Thanks! However, modifying the value of this property form within Series1GetPointerStyle event handler leads to unexpected behavior (at least for me). What I really wanted was to be able to flip the data point color by clicking the pointer with the mouse (i.e. simulating an inclusion/exclusion of certain data point to/from a selection). Finally, I have managed to work it out. Here is an example code for anyone who might be interested:

Code: Select all

type
  TForm4 = class(TForm)
    Chart1: TChart;
    Series1: TPointSeries;
    procedure Series1Click(Sender: TChartSeries; ValueIndex: Integer;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    fPointStates: array [0..5] of Boolean;
    procedure AddPoint(const X, Y: Double);
    function GetPointColor(aIndex: Integer): TColor;
  public
  end;

procedure TForm4.AddPoint(const X, Y: Double);
var
  vIndex: Integer;
begin
  vIndex := Series1.AddXY(X, Y);
  Assert(vIndex <= High(fPointStates));
  Series1.ValueColor[vIndex] := GetPointColor(vIndex);
end;

function TForm4.GetPointColor(aIndex: Integer): TColor;
const
  cColors: array [Boolean] of TColor = (clSilver, clBlue);
begin
  Result := cColors[fPointStates[aIndex]];
end;

procedure TForm4.FormCreate(Sender: TObject);
begin
  AddPoint(10, 50);
  AddPoint(20, 80);
  AddPoint(30, 111);
  AddPoint(40, 101);
  AddPoint(50, 80);
  AddPoint(60, 60);
end;

procedure TForm4.Series1Click(Sender: TChartSeries; ValueIndex: Integer;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  fPointStates[ValueIndex] := not fPointStates[ValueIndex];
  Series1.ValueColor[ValueIndex] := GetPointColor(ValueIndex);
end;