fpspreadsheet: Fix default number format in ods chart writer

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9162 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2024-01-23 18:43:49 +00:00
parent 59cb94609d
commit bf4530543e
2 changed files with 49 additions and 24 deletions

View File

@ -3857,9 +3857,27 @@ begin
while Assigned(NumFormatNode) do
begin
numfmt_nodename := NumFormatNode.NodeName;
numfmtName := GetAttrValue(NumFormatNode, 'style:name');
case numfmt_nodename of
// Numbers
'number:number-style',
'number:percentage-style',
'number:currency-style':
ReadNumberStyle(NumFormatNode, numfmtName);
// Date/time values
'number:date-style',
'number:time-style':
ReadDateTimeStyle(NumFormatNode, numFmtName);
// Text values
'number:text-style':
ReadTextStyle(NumFormatNode, numFmtName);
end;
{
if NumFormatNode.HasAttributes then
numfmtName := GetAttrValue(NumFormatNode, 'style:name') else
numfmtName := GetAttrValue(NumFormatNode, 'style:name')
else
numfmtName := '';
// Numbers (nfFixed, nfFixedTh, nfExp, nfPercentage)
@ -3876,6 +3894,7 @@ begin
// Text values
if (numfmt_nodename = 'number:text-style') then
ReadTextStyle(NumFormatNode, numfmtName);
}
// Next node
NumFormatNode := NumFormatNode.NextSibling;

View File

@ -308,24 +308,28 @@ type
constructor Create;
function Add(const ANumFormat: String): Integer; override;
function FindFormatByName(const AName: String): String;
function IndexOfFormat(ANumFormat: String): Integer;
end;
constructor TsChartNumberFormatList.Create;
begin
inherited;
Add('N0'); // default number format
NameValueSeparator := ':';
Add(''); // default number format
end;
// Adds a new format, but make sure to avoid duplicates.
// The format list stores the item internally as name:value pair with
// name = 'N'+index and value = ANumFormat
function TsChartNumberFormatList.Add(const ANumFormat: String): Integer;
begin
if (ANumFormat = '') and (Count > 0) then
Result := 0
else
begin
Result := IndexOf(ANumFormat);
Result := IndexOfFormat(ANumFormat);
if Result = -1 then
Result := inherited Add(ANumFormat);
Result := inherited Add(Format('N%d:%s', [Count, ANumFormat]));
end;
end;
@ -345,6 +349,24 @@ begin
end;
end;
function TsChartNumberFormatList.IndexOfFormat(ANumFormat: String): Integer;
var
i: Integer;
fmt: String;
begin
ANumFormat := lowercase(ANumFormat);
for i := 0 to Count-1 do
begin
fmt := Lowercase(ValueFromIndex[i]);
if fmt = ANumFormat then
begin
Result := i;
exit;
end;
end;
Result := -1;
end;
{------------------------------------------------------------------------------}
{ internal picture storage }
@ -395,8 +417,6 @@ begin
FChartFiles := TStringList.Create;
FNumberFormatList := TsChartNumberFormatList.Create;
FNumberFormatList.NameValueSeparator := ':';
FNumberFormatList.Add('N0');
FStreamList := TStreamList.Create;
FPieSeriesStartAngle := 999;
@ -2670,6 +2690,8 @@ begin
series := AChart.Series[ASeriesIndex];
dataPointStyle := series.DataPointStyles[APointIndex];
if dataPointStyle = nil then
exit;
chartProps := 'chart:solid-type="cuboid" ';
@ -2847,7 +2869,7 @@ var
regression: TsChartRegression;
begin
FNumberFormatList.Clear;
FNumberFormatList.Add('N0');
FNumberFormatList.Add('');
// Formats of axis labels
FNumberFormatList.Add(AChart.XAxis.LabelFormat);
@ -3508,7 +3530,7 @@ begin
for i := 0 to FNumberFormatList.Count-1 do begin
numFmtName := Format('N%d', [i]);
numFmtStr := FNumberFormatList[i];
numFmtStr := FNumberFormatList.ValueFromIndex[i];
parser := TsSpreadOpenDocNumFormatParser.Create(numFmtStr, FWriter.Workbook.FormatSettings);
try
numFmtXML := parser.BuildXMLAsString(numFmtName);
@ -3518,22 +3540,6 @@ begin
parser.Free;
end;
end;
{
AppendToStream(AStream,
indent + '<number:number-style style:name="N0">' + LE +
indent + ' <number:number number:min-integer-digits="1"/>' + LE +
indent + '</number:number-style>' + LE
);
if AChart.StackMode = csmStackedPercentage then
AppendToStream(AStream,
indent + '<number:percentage-style style:name="N10010">' + LE +
indent + ' <number:number number:decimal-places="0" number:min-decimal-places="0" number:min-integer-digits="1"/>' + LE +
indent + ' <number:text>%</number:text>' + LE +
indent + '</number:percentage-style>' + LE
);
}
end;
{ Writes the file "Object N/styles.xml" (N = 1, 2, ...) which is needed by the