fpspreadsheet: Fix ods writer to create stock+volume charts. Add demo for it.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9080 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2023-12-13 21:51:57 +00:00
parent 12849441cd
commit c7f8430eb8
4 changed files with 205 additions and 2 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_volume_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_volume_write_demo.lpr"/>
<IsPartOfProject Value="True"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="stock_volume_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,130 @@
program stock_volume_write_demo;
{$mode objfpc}{$H+}
uses
SysUtils,
fpspreadsheet, fpstypes, fpsUtils, fpschart, xlsxooxml, fpsopendocument;
const
FILE_NAME = 'stock-vol';
CANDLE_STICK = true; //false;
var
book: TsWorkbook;
sheet: TsWorksheet;
ch: TsChart;
ser: TsStockSeries;
vser: TsChartSeries;
r: Integer;
d: TDate;
fn: String;
procedure WriteData(var ARow: Integer; var ADate: TDate; AVolume, AOpen, AHigh, ALow, AClose: Double);
begin
sheet.WriteDateTime(ARow, 0, ADate, nfShortDate);
sheet.WriteNumber (ARow, 1, AVolume, nfFixed, 0);
sheet.WriteNumber (ARow, 2, AOpen, nfFixed, 2);
sheet.WriteNumber (ARow, 3, AHigh, nfFixed, 2);
sheet.WriteNumber (ARow, 4, ALow, nfFixed, 2);
sheet.WriteNumber (ARow, 5, 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, 'Volume');
sheet.WriteText ( 2, 2, 'Open');
sheet.WriteText ( 2, 3, 'High');
sheet.WriteText ( 2, 4, 'Low');
sheet.WriteText ( 2, 5, 'Close');
d := EncodeDate(2023, 3, 6);
r := 3; // Vol O H L C
WriteData(r, d, 100000, 100, 110, 95, 105);
WriteData(r, d, 90000, 107, 112, 101, 104);
WriteData(r, d, 120000, 108, 113, 100, 106);
WriteData(r, d, 110000, 109, 115, 99, 110);
WriteData(r, d, 95000, 110, 119, 103, 115);
// Create chart: left/top in cell D4, 150 mm x 100 mm
ch := book.AddChart(sheet, 2, 6, 150, 100);
// Chart properties
ch.Border.Style := clsNoLine;
ch.Legend.Border.Style := clsNoLine;
ch.Legend.Position := lpBottom;
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 := 80;
ch.Y2Axis.Title.Caption := 'Volume';
ch.Y2Axis.MajorGridLines.Style := clsNoLine;
ch.Y2Axis.MinorGridLines.Style := clsNoLine;
ch.Y2Axis.AutomaticMax := false;
ch.Y2Axis.AutomaticMax := false;
ch.Y2Axis.Min := 0;
ch.Y2Axis.Max := 300000;
// Add stock series
ser := TsStockSeries.Create(ch);
// Stock series properties
ser.YAxis := alPrimary;
ser.CandleStick := CANDLE_STICK;
ser.CandleStickUpFill.Color := scGreen;
ser.CandlestickDownFill.Color := scRed;
ser.SetTitleAddr (0, 0);
if CANDLE_STICK then ser.SetOpenRange (3, 2, 7, 2);
ser.SetHighRange (3, 3, 7, 3);
ser.SetLowRange (3, 4, 7, 4);
ser.SetCloseRange(3, 5, 7, 5);
ser.SetLabelRange(3, 0, 7, 0);
// Add bar series for volume data
// (Activate one of the three next lines)
vser := TsBarSeries.Create(ch);
//vser := TsAreaSeries.Create(ch);
//vser := TsLineSeries.Create(ch);
// Bar series properties
vser.YAxis := alSecondary;
vser.SetLabelRange(3, 0, 7, 0);
vser.SetYRange (3, 1, 7, 1);
vser.SetTitleAddr (2, 1);
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

@ -52,6 +52,11 @@
<Mode Name="Default"/>
</BuildModes>
</Target>
<Target FileName="stock_volume_write_demo.lpi">
<BuildModes>
<Mode Name="Default"/>
</BuildModes>
</Target>
</Targets>
</ProjectGroup>
</CONFIG>

View File

@ -3447,8 +3447,8 @@ begin
// series
for i := 0 to AChart.Series.Count-1 do
if AChart.GetChartType = ctStock then
WriteChartStockSeries(AChartStream, AStyleStream, AchartIndent+2, AStyleIndent, AChart, i, AStyleID)
if AChart.Series[i].ChartType = ctStock then
WriteChartStockSeries(AChartStream, AStyleStream, AChartIndent+2, AStyleIndent, AChart, i, AStyleID)
else
WriteChartSeries(AChartStream, AStyleStream, AChartIndent+2, AStyleIndent, AChart, i, AStyleID);