Recent

Author Topic: TAChart created at runtime  (Read 4249 times)

stellar41

  • New member
  • *
  • Posts: 7
TAChart created at runtime
« on: November 11, 2016, 01:30:45 pm »
My code creates a set of TACharts at run-time, anchored to each other in a scrollbox.
I want to add a right axis for each chart and I assume I do something like change the AxisList property.  I cant see how to do that dynamically.  I would be grateful if someone could give me an example.

thanks

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: TAChart created at runtime
« Reply #1 on: November 11, 2016, 02:52:08 pm »
Look at this code (working):
  • Begin with an empty chart created at designtime
  • Adds a second y axis at runtime
  • Adds autoscale axis transformations for both vertical axes
  • Adds series for both vertical axes
  • Colorizes axis and linked series in the same color
Code: Pascal  [Select][+][-]
  1. uses
  2.   TAChartAxis, TAChartAxisUtils, TALegend;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. const
  6.   LEFT_COLOR = clRed;
  7.   RIGHT_COLOR = clBlue;
  8. var
  9.   leftAxis, rightAxis: TChartAxis;
  10.   t: TAutoscaleAxisTransform;
  11.   leftSeries, rightSeries: TLineSeries;
  12.   i: Integer;
  13. begin
  14.   // Left axis
  15.   // it is already creates, but we set some properties
  16.   leftAxis := Chart1.LeftAxis;
  17.   leftAxis.Marks.LabelFont.Color := LEFT_COLOR;
  18.   leftAxis.Title.Font.Color := LEFT_COLOR;
  19.   leftAxis.Title.Caption := 'Left axis';
  20.   leftAxis.Title.Visible := true;
  21.   leftAxis.AxisPen.Color := LEFT_COLOR;
  22.   leftAxis.AxisPen.Visible := true;
  23.   leftAxis.TickColor := LEFT_COLOR;
  24.  
  25.   // AxisTransformation for left axis
  26.   Chart1.LeftAxis.Transformations := TChartAxisTransformations.Create(self);
  27.   t := TAutoscaleAxisTransform.Create(Chart1.LeftAxis.Transformations);
  28.   t.Transformations := Chart1.LeftAxis.Transformations;
  29.   t.Minvalue := 0;
  30.   t.MaxValue := 0.5;
  31.  
  32.   // Right axis
  33.   rightAxis := Chart1.AxisList.Add;
  34.   rightAxis.Alignment := calRight;
  35.   rightAxis.Title.Caption  := 'Right axis';
  36.   rightAxis.Title.Visible := true;
  37.   rightAxis.Grid.Visible := false;
  38.   rightAxis.Marks.LabelFont.Color := RIGHT_COLOR;
  39.   rightAxis.Title.Font.Color := RIGHT_COLOR;
  40.   rightAxis.Title.Font.Orientation := -900;  // in 1/10 degrees
  41.   rightAxis.AxisPen.Color := RIGHT_COLOR;
  42.   rightAxis.AxisPen.Visible := true;
  43.   rightAxis.TickColor := RIGHT_COLOR;
  44.  
  45.   // Axis transformation for right axis
  46.   rightAxis.Transformations := TChartAxisTransformations.Create(self);
  47.   t := TAutoscaleAxisTransform.Create(rightAxis.Transformations);
  48.   t.MinValue := 0.5;
  49.   t.MaxValue := 1.0;
  50.   t.Transformations := rightAxis.Transformations;
  51.  
  52.   // Series for left axis
  53.   leftSeries := TLineSeries.Create(Chart1);
  54.   leftSeries.SeriesColor := LEFT_COLOR;
  55.   leftSeries.Title := 'red (left)';
  56.   leftSeries.AxisIndexY := Chart1.LeftAxis.Index;
  57.   for i:=0 to 10 do leftSeries.AddXY(i, random);
  58.   Chart1.AddSeries(leftSeries);
  59.  
  60.   // series for right axis
  61.   rightSeries := TLineSeries.Create(Chart1);
  62.   rightSeries.SeriesColor := RIGHT_COLOR;
  63.   rightSeries.Title := 'blue (right)';
  64.   rightSeries.AxisIndexY := rightAxis.Index;
  65.   for i:=5 to 20 do rightSeries.AddXY(i, random*10);
  66.   Chart1.AddSeries(rightSeries);
  67.  
  68.   // second series for right axis
  69.   rightSeries := TLineSeries.Create(Chart1);
  70.   rightSeries.SeriesColor := RIGHT_COLOR;
  71.   rightSeries.LinePen.Style := psDot;
  72.   rightSeries.Title := 'blue dotted (right)';
  73.   rightSeries.AxisIndexY := rightAxis.Index;
  74.   for i:=3 to 15 do rightSeries.AddXY(i, random*12 + 5);
  75.   Chart1.AddSeries(rightSeries);
  76.  
  77.   // Show legend
  78.   Chart1.Legend.Visible := true;
  79.   Chart1.Legend.Alignment := laBottomCenter;
  80.   Chart1.Legend.ColumnCount := 3;  
  81. end;

stellar41

  • New member
  • *
  • Posts: 7
Re: TAChart created at runtime
« Reply #2 on: November 14, 2016, 05:23:07 pm »
Thanks, that worked nicely.

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: TAChart created at runtime
« Reply #3 on: March 03, 2017, 12:16:20 am »
Bringing up this old thread in order to communicate that this sample project is now in the demo/runtime folder of the TAChart installation.

 

TinyPortal © 2005-2018