From 87c0d8cc73bb9805aed7512debf923c7975fd771 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Fri, 27 Oct 2023 20:44:33 +0000 Subject: [PATCH] fpspreadsheet: Add TsAreaSeries. Add stack and 100%-stack modes (not working) git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8988 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/other/chart/write_chart_demo.lpr | 17 +++++---- .../fpspreadsheet/source/common/fpschart.pas | 20 ++++++++++ .../source/common/fpsopendocument.pas | 37 ++++++++++++------- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/components/fpspreadsheet/examples/other/chart/write_chart_demo.lpr b/components/fpspreadsheet/examples/other/chart/write_chart_demo.lpr index bd9b0064a..bb6494a4e 100644 --- a/components/fpspreadsheet/examples/other/chart/write_chart_demo.lpr +++ b/components/fpspreadsheet/examples/other/chart/write_chart_demo.lpr @@ -5,7 +5,7 @@ program write_chart_demo; uses SysUtils, fpspreadsheet, fpstypes, fpschart, xlsxooxml, fpsopendocument; const - SERIES_CLASS: TsChartSeriesClass = TsBarSeries; + SERIES_CLASS: TsChartSeriesClass = TsAreaSeries; var b: TsWorkbook; sh1, sh2, sh3: TsWorksheet; @@ -63,20 +63,21 @@ begin ch.Background.Style := fsSolidFill; ch.Border.Style := clsSolid; ch.PlotArea.Background.Style := fsSolidFill; - ch.RotatedAxes := true; + //ch.RotatedAxes := true; + ch.StackMode := csmStackedPercentage; ch.XAxis.ShowLabels := true; - ch.XAxis.LabelFont.Size := 8; + ch.XAxis.LabelFont.Size := 9; ch.XAxis.LabelFont.Color := scRed; - ch.XAxis.LabelFont.Style := [fssStrikeout]; + //ch.XAxis.LabelFont.Style := [fssStrikeout]; ch.XAxis.AxisLine.Color := scRed; ch.XAxis.Caption := 'This is the x axis'; ch.XAxis.CaptionFont.Color := scRed; ch.XAxis.CaptionFont.Size := 12; - ch.XAxis.Inverted := true; + //ch.XAxis.Inverted := true; ch.XAxis.MajorGridLines.Color := scRed; ch.XAxis.MinorGridLines.Color := scBlue; - ch.XAxis.MajorGridLines.Style := clsNoLine;//Solid; + ch.XAxis.MajorGridLines.Style := clsNoLine; //Solid; ch.XAxis.MinorGridLines.Style := clsNoLine; //Solid; ch.YAxis.ShowLabels := true; @@ -86,8 +87,8 @@ begin ch.YAxis.Caption := 'This is the y axis'; ch.YAxis.CaptionFont.Color := scBlue; ch.YAxis.CaptionFont.Size := 12; - ch.YAxis.LabelRotation := 90; - ch.YAxis.CaptionRotation := 90; + //ch.YAxis.LabelRotation := 90; + //ch.YAxis.CaptionRotation := 90; ch.YAxis.MajorGridLines.Color := scBlue; ch.YAxis.MajorGridLines.Style := clsLongDash; //clsSolid; ch.YAxis.MajorGridLines.Width := 0.5; // mm diff --git a/components/fpspreadsheet/source/common/fpschart.pas b/components/fpspreadsheet/source/common/fpschart.pas index 8082a4039..38ea80129 100644 --- a/components/fpspreadsheet/source/common/fpschart.pas +++ b/components/fpspreadsheet/source/common/fpschart.pas @@ -230,6 +230,11 @@ type end; TsChartSeriesClass = class of TsChartSeries; + TsAreaSeries = class(TsChartSeries) + public + constructor Create(AChart: TsChart); override; + end; + TsBarSeries = class(TsChartSeries) public constructor Create(AChart: TsChart); override; @@ -268,6 +273,8 @@ type property Items[AIndex: Integer]: TsChartSeries read GetItem write SetItem; default; end; + TsChartStackMode =(csmSideBySide, csmStacked, csmStackedPercentage); + TsChart = class(TsChartFillElement) private FIndex: Integer; // Index in workbook's chart list @@ -284,6 +291,7 @@ type FY2Axis: TsChartAxis; FRotatedAxes: Boolean; // For bar series: vertical columns <--> horizontal bars + FStackMode: TsChartStackMode; FTitle: TsChartText; FSubTitle: TsChartText; @@ -350,8 +358,11 @@ type property YAxis: TsChartAxis read FYAxis write FYAxis; { Attributes of the plot's secondary y axis (right) } property Y2Axis: TsChartAxis read FY2Axis write FY2Axis; + { x and y axes exchanged (for bar series) } property RotatedAxes: Boolean read FRotatedAxes write FRotatedAxes; + { stacked series } + property StackMode: TsChartStackMode read FStackMode write FStackMode; property CategoryLabelRange: TsCellRange read GetCategoryLabelRange; @@ -685,6 +696,15 @@ begin end; +{ TsAreaSeries } + +constructor TsAreaSeries.Create(AChart: TsChart); +begin + inherited Create(AChart); + FChartType := ctArea; +end; + + { TsBarSeries } constructor TsBarSeries.Create(AChart: TsChart); diff --git a/components/fpspreadsheet/source/common/fpsopendocument.pas b/components/fpspreadsheet/source/common/fpsopendocument.pas index 8d6239ca1..a6023002a 100644 --- a/components/fpspreadsheet/source/common/fpsopendocument.pas +++ b/components/fpspreadsheet/source/common/fpsopendocument.pas @@ -311,7 +311,7 @@ type AChartIndent, AStyleIndent: Integer; AChart: TsChart; var AStyleID: Integer); procedure WriteChartLegend(AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer; AChart: TsChart; var AStyleID: Integer); - procedure WriteChartNumberStyles(AStream: TStream; AIndent: Integer); + procedure WriteChartNumberStyles(AStream: TStream; AIndent: Integer; AChart: TsChart); procedure WriteChartPlotArea(AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer; AChart: TsChart; var AStyleID: Integer); procedure WriteChartSeries(AChartStream, AStyleStream: TStream; @@ -6771,7 +6771,7 @@ begin chartStream := TMemoryStream.Create; styleStream := TMemoryStream.Create; try - WriteChartNumberStyles(styleStream, 4); + WriteChartNumberStyles(styleStream, 4, AChart); styleID := 1; WriteChartBackground(chartStream, styleStream, 6, 4, AChart, styleID); @@ -7043,7 +7043,7 @@ begin end; procedure TsSpreadOpenDocWriter.WriteChartNumberStyles(AStream: TStream; - AIndent: Integer); + AIndent: Integer; AChart: TsChart); var indent: String; begin @@ -7055,7 +7055,13 @@ begin indent + '' + LE ); - // Other styles follow here, for example for "percentage" stacked bars. + if AChart.StackMode = csmStackedPercentage then + AppendToStream(AStream, + indent + '' + LE + + indent + ' ' + LE + + indent + ' %' + LE + + indent + '' + LE + ); end; procedure TsSpreadOpenDocWriter.WriteChartPlotArea(AChartStream, AStyleStream: TStream; @@ -7191,6 +7197,7 @@ var textProps: String = ''; graphProps: String = ''; chartProps: String = ''; + numStyle: String = 'N0'; begin Result := ''; if not Axis.Visible then @@ -7198,6 +7205,9 @@ begin chart := Axis.Chart; + if (Axis = chart.YAxis) and (chart.StackMode = csmStackedPercentage) then + numStyle := 'N10010'; + if Axis.ShowLabels then chartProps := chartProps + 'chart:display-label="true" '; @@ -7208,12 +7218,6 @@ begin chartProps := chartProps + 'chart:reverse-direction="true" '; angle := Axis.LabelRotation; - { - if chart.RotatedAxes then - begin - if angle = 0 then angle := 90 else if angle = 90 then angle := 0; - end; - } chartProps := chartProps + Format('style:rotation-angle="%d" ', [angle]); graphProps := 'svg:stroke-color="' + ColorToHTMLColorStr(Axis.AxisLine.Color) + '" '; @@ -7222,12 +7226,12 @@ begin indent := DupeString(' ', AIndent); Result := Format( - indent + '' + LE + + indent + '' + LE + indent + ' ' + LE + indent + ' ' + LE + indent + ' ' + LE + indent + '' + LE, - [ AStyleID, chartProps, graphProps, textProps ] + [ AStyleID, numStyle, chartProps, graphProps, textProps ] ); end; @@ -7429,16 +7433,23 @@ function TsSpreadOpenDocWriter.GetChartPlotAreaStyleAsXML(AChart: TsChart; var indent: String; verticalStr: String = ''; + stackModeStr: String = ''; begin indent := DupeString(' ', AIndent); - if (AChart.Series.Count > 0) and (AChart.Series[0] is TsBarSeries) and AChart.RotatedAxes then + if AChart.RotatedAxes then verticalStr := 'chart:vertical="true" '; + case AChart.StackMode of + csmSideBySide: ; + csmStacked: stackModeStr := 'chart:stacked="true" '; + csmStackedPercentage: stackModeStr := 'chart:percentage="true" '; + end; Result := Format( indent + ' ' + LE + indent + '