@@ -253,3 +253,116 @@ func TestDeleteChart(t *testing.T) {
253
253
// Test delete chart on no chart worksheet.
254
254
assert .NoError (t , NewFile ().DeleteChart ("Sheet1" , "A1" ))
255
255
}
256
+
257
+ func TestChartWithLogarithmicBase (t * testing.T ) {
258
+ // Create test XLSX file with data
259
+ xlsx := NewFile ()
260
+ sheet1 := xlsx .GetSheetName (0 )
261
+ categories := map [string ]float64 {
262
+ "A1" : 1 ,
263
+ "A2" : 2 ,
264
+ "A3" : 3 ,
265
+ "A4" : 4 ,
266
+ "A5" : 5 ,
267
+ "A6" : 6 ,
268
+ "A7" : 7 ,
269
+ "A8" : 8 ,
270
+ "A9" : 9 ,
271
+ "A10" : 10 ,
272
+ "B1" : 0.1 ,
273
+ "B2" : 1 ,
274
+ "B3" : 2 ,
275
+ "B4" : 3 ,
276
+ "B5" : 20 ,
277
+ "B6" : 30 ,
278
+ "B7" : 100 ,
279
+ "B8" : 500 ,
280
+ "B9" : 700 ,
281
+ "B10" : 5000 ,
282
+ }
283
+ for cell , v := range categories {
284
+ assert .NoError (t , xlsx .SetCellValue (sheet1 , cell , v ))
285
+ }
286
+
287
+ // Add two chart, one without and one with log scaling
288
+ assert .NoError (t , xlsx .AddChart (sheet1 , "C1" ,
289
+ `{"type":"line","dimension":{"width":640, "height":480},` +
290
+ `"series":[{"name":"value","categories":"Sheet1!$A$1:$A$19","values":"Sheet1!$B$1:$B$10"}],` +
291
+ `"title":{"name":"Line chart without log scaling"}}` ))
292
+ assert .NoError (t , xlsx .AddChart (sheet1 , "M1" ,
293
+ `{"type":"line","dimension":{"width":640, "height":480},` +
294
+ `"series":[{"name":"value","categories":"Sheet1!$A$1:$A$19","values":"Sheet1!$B$1:$B$10"}],` +
295
+ `"y_axis":{"logbase":10.5},` +
296
+ `"title":{"name":"Line chart with log 10 scaling"}}` ))
297
+ assert .NoError (t , xlsx .AddChart (sheet1 , "A25" ,
298
+ `{"type":"line","dimension":{"width":320, "height":240},` +
299
+ `"series":[{"name":"value","categories":"Sheet1!$A$1:$A$19","values":"Sheet1!$B$1:$B$10"}],` +
300
+ `"y_axis":{"logbase":1.9},` +
301
+ `"title":{"name":"Line chart with log 1.9 scaling"}}` ))
302
+ assert .NoError (t , xlsx .AddChart (sheet1 , "F25" ,
303
+ `{"type":"line","dimension":{"width":320, "height":240},` +
304
+ `"series":[{"name":"value","categories":"Sheet1!$A$1:$A$19","values":"Sheet1!$B$1:$B$10"}],` +
305
+ `"y_axis":{"logbase":2},` +
306
+ `"title":{"name":"Line chart with log 2 scaling"}}` ))
307
+ assert .NoError (t , xlsx .AddChart (sheet1 , "K25" ,
308
+ `{"type":"line","dimension":{"width":320, "height":240},` +
309
+ `"series":[{"name":"value","categories":"Sheet1!$A$1:$A$19","values":"Sheet1!$B$1:$B$10"}],` +
310
+ `"y_axis":{"logbase":1000.1},` +
311
+ `"title":{"name":"Line chart with log 1000.1 scaling"}}` ))
312
+ assert .NoError (t , xlsx .AddChart (sheet1 , "P25" ,
313
+ `{"type":"line","dimension":{"width":320, "height":240},` +
314
+ `"series":[{"name":"value","categories":"Sheet1!$A$1:$A$19","values":"Sheet1!$B$1:$B$10"}],` +
315
+ `"y_axis":{"logbase":1000},` +
316
+ `"title":{"name":"Line chart with log 1000 scaling"}}` ))
317
+
318
+ // Export XLSX file for human confirmation
319
+ assert .NoError (t , xlsx .SaveAs (filepath .Join ("test" , "TestChartWithLogarithmicBase10.xlsx" )))
320
+
321
+ // Write the XLSX file to a buffer
322
+ var buffer bytes.Buffer
323
+ assert .NoError (t , xlsx .Write (& buffer ))
324
+
325
+ // Read back the XLSX file from the buffer
326
+ newFile , err := OpenReader (& buffer )
327
+ assert .NoError (t , err )
328
+
329
+ // Check the number of charts
330
+ expectedChartsCount := 6
331
+ chartsNum := newFile .countCharts ()
332
+ if ! assert .Equal (t , expectedChartsCount , chartsNum ,
333
+ "Expected %d charts, actual %d" , expectedChartsCount , chartsNum ) {
334
+ t .FailNow ()
335
+ }
336
+
337
+ chartSpaces := make ([]xlsxChartSpace , expectedChartsCount )
338
+ type xmlChartContent []byte
339
+ xmlCharts := make ([]xmlChartContent , expectedChartsCount )
340
+ expectedChartsLogBase := []float64 {0 , 10.5 , 0 , 2 , 0 , 1000 }
341
+ var ok bool
342
+
343
+ for i := 0 ; i < expectedChartsCount ; i ++ {
344
+ chartPath := fmt .Sprintf ("xl/charts/chart%d.xml" , i + 1 )
345
+ xmlCharts [i ], ok = newFile .XLSX [chartPath ]
346
+ assert .True (t , ok , "Can't open the %s" , chartPath )
347
+
348
+ err = xml .Unmarshal ([]byte (xmlCharts [i ]), & chartSpaces [i ])
349
+ if ! assert .NoError (t , err ) {
350
+ t .FailNow ()
351
+ }
352
+
353
+ chartLogBasePtr := chartSpaces [i ].Chart .PlotArea .ValAx [0 ].Scaling .LogBase
354
+ if expectedChartsLogBase [i ] == 0 {
355
+ if ! assert .Nil (t , chartLogBasePtr , "LogBase is not nil" ) {
356
+ t .FailNow ()
357
+ }
358
+ } else {
359
+ if ! assert .NotNil (t , chartLogBasePtr , "LogBase is nil" ) {
360
+ t .FailNow ()
361
+ }
362
+ if ! assert .Equal (t , expectedChartsLogBase [i ], * (chartLogBasePtr .Val ),
363
+ "Expected log base to %f, actual %f" , expectedChartsLogBase [i ], * (chartLogBasePtr .Val )) {
364
+ t .FailNow ()
365
+ }
366
+ }
367
+ }
368
+ }
0 commit comments