From ce4e36ca3837305e0c7e4f6536673663d48ae887 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Mon, 23 Oct 2023 09:28:05 +0000 Subject: [PATCH] fpspreadsheet: More user-friendly chart background style. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8972 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/other/chart/write_chart_demo.lpr | 18 +-- .../fpspreadsheet/source/common/fpschart.pas | 124 +++++++++++++++--- .../source/common/fpschartstyles.pas | 16 +-- 3 files changed, 122 insertions(+), 36 deletions(-) diff --git a/components/fpspreadsheet/examples/other/chart/write_chart_demo.lpr b/components/fpspreadsheet/examples/other/chart/write_chart_demo.lpr index adae1f950..e9e7834cd 100644 --- a/components/fpspreadsheet/examples/other/chart/write_chart_demo.lpr +++ b/components/fpspreadsheet/examples/other/chart/write_chart_demo.lpr @@ -1,5 +1,7 @@ program write_chart_demo; +{.$DEFINE DARK_MODE} + uses SysUtils, fpspreadsheet, fpstypes, fpschart, xlsxooxml, fpsopendocument; var @@ -8,8 +10,6 @@ var ch: TsChart; ser: TsChartSeries; i: Integer; - bg: TsChartFill; - frm: TsChartLine; begin b := TsWorkbook.Create; try @@ -28,18 +28,20 @@ begin ser.SetLabelRange(1, 0, 7, 0); ser.SetYRange(1, 1, 7, 1); - bg.FgColor := scYellow; - bg.Style := fsSolidFill; - ch.Background := bg; + {$IFDEF DARK_MODE} + ch.Background.FgColor := scBlack; + ch.Background.Style := fsSolidFill; - frm.color := scRed; - frm.Style := clsSolid; - ch.Border := frm; + ch.Border.Color := scWhite; + ch.Border.Style := clsSolid; + {$ENDIF} ch.Title.Caption := 'HALLO'; ch.Title.Visible := true; + ch.SubTitle.Caption := 'hallo'; ch.SubTitle.Visible := true; + ch.YAxis.ShowMajorGridLines := true; ch.YAxis.ShowMinorGridLines := true; diff --git a/components/fpspreadsheet/source/common/fpschart.pas b/components/fpspreadsheet/source/common/fpschart.pas index 9d02f60de..25903a51e 100644 --- a/components/fpspreadsheet/source/common/fpschart.pas +++ b/components/fpspreadsheet/source/common/fpschart.pas @@ -27,11 +27,36 @@ var type TsChart = class; - TsChartFill = record + TsChartLineRec = record + Style: Integer; // index into chart's LineStyle list or predefined clsSolid/clsNoLine + Width: Double; // mm + Color: TsColor; // in hex: $00bbggrr, r=red, g=green, b=blue + Transparency: Double; // in percent + class operator = (A, B: TsChartLineRec): Boolean; + end; + + TsChartLine = class + Style: Integer; // index into chart's LineStyle list or predefined clsSolid/clsNoLine + Width: Double; // mm + Color: TsColor; // in hex: $00bbggrr, r=red, g=green, b=blue + Transparency: Double; // in percent + procedure FromRecord(const ALine: TsChartLineRec); + function ToRecord: TsChartLineRec; + end; + + TsChartFillRec = record Style: TsFillStyle; FgColor: TsColor; BgColor: TsColor; - class operator = (A, B: TsChartFill): Boolean; + class operator = (A, B: TsChartFillRec): Boolean; + end; + + TsChartFill = class + Style: TsFillStyle; + FgColor: TsColor; + BgColor: TsColor; + procedure FromRecord(const AFill: TsChartFillRec); + function ToRecord: TsChartFillRec; end; TsChartLineSegment = record @@ -60,13 +85,6 @@ type property Items[AIndex: Integer]: TsChartLineStyle read GetItem write SetItem; default; end; - TsChartLine = record - Style: Integer; // index into chart's LineStyle list or predefined clsSolid/clsNoLine - Width: Double; // mm - Color: TsColor; // in hex: $00bbggrr, r=red, g=green, b=blue - Transparency: Double; // in percent - end; - TsChartElement = class private FChart: TsChart; @@ -83,6 +101,7 @@ type FBorder: TsChartLine; public constructor Create(AChart: TsChart); + destructor Destroy; override; property Background: TsChartFill read FBackground write FBackground; property Border: TsChartLine read FBorder write FBorder; end; @@ -201,9 +220,7 @@ type TsLineSeries = class(TsChartSeries) private - FLineStyle: TsChartLine; - FShowLines: Boolean; - FShowSymbols: Boolean; + FLine: TsChartLine; FSymbol: TsChartSeriesSymbol; FSymbolFill: TsChartFill; FSymbolBorder: TsChartLine; @@ -211,9 +228,8 @@ type FSymbolWidth: Double; // in mm public constructor Create(AChart: TsChart); - property LineStyle: TsChartLine read FLineStyle write FLineStyle; - property ShowLines: Boolean read FShowLines write FShowLines; - property ShowSymbols: Boolean read FShowSymbols write FShowSymbols; + destructor Destroy; override; + property Line: TsChartLine read FLine write FLine; property Symbol: TsChartSeriesSymbol read FSymbol write FSymbol; property SymbolBorder: TsChartLine read FSymbolBorder write FSymbolBorder; property SymbolFill: TsChartFill read FSymbolFill write FSymbolFill; @@ -330,13 +346,51 @@ implementation const DEFAULT_LINE_WIDTH = 0.75; // pts -{ TsChartFill } +{ TsChartLineRec } +class operator TsChartLineRec.= (A, B: TsChartLineRec): Boolean; +begin + Result := (A.Style = B.Style) and (A.Width = B.Width) and + (A.Color = B.Color) and (A.Transparency = B.Transparency); +end; -class operator TsChartFill.= (A, B: TsChartFill): Boolean; +{ TsChartLine } +procedure TsChartLine.FromRecord(const ALine: TsChartLineRec); +begin + Style := ALine.Style; + Width := ALine.Width; + Color := ALine.Color; + Transparency := ALine.Transparency; +end; + +function TsChartLine.ToRecord: TsChartLineRec; +begin + Result.Style := Style; + Result.Width := Width; + Result.Color := Color; + Result.Transparency := Transparency; +end; + +{ TsChartFillRec } +class operator TsChartFillRec.= (A, B: TsChartFillRec): Boolean; begin Result := (A.Style = B.Style) and (A.FgColor = B.FgColor) and (A.BgColor = B.BgColor); end; +{ TsChartFill } +procedure TsChartFill.FromRecord(const AFill: TsChartFillRec); +begin + Style := AFill.Style; + FgColor := AFill.FgColor; + BgColor := AFill.BgColor; +end; + +function TsChartFill.ToRecord: TsChartFillRec; +begin + Result.Style := Style; + Result.BgColor := BgColor; + Result.FgColor := FgColor; +end; + { TsChartLineStyle } @@ -397,14 +451,23 @@ end; constructor TsChartFillElement.Create(AChart: TsChart); begin inherited Create(AChart); + FBackground := TsChartFill.Create; FBackground.Style := fsSolidFill; FBackground.BgColor := scWhite; FBackground.FgColor := scWhite; + FBorder := TsChartLine.Create; FBorder.Style := clsSolid; FBorder.Width := PtsToMM(DEFAULT_LINE_WIDTH); FBorder.Color := scBlack; end; +destructor TsChartFillElement.Destroy; +begin + FBorder.Free; + FBackground.Free; + inherited; +end; + { TsChartText } @@ -443,22 +506,27 @@ begin FShowLabels := true; + FAxisLine := TsChartLine.Create; FAxisLine.Color := scBlack; FAxisLine.Style := clsSolid; FAxisLine.Width := PtsToMM(DEFAULT_LINE_WIDTH); + FMajorTickLines := TsChartLine.Create; FMajorTickLines.Color := scBlack; FMajorTickLines.Style := clsSolid; FMajorTickLines.Width := PtsToMM(DEFAULT_LINE_WIDTH); + FMinorTickLines := TsChartLine.Create; FMinorTickLines.Color := scBlack; FMinorTickLines.Style := clsSolid; FMinorTickLines.Width := PtsToMM(DEFAULT_LINE_WIDTH); + FMajorGridLines := TsChartLine.Create; FMajorGridLines.Color := scSilver; FMajorGridLines.Style := clsSolid; FMajorGridLines.Width := PtsToMM(DEFAULT_LINE_WIDTH); + FMinorGridLines := TsChartLine.Create; FMinorGridLines.Color := scSilver; FMinorGridLines.Style := clsDot; FMinorGridLines.Width := PtsToMM(DEFAULT_LINE_WIDTH); @@ -466,6 +534,11 @@ end; destructor TsChartAxis.Destroy; begin + FMinorGridLines.Free; + FMajorGridLines.Free; + FMinorTickLines.Free; + FMajorTickLines.Free; + FAxisLine.Free; FLabelFont.Free; inherited; end; @@ -623,14 +696,17 @@ begin FChartType := ctLine; - FLineStyle.Color := scBlack; - FLineStyle.Style := clsSolid; - FLineStyle.Width := PtsToMM(DEFAULT_LINE_WIDTH); + FLine := TsChartLine.Create; + FLine.Color := scBlack; + FLine.Style := clsSolid; + FLine.Width := PtsToMM(DEFAULT_LINE_WIDTH); + FSymbolBorder := TsChartline.Create; FSymbolBorder.Color := scBlack; FSymbolBorder.Style := clsSolid; FSymbolBorder.Width := PtsToMM(DEFAULT_LINE_WIDTH); + FSymbolFill := TsChartFill.Create; FSymbolFill.FgColor := scWhite; FSymbolFill.BgColor := scWhite; FSymbolFill.Style := fsSolidFill; @@ -639,6 +715,14 @@ begin FSymbolHeight := 2.5; end; +destructor TsLineSeries.Destroy; +begin + FSymbolFill.Free; + FSymbolBorder.Free; + FLine.Free; + inherited; +end; + { TsChart } diff --git a/components/fpspreadsheet/source/common/fpschartstyles.pas b/components/fpspreadsheet/source/common/fpschartstyles.pas index 3a45be860..cdd5ae590 100644 --- a/components/fpspreadsheet/source/common/fpschartstyles.pas +++ b/components/fpspreadsheet/source/common/fpschartstyles.pas @@ -16,13 +16,13 @@ type TsChartBackgroundStyle = class(TsChartStyle) private - FBackground: TsChartFill; - FBorder: TsChartLine; + FBackground: TsChartFillRec; + FBorder: TsChartLineRec; public procedure ApplyToChart(AChart: TsChart); override; procedure ExtractFromChart(AChart: TsChart); override; - property Background: TsChartFill read FBackground; - property Border: TsChartLine read FBorder; + property Background: TsChartFillRec read FBackground; + property Border: TsChartLineRec read FBorder; end; TsChartStyleList = class(TFPList) @@ -38,14 +38,14 @@ implementation procedure TsChartBackgroundStyle.ApplyToChart(AChart: TsChart); begin - AChart.Background := FBackground; - AChart.Border := FBorder; + AChart.Background.FromRecord(FBackground); + AChart.Border.FromRecord(FBorder); end; procedure TsChartBackgroundStyle.ExtractFromChart(AChart: TsChart); begin - FBackground := AChart.Background; - FBorder := AChart.Border; + FBackground := AChart.Background.ToRecord; + FBorder := AChart.Border.ToRecord; end; { TsChartStyleList }