格式化 TChart 上的数据 Delphi Seattle FMX

formatting data on TChart Delphi Seattle FMX

使用 TChart,在 y 轴上,我的数据范围为 0 - 100,000 的整数值。我如何格式化 TChart 上的标签,如果当前系列的范围是 10,000-100,000,它在图表上显示为 10k、50k、90、100k 等。这是针对移动应用程序的,因此这样做的目的是在手机上保存 space 以最大化图表显示。

使用 Delphi 西雅图,FMX,为 iOS/Android

开发

似乎有多种可能性,这是使用 GetAxisLabel 的一种方法。对我来说关键是将标签样式设置为 talText。

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMXTee.Engine,
  FMXTee.Series, FMXTee.Procs, FMXTee.Chart;

type
  TForm1 = class(TForm)
    Chart1: TChart;
    procedure Chart1GetAxisLabel(Sender: TChartAxis; Series: TChartSeries;
      ValueIndex: Integer; var LabelText: string);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    fSeries: TPointSeries;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Chart1GetAxisLabel(Sender: TChartAxis; Series: TChartSeries;
  ValueIndex: Integer; var LabelText: string);
begin
  if (fSeries = Series) then
  begin
    LabelText := IntToStr(Round(Series.YValue[ValueIndex] / 1000)) + 'K';
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: NativeInt;
begin
  fSeries := TPointSeries.Create(self);
  fSeries.ParentChart := Chart1;
  for i := 1 to 10 do
  begin
    fSeries.Add(i * 10000);
  end;
  Chart1.Axes.Left.LabelStyle := talText;
end;

end.