fpspreadsheet: ods writer supports stockseries

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9079 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2023-12-13 21:22:33 +00:00
parent b659d15d6a
commit 12849441cd
7 changed files with 473 additions and 23 deletions

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="12"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<Title Value="stock_write_demo"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<BuildModes>
<Item Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<RequiredPackages>
<Item>
<PackageName Value="laz_fpspreadsheet"/>
</Item>
</RequiredPackages>
<Units>
<Unit>
<Filename Value="stock_write_demo.lpr"/>
<IsPartOfProject Value="True"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="stock_write_demo"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf3"/>
</Debugging>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions>
<Item>
<Name Value="EAbort"/>
</Item>
<Item>
<Name Value="ECodetoolError"/>
</Item>
<Item>
<Name Value="EFOpenError"/>
</Item>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,104 @@
program stock_write_demo;
{$mode objfpc}{$H+}
uses
SysUtils,
fpspreadsheet, fpstypes, fpsUtils, fpschart, xlsxooxml, fpsopendocument;
const
FILE_NAME = 'stock';
CANDLE_STICK = false;
var
book: TsWorkbook;
sheet: TsWorksheet;
ch: TsChart;
ser: TsStockSeries;
r: Integer;
d: TDate;
fn: String;
procedure WriteData(var ARow: Integer; var ADate: TDate; AOpen, AHigh, ALow, AClose: Double);
begin
sheet.WriteDateTime(ARow, 0, ADate, nfShortDate);
sheet.WriteNumber (ARow, 1, AOpen, nfFixed, 2);
sheet.WriteNumber (ARow, 2, AHigh, nfFixed, 2);
sheet.WriteNumber (ARow, 3, ALow, nfFixed, 2);
sheet.WriteNumber (ARow, 4, AClose,nfFixed, 2);
inc(ARow);
ADate := ADate + 1;
end;
begin
book := TsWorkbook.Create;
try
// Worksheet
sheet := book.AddWorksheet('test');
// Enter data
sheet.WriteText (0, 0, 'My Company');
sheet.WriteFont (0, 0, '', 12, [fssBold], scBlack);
sheet.WriteText ( 2, 0, 'Date');
sheet.WriteText ( 2, 1, 'Open');
sheet.WriteText ( 2, 2, 'High');
sheet.WriteText ( 2, 3, 'Low');
sheet.WriteText ( 2, 4, 'Close');
d := EncodeDate(2023, 3, 6);
r := 3; // O H L C
WriteData(r, d, 100, 110, 95, 105);
WriteData(r, d, 107, 112, 101, 104);
WriteData(r, d, 108, 113, 100, 106);
WriteData(r, d, 109, 115, 99, 110);
WriteData(r, d, 110, 119, 103, 115);
// Create chart: left/top in cell D4, 150 mm x 100 mm
ch := book.AddChart(sheet, 2, 5, 150, 100);
// Chart properties
ch.Border.Style := clsNoLine;
ch.Legend.Border.Style := clsNoLine;
ch.XAxis.DateTime := true;
ch.XAxis.Title.Caption := 'Date';
ch.XAxis.MajorGridLines.Style := clsNoLine;
ch.XAxis.MinorGridLines.Style := clsNoLine;
ch.YAxis.Title.Caption := 'Stock price';
ch.YAxis.MajorGridLines.Style := clsSolid;
ch.YAxis.MinorGridLines.Style := clsNoLine;
ch.YAxis.AutomaticMin := false;
ch.YAxis.AutomaticMax := false;
ch.YAxis.Max := 120;
ch.YAxis.Min := 90;
// Add stock series
ser := TsStockSeries.Create(ch);
// Series properties
ser.CandleStick := CANDLE_STICK;
ser.CandleStickUpFill.Color := scGreen;
ser.CandlestickDownFill.Color := scRed;
ser.SetTitleAddr (0, 0);
if CANDLE_STICK then ser.SetOpenRange (3, 1, 7, 1);
ser.SetHighRange (3, 2, 7, 2);
ser.SetLowRange (3, 3, 7, 3);
ser.SetCloseRange(3, 4, 7, 4);
ser.SetXRange (3, 0, 7, 0);
ser.SetLabelRange(3, 0, 7, 0);
if CANDLE_STICK then
fn := FILE_NAME + '-candle'
else
fn := FILE_NAME + '-hlc';
{
book.WriteToFile(fn + '.xlsx', true); // Excel fails to open the file
WriteLn('Data saved with chart to ', fn, '.xlsx');
}
book.WriteToFile(fn + '.ods', true);
WriteLn('Data saved with chart to ', fn, '.ods');
finally
book.Free;
end;
end.

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectGroup FileVersion="2">
<Targets>
<Target FileName="barchart_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="barchart_2axes_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="bubblechart_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="piechart_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="radarchart_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="regressionchart_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="scatter_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="scatter_log_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="scatter_loglog_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="stock_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
</Targets>
</ProjectGroup>
</CONFIG>

