[SOLVED] TAChart - lineColor - what property to use lazarus / 55-TAChart ---------------------------------------------------------------------- bnl · 2020-08-04 17:38 Hi! I have a Treeview which I fill with a key (to a database) When selectionChanged in that  TreeView I was to use that key to draw a graph. It is a simple line graph I have 8 - 16 series, and I want 1 to have a special color (green) and the rest should have line color red. I create the series dynamically and add to the graph. All is well - but for line color. I tried these three properties Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         Series[cnt].LinePen.Color := clRed;    Series[cnt].SeriesColor:= clRed     ;    Series[cnt].Pointer.Pen.Color := clRed   ;  and no matter how I do I usually get 1 green, 1 red and the rest black. here an excerpt from it. How should I do instead ? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Chart1: TChart;    Chart1LineSeries1: TLineSeries;    Edit1: TEdit;    Edit2: TEdit;    TrackBar1: TTrackBar;    TreeView1: TTreeView;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);    procedure FormCreate(Sender: TObject);     procedure TreeView1SelectionChanged(Sender: TObject);  private    procedure DrawGraph(marketid: string);   public   end;                                               var  Form1: TForm1;  T: TSQLTransaction;  All_Markets, runner_price_data, runners_in_race: TSQLQuery;  PQConn: TPQConnection;  Series: array[0..15] of TLineSeries;                   this is the bulk of it Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   while not runners_in_race.EOF do begin    cnt := cnt + 1;    selectionid := runners_in_race.FieldByName('selectionid').AsInteger;    placement := runners_in_race.FieldByName('status').AsString;    runner_price_data.params.parambyname('marketid').AsString := marketid;    runner_price_data.params.parambyname('selectionid').AsInteger := selectionid;    edit1.Text := IntToStr(selectionid);     runner_price_data.Prepare;    runner_price_data.Open;    x := 0.0;    Series[cnt] := TLineSeries.Create(Chart1);    Series[cnt].BeginUpdate;     if placement = 'WINNER' then begin      Series[cnt].LinePen.Color := clGreen;   //   Series[cnt].SeriesColor:= clGreen   ;    //  Series[cnt].Pointer.Pen.Color := clGreen   ;    end    else if placement = 'LOSER' then begin      Series[cnt].LinePen.Color := clRed;     // Series[cnt].SeriesColor:= clRed     ;   //   Series[cnt].Pointer.Pen.Color := clRed   ;    end  else begin      Series[cnt].LinePen.Color := clPurple;   //   Series[cnt].SeriesColor:= clPurple  ;   //   Series[cnt].Pointer.Pen.Color := clPurple   ;    end; //    while not runner_price_data.EOF and (cnt <= length(Series)) do begin    while not runner_price_data.EOF do begin      x := x + 1.0;      backprice := runner_price_data.FieldByName('backprice').AsFloat;      if backprice <= Real(pos) then        Series[cnt].AddXy(x, backprice)    else        Series[cnt].AddXy(x, Real(pos));       runner_price_data.Next;    end;    runner_price_data.Close;    Series[cnt].EndUpdate;    Chart1.AddSeries(Series[cnt]);     runners_in_race.Next;  end;   thanks /Björn ---------------------------------------------------------------------- wp · 2020-08-04 19:59 This is not enough information. Please make a copy of your project and strip everything which is not needed so that only essential parts to reproduce the issue are left. Pack the pas, lfm, lpi, lpr and data files into a shared zip which you can upload under "Attachment and other options" (do not include the exe, res, ppu, o and other compiler-generated files because the upload may become too large otherwise). And use only a simple data base format (sqlite4, dbase, csv) - I don want to install a database server just to answer this question. ---------------------------------------------------------------------- bnl · 2020-08-05 07:41 Ok, thanks for wanting to help. I see I did not mention any versions. I'm on macOS 10.13.6 (High Sierra) with Lazarus 2.0.6 and freepascal 3.0.4 Instead of migrating the data to another database, I created a read- only user which is in the code (function CreateConnection). It is reachable from the internet. compile, start and open the treeview to the left, then click on any entry in the list. I want 1 green line, the rest red. However most get black (and not purple as the code indicates if the if statement at line 137 fails - which is does not - I followed it with debug) ---------------------------------------------------------------------- bnl · 2020-08-05 07:51 Here's an attached screenshot. Interestingly, the colors of the legend (top right corner) seems correct /Björn ---------------------------------------------------------------------- wp · 2020-08-05 16:10 Quote from: bnl on August 05, 2020, 09:41:56 amInstead of migrating the data to another database, I created a read-only user which is in the code (function CreateConnection). It is reachable from the internet. But I need to install PostgreSQL, at least on Windows and on the Linux that I tested. I downloaded the libpq.dll and stored it in the exe folder of you your demo, no success. Please understand that I do not want to install (and learn) big database client software on my system (and VMs) which I don't need, just to answer a forum question. ---------------------------------------------------------------------- bnl · 2020-08-05 16:53 QuoteBut I need to install PostgreSQL, Hmm, yes. I didn't think of that  - sorry. I've been digging around the forum and seen some with similar - but not identical - problems on osx. It may be a cocoa problem. Anyway, when I get home (tomorrow ) I''ll try to make a small reproducible. Thinking of It - I don't think I need a db at all to reproduce. Thanks for the effort /Björn ---------------------------------------------------------------------- bnl · 2020-08-06 12:03 ok - new project with no db run it and press Draw-button top left. I seem to often get a BLACK line or two as well The code should give no reason fo a black line at all Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure TForm1.Button1Click(Sender: TObject);var cnt : integer;   x : real;   r : real;   i,j : integer;begin  cnt := 0;  for i := 1 to 5 do begin     x := 0.0;     //Series[cnt]     Series[cnt] := TLineSeries.Create(Chart1);     Series[cnt].BeginUpdate;     r := Random;      if r < 0.2 then begin       Series[cnt].LinePen.Color := clGreen;    ////   Series[cnt].SeriesColor:= clGreen   ;    // //  Series[cnt].Pointer.Pen.Color := clGreen   ;     end     else if r < 0.4 then begin       Series[cnt].LinePen.Color := clRed;    //  // Series[cnt].SeriesColor:= clRed     ;    ////   Series[cnt].Pointer.Pen.Color := clRed   ;     end     else begin     Series[cnt].LinePen.Color := clPurple;    ////   Series[cnt].SeriesColor:= clPurple  ;    ////   Series[cnt].Pointer.Pen.Color := clPurple   ;     end;      for j := 1 to 5 do begin       x := x + 1.0;       Series[cnt].AddXy(x, j*r)     end ; //loop      Series[cnt].EndUpdate;     Chart1.AddSeries(Series[cnt]);     cnt := cnt +1;  end; //loop end;                 As I mentioned before: imac:~ bnl$ uname -a Darwin imac.lan 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 18 21:21:34 PDT 2020; root:xnu-4570.71.82.5~1/RELEASE_X86_64 x86_64 (High Sierra - 10.13.6) Lazarus 2.0.6 fpc 3.0.4 /Björn ---------------------------------------------------------------------- wp · 2020-08-06 20:33 Thank you -- this is the kind of demo that I can work with. I tested the project on Windows - everything correct. Then I tried on a VM with macOS 10.14.3 which is painfull slow, but I seems that it is working correctly, too. One, maybe stupid, note: I noticed that it is a bit difficult to distingush clPurple from clBlack, at least on my monitor. Could you use clYellow instead -- it will definitely make a big difference. The other note: This issue reminds me of a recent, still unsolved, issue discussed in https://forum.lazarus.freepascal.org/index.php/topic,48842.0.html. In this issue the brush color is changed but not always propagated to the macOS drawing engine. Looks as if we have the same issue here, now with the pen. Let me investigate further... ---------------------------------------------------------------------- bnl · 2020-08-06 20:51 QuoteThe other note: This issue reminds me of a recent, still unsolved, issue discussed in https://forum.lazarus.freepascal.org/index.php/topic,48842.0.html. In this issue the brush color is changed but not always propagated to the macOS drawing engine. Looks as if we have the same issue here, now with the pen. Yes, not exactly the same but similar. Three screenshoots attached, in two I've changed purple -> yellow. Still black lines appear. And in the last one, I also show the legend - which gets the colors correct. 1 green 1 red 3 yellow while the graph itself has 1 green 1 red 2 black 1 yellow /Björn ---------------------------------------------------------------------- wp · 2020-08-06 21:11 Please try the following code which avoids the random numbers to get reproducible results. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button3Click(Sender: TObject);const  COLORS: array[1..5] of TColor = (clRed, clGreen, clBlue, clYellow, clPurple);var  cnt: Integer;  ser: TLineseries;begin  Chart1.ClearSeries;  for cnt := 1 to 5 do  begin     ser := TLineSeries.Create(Chart1);     ser.SeriesColor := COLORS[cnt];     ser.Title := IntToStr(cnt);     ser.AddXY(0, cnt);     ser.AddXY(1, cnt+1);     Chart1.AddSeries(ser);  end;end; Play with the colors in tha COLORS array. You should also try a combination which contains three consecutive yellow colors. I suspect that setting a color to one which has been previously set is not working; this is the issue in the brush.color case. [EDIT] I could reproduce the issue on macOS when I select the following colors in above array: clred, clBlue, clYellow, clYellow, clYellow; only the center yellow line is correct, the others are black. ---------------------------------------------------------------------- bnl · 2020-08-07 12:35 QuoteI could reproduce the issue on macOS when I select the following colors in above array: clred, clBlue, clYellow, clYellow, clYellow; only the center yellow line is correct, the others are black. Hmm, so can I. But not in the same way. I get (from bottom and up) red, blue,BLACK,yellow,BLACK Or is that what you mean by the center yellow? (I interpret 'center yellow line' as the 3rd line, and that that one is yellow, but it could be the 4th as well , the one in center of the should-be-yellow lines) I'll attach a screenshot as well /Björn ---------------------------------------------------------------------- wp · 2020-08-07 12:39 Yes, exactly like here. ---------------------------------------------------------------------- bnl · 2020-08-07 12:49 QuotePlay with the colors in tha COLORS array. You should also try a combination which contains three consecutive yellow colors. I suspect that setting a color to one which has been previously set is not working; this is the issue in the brush.color case. Not quite - or it is nob obvious to me. I set the array to red,blue,red,blue,red,blue (and increased it to 6 elements) This was fine but red,red,red,blue,blue,blue is not. Only the FIRST occurrence works, the others are black. so it became red,BLACK,BLACK, blue,BLACK,BLACK with the yellow ones, the SECOND occurrence was correct, but not the first I'll attach these screenshot as well. The legend shows how it was intended /Björn ---------------------------------------------------------------------- wp · 2020-08-07 14:04 Yes I don't understand the sequence of "correct" and "black" either. But I found a workaround in the mean-time: When you open unit TADrawerCanvas.pas (in folder "components/tachart" of your Lazarus installation), find TCanvasDrawer.SetPen and, in the "else" part of the "if FXor" instruction add code which makes sure that the Pen.Color is different from the new value: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCanvasDrawer.SetPen(APen: TFPCustomPen);begin  with GetCanvas do begin    if FXor then begin      ...    end    else begin      {$ifdef LCLCocoa}   // ADD THIS FROM HERE...      // Workaround for series color issue in cocoa: make sure that color is different      if Pen.Color = clWhite then        Pen.Color := clBlack      else        Pen.Color := clWhite;      {$endif}    // ... TO HERE       if APen is TPen then        Pen.Assign(APen)      else  begin        Pen.Color := FPColorToChartColor(APen.FPColor);        ...      end;    ....  end;end; The reason is that for some reason (which I don't understand) the pen must call FreeReference in Cocoa before changing a color, but this does not happen because the TPen's Color setter code is exited immediately when the LCL color value does not change. After this modification recompile the TAChart package, and the series colors will be correct. Of course, this is only a workaround which will probably slow down execution of TAChart on cocoa.  The real cause must be somewhere in the widgetset part of LCL graphics. Unfortinately I do not understand anything about the internals of macOS and its graphics. For a bug report I need a very simple example without TAChart (which is too complex). Something like this OnPaint code for a Paintbox on a form: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.PaintBox1Paint(Sender: TObject);var  y: Integer;begin  with Paintbox1 do  begin    Canvas.Brush.Color := clWhite;    Canvas.FillRect(0, 0, Width,Height);     Canvas.Pen.Color := clRed;  Canvas.Line(0, y, Width, y);     inc(y, 5);    Canvas.Pen.Color := clYellow;    Canvas.Line(0, y, Width, y);     // What to do here to break the pen.Reference?     inc(y, 5);    Canvas.Pen.Color := clYellow;    Canvas.Line(0, y, Width, y);     inc(y, 5);    Canvas.Pen.Color := clYellow;    Canvas.Line(0, y, Width, y);     inc(y, 5);    Canvas.Pen.Color := clYellow;    Canvas.Line(0, y, Width, y);  end;end; As expected this code is working correctly, 1 red and 4 yellow lines. Something is happening in TAChart which breaks the pen's reference. When we find something to insert between the 1st and 2nd yellow line drawing code which makes the subsequent yellow lines to be drawn in black, we have a good example for the bug report. ---------------------------------------------------------------------- bnl · 2020-08-07 14:44 QuoteBut I found a workaround in the mean-time Thanks, that did the trick. While a work-around, it sure is good enough for me. Thanks again for you time and effort /Björn ---------------------------------------------------------------------- wp · 2020-08-07 21:53 Now I found the missing piece: it is clipping. Series are drawn within a clipped rectangle. And when I activate Canvas.Clipping in my simple demo the clipped line turns into black and - even more - forgets the line width and the pen style. Of course, cocoa only, win32 is fine. [EDIT] Bugreport submitted: https://bugs.freepascal.org/view.php?id=37520 ---------------------------------------------------------------------- bnl · 2020-08-07 22:07 Yes , when I click the clipping checkbox, the red dotted line turns into a (shorter) black line ---------------------------------------------------------------------- wp · 2020-08-09 13:20 The clippling bug in the cocoa canvas has been fixed by Dmitry. In my own tests, series color selection should work now in cocoa. Please test yourself and report back. Note: you need Lazarus trunk to get the fix. Alternatively - without warranty - you could do this: Open cocoagdiobject.pas in folder lcl/interfaces/cocoa of your Lazarus installation  and find procedure TCocoaContext.ClearClipping.At the end of the "if" block insert the lines "if Assigned(FPen) then FPen.Apply(Self)" and "if Assigned(FBrush) then FBrush.Apply(Self)".The entire procedure should be as follows. Afterwards rebuild the IDE.Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCocoaContext.ClearClipping;var  Trans: CGAffineTransform;  cgc: CGContextRef;begin  if FClipped then  begin  cgc := CGContext();    Trans := CGContextGetCTM(cgc);    CGContextRestoreGState(cgc);    ApplyTransform(Trans);    if Assigned(FPen) then FPen.Apply(Self);      // <----- INSERTED    if Assigned(FBrush) then FBrush.Apply(Self);  // <----- INSERTED  end;end;As I said -- I cannot give a guarantee that this change does not depend on other changes missing. So, please make a backup copy of the modified file so that you can restore the old state in case Lazarus does not compile. ---------------------------------------------------------------------- bnl · 2020-08-09 19:57 QuoteNote: you need Lazarus trunk to get the fix. Alternatively - without warranty - you could do this: ... Afterwards rebuild the IDE. Hmm, I'll need to do some reading up on details for that. I think I got Lazarus/fpc installed via macports. I'm on vacation now, I hope to get to my dev computer during next week /Björn ---------------------------------------------------------------------- bnl · 2020-08-11 15:13 So I found fpcupdeluxe, and downloaded/compiled trunk of Lazarus and fpc. It works as expected now, I get colors as I want. Thanks /Björn