I am assuming your dataset contains five fields named "x", "y1", "y2", "y3", "y4". The dataset is linked to a Datasource.
Add a DbChartSource to the form, link it to the DataSource.
Add a button which adds a new series to the chart:
procedure TForm1.Button1Click(Sender: TObject);
begin
// of course, you should use a more sophisticated logic here...
case Chart1.SeriesCount of
0: AddSeries('y1');
1: AddSeries('y2');
2: AddSeries('y3');
3: AddSeries('y4');
end;
end;
The method "AddSeries" adds a series at runtime; it copies the data from the DBChartsource to the internal ListSource of the series; you could also work with the DBChartSource directly but this requires to load the data from the server with each change of data parameters:
const
COLORS: Array[0..3] of TColor = (clBlue, clRed, clGreen, clFuchsia); // a color for each series
procedure TForm1.AddSeries(AFieldName: String);
var
series: TLineSeries;
begin
DBChartSource1.FieldX := 'x';
DBChartSource1.FieldY := AFieldName;
DBChartSource1.DataSource := DataSource1;
series := TLineseries.Create(self); // or you could create a bar series by "TBarSeries.Create", etc.
series.SeriesColor := COLORS[Chart1.SeriesCount];
series.ListSource.CopyFrom(DBChartSource1);
series.Title := AFieldName; // for the legend
// set other properties here...
Chart1.AddSeries(series);
end;
Since the table data are copied to the series the table can be closed afterwards.