gpt4 book ai didi

delphi - 如何在delphi中运行时将图表系列添加到dbchart

转载 作者:行者123 更新时间:2023-12-03 19:46:21 25 4
gpt4 key购买 nike

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

我的大多数图表只需要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,您的代码就可以正常工作。

除此之外,您在运行时创建系列的方式有些尴尬。设置系列的属性AddSeries后,无需调用ParentChart。这两个基本上是等效的。创建父级系列的最简单方法是described in the documentation,它可以是单线的(此创建条形系列):

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


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

关于delphi - 如何在delphi中运行时将图表系列添加到dbchart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57269558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com