View File

@ -103,6 +103,8 @@ object Form1: TForm1
'../../../other/chart/scatter.ods' '../../../other/chart/scatter.ods'
'../../../other/chart/scatter_log.ods' '../../../other/chart/scatter_log.ods'
'../../../other/chart/scatter_loglog.ods' '../../../other/chart/scatter_loglog.ods'
'../../../other/chart/stock-candle.ods'
'../../../other/chart/stock-hlc.ods'
) )
TabOrder = 0 TabOrder = 0
OnCloseUp = ComboBox1CloseUp OnCloseUp = ComboBox1CloseUp

View File

@ -267,6 +267,8 @@ type
constructor Create(AChart: TsChart); constructor Create(AChart: TsChart);
destructor Destroy; override; destructor Destroy; override;
function OtherAxis: TsChartAxis; function OtherAxis: TsChartAxis;
procedure SetCategoryRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
procedure SetCategoryRange(ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal);
property AutomaticMax: Boolean read FAutomaticMax write FAutomaticMax; property AutomaticMax: Boolean read FAutomaticMax write FAutomaticMax;
property AutomaticMin: Boolean read FAutomaticMin write FAutomaticMin; property AutomaticMin: Boolean read FAutomaticMin write FAutomaticMin;
property AutomaticMajorInterval: Boolean read FAutomaticMajorInterval write FAutomaticMajorInterval; property AutomaticMajorInterval: Boolean read FAutomaticMajorInterval write FAutomaticMajorInterval;
@ -558,9 +560,13 @@ type
public public
constructor Create(AChart: TsChart); override; constructor Create(AChart: TsChart); override;
destructor Destroy; override; destructor Destroy; override;
procedure SetOpenRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
procedure SetOpenRange(ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal); procedure SetOpenRange(ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal);
procedure SetHighRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
procedure SetHighRange(ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal); procedure SetHighRange(ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal);
procedure SetLowRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
procedure SetLowRange (ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal); procedure SetLowRange (ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal);
procedure SetCloseRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
procedure SetCloseRange(ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal); procedure SetCloseRange(ASheet1: String; ARow1, ACol1: Cardinal; ASheet2: String; ARow2, ACol2: Cardinal);
property CandleStick: Boolean read FCandleStick write FCandleStick; property CandleStick: Boolean read FCandleStick write FCandleStick;
property CandleStickDownFill: TsChartFill read FCandleStickDownFill write FCandleStickDownFill; property CandleStickDownFill: TsChartFill read FCandleStickDownFill write FCandleStickDownFill;
@ -1252,6 +1258,24 @@ begin
Result := Chart.YAxis; Result := Chart.YAxis;
end; end;
procedure TsChartAxis.SetCategoryRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
begin
SetCategoryRange('', ARow1, ACol1, '', ARow2, ACol2);
end;
procedure TsChartAxis.SetCategoryRange(ASheet1: String; ARow1, ACol1: Cardinal;
ASheet2: String; ARow2, ACol2: Cardinal);
begin
if (ARow1 <> ARow2) and (ACol1 <> ACol2) then
raise Exception.Create('Category values can only be located in a single column or row.');
FCategoryRange.Sheet1 := ASheet1;
FCategoryRange.Row1 := ARow1;
FCategoryRange.Col1 := ACol1;
FCategoryRange.Sheet2 := ASheet2;
FCategoryRange.Row2 := ARow2;
FCategoryRange.Col2 := ACol2;
end;
{ TsChartLegend } { TsChartLegend }
constructor TsChartLegend.Create(AChart: TsChart); constructor TsChartLegend.Create(AChart: TsChart);
@ -1818,6 +1842,11 @@ begin
inherited; inherited;
end; end;
procedure TsStockSeries.SetOpenRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
begin
SetOpenRange('', ARow1, ACol1, '', ARow2, ACol2);
end;
procedure TsStockSeries.SetOpenRange(ASheet1: String; ARow1, ACol1: Cardinal; procedure TsStockSeries.SetOpenRange(ASheet1: String; ARow1, ACol1: Cardinal;
ASheet2: String; ARow2, ACol2: Cardinal); ASheet2: String; ARow2, ACol2: Cardinal);
begin begin
@ -1831,6 +1860,10 @@ procedure TsStockSeries.SetOpenRange(ASheet1: String; ARow1, ACol1: Cardinal;
FOpenRange.Col2 := ACol2; FOpenRange.Col2 := ACol2;
end; end;
procedure TsStockSeries.SetHighRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
begin
SetHighRange('', ARow1, ACol1, '', ARow2, ACol2);
end;
procedure TsStockSeries.SetHighRange(ASheet1: String; ARow1, ACol1: Cardinal; procedure TsStockSeries.SetHighRange(ASheet1: String; ARow1, ACol1: Cardinal;
ASheet2: String; ARow2, ACol2: Cardinal); ASheet2: String; ARow2, ACol2: Cardinal);
begin begin
@ -1844,6 +1877,11 @@ begin
FHighRange.Col2 := ACol2; FHighRange.Col2 := ACol2;
end; end;
procedure TsStockSeries.SetLowRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
begin
SetLowRange('', ARow1, ACol1, '', ARow2, ACol2);
end;
procedure TsStockSeries.SetLowRange(ASheet1: String; ARow1, ACol1: Cardinal; procedure TsStockSeries.SetLowRange(ASheet1: String; ARow1, ACol1: Cardinal;
ASheet2: String; ARow2, ACol2: Cardinal); ASheet2: String; ARow2, ACol2: Cardinal);
begin begin
@ -1857,6 +1895,11 @@ procedure TsStockSeries.SetLowRange(ASheet1: String; ARow1, ACol1: Cardinal;
FLowRange.Col2 := ACol2; FLowRange.Col2 := ACol2;
end; end;
procedure TsStockSeries.SetCloseRange(ARow1, ACol1, ARow2, ACol2: Cardinal);
begin
SetCloseRange('', ARow1, ACol1, '', ARow2, ACol2);
end;
procedure TsStockSeries.SetCloseRange(ASheet1: String; ARow1, ACol1: Cardinal; procedure TsStockSeries.SetCloseRange(ASheet1: String; ARow1, ACol1: Cardinal;
ASheet2: String; ARow2, ACol2: Cardinal); ASheet2: String; ARow2, ACol2: Cardinal);
begin begin
@ -1974,10 +2017,19 @@ begin
end; end;
function TsChart.GetChartType: TsChartType; function TsChart.GetChartType: TsChartType;
var
i: Integer;
begin begin
if FSeriesList.Count > 0 then if FSeriesList.Count > 0 then
Result := Series[0].ChartType begin
else Result := Series[0].ChartType;
for i := 0 to FSeriesList.Count-1 do
if FSeriesList[i] is TsStockSeries then
begin
Result := ctStock;
exit;
end;
end else
Result := ctEmpty; Result := ctEmpty;
end; end;

View File

@ -5901,7 +5901,6 @@ end;
procedure TsSpreadOpenDocWriter.InternalWriteToStream(AStream: TStream); procedure TsSpreadOpenDocWriter.InternalWriteToStream(AStream: TStream);
var var
FZip: TZipper; FZip: TZipper;
i: Integer;
begin begin
{ Analyze the workbook and collect all information needed } { Analyze the workbook and collect all information needed }
ListAllNumFormats; ListAllNumFormats;

View File

@ -91,13 +91,13 @@ type
ALine: TsChartLine; AIndent, AStyleID: Integer): String; ALine: TsChartLine; AIndent, AStyleID: Integer): String;
function GetChartLineStyleGraphicPropsAsXML(AChart: TsChart; function GetChartLineStyleGraphicPropsAsXML(AChart: TsChart;
ALine: TsChartLine): String; ALine: TsChartLine): String;
function GetChartPlotAreaStyleAsXML(AChart: TsChart; function GetChartPlotAreaStyleAsXML(AChart: TsChart; AIndent, AStyleID: Integer): String;
AIndent, AStyleID: Integer): String;
function GetChartRegressionEquationStyleAsXML(AChart: TsChart; function GetChartRegressionEquationStyleAsXML(AChart: TsChart;
AEquation: TsRegressionEquation; AIndent, AStyleID: Integer): String; AEquation: TsRegressionEquation; AIndent, AStyleID: Integer): String;
function GetChartRegressionStyleAsXML(AChart: TsChart; ASeriesIndex, AIndent, AStyleID: Integer): String; function GetChartRegressionStyleAsXML(AChart: TsChart; ASeriesIndex, AIndent, AStyleID: Integer): String;
function GetChartSeriesDataPointStyleAsXML(AChart: TsChart; ASeriesIndex, APointIndex, AIndent, AStyleID: Integer): String; function GetChartSeriesDataPointStyleAsXML(AChart: TsChart; ASeriesIndex, APointIndex, AIndent, AStyleID: Integer): String;
function GetChartSeriesStyleAsXML(AChart: TsChart; ASeriesIndex, AIndent, AStyleID: integer): String; function GetChartSeriesStyleAsXML(AChart: TsChart; ASeriesIndex, AIndent, AStyleID: integer): String;
function GetChartStockSeriesStyleAsXML(AChart: TsChart; ASeries: TsStockSeries; AKind: Integer; AIndent, AStyleID: Integer): String;
procedure CheckAxis(AChart: TsChart; Axis: TsChartAxis); procedure CheckAxis(AChart: TsChart; Axis: TsChartAxis);
function GetNumberFormatID(ANumFormat: String): String; function GetNumberFormatID(ANumFormat: String): String;
@ -125,6 +125,9 @@ type
procedure WriteChartSeries(AChartStream, AStyleStream: TStream; procedure WriteChartSeries(AChartStream, AStyleStream: TStream;
AChartIndent, AStyleIndent: Integer; AChart: TsChart; ASeriesIndex: Integer; AChartIndent, AStyleIndent: Integer; AChart: TsChart; ASeriesIndex: Integer;
var AStyleID: Integer); var AStyleID: Integer);
procedure WriteChartStockSeries(AChartStream, AStyleStream: TStream;
AChartIndent, AStyleIndent: Integer; AChart: TsChart; ASeriesIndex: Integer;
var AStyleID: Integer);
// procedure WriteChartTable(AStream: TStream; AChart: TsChart; AIndent: Integer); // procedure WriteChartTable(AStream: TStream; AChart: TsChart; AIndent: Integer);
procedure WriteChartTitle(AChartStream, AStyleStream: TStream; procedure WriteChartTitle(AChartStream, AStyleStream: TStream;
AChartIndent, AStyleIndent: Integer; AChart: TsChart; IsSubtitle: Boolean; AChartIndent, AStyleIndent: Integer; AChart: TsChart; IsSubtitle: Boolean;
@ -2304,6 +2307,8 @@ var
stackModeStr: String = ''; stackModeStr: String = '';
rightAngledAxes: String = ''; rightAngledAxes: String = '';
startAngleStr: String = ''; startAngleStr: String = '';
candleStickStr: String = '';
i: Integer;
begin begin
indent := DupeString(' ', AIndent); indent := DupeString(' ', AIndent);
@ -2332,6 +2337,13 @@ begin
if not (AChart.GetChartType in [ctRadar, ctPie]) then if not (AChart.GetChartType in [ctRadar, ctPie]) then
rightAngledAxes := 'chart:right-angled-axes="true" '; rightAngledAxes := 'chart:right-angled-axes="true" ';
for i := 0 to AChart.Series.Count-1 do
if (AChart.Series[i] is TsStockSeries) and TsStockSeries(AChart.Series[i]).CandleStick then
begin
candleStickStr := 'chart:japanese-candle-stick="true" ';
break;
end;
Result := Format( Result := Format(
indent + ' <style:style style:name="ch%d" style:family="chart">', [ AStyleID ]) + LE + indent + ' <style:style style:name="ch%d" style:family="chart">', [ AStyleID ]) + LE +
indent + ' <style:chart-properties ' + indent + ' <style:chart-properties ' +
@ -2339,6 +2351,7 @@ begin
verticalStr + verticalStr +
stackModeStr + stackModeStr +
startAngleStr + startAngleStr +
candleStickStr +
'chart:symbol-type="automatic" ' + 'chart:symbol-type="automatic" ' +
'chart:include-hidden-cells="false" ' + 'chart:include-hidden-cells="false" ' +
'chart:auto-position="true" ' + 'chart:auto-position="true" ' +
@ -2575,6 +2588,41 @@ begin
); );
end; end;
function TsSpreadOpenDocChartWriter.GetChartStockSeriesStyleAsXML(AChart: TsChart;
ASeries: TsStockSeries; AKind: Integer; AIndent, AStyleID: Integer): String;
var
indent: String;
fillStr: String = '';
lineStr: String = '';
begin
case AKind of
0: // gain marker
begin
fillStr := GetChartFillStyleGraphicPropsAsXML(AChart, ASeries.CandleStickUpFill);
lineStr := GetChartLineStyleGraphicPropsAsXML(AChart, ASeries.CandleStickUpBorder);
end;
1: // loss marker
begin
fillStr := GetChartFillStyleGraphicPropsAsXML(AChart, ASeries.CandleStickDownFill);
lineStr := GetChartLineStyleGraphicPropsAsXML(AChart, ASeries.CandleStickDownBorder);
end;
2: // range line
lineStr := GetChartLineStyleGraphicPropsAsXML(AChart, ASeries.RangeLine);
end;
if (fillStr <> '') or (lineStr <> '') then
begin
indent := DupeString(' ', AIndent);
Result := Format(
indent + '<style:style style:name="ch%d" style:family="chart">' + LE +
indent + ' <style:graphic-properties ' + fillstr + lineStr + '/>' + LE +
indent + '</style:style>' + LE,
[ AStyleID ]
);
end else
Result := '';
end;
function TsSpreadOpenDocChartWriter.GetNumberFormatID(ANumFormat: String): String; function TsSpreadOpenDocChartWriter.GetNumberFormatID(ANumFormat: String): String;
var var
idx: Integer; idx: Integer;
@ -2931,9 +2979,8 @@ begin
end; end;
procedure TsSpreadOpenDocChartWriter.WriteChartAxis( procedure TsSpreadOpenDocChartWriter.WriteChartAxis(
AChartStream, AStyleStream: TStream; AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer;
AChartIndent, AStyleIndent: Integer; Axis: TsChartAxis; Axis: TsChartAxis; var AStyleID: Integer);
var AStyleID: Integer);
var var
indent: String; indent: String;
captionKind: Integer; captionKind: Integer;
@ -2966,6 +3013,11 @@ begin
[ AStyleID, AXIS_ID[captionKind], AXIS_LEVEL[captionKind], AXIS_ID[captionKind] ] [ AStyleID, AXIS_ID[captionKind], AXIS_LEVEL[captionKind], AXIS_ID[captionKind] ]
)); ));
if Axis.DateTime then
AppendToStream(AChartStream,
indent + ' <chartooo:date-scale/>' + LE
);
if (Axis = chart.XAxis) and (not chart.IsScatterChart) and (chart.Series.Count > 0) then if (Axis = chart.XAxis) and (not chart.IsScatterChart) and (chart.Series.Count > 0) then
begin begin
series := chart.Series[0]; series := chart.Series[0];
@ -3050,8 +3102,8 @@ end;
{ Writes the chart's background to the xml stream } { Writes the chart's background to the xml stream }
procedure TsSpreadOpenDocChartWriter.WriteChartBackground( procedure TsSpreadOpenDocChartWriter.WriteChartBackground(
AChartStream, AStyleStream: TStream; AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer;
AChartIndent, AStyleIndent: Integer; AChart: TsChart; var AStyleID: Integer); AChart: TsChart; var AStyleID: Integer);
var var
indent: String; indent: String;
chartClass: String; chartClass: String;
@ -3064,7 +3116,7 @@ begin
AppendToStream(AChartStream, Format( AppendToStream(AChartStream, Format(
indent + '<chart:chart chart:style-name="ch%d" %s' + LE + indent + '<chart:chart chart:style-name="ch%d" %s' + LE +
indent + ' svg:width="%.3fmm" svg:height="%.3fmm" ' + LE + indent + ' svg:width="%.3fmm" svg:height="%.3fmm" ' + LE +
indent + ' xlink:href=".." xlink:type="simple">' + LE, [ indent + ' xlink:type="simple" xlink:href="..">' + LE, [
AStyleID, AStyleID,
chartClass, chartClass,
AChart.Width, AChart.Height // Width, Height are in mm AChart.Width, AChart.Height // Width, Height are in mm
@ -3205,8 +3257,9 @@ begin
end; end;
{ Writes the chart's legend to the xml stream } { Writes the chart's legend to the xml stream }
procedure TsSpreadOpenDocChartWriter.WriteChartLegend(AChartStream, AStyleStream: TStream; procedure TsSpreadOpenDocChartWriter.WriteChartLegend(
AChartIndent, AStyleIndent: Integer; AChart: TsChart; var AStyleID: Integer); AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer;
AChart: TsChart; var AStyleID: Integer);
var var
indent: String; indent: String;
canOverlap: String = ''; canOverlap: String = '';
@ -3329,8 +3382,9 @@ begin
); );
end; end;
procedure TsSpreadOpenDocChartWriter.WriteChartPlotArea(AChartStream, AStyleStream: TStream; procedure TsSpreadOpenDocChartWriter.WriteChartPlotArea(
AChartIndent, AStyleIndent: Integer; AChart: TsChart; var AStyleID: Integer); AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer;
AChart: TsChart; var AStyleID: Integer);
var var
indent: String; indent: String;
i: Integer; i: Integer;
@ -3393,6 +3447,9 @@ begin
// series // series
for i := 0 to AChart.Series.Count-1 do for i := 0 to AChart.Series.Count-1 do
if AChart.GetChartType = ctStock then
WriteChartStockSeries(AChartStream, AStyleStream, AchartIndent+2, AStyleIndent, AChart, i, AStyleID)
else
WriteChartSeries(AChartStream, AStyleStream, AChartIndent+2, AStyleIndent, AChart, i, AStyleID); WriteChartSeries(AChartStream, AStyleStream, AChartIndent+2, AStyleIndent, AChart, i, AStyleID);
// close xml node // close xml node
@ -3402,9 +3459,8 @@ begin
end; end;
procedure TsSpreadOpenDocChartWriter.WriteChartSeries( procedure TsSpreadOpenDocChartWriter.WriteChartSeries(
AChartStream, AStyleStream: TStream; AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer;
AChartIndent, AStyleIndent: Integer; AChart: TsChart; ASeriesIndex: Integer; AChart: TsChart; ASeriesIndex: Integer; var AStyleID: Integer);
var AStyleID: Integer);
var var
indent: String; indent: String;
series: TsChartSeries; series: TsChartSeries;
@ -3513,7 +3569,6 @@ begin
else else
chartClass := CHART_TYPE_NAMES[series.ChartType]; chartClass := CHART_TYPE_NAMES[series.ChartType];
// Store the series properties
AppendToStream(AChartStream, Format( AppendToStream(AChartStream, Format(
indent + '<chart:series chart:style-name="ch%d" ' + indent + '<chart:series chart:style-name="ch%d" ' +
'chart:class="chart:%s" ' + // series type 'chart:class="chart:%s" ' + // series type
@ -3657,6 +3712,119 @@ begin
inc(AStyleID); inc(AStyleID);
end; end;
procedure TsSpreadOpenDocChartWriter.WriteChartStockSeries(
AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer;
AChart: TsChart; ASeriesIndex: Integer; var AStyleID: Integer);
procedure WriteRange(const AIndent, ARangeStr, ATitleStr, AYAxisStr: String; ACount: Integer);
begin
AppendToStream(AChartStream, Format(
AIndent + '<chart:series ' + ARangeStr + AtitleStr + AYAxisStr + '>' + LE +
AIndent + ' <chart:data-point chart:repeated="%d"/>' + LE +
AIndent + '</chart:series>' + LE,
[ ACount ] ));
end;
var
indent: String;
openRange: String = '';
highRange: String = '';
lowRange: String = '';
closeRange: String = '';
titleAddr: String = '';
seriesYAxis: String = '';
series: TsStockSeries;
count: Integer;
begin
if not (AChart.Series[ASeriesIndex] is TsStockSeries) then
exit;
series := TsStockSeries(AChart.Series[ASeriesIndex]);
indent := DupeString(' ', AChartIndent);
// These are the open/high/low/close values of the OHLC series
if series.CandleStick and (not series.OpenRange.IsEmpty) then
openRange := Format('chart:values-cell-range-address="%s" ', [
GetSheetCellRangeString_ODS(
series.OpenRange.GetSheet1Name, series.OpenRange.GetSheet2Name,
series.OpenRange.Row1, series.OpenRange.Col1,
series.OpenRange.Row2, series.OpenRange.Col2,
rfAllRel, false)
]);
if not series.HighRange.IsEmpty then
highRange := Format('chart:values-cell-range-address="%s" ', [
GetSheetCellRangeString_ODS(
series.HighRange.GetSheet1Name, series.HighRange.GetSheet2Name,
series.HighRange.Row1, series.HighRange.Col1,
series.HighRange.Row2, series.HighRange.Col2,
rfAllRel, false)
]);
if not series.LowRange.IsEmpty then
lowRange := Format('chart:values-cell-range-address="%s" ', [
GetSheetCellRangeString_ODS(
series.LowRange.GetSheet1Name, series.LowRange.GetSheet2Name,
series.LowRange.Row1, series.LowRange.Col1,
series.LowRange.Row2, series.LowRange.Col2,
rfAllRel, false)
]);
if not series.CloseRange.IsEmpty then
closeRange := Format('chart:values-cell-range-address="%s" ',[
GetSheetCellRangeString_ODS(
series.CloseRange.GetSheet1Name, series.CloseRange.GetSheet2Name,
series.CloseRange.Row1, series.CloseRange.Col1,
series.CloseRange.Row2, series.CloseRange.Col2,
rfAllRel, false)
]);
// Title of the series for the legend
titleAddr := Format('chart:label-cell-address="%s" ', [
GetSheetCellRangeString_ODS(
series.TitleAddr.GetSheetName, series.TitleAddr.GetSheetName,
series.TitleAddr.Row, series.TitleAddr.Col,
series.TitleAddr.Row, series.TitleAddr.Col,
rfAllRel, false)
]);
// Axis of the series
case series.YAxis of
alPrimary : seriesYAxis := 'chart:attached-axis="primary-y" ';
alSecondary: seriesYAxis := 'chart:attached-axis="secondary-y" ';
end;
// Number of data points
if series.YValuesInCol then
count := series.YRange.Row2 - series.YRange.Row1 + 1
else
count := series.YRange.Col2 - series.YRange.Col1 + 1;
// Store the series properties
// "Open" values, only for CandleStick mode
if series.CandleStick then
WriteRange(indent, openRange, '', seriesYAxis, count);
// "Low" values
WriteRange(indent, lowRange, '', seriesYAxis, count);
// "High" values
WriteRange(indent, highRange, '', seriesYAxis, count);
// "Close" values
WriteRange(indent, closeRange, titleAddr, seriesYAxis, count);
// Stock series styles
AppendToStream(AChartStream, Format(
indent + '<chart:stock-gain-marker chart:style-name="ch%d" />' + LE +
indent + '<chart:stock-loss-marker chart:style-name="ch%d" />' + LE +
indent + '<chart:stock-range-line chart:style-name="ch%d" />' + LE,
[ AStyleID, AStyleID + 1, AStyleID + 2 ]));
AppendToStream(AStyleStream,
GetChartStockSeriesStyleAsXML(AChart, series, 0, AStyleIndent, AStyleID));
AppendToStream(AStyleStream,
GetChartStockSeriesStyleAsXML(AChart, series, 1, AStyleIndent, AStyleID + 1));
AppendToStream(AStyleStream,
GetChartStockSeriesStyleAsXML(AChart, series, 2, AStyleIndent, AStyleID + 2));
inc(AStyleID, 3);
end;
procedure TsSpreadOpenDocChartWriter.WriteCharts; procedure TsSpreadOpenDocChartWriter.WriteCharts;
var var
i: Integer; i: Integer;
@ -3816,9 +3984,9 @@ end;
*) *)
{ Writes the chart's title (or subtitle, depending on the value of IsSubTitle) { Writes the chart's title (or subtitle, depending on the value of IsSubTitle)
to the xml stream (chart stream) and the corresponding style to the stylestream. } to the xml stream (chart stream) and the corresponding style to the stylestream. }
procedure TsSpreadOpenDocChartWriter.WriteChartTitle(AChartStream, AStyleStream: TStream; procedure TsSpreadOpenDocChartWriter.WriteChartTitle(
AChartIndent, AStyleIndent: Integer; AChart: TsChart; IsSubtitle: Boolean; AChartStream, AStyleStream: TStream; AChartIndent, AStyleIndent: Integer;
var AStyleID: Integer); AChart: TsChart; IsSubtitle: Boolean; var AStyleID: Integer);
var var
title: TsChartText; title: TsChartText;
captionKind: Integer; captionKind: Integer;