如何在Delphi的运行时向DBHART添加图表系列



我需要在运行时添加图表系列,因为我不知道我在设计时我有多少个图表系列,所以我编写了一些代码来添加它。但是,由于某种原因,它不起作用...

我的大多数图表只需要1个图表系列,除1个需要2的图表。因此,我可以在2个图表系列中添加一个额外的表格,但这将与我的设计相冲突,以使我的设计与何种数据相冲突。展示。因此,目前,在设计时间内有1个图表系列 在运行时额外1个图表系列。设计模式中添加的图表系列可完美。我在运行时添加的一个几乎是表单文件(.dfm(中图表属性的确切副本。

形式背后:

object frmChartTest: TfrmChartTest
  Left = 0
  Top = 0
  Caption = 'Test'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object chtTestChart: TDBChart
    Left = 0
    Top = 0
    Width = 635
    Height = 299
    Title.Text.Strings = (
      'Title Chart')
    BottomAxis.DateTimeFormat = 'dd/MM/yyyy'
    BottomAxis.Increment = 1.000000000000000000
    BottomAxis.LabelStyle = talValue
    BottomAxis.Title.Caption = 'Title bottom axe'
    LeftAxis.Automatic = False
    LeftAxis.AutomaticMaximum = False
    LeftAxis.AutomaticMinimum = False
    LeftAxis.ExactDateTime = False
    LeftAxis.Maximum = 100.000000000000000000
    LeftAxis.Title.Caption = 'Title Y-ax'
    Legend.Visible = False
    View3D = False
    View3DOptions.Elevation = 344
    Zoom.Pen.Mode = pmNotXor
    Align = alClient
    BevelOuter = bvNone
    Color = clWhite
    TabOrder = 0
    DefaultCanvas = 'TGDIPlusCanvas'
    ColorPaletteIndex = 13
  end
end

实际代码:

unit ChartTest;
interface
uses
  Data.DB, FireDAC.Comp.Client, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, System.DateUtils,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VclTee.TeeGDIPlus, VclTee.TeEngine, Vcl.ExtCtrls, VclTee.TeeProcs,
  VclTee.Chart, VclTee.DBChart;
type
  TfrmChartTest = class(TForm)
    chtTestChart: TDBChart;
    procedure FormShow(Sender: TObject);
  private
    procedure CreateField(MemoryDataSet: TFDMemTable; AFieldName: string; AFieldType: TFieldType; ASize: integer);
    procedure AddValues(FieldNameXAxe, FieldNameYAxe: string; LineColor: TColor);
    function GetData(): TFDMemTable;
  end;
var
  frmChartTest: TfrmChartTest;
implementation
{$R *.dfm}
const
  dateField: string = 'date1';
  intField1: string = 'int1';
  intField2: string = 'int2';
{ TfrmChartTest }
procedure TfrmChartTest.AddValues(FieldNameXAxe, FieldNameYAxe: string;
  LineColor: TColor);
var
  chartSeries: TChartSeries;
begin
  chartSeries := TChartSeries.Create(chtTestChart);
  with chartSeries do begin
    ParentChart := chtTestChart;
    Marks.Style := smsXValue;
    DataSource := GetData();
    SeriesColor := LineColor;
    XLabelsSource := FieldNameXAxe;
    Pen.Color := LineColor;
    Pen.Width := 3;
    XValues.DateTime := True;
    XValues.Name := 'X';
    XValues.Order := loAscending;
    XValues.ValueSource := FieldNameXAxe;
    YValues.Name := 'Y';
    YValues.Order := loNone;
    YValues.ValueSource := FieldNameYAxe;
    DrawBetweenPoints := True;
  end;
  chtTestChart.AddSeries(chartSeries);
  chartSeries.Active := True;
end;
procedure TfrmChartTest.CreateField(MemoryDataSet: TFDMemTable;
  AFieldName: string; AFieldType: TFieldType; ASize: integer);
begin
  with MemoryDataSet.FieldDefs.AddFieldDef do begin
    name := AFieldName;
    DataType := AFieldType;
    if ASize > 0 then begin
      Size := ASize;
    end;
  end;
end;
procedure TfrmChartTest.FormShow(Sender: TObject);
begin
  AddValues(dateField, intField1, clRed);
  AddValues(dateField, intField2, clBlue);
  chtTestChart.LeftAxis.Minimum := 45;
  chtTestChart.LeftAxis.Maximum := 175;
end;
function TfrmChartTest.GetData: TFDMemTable;
var
  Data: TFDMemTable;
begin
  Data := TFDMemTable.Create(nil);
  CreateField(Data, dateField, ftDate, 0);
  CreateField(Data, intField1, ftInteger, 0);
  CreateField(Data, intField2, ftInteger, 0);
  Data.CreateDataSet();
  Data.Append();
  Data.FieldByName(dateField).AsDateTime := Yesterday;
  Data.FieldByName(intField1).AsInteger := 50;
  Data.FieldByName(intField2).AsInteger := 130;
  Data.Append();
  Data.FieldByName(dateField).AsDateTime := Today;
  Data.FieldByName(intField1).AsInteger := 70;
  Data.FieldByName(intField2).AsInteger := 150;
  Data.Append();
  Data.FieldByName(dateField).AsDateTime := Tomorrow;
  Data.FieldByName(intField1).AsInteger := 90;
  Data.FieldByName(intField2).AsInteger := 170;
  Exit(Data);
end;
end.

显示y轴的值。显示X轴上的值。没有显示错误消息,它只是无法按预期工作。

代码的唯一功能问题是您创建一个抽象TChartSeries的实例。您没有提及您要在运行时创建哪种系列。为了解决此问题,只需选择一个特定的系列,例如TLineSeriesTBarSeries,您的代码应该起作用。

除此之外,您在运行时创建系列的方式有些尴尬。设置系列属性ParentChart后,您无需致电AddSeries。这两个基本上是等效的。创建育儿系列的最简单方法是在文档中描述的,它可以是单线仪(这是一个创建条系列(:

chartSeries := chtTestChart.AddSeries(TBarSeries.Create(Self));
{ setup series here }

您也不需要将系列的属性Active设置为 True,因为它是默认值。

相关内容

  • 没有找到相关文章

最新更新