Recent

Author Topic: TAChart  (Read 1592 times)

MISV

  • Hero Member
  • *****
  • Posts: 642
TAChart
« on: August 20, 2018, 02:21:57 pm »
I have this code porting from Delphi to Lazarus

Code: Pascal  [Select]
  1.       TmpSeries := nil;
  2.       if TmpSeries = nil then
  3.         begin
  4.           TmpSeries := TBarSeries.Create(FWebsiteMain_Edit_LR_Summary_Chart);
  5.           TmpSeries.Marks.Style := smsValue; // box above each bar
  6. {$IFDEF mymsDELPHI}
  7.           TmpSeries.XValues.DateTime := False;
  8.           TmpSeries.ParentChart := FWebsiteMain_Edit_LR_Summary_Chart;
  9. {$ENDIF}
  10. {$IFDEF mymsFPCDevTool}
  11.           FWebsiteMain_Edit_LR_Summary_Chart.AddSeries(TmpSeries);
  12. {$ENDIF}
  13.         end
  14.       ;
  15.       if Assigned(TmpSeries) then
  16.         begin
  17.           for J := Low(A) to High(A) do
  18.           begin
  19. {$IFDEF mymsFPCDevTool}
  20.              TmpSeries.AddY(A[J].Value, A[J].Key);
  21. {$ENDIF}
  22. {$IFDEF mymsDELPHI}
  23.             TmpSeries.AddY(A[J].Value, A[J].Key);
  24. {$ENDIF}
  25.           end;
  26.         end
  27.       ;
  28.  

My problem is the xaxis on the chart...

Instead of x-axis having a named label below each vertical bar (e.g. "students", "teachers" like my Delphi version has) the xaxis is instead filled with numbers: 0.65, 0.7, 0.75 (continuing full width of control) which I do not want.



The chart itself is declared like this this

Code: Pascal  [Select]
  1.     FWebsiteMain_Edit_LR_Summary_Chart: TChart;
  2.     {$IFDEF mymsFPCDevTool}
  3.     FWebsiteMain_Edit_LR_Summary_Chart_LinearAxisTransform: TLinearAxisTransform;
  4.     {$ENDIF}
  5.  

and initialized like this

