[SOLVED] Create chart with number of series known at runtime lazarus / 55-TAChart ---------------------------------------------------------------------- tudi_x · 2014-10-14 20:43 I am trying to write a procedure in which a chart is to be created based on the number of columns in a database table (let us say table has three columns, first two columns have values for Y series one and two and the third column for X series). Basically I can have charts that would have a variable number of (Y) series. Please advise how could I create these line series at run time. Thank you (Lazarus 1.2.4 32 bit on Windows 7 64 bit) ---------------------------------------------------------------------- wp · 2014-10-14 22:42 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: Code: [Select]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: Code: [Select]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. ---------------------------------------------------------------------- tudi_x · 2014-10-16 01:47 Thank you for putting me in the right direction.