Recent

Author Topic: how to color a single point of a lineseries  (Read 3800 times)

frederic

  • Full Member
  • ***
  • Posts: 226
how to color a single point of a lineseries
« on: December 19, 2017, 05:30:57 pm »
dear specialists

i have a lineseries  with  showpoints:=true
all these points  have the color defined by pointer.brush

i want to change the color of one point of the series differently by code
is that possible ?

fredreic

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: how to color a single point of a lineseries
« Reply #1 on: December 19, 2017, 05:45:22 pm »
When you add a point to the series by calling the Series' Add or AddXY you can specify its color as a parameter:
Code: Pascal  [Select][+][-]
  1. function Add(AValue: Double; AXLabel: String = ''; AColor: TColor = clTAColor): Integer;
  2. function AddXY(AX, AY: Double; AXLabel: String = ''; AColor: TColor = clTAColor): Integer; inline;
  3. function AddXY(AX, AY: Double; const AYList: array of Double;  AXLabel: String = ''; AColor: TColor = clTAColor): Integer;
  4.  
  5. e.g.
  6.  
  7. procedure TForm1.FormCreate(Sender: TObject);
  8. begin
  9.   Chart1LineSeries1.AddXY(0, 10);
  10.   Chart1LineSeries1.AddXY(1, 12);
  11.   Chart1LineSeries1.AddXY(2, 11, '', clRed);   // <-- this one will be red
  12.   Chart1LineSeries1.AddXY(3, 10);
  13. end;  

If you add points to a TListChartSource the calls are similar
Code: Pascal  [Select][+][-]
  1. function Add(AX, AY: Double; const ALabel: String = ''; AColor: TChartColor = clTAColor): Integer;
  2. function AddXYList(AX: Double; const AY: array of Double; const ALabel: String = ''; AColor: TChartColor = clTAColor): Integer;

If you pass the points via a UserDefinedChartSource then you specify the color in the parameter AItem:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.UserDefinedChartSource1GetChartDataItem(
  2.   ASource: TUserDefinedChartSource; AIndex: Integer; var AItem: TChartDataItem);
  3. begin
  4.   AItem.X := <some x>;
  5.   AItem.Y := <some y>;
  6.   if AIndex = 2 then AItem.Color := clRed;      // Force point with index 2 to be red.
  7. end;


frederic

  • Full Member
  • ***
  • Posts: 226
Re: how to color a single point of a lineseries
« Reply #2 on: December 19, 2017, 09:30:42 pm »
thanks wp,

so ,when i start with
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Chart1LineSeries1.AddXY(0, 10);
  4.   Chart1LineSeries1.AddXY(1, 12);
  5.   Chart1LineSeries1.AddXY(2, 11);
  6.   Chart1LineSeries1.AddXY(3, 10);
  7. end;
  8.  
  9.  
  10.  
  11. //an change color by buttonclick
  12. procedure TForm1.Button1Click(Sender: TObject);
  13. begin
  14.  Chart1LineSeries1.AddXY(2, 11, '', clRed);   // <-- this one will be red
  15. end;
  16.  
  17.  
  18. // and back
  19.  
  20. procedure TForm1.Button2Click(Sender: TObject);
  21. begin
  22.  Chart1LineSeries1.AddXY(2, 11);  // back to the original color setting
  23. end;
  24.                                    
  25.  

that's simple; only overwriting

i hope this is also valid when drag  this specific point;
we are going to test it

frederic





wp

  • Hero Member
  • *****
  • Posts: 11853
Re: how to color a single point of a lineseries
« Reply #3 on: December 19, 2017, 10:19:22 pm »
Hmmm..., looks strange. In FormCreate you add a point at x=2, y=11 in normal color. In Button1Click you add another point at the same location in red color, and in Button2Click you add another one, now in normal color again. In the end you'll have multiple points at the same location. I guess this is not what you want.

In order to colorize an already existing point you should use the method SetColor which is available for most series; it gets the point index and the new color as parameters. If the new color is clDefault (or clTAColor) then the color is restored to the series color.

See attached demo.

frederic

  • Full Member
  • ***
  • Posts: 226
Re: how to color a single point of a lineseries
« Reply #4 on: December 20, 2017, 09:44:11 am »
wp,
yes, i misinterpreted your answer

addxy means :adding a new point(although it has the same xyvalues ),and not replace
this explains much of the troubles i got.

so dragging is also not a problem anymore because the pointindex is the reference for color modifications of points

thanks for leading me out of the tunnel
frederic

 

TinyPortal © 2005-2018