Code: Pascal  [Select]
  1.   FWebsiteMain_Edit_LR_Summary_Chart := TChart.Create(pnlWebsiteMain_Edit_LR_Summary_Chart);
  2.   FWebsiteMain_Edit_LR_Summary_Chart.Parent := pnlWebsiteMain_Edit_LR_Summary_Chart;
  3.   FWebsiteMain_Edit_LR_Summary_Chart.Align := alClient;
  4.   FWebsiteMain_Edit_LR_Summary_Chart.Color := clWindow;
  5.   FWebsiteMain_Edit_LR_Summary_Chart.LeftAxis.Inverted := False;
  6.   FWebsiteMain_Edit_LR_Summary_Chart.LeftAxis.Logarithmic := False;
  7. {$IFDEF mymsDELPHI}
  8.   FWebsiteMain_Edit_LR_Summary_Chart.LeftAxis.Automatic := True;
  9.   FWebsiteMain_Edit_LR_Summary_Chart.DepthAxis.Automatic := False;
  10.   FWebsiteMain_Edit_LR_Summary_Chart.DepthAxis.AutomaticMaximum := False;
  11.   FWebsiteMain_Edit_LR_Summary_Chart.DepthAxis.AutomaticMinimum := False;
  12.   FWebsiteMain_Edit_LR_Summary_Chart.DepthAxis.Maximum := 0.5;
  13.   FWebsiteMain_Edit_LR_Summary_Chart.DepthAxis.Minimum := -0.5;
  14.   FWebsiteMain_Edit_LR_Summary_Chart.DepthTopAxis.Automatic := False;
  15.   FWebsiteMain_Edit_LR_Summary_Chart.DepthTopAxis.AutomaticMaximum := False;
  16.   FWebsiteMain_Edit_LR_Summary_Chart.DepthTopAxis.AutomaticMinimum := False;
  17.   FWebsiteMain_Edit_LR_Summary_Chart.DepthTopAxis.Maximum := 0.5;
  18.   FWebsiteMain_Edit_LR_Summary_Chart.DepthTopAxis.Minimum := -0.5;
  19.   FWebsiteMain_Edit_LR_Summary_Chart.RightAxis.Automatic := False;
  20.   FWebsiteMain_Edit_LR_Summary_Chart.BackWall.Brush.Color := clWhite;
  21.   FWebsiteMain_Edit_LR_Summary_Chart.BackWall.Brush.Style := bsClear;
  22.   FWebsiteMain_Edit_LR_Summary_Chart.Zoom.Allow := False;
  23.   FWebsiteMain_Edit_LR_Summary_Chart.Legend.LegendStyle := lsSeries;
  24.   FWebsiteMain_Edit_LR_Summary_Chart.View3D := False;
  25. {$ENDIF}
  26.   FWebsiteMain_Edit_LR_Summary_Chart.Foot.Text.Text := '';
  27.   FWebsiteMain_Edit_LR_Summary_Chart.Foot.Visible := False;
  28. {$IFDEF mymsFPCDevTool}
  29.   FWebsiteMain_Edit_LR_Summary_Chart_LinearAxisTransform := TLinearAxisTransform.Create(FWebsiteMain_Edit_LR_Summary_Chart);
  30.   if
  31.     (FWebsiteMain_Edit_LR_Summary_Chart.LeftAxis <> nil)
  32.   then
  33.     begin
  34.       FWebsiteMain_Edit_LR_Summary_Chart.LeftAxis.Transformations := TChartAxisTransformations.Create(nil);
  35.       if
  36.         (FWebsiteMain_Edit_LR_Summary_Chart.LeftAxis.Transformations <> nil)
  37.       then
  38.         begin
  39.           if
  40.             (FWebsiteMain_Edit_LR_Summary_Chart.LeftAxis.Transformations.List <> nil)
  41.           then
  42.             begin
  43.               FWebsiteMain_Edit_LR_Summary_Chart.LeftAxis.Transformations.List.Add(FWebsiteMain_Edit_LR_Summary_Chart_LinearAxisTransform);
  44.             end
  45.           ;
  46.         end
  47.       ;
  48.     end
  49.   ;
  50.   FWebsiteMain_Edit_LR_Summary_Chart.Legend.Alignment := laBottomCenter;
  51. {$ENDIF}
  52. {$IFDEF mymsDELPHI}
  53.   FWebsiteMain_Edit_LR_Summary_Chart.Legend.Alignment := laBottom;
  54. {$ENDIF}
  55.  
« Last Edit: August 20, 2018, 02:24:16 pm by MISV »

wp

  • Hero Member
  • *****
  • Posts: 6450
Re: TAChart
« Reply #1 on: August 20, 2018, 03:00:12 pm »
You must assign the Chartsource of the series to the ChartSource of the axis (Axis.Marks.Source) and the Axis.Marks.Style to smsLabel in order to show the series labels as axis labels. Since you add the data values directly to the series their ChartSource is accessible via the ListSource of the series.
Code: Pascal  [Select]
  1.   FWebsiteMain_Edit_LR_Summary_Chart.BottomAxis.Marks.Source := TmpSeries.ListSource;
  2.   FWebsiteMain_Edit_LR_Summary_Chart.BottomAxis.Marks.Style := smsLabel;

You don't need to add a TLinearAxisTransform to have linear axis divisions.

Please have a look at http://wiki.lazarus.freepascal.org/Comparing_TAChart_with_Delphi%27s_TeeChart_Standard for a comparison between TAChart and TeeChart properties.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

MISV

  • Hero Member
  • *****
  • Posts: 642
Re: TAChart
« Reply #2 on: August 21, 2018, 12:32:56 am »
Thank you very much - appears to solve my problem :)