-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChartAdvancedDepthTests.cs
More file actions
249 lines (224 loc) · 9.7 KB
/
Copy pathChartAdvancedDepthTests.cs
File metadata and controls
249 lines (224 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System.IO;
using OdfKit.Chart;
using OdfKit.Compliance;
using OdfKit.DOM;
using OdfKit.Spreadsheet;
using Xunit;
namespace OdfKit.Tests;
/// <summary>
/// 鎖定圖表實務深度 API 的 round-trip 行為。
/// </summary>
[Trait(TestCategories.Kind, TestCategories.Scenario)]
public class ChartAdvancedDepthTests
{
/// <summary>
/// 驗證 bubble、stock 與 3D preset 可建立預期圖表類別。
/// </summary>
[Fact]
public void ChartPreset_BubbleStockAnd3D_CreateExpectedChartClass()
{
using ChartDocument bubble = ChartDocument.Builder()
.WithPreset(OdfChartPreset.Bubble)
.Build();
using ChartDocument stock = ChartDocument.Builder()
.WithPreset(OdfChartPreset.Stock)
.Build();
using ChartDocument column3d = ChartDocument.Builder()
.WithPreset(OdfChartPreset.Column3D)
.Build();
Assert.Equal("chart:bubble", bubble.ChartClass);
Assert.Equal("chart:stock", stock.ChartClass);
Assert.Equal("chart:bar", column3d.ChartClass);
Assert.True(column3d.PlotAreaStyle.ThreeDimensional);
Assert.Equal(OdfDr3dProjection.Perspective, column3d.PlotAreaStyle.Projection);
}
/// <summary>
/// 驗證泡泡圖可用 X、Y 與大小範圍建立並 round-trip。
/// </summary>
[Fact]
public void BubbleChart_FromRanges_RoundTripsSeriesDomains()
{
using ChartDocument chart = ChartDocument.CreateBubble(
"泡泡圖",
new OdfBubbleChartSeriesRequest(
"Data.$A$2:.$A$4",
"Data.$B$2:.$B$4",
"Data.$C$2:.$C$4",
"Data.$B$1",
"BubbleStyle"));
OdfBubbleChartSeriesInfo series = Assert.Single(chart.GetBubbleSeries());
Assert.Equal("Data.$A$2:.$A$4", series.XValuesCellRangeAddress);
Assert.Equal("Data.$B$2:.$B$4", series.YValuesCellRangeAddress);
Assert.Equal("Data.$C$2:.$C$4", series.BubbleSizeCellRangeAddress);
Assert.Equal("Data.$B$1", series.LabelCellAddress);
Assert.Equal("BubbleStyle", series.StyleName);
using ChartDocument loaded = RoundTrip(chart);
OdfBubbleChartSeriesInfo reloaded = Assert.Single(loaded.GetBubbleSeries());
Assert.Equal("chart:bubble", loaded.ChartClass);
Assert.Equal(series, reloaded);
}
/// <summary>
/// 驗證股票圖 OHLC、成交量與漲跌標記樣式可 round-trip。
/// </summary>
[Fact]
public void StockChart_OhlcRanges_RoundTripsMarkers()
{
using ChartDocument chart = ChartDocument.CreateStock(
"股票圖",
new OdfStockChartSeriesRequest(
"Stock.$B$2:.$B$6",
"Stock.$C$2:.$C$6",
"Stock.$D$2:.$D$6",
"Stock.$E$2:.$E$6",
"Stock.$F$2:.$F$6",
"Stock.$A$1",
"StockStyle"));
chart.ApplyStockMarkerStyle(new OdfStockMarkerStyle(
new OdfChartSurfaceStyle("GainStyle", FillColor: "#00AA66"),
new OdfChartSurfaceStyle("LossStyle", FillColor: "#CC3333"),
new OdfChartSurfaceStyle("RangeStyle", StrokeColor: "#333333")));
using ChartDocument loaded = RoundTrip(chart);
OdfStockChartSeriesInfo series = Assert.Single(loaded.GetStockSeries());
Assert.Equal("chart:stock", loaded.ChartClass);
Assert.Equal("Stock.$B$2:.$B$6", series.OpenCellRangeAddress);
Assert.Equal("Stock.$C$2:.$C$6", series.HighCellRangeAddress);
Assert.Equal("Stock.$D$2:.$D$6", series.LowCellRangeAddress);
Assert.Equal("Stock.$E$2:.$E$6", series.CloseCellRangeAddress);
Assert.Equal("Stock.$F$2:.$F$6", series.VolumeCellRangeAddress);
Assert.Equal("GainStyle", loaded.GetStockGainMarkerStyleName());
Assert.Equal("LossStyle", loaded.GetStockLossMarkerStyleName());
Assert.Equal("RangeStyle", loaded.GetStockRangeLineStyleName());
}
/// <summary>
/// 驗證 3D 投影、光源與 wall/floor 樣式可 round-trip。
/// </summary>
[Fact]
public void ThreeDChart_AppliesProjectionLightsWallAndFloor()
{
using ChartDocument chart = ChartDocument.FromTable(
"Data",
new OdfCellRange(0, 0, 4, 2, "Data"),
OdfChartPreset.Bar,
"3D 長條圖");
chart.Apply3DOptions(new OdfChart3DOptions
{
Projection = OdfDr3dProjection.Parallel,
AngleOffset = 35,
LightingMode = true,
WallStyle = new OdfChartSurfaceStyle("WallStyle", FillColor: "#EEEEEE"),
FloorStyle = new OdfChartSurfaceStyle("FloorStyle", FillColor: "#DDDDDD"),
Lights =
{
new OdfChartLightRequest("(0 0 1)", "#FFFFFF", Enabled: true, Specular: false),
},
});
using ChartDocument loaded = RoundTrip(chart);
OdfChartLightInfo light = Assert.Single(loaded.GetLights());
Assert.True(loaded.PlotAreaStyle.ThreeDimensional);
Assert.Equal(OdfDr3dProjection.Parallel, loaded.PlotAreaStyle.Projection);
Assert.Equal(35, loaded.PlotAreaStyle.AngleOffset);
Assert.True(loaded.PlotAreaStyle.LightingMode);
Assert.Equal("(0 0 1)", light.Direction);
Assert.Equal("#FFFFFF", light.DiffuseColor);
Assert.Equal("WallStyle", loaded.GetWallStyleName());
Assert.Equal("FloorStyle", loaded.GetFloorStyleName());
}
/// <summary>
/// 驗證折線圖標記樣式與座標軸數字格式可 round-trip。
/// </summary>
[Fact]
public void MarkerStyleAndAxisNumberFormat_RoundTrip()
{
using ChartDocument chart = ChartDocument.FromTable(
"Data",
new OdfCellRange(0, 0, 3, 1, "Data"),
OdfChartPreset.Line,
"Marker");
chart.GetSeriesEditor(0).ApplyMarkerStyle(new OdfChartMarkerStyle("circle", "0.25cm", "#FF0000", "#333333"));
chart.SetAxisNumberFormat("y", "N2");
using ChartDocument loaded = RoundTrip(chart);
OdfChartMarkerStyle marker = loaded.GetSeriesEditor(0).GetMarkerStyle()!;
Assert.Equal("circle", marker.Symbol);
Assert.Equal("0.25cm", marker.Size);
Assert.Equal("#FF0000", marker.FillColor);
Assert.Equal("N2", loaded.GetAxisNumberFormat("y"));
}
/// <summary>
/// 驗證圖表樣式摘要可讀回 marker 與 data style。
/// </summary>
[Fact]
public void ChartStyleInfo_ReadsMarkerAndDataStyle()
{
using ChartDocument chart = ChartDocument.FromTable(
"Data",
new OdfCellRange(0, 0, 3, 1, "Data"),
OdfChartPreset.Scatter,
"Styles");
chart.GetSeriesEditor(0).ApplyMarkerStyle(new OdfChartMarkerStyle("square", "0.3cm", "#00AA66", "#333333"));
chart.SetAxisNumberFormat("y", "Percent2");
OdfChartStyleInfo seriesStyle = Assert.Single(chart.GetChartStyles(), style => style.SymbolName == "square");
Assert.Equal("named-symbol", seriesStyle.SymbolType);
Assert.Equal("0.3cm", seriesStyle.SymbolSize);
Assert.Contains(chart.GetChartStyles(), style => style.DataStyleName == "Percent2");
}
/// <summary>
/// 驗證進階圖表設定在 ODS 嵌入圖表中可 round-trip。
/// </summary>
[Fact]
public void EmbeddedChart_AdvancedOptionsRoundTripWithStandaloneParity()
{
using SpreadsheetDocument document = SpreadsheetDocument.Create();
OdfTableSheet sheet = document.Worksheets.Add("Data");
sheet.SetValues(
new OdfCellAddress(0, 0, "Data"),
new object?[,]
{
{ "Name", "Value" },
{ "A", 10d },
{ "B", 20d },
});
var options = new OdfEmbeddedChartOptions
{
Preset = OdfChartPreset.Column3D,
Title = "Embedded 3D",
YAxisNumberFormat = "N2",
ThreeDOptions = new OdfChart3DOptions
{
Projection = OdfDr3dProjection.Parallel,
AngleOffset = 30,
WallStyle = new OdfChartSurfaceStyle("WallStyle", FillColor: "#EEEEEE"),
FloorStyle = new OdfChartSurfaceStyle("FloorStyle", FillColor: "#DDDDDD"),
}
};
options.MarkerStyles.Add(new OdfChartMarkerStyle("circle", "0.25cm", "#FF0000", "#333333"));
_ = document.InsertChartFromRange(
"Data",
new OdfCellAddress(0, 3, "Data"),
new OdfCellRange(0, 0, 2, 1, "Data"),
options);
using SpreadsheetDocument loaded = RoundTripSpreadsheet(document);
OdfChartDocument chart = loaded.GetEmbeddedChartDocument(Assert.Single(loaded.GetEmbeddedCharts()));
Assert.Equal("Embedded 3D", chart.ChartTitle);
Assert.True(chart.PlotAreaStyle.ThreeDimensional);
Assert.Equal(OdfDr3dProjection.Parallel, chart.PlotAreaStyle.Projection);
Assert.Equal(30, chart.PlotAreaStyle.AngleOffset);
Assert.Equal("WallStyle", chart.GetWallStyleName());
Assert.Equal("FloorStyle", chart.GetFloorStyleName());
Assert.Equal("N2", chart.GetAxisNumberFormat("y"));
Assert.Equal("circle", chart.GetSeriesEditor(0).GetMarkerStyle()?.Symbol);
}
private static ChartDocument RoundTrip(ChartDocument chart)
{
var stream = new MemoryStream();
chart.SaveToStream(stream);
stream.Position = 0;
return ChartDocument.Load(stream, "chart.odc");
}
private static SpreadsheetDocument RoundTripSpreadsheet(SpreadsheetDocument document)
{
var stream = new MemoryStream();
document.SaveToStream(stream);
stream.Position = 0;
return SpreadsheetDocument.Load(stream, "chart.ods");
}
}