Thanks a lot! It works nice.
Thanks @wp for the nice piece of code!
I made a small add to your code to control, at the same time the zposition, in order to have the series on top on the ChartListBox, on top in the chart:
procedure TForm1.ToolButton1Click(Sender: TObject);
var
L: TStringList;
i: Integer;
begin
L := TStringList.Create;
try
for i := 0 to ChartListbox1.SeriesCount-1 do
L.Add(ChartListbox1.Series[i].Title);
L.Sort;
ChartListbox1.Chart := nil;
for i := 0 to L.Count-1 do
begin
FindSeriesByTitle(Chart1, L[i]).Index := i;
FindSeriesByTitle(Chart1, L[i]).zPosition := L.Count-1-i;
end;
ChartListbox1.Chart := Chart1;
finally
L.Free;
end;
end;
In the same way, i added the reverse sequence, by this:
procedure TForm1.ToolButton2Click(Sender: TObject);
var
L: TStringList;
i: Integer;
begin
L := TStringList.Create;
try
for i := 0 to ChartListbox1.SeriesCount-1 do
L.Add(ChartListbox1.Series[i].Title);
L.Sort;
ChartListbox1.Chart := nil;
for i := 0 to L.Count-1 do
begin
FindSeriesByTitle(Chart1, L[i]).Index := L.Count-1-i;
FindSeriesByTitle(Chart1, L[i]).zposition := i;
end;
ChartListbox1.Chart := Chart1;
finally
L.Free;
end;
end;
I also added this code to recover the original sequence, as i saved the z position in the tag propertie of each serie when the series were added:
procedure TForm1.ToolButton3Click(Sender: TObject);
var
i: integer;
begin
ChartListbox1.Chart := nil;
for i := 0 to ChartListbox1.SeriesCount-1 do
begin
ChartListbox1.Series[i].index := ChartListbox1.SeriesCount-1 - ChartListbox1.Series[i].tag;
ChartListbox1.Series[i].zposition := ChartListbox1.Series[i].tag;
end;
ChartListbox1.Chart := chart1;
end;
Next steps to work on: to move a selected serie one position up (index -1; zposition +1) or down (index +1; zposition-1) respect the others, or to the top (index =0; Zposition = X); or the bottom (index = X; zposition= 0).
[Edit]
I found this topic in this forum about ListBox that could inspire the solution for the ChartListBox. I will study it so see if i could adapt it to this component.
https://forum.lazarus.freepascal.org/index.php?topic=45069.0