2023-10-20 16:56:49 +00:00
|
|
|
program write_chart_demo;
|
|
|
|
|
2023-10-23 09:28:05 +00:00
|
|
|
{.$DEFINE DARK_MODE}
|
|
|
|
|
2023-10-20 16:56:49 +00:00
|
|
|
uses
|
2023-10-29 15:52:46 +00:00
|
|
|
SysUtils, fpspreadsheet, fpstypes, fpsUtils, fpschart, xlsxooxml, fpsopendocument;
|
2023-10-27 17:45:33 +00:00
|
|
|
const
|
2023-10-29 16:12:34 +00:00
|
|
|
// SERIES_CLASS: TsChartSeriesClass = TsAreaSeries;
|
2023-10-29 15:52:46 +00:00
|
|
|
// SERIES_CLASS: TsChartSeriesClass = TsBarSeries;
|
|
|
|
// SERIES_CLASS: TsChartSeriesClass = TsBubbleSeries;
|
2023-10-29 16:12:34 +00:00
|
|
|
SERIES_CLASS: TsChartSeriesClass = TsLineSeries;
|
|
|
|
// SERIES_CLASS: TsChartSeriesClass = TsScatterSeries;
|
2023-10-27 21:24:32 +00:00
|
|
|
r1 = 1;
|
|
|
|
r2 = 8;
|
2023-10-29 15:52:46 +00:00
|
|
|
FILL_COLORS: array[0..r2-r1] of TsColor = (scRed, scGreen, scBlue, scYellow, scMagenta, scSilver, scBlack, scOlive);
|
2023-10-20 16:56:49 +00:00
|
|
|
var
|
|
|
|
b: TsWorkbook;
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet1, sheet2, sheet3: TsWorksheet;
|
2023-10-20 16:56:49 +00:00
|
|
|
ch: TsChart;
|
|
|
|
ser: TsChartSeries;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
b := TsWorkbook.Create;
|
|
|
|
try
|
|
|
|
// 1st sheet
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet1 := b.AddWorksheet('test1');
|
|
|
|
sheet1.WriteText(0, 1, '1+sin(x)');
|
|
|
|
sheet1.WriteText(0, 2, '1+sin(x/2)');
|
2023-10-29 15:52:46 +00:00
|
|
|
sheet1.WriteText(0, 3, 'Bubble Radius');
|
|
|
|
sheet1.WriteText(0, 4, 'Fill Color');
|
|
|
|
sheet1.WriteText(0, 5, 'Border Color');
|
2023-10-27 21:24:32 +00:00
|
|
|
for i := r1 to r2-1 do
|
2023-10-20 16:56:49 +00:00
|
|
|
begin
|
2023-10-29 15:52:46 +00:00
|
|
|
// x values or labels
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet1.WriteNumber(i, 0, i-1);
|
2023-10-29 15:52:46 +00:00
|
|
|
// 1st series y values
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet1.WriteNumber(i, 1, 1+sin(i-1));
|
2023-10-29 15:52:46 +00:00
|
|
|
// 2nd series y values
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet1.WriteNumber(i, 2, 1+sin((i-1)/2));
|
2023-10-29 15:52:46 +00:00
|
|
|
// Bubble radii
|
|
|
|
sheet1.WriteNumber(i, 3, i*i);
|
|
|
|
// Fill colors
|
|
|
|
sheet1.WriteNumber(i, 4, FlipColorBytes(FILL_COLORS[i-r1])); // !! ODS need red and blue channels exchanged !!
|
|
|
|
// Border colors
|
|
|
|
sheet1.WriteNumber(i, 5, FlipColorBytes(FILL_COLORS[r2-i]));
|
2023-10-20 16:56:49 +00:00
|
|
|
end;
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet1.WriteNumber(r2, 0, 9);
|
|
|
|
sheet1.WriteNumber(r2, 1, 2);
|
|
|
|
sheet1.WriteNumber(r2, 2, 2.5);
|
2023-10-28 12:07:10 +00:00
|
|
|
sheet1.WriteNumber(r2, 3, r2*r2);
|
2023-10-20 16:56:49 +00:00
|
|
|
|
2023-10-27 17:45:33 +00:00
|
|
|
// Create chart
|
2023-10-28 12:07:10 +00:00
|
|
|
ch := b.AddChart(sheet1, 4, 6, 160, 100);
|
2023-10-27 17:45:33 +00:00
|
|
|
|
|
|
|
// Add first series (type depending on SERIES_CLASS)
|
|
|
|
ser := SERIES_CLASS.Create(ch);
|
2023-10-20 16:56:49 +00:00
|
|
|
ser.SetTitleAddr(0, 1);
|
2023-10-27 21:24:32 +00:00
|
|
|
ser.SetLabelRange(r1, 0, r2, 0);
|
|
|
|
ser.SetXRange(r1, 0, r2, 0); // is used only by scatter series
|
|
|
|
ser.SetYRange(r1, 1, r2, 1);
|
2023-10-25 17:15:57 +00:00
|
|
|
ser.Line.Color := scBlue;
|
2023-10-27 17:45:33 +00:00
|
|
|
ser.Fill.FgColor := scBlue;
|
2023-10-29 15:52:46 +00:00
|
|
|
ser.SetFillColorRange(r1, 4, r2, 4);
|
2023-10-27 17:45:33 +00:00
|
|
|
if (ser is TsLineSeries) then
|
|
|
|
begin
|
|
|
|
TsLineSeries(ser).ShowSymbols := true;
|
|
|
|
TsLineSeries(ser).Symbol := cssCircle;
|
|
|
|
end;
|
2023-10-28 12:07:10 +00:00
|
|
|
if (ser is TsBubbleSeries) then
|
|
|
|
begin
|
|
|
|
TsBubbleSeries(ser).SetXRange(r1, 0, r2, 0);
|
|
|
|
TsBubbleSeries(ser).SetYRange(r1, 2, r2, 2);
|
|
|
|
TsBubbleSeries(ser).SetBubbleRange(r1, 3, r2, 3);
|
|
|
|
end;
|
2023-10-25 17:15:57 +00:00
|
|
|
|
2023-10-28 12:07:10 +00:00
|
|
|
if SERIES_CLASS <> TsBubbleSeries then
|
|
|
|
begin
|
|
|
|
// Add second series
|
|
|
|
ser := SERIES_CLASS.Create(ch);
|
|
|
|
// ser := TsBarSeries.Create(ch);
|
|
|
|
ser.SetTitleAddr(0, 2);
|
|
|
|
ser.SetLabelRange(r1, 0, r2, 0);
|
|
|
|
ser.SetXRange(r1, 0, r2, 0);
|
|
|
|
ser.SetYRange(r1, 2, r2, 2);
|
|
|
|
ser.Line.Color := scRed;
|
|
|
|
ser.Fill.FgColor := scRed;
|
|
|
|
end;
|
2023-10-23 00:03:45 +00:00
|
|
|
|
2023-10-23 22:18:31 +00:00
|
|
|
{$IFDEF DARK_MODE}
|
|
|
|
ch.Background.FgColor := scBlack;
|
|
|
|
ch.Border.Color := scWhite;
|
2023-10-23 10:58:15 +00:00
|
|
|
ch.PlotArea.Background.FgColor := $1F1F1F;
|
|
|
|
{$ELSE}
|
|
|
|
ch.Background.FgColor := scWhite;
|
|
|
|
ch.Border.Color := scBlack;
|
|
|
|
ch.PlotArea.Background.FgColor := $F0F0F0;
|
2023-10-23 09:28:05 +00:00
|
|
|
{$ENDIF}
|
2023-10-25 17:15:57 +00:00
|
|
|
// Background and wall working
|
|
|
|
ch.Background.Style := fsSolidFill;
|
|
|
|
ch.Border.Style := clsSolid;
|
|
|
|
ch.PlotArea.Background.Style := fsSolidFill;
|
2023-10-29 12:10:50 +00:00
|
|
|
//ch.RotatedAxes := true;
|
2023-10-29 16:12:34 +00:00
|
|
|
//ch.StackMode := csmStackedPercentage;
|
2023-10-27 22:28:13 +00:00
|
|
|
//ch.Interpolation := ciCubicSpline;
|
2023-10-25 17:15:57 +00:00
|
|
|
|
2023-10-23 18:21:54 +00:00
|
|
|
ch.XAxis.ShowLabels := true;
|
2023-10-27 20:44:33 +00:00
|
|
|
ch.XAxis.LabelFont.Size := 9;
|
2023-10-23 18:21:54 +00:00
|
|
|
ch.XAxis.LabelFont.Color := scRed;
|
2023-10-27 20:44:33 +00:00
|
|
|
//ch.XAxis.LabelFont.Style := [fssStrikeout];
|
2023-10-23 18:21:54 +00:00
|
|
|
ch.XAxis.AxisLine.Color := scRed;
|
2023-10-25 17:15:57 +00:00
|
|
|
ch.XAxis.Caption := 'This is the x axis';
|
2023-10-23 18:21:54 +00:00
|
|
|
ch.XAxis.CaptionFont.Color := scRed;
|
|
|
|
ch.XAxis.CaptionFont.Size := 12;
|
2023-10-27 20:44:33 +00:00
|
|
|
//ch.XAxis.Inverted := true;
|
2023-10-25 17:15:57 +00:00
|
|
|
ch.XAxis.MajorGridLines.Color := scRed;
|
|
|
|
ch.XAxis.MinorGridLines.Color := scBlue;
|
2023-10-27 20:44:33 +00:00
|
|
|
ch.XAxis.MajorGridLines.Style := clsNoLine; //Solid;
|
2023-10-25 17:15:57 +00:00
|
|
|
ch.XAxis.MinorGridLines.Style := clsNoLine; //Solid;
|
2023-10-23 18:21:54 +00:00
|
|
|
|
|
|
|
ch.YAxis.ShowLabels := true;
|
|
|
|
ch.YAxis.LabelFont.Size := 8;
|
|
|
|
ch.YAxis.LabelFont.Color := scBlue;
|
|
|
|
ch.YAxis.AxisLine.Color := scBlue;
|
2023-10-25 17:15:57 +00:00
|
|
|
ch.YAxis.Caption := 'This is the y axis';
|
2023-10-23 18:21:54 +00:00
|
|
|
ch.YAxis.CaptionFont.Color := scBlue;
|
|
|
|
ch.YAxis.CaptionFont.Size := 12;
|
2023-10-27 20:44:33 +00:00
|
|
|
//ch.YAxis.LabelRotation := 90;
|
|
|
|
//ch.YAxis.CaptionRotation := 90;
|
2023-10-29 18:36:13 +00:00
|
|
|
ch.YAxis.Min := -5;
|
|
|
|
ch.yAxis.Max := 5;
|
|
|
|
ch.YAxis.AutomaticMin := false;
|
|
|
|
ch.YAxis.AutomaticMax := false;
|
2023-10-25 17:15:57 +00:00
|
|
|
ch.YAxis.MajorGridLines.Color := scBlue;
|
2023-10-26 09:10:41 +00:00
|
|
|
ch.YAxis.MajorGridLines.Style := clsLongDash; //clsSolid;
|
|
|
|
ch.YAxis.MajorGridLines.Width := 0.5; // mm
|
|
|
|
// ch.YAxis.MinorGridLines.Style := clsLongDashDot; //Dash; //clsSolid;
|
2023-10-23 00:03:45 +00:00
|
|
|
|
2023-10-20 16:56:49 +00:00
|
|
|
ch.Title.Caption := 'HALLO';
|
|
|
|
ch.Title.Visible := true;
|
2023-10-26 09:10:41 +00:00
|
|
|
ch.Title.Font.Color := scMagenta;
|
|
|
|
ch.Title.Font.Size := 20;
|
|
|
|
ch.Title.Font.Style := [fssBold];
|
2023-10-23 09:28:05 +00:00
|
|
|
|
2023-10-20 16:56:49 +00:00
|
|
|
ch.SubTitle.Caption := 'hallo';
|
|
|
|
ch.SubTitle.Visible := true;
|
2023-10-23 09:28:05 +00:00
|
|
|
|
2023-10-25 17:15:57 +00:00
|
|
|
// Legend working
|
|
|
|
ch.Legend.Font.Size := 12;
|
|
|
|
ch.Legend.Font.Color := scBlue;
|
|
|
|
ch.Legend.Border.Width := 0.3; // mm
|
|
|
|
ch.Legend.Border.Color := scGray;
|
2023-10-26 21:21:51 +00:00
|
|
|
ch.Legend.Background.FgColor := $F0F0F0;
|
2023-10-23 22:18:31 +00:00
|
|
|
ch.Legend.Background.Style := fsSolidFill;
|
2023-10-26 21:21:51 +00:00
|
|
|
//ch.Legend.CanOverlapPlotArea := true;
|
|
|
|
ch.Legend.Position := lpBottom;
|
2023-10-23 22:18:31 +00:00
|
|
|
|
2023-10-20 16:56:49 +00:00
|
|
|
// 2nd sheet
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet2 := b.AddWorksheet('test2');
|
|
|
|
sheet2.WriteText(0, 0, 'abc');
|
2023-10-20 16:56:49 +00:00
|
|
|
|
|
|
|
// 3rd sheet
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet3 := b.AddWorksheet('test3');
|
|
|
|
sheet3.WriteText(0, 1, 'cos(x)');
|
|
|
|
sheet3.WriteText(0, 2, 'sin(x)');
|
2023-10-20 16:56:49 +00:00
|
|
|
for i := 1 to 7 do
|
|
|
|
begin
|
2023-10-27 21:24:32 +00:00
|
|
|
sheet3.WriteNumber(i, 0, i-1);
|
|
|
|
sheet3.WriteNumber(i, 1, cos(i-1), nfFixed, 2);
|
|
|
|
sheet3.WriteNumber(i, 2, sin(i-1), nfFixed, 2);
|
2023-10-20 16:56:49 +00:00
|
|
|
end;
|
|
|
|
|
2023-10-27 21:24:32 +00:00
|
|
|
ch := b.AddChart(sheet3, 1, 3, 125, 95);
|
2023-10-20 16:56:49 +00:00
|
|
|
ser := TsLineSeries.Create(ch);
|
|
|
|
ser.SetTitleAddr(0, 1);
|
|
|
|
ser.SetLabelRange(1, 0, 7, 0);
|
|
|
|
ser.SetYRange(1, 1, 7, 1);
|
|
|
|
ser := TsLineSeries.Create(ch);
|
|
|
|
ser.SetTitleAddr(0, 2);
|
|
|
|
ser.SetLabelRange(1, 0, 7, 0);
|
|
|
|
ser.SetYRange(1, 2, 7, 2);
|
2023-10-26 09:10:41 +00:00
|
|
|
ch.Border.Style := clsNoLine;
|
2023-10-20 16:56:49 +00:00
|
|
|
ch.Title.Caption := 'HALLO';
|
|
|
|
ch.Title.Visible := true;
|
|
|
|
ch.SubTitle.Caption := 'hallo';
|
|
|
|
ch.Subtitle.Visible := true;
|
2023-10-25 17:15:57 +00:00
|
|
|
ch.XAxis.MajorGridLines.Style := clsSolid; //NoLine;
|
2023-10-23 18:21:54 +00:00
|
|
|
ch.XAxis.MinorGridLines.Style := clsNoLine;
|
|
|
|
ch.YAxis.MajorGridLines.Style := clsNoLine;
|
|
|
|
ch.YAxis.MinorGridLines.Style := clsNoLine;
|
2023-10-25 17:15:57 +00:00
|
|
|
ch.YAxis.CaptionRotation := 0;
|
|
|
|
ch.XAxis.CaptionFont.Size := 18;
|
|
|
|
ch.YAxis.CaptionFont.Size := 18;
|
|
|
|
ch.XAxis.LabelFont.Style := [fssItalic];
|
|
|
|
ch.YAxis.LabelFont.Style := [fssItalic];
|
2023-10-20 16:56:49 +00:00
|
|
|
|
|
|
|
b.WriteToFile('test.xlsx', true); // Excel fails to open the file
|
|
|
|
b.WriteToFile('test.ods', true);
|
|
|
|
finally
|
|
|
|
b.Free;
|
|
|
|
end;
|
2023-10-25 17:15:57 +00:00
|
|
|
|
2023-10-26 11:07:05 +00:00
|
|
|
WriteLn;
|
|
|
|
Write('Press ENTER to close...');
|
2023-10-27 17:45:33 +00:00
|
|
|
// ReadLn;
|
2023-10-20 16:56:49 +00:00
|
|
|
end.
|
|
|
|
|