Delphi 创建一个多折线图';从一个表中按日期分组的s系列
dbgrid显示的查询中有一个动态表,如下所示Delphi 创建一个多折线图';从一个表中按日期分组的s系列,delphi,charts,delphi-7,Delphi,Charts,Delphi 7,dbgrid显示的查询中有一个动态表,如下所示 code | Name | Date | Time |Score 1 | Alex | 01-Feb-14 | 0600 |5 1 | Alex | 01-Feb-14 | 0700 |7 1 | Alex | 01-Feb-14 | 0800 |8 1 | Alex | 02-Feb-14 | 0600 |7 1 | Alex | 02-Feb-14 | 0800 |8 1 | Alex | 02-F
code | Name | Date | Time |Score
1 | Alex | 01-Feb-14 | 0600 |5
1 | Alex | 01-Feb-14 | 0700 |7
1 | Alex | 01-Feb-14 | 0800 |8
1 | Alex | 02-Feb-14 | 0600 |7
1 | Alex | 02-Feb-14 | 0800 |8
1 | Alex | 02-Feb-14 | 0900 |8
1 | Alex | 03-Feb-14 | 0700 |7
1 | Alex | 03-Feb-14 | 0800 |5
1 | Alex | 03-Feb-14 | 0900 |8
查询输入为code=1,date1=01-feb-14,date2=03-feb-14
如何制作具有多个系列的折线图,按日期分组,xlabel=时间,y值=分数?
我的伪代码是:
for date1 to date 2
chart1.addseries
for time=lowesttime to highesttime do
begin
series.xlabel = time
series.yvalue = score
end
它看起来像这样
您应该手动为每个日期创建序列。因此,您应该迭代数据集,每次遇到新的日期时,都应该创建新的系列,将其添加到图表中,并添加值 如果您的数据集未排序,您可以使用临时字典(
泛型.Collections
)存储系列引用
DateTime值实际上是一个double
,其中int部分表示日期。因此,每个日期都有自己的密钥。此外,您还可以为其指定series标记
属性,以供进一步使用
在填充序列值之前和之后使用BeginUpdate()
和EndUpdate()
方法。
创建系列时,请将图表引用用作所有者。下次你们清除图表中的所有序列时,它们将成功地被销毁
下面是一个示例代码:
procedure TForm7.UpdateSeries();
var sd : TDictionary<integer, TLineSeries>;
s : TLineSeries;
d : TDate;
dInt : integer;
x : double;
xlabel : string;
i : integer;
begin
DataChart.SeriesList.Clear();
sd := TDictionary<integer, TLineSeries>.Create();
try
while not query.eof do begin
d := query.FieldByName('Date').AsDateTime;
dInt := round(d);
if not sd.ContainsKey(dInt) then begin
s := TLineSeries.Create(DataChart);
s.Tag := dInt; {for further use}
DataChart.AddSeries(s);
s.ParentChart := DataChart;
s.Title := DateToStr(query.FieldByName('date').AsDateTime);
s.Color := Random($FFFFFF);
s.BeginUpdate();
sd.AddOrSetValue(dInt, s);
end
else s := sd[dInt];
//x value and label
x := query.FieldByName('time').AsDateTime;
xlabel := Format('Label %f', [x]);
s.AddXY(x, query.FieldByName('score').AsInteger, xlabel);
query.Next();
end;
finally
for i := 0 to DataChart.SeriesCount - 1 do begin
DataChart.Series[i].EndUpdate();
end;
sd.Free();
end;
end;
谢谢鲁福爵士的图片,无论如何,这是我的第一个问题,如果我需要提供更多的数据,或者请告诉我。感谢可能的重复。我不是真的想要标签的系统时间,我指的时间是记录在表中的时间。此外,每次更改日期时,我都需要将其设置为一个新系列,但如果我问错了问题,我很抱歉。只需将x值替换为表时间(作为TDateTime值),并定义x轴的显示方式<代码>图表1.Series[dateX].XValues.DateTime:=True;Chart1.BottomAxis.DateTimeFormat:=“hh:nn”如果列表已按组键先排序
按日期、时间排序
,则不需要字典。如果组键为changes@SirRufo是的,这就是为什么我写了“如果你的数据集没有排序”哦,我只是专注于代码。。。但问题是Delphi7和泛型还不清楚there@SirRufo哦,真的,我错过了delphi-7
标记。谢谢,我已经对创建sql语句的日期和时间进行了排序,仍在努力使其工作,我将再次对结果进行评论
dInt := 0;
try
while not query.eof do begin
d := query.FieldByName('Date').AsDateTime;
if round(d) <> dInt then begin
dInt := round(d);
s := TLineSeries.Create(DataChart);
....