chart.go 61.2 KB
Newer Older
xurime's avatar
xurime 已提交
1
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
xurime's avatar
xurime 已提交
2 3 4 5 6 7
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
// Package excelize providing a set of functions that allow you to write to
// and read from XLSX files. Support reads and writes XLSX file generated by
// Microsoft Excel™ 2007 and later. Support save file without losing original
8
// charts of XLSX. This library needs Go version 1.10 or later.
xurime's avatar
xurime 已提交
9

10 11 12
package excelize

import (
13
	"bytes"
14 15
	"encoding/json"
	"encoding/xml"
16
	"errors"
17 18
	"io"
	"log"
19 20 21 22 23 24
	"strconv"
	"strings"
)

// This section defines the currently supported chart types.
const (
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
	Area                        = "area"
	AreaStacked                 = "areaStacked"
	AreaPercentStacked          = "areaPercentStacked"
	Area3D                      = "area3D"
	Area3DStacked               = "area3DStacked"
	Area3DPercentStacked        = "area3DPercentStacked"
	Bar                         = "bar"
	BarStacked                  = "barStacked"
	BarPercentStacked           = "barPercentStacked"
	Bar3DClustered              = "bar3DClustered"
	Bar3DStacked                = "bar3DStacked"
	Bar3DPercentStacked         = "bar3DPercentStacked"
	Bar3DConeClustered          = "bar3DConeClustered"
	Bar3DConeStacked            = "bar3DConeStacked"
	Bar3DConePercentStacked     = "bar3DConePercentStacked"
	Bar3DPyramidClustered       = "bar3DPyramidClustered"
	Bar3DPyramidStacked         = "bar3DPyramidStacked"
	Bar3DPyramidPercentStacked  = "bar3DPyramidPercentStacked"
	Bar3DCylinderClustered      = "bar3DCylinderClustered"
	Bar3DCylinderStacked        = "bar3DCylinderStacked"
	Bar3DCylinderPercentStacked = "bar3DCylinderPercentStacked"
	Col                         = "col"
	ColStacked                  = "colStacked"
	ColPercentStacked           = "colPercentStacked"
	Col3D                       = "col3D"
	Col3DClustered              = "col3DClustered"
	Col3DStacked                = "col3DStacked"
	Col3DPercentStacked         = "col3DPercentStacked"
	Col3DCone                   = "col3DCone"
	Col3DConeClustered          = "col3DConeClustered"
	Col3DConeStacked            = "col3DConeStacked"
	Col3DConePercentStacked     = "col3DConePercentStacked"
	Col3DPyramid                = "col3DPyramid"
	Col3DPyramidClustered       = "col3DPyramidClustered"
	Col3DPyramidStacked         = "col3DPyramidStacked"
	Col3DPyramidPercentStacked  = "col3DPyramidPercentStacked"
	Col3DCylinder               = "col3DCylinder"
	Col3DCylinderClustered      = "col3DCylinderClustered"
	Col3DCylinderStacked        = "col3DCylinderStacked"
	Col3DCylinderPercentStacked = "col3DCylinderPercentStacked"
	Doughnut                    = "doughnut"
	Line                        = "line"
	Pie                         = "pie"
	Pie3D                       = "pie3D"
	Radar                       = "radar"
	Scatter                     = "scatter"
71 72 73 74
	Surface3D                   = "surface3D"
	WireframeSurface3D          = "wireframeSurface3D"
	Contour                     = "contour"
	WireframeContour            = "wireframeContour"
75 76
	Bubble                      = "bubble"
	Bubble3D                    = "bubble3D"
77 78 79 80
)

// This section defines the default value of chart properties.
var (
xurime's avatar
xurime 已提交
81
	chartView3DRotX = map[string]int{
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
		Area:                        0,
		AreaStacked:                 0,
		AreaPercentStacked:          0,
		Area3D:                      15,
		Area3DStacked:               15,
		Area3DPercentStacked:        15,
		Bar:                         0,
		BarStacked:                  0,
		BarPercentStacked:           0,
		Bar3DClustered:              15,
		Bar3DStacked:                15,
		Bar3DPercentStacked:         15,
		Bar3DConeClustered:          15,
		Bar3DConeStacked:            15,
		Bar3DConePercentStacked:     15,
		Bar3DPyramidClustered:       15,
		Bar3DPyramidStacked:         15,
		Bar3DPyramidPercentStacked:  15,
		Bar3DCylinderClustered:      15,
		Bar3DCylinderStacked:        15,
		Bar3DCylinderPercentStacked: 15,
		Col:                         0,
		ColStacked:                  0,
		ColPercentStacked:           0,
		Col3D:                       15,
		Col3DClustered:              15,
		Col3DStacked:                15,
		Col3DPercentStacked:         15,
		Col3DCone:                   15,
		Col3DConeClustered:          15,
		Col3DConeStacked:            15,
		Col3DConePercentStacked:     15,
		Col3DPyramid:                15,
		Col3DPyramidClustered:       15,
		Col3DPyramidStacked:         15,
		Col3DPyramidPercentStacked:  15,
		Col3DCylinder:               15,
		Col3DCylinderClustered:      15,
		Col3DCylinderStacked:        15,
		Col3DCylinderPercentStacked: 15,
		Doughnut:                    0,
		Line:                        0,
		Pie:                         0,
		Pie3D:                       30,
		Radar:                       0,
		Scatter:                     0,
128 129 130 131
		Surface3D:                   15,
		WireframeSurface3D:          15,
		Contour:                     90,
		WireframeContour:            90,
xurime's avatar
xurime 已提交
132 133
	}
	chartView3DRotY = map[string]int{
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
		Area:                        0,
		AreaStacked:                 0,
		AreaPercentStacked:          0,
		Area3D:                      20,
		Area3DStacked:               20,
		Area3DPercentStacked:        20,
		Bar:                         0,
		BarStacked:                  0,
		BarPercentStacked:           0,
		Bar3DClustered:              20,
		Bar3DStacked:                20,
		Bar3DPercentStacked:         20,
		Bar3DConeClustered:          20,
		Bar3DConeStacked:            20,
		Bar3DConePercentStacked:     20,
		Bar3DPyramidClustered:       20,
		Bar3DPyramidStacked:         20,
		Bar3DPyramidPercentStacked:  20,
		Bar3DCylinderClustered:      20,
		Bar3DCylinderStacked:        20,
		Bar3DCylinderPercentStacked: 20,
		Col:                         0,
		ColStacked:                  0,
		ColPercentStacked:           0,
		Col3D:                       20,
		Col3DClustered:              20,
		Col3DStacked:                20,
		Col3DPercentStacked:         20,
		Col3DCone:                   20,
		Col3DConeClustered:          20,
		Col3DConeStacked:            20,
		Col3DConePercentStacked:     20,
		Col3DPyramid:                20,
		Col3DPyramidClustered:       20,
		Col3DPyramidStacked:         20,
		Col3DPyramidPercentStacked:  20,
		Col3DCylinder:               20,
		Col3DCylinderClustered:      20,
		Col3DCylinderStacked:        20,
		Col3DCylinderPercentStacked: 20,
		Doughnut:                    0,
		Line:                        0,
		Pie:                         0,
		Pie3D:                       0,
		Radar:                       0,
		Scatter:                     0,
180 181 182 183
		Surface3D:                   20,
		WireframeSurface3D:          20,
		Contour:                     0,
		WireframeContour:            0,
xurime's avatar
xurime 已提交
184
	}
xurime's avatar
xurime 已提交
185 186 187 188 189
	plotAreaChartOverlap = map[string]int{
		BarStacked:        100,
		BarPercentStacked: 100,
		ColStacked:        100,
		ColPercentStacked: 100,
190 191 192 193
	}
	chartView3DPerspective = map[string]int{
		Contour:          0,
		WireframeContour: 0,
xurime's avatar
xurime 已提交
194 195
	}
	chartView3DRAngAx = map[string]int{
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
		Area:                        0,
		AreaStacked:                 0,
		AreaPercentStacked:          0,
		Area3D:                      1,
		Area3DStacked:               1,
		Area3DPercentStacked:        1,
		Bar:                         0,
		BarStacked:                  0,
		BarPercentStacked:           0,
		Bar3DClustered:              1,
		Bar3DStacked:                1,
		Bar3DPercentStacked:         1,
		Bar3DConeClustered:          1,
		Bar3DConeStacked:            1,
		Bar3DConePercentStacked:     1,
		Bar3DPyramidClustered:       1,
		Bar3DPyramidStacked:         1,
		Bar3DPyramidPercentStacked:  1,
		Bar3DCylinderClustered:      1,
		Bar3DCylinderStacked:        1,
		Bar3DCylinderPercentStacked: 1,
		Col:                         0,
		ColStacked:                  0,
		ColPercentStacked:           0,
		Col3D:                       1,
		Col3DClustered:              1,
		Col3DStacked:                1,
		Col3DPercentStacked:         1,
		Col3DCone:                   1,
		Col3DConeClustered:          1,
		Col3DConeStacked:            1,
		Col3DConePercentStacked:     1,
		Col3DPyramid:                1,
		Col3DPyramidClustered:       1,
		Col3DPyramidStacked:         1,
		Col3DPyramidPercentStacked:  1,
		Col3DCylinder:               1,
		Col3DCylinderClustered:      1,
		Col3DCylinderStacked:        1,
		Col3DCylinderPercentStacked: 1,
		Doughnut:                    0,
		Line:                        0,
		Pie:                         0,
		Pie3D:                       0,
		Radar:                       0,
		Scatter:                     0,
242 243 244
		Surface3D:                   0,
		WireframeSurface3D:          0,
		Contour:                     0,
245 246
		Bubble:                      0,
		Bubble3D:                    0,
247
	}
xurime's avatar
xurime 已提交
248 249 250 251 252 253 254
	chartLegendPosition = map[string]string{
		"bottom":    "b",
		"left":      "l",
		"right":     "r",
		"top":       "t",
		"top_right": "tr",
	}
255
	chartValAxNumFmtFormatCode = map[string]string{
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
		Area:                        "General",
		AreaStacked:                 "General",
		AreaPercentStacked:          "0%",
		Area3D:                      "General",
		Area3DStacked:               "General",
		Area3DPercentStacked:        "0%",
		Bar:                         "General",
		BarStacked:                  "General",
		BarPercentStacked:           "0%",
		Bar3DClustered:              "General",
		Bar3DStacked:                "General",
		Bar3DPercentStacked:         "0%",
		Bar3DConeClustered:          "General",
		Bar3DConeStacked:            "General",
		Bar3DConePercentStacked:     "0%",
		Bar3DPyramidClustered:       "General",
		Bar3DPyramidStacked:         "General",
		Bar3DPyramidPercentStacked:  "0%",
		Bar3DCylinderClustered:      "General",
		Bar3DCylinderStacked:        "General",
		Bar3DCylinderPercentStacked: "0%",
		Col:                         "General",
		ColStacked:                  "General",
		ColPercentStacked:           "0%",
		Col3D:                       "General",
		Col3DClustered:              "General",
		Col3DStacked:                "General",
		Col3DPercentStacked:         "0%",
		Col3DCone:                   "General",
		Col3DConeClustered:          "General",
		Col3DConeStacked:            "General",
		Col3DConePercentStacked:     "0%",
		Col3DPyramid:                "General",
		Col3DPyramidClustered:       "General",
		Col3DPyramidStacked:         "General",
		Col3DPyramidPercentStacked:  "0%",
		Col3DCylinder:               "General",
		Col3DCylinderClustered:      "General",
		Col3DCylinderStacked:        "General",
		Col3DCylinderPercentStacked: "0%",
		Doughnut:                    "General",
		Line:                        "General",
		Pie:                         "General",
		Pie3D:                       "General",
		Radar:                       "General",
		Scatter:                     "General",
302 303 304 305
		Surface3D:                   "General",
		WireframeSurface3D:          "General",
		Contour:                     "General",
		WireframeContour:            "General",
306 307
		Bubble:                      "General",
		Bubble3D:                    "General",
308 309
	}
	chartValAxCrossBetween = map[string]string{
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
		Area:                        "midCat",
		AreaStacked:                 "midCat",
		AreaPercentStacked:          "midCat",
		Area3D:                      "midCat",
		Area3DStacked:               "midCat",
		Area3DPercentStacked:        "midCat",
		Bar:                         "between",
		BarStacked:                  "between",
		BarPercentStacked:           "between",
		Bar3DClustered:              "between",
		Bar3DStacked:                "between",
		Bar3DPercentStacked:         "between",
		Bar3DConeClustered:          "between",
		Bar3DConeStacked:            "between",
		Bar3DConePercentStacked:     "between",
		Bar3DPyramidClustered:       "between",
		Bar3DPyramidStacked:         "between",
		Bar3DPyramidPercentStacked:  "between",
		Bar3DCylinderClustered:      "between",
		Bar3DCylinderStacked:        "between",
		Bar3DCylinderPercentStacked: "between",
		Col:                         "between",
		ColStacked:                  "between",
		ColPercentStacked:           "between",
		Col3D:                       "between",
		Col3DClustered:              "between",
		Col3DStacked:                "between",
		Col3DPercentStacked:         "between",
		Col3DCone:                   "between",
		Col3DConeClustered:          "between",
		Col3DConeStacked:            "between",
		Col3DConePercentStacked:     "between",
		Col3DPyramid:                "between",
		Col3DPyramidClustered:       "between",
		Col3DPyramidStacked:         "between",
		Col3DPyramidPercentStacked:  "between",
		Col3DCylinder:               "between",
		Col3DCylinderClustered:      "between",
		Col3DCylinderStacked:        "between",
		Col3DCylinderPercentStacked: "between",
		Doughnut:                    "between",
		Line:                        "between",
		Pie:                         "between",
		Pie3D:                       "between",
		Radar:                       "between",
		Scatter:                     "between",
356 357 358 359
		Surface3D:                   "midCat",
		WireframeSurface3D:          "midCat",
		Contour:                     "midCat",
		WireframeContour:            "midCat",
360 361
		Bubble:                      "midCat",
		Bubble3D:                    "midCat",
362 363
	}
	plotAreaChartGrouping = map[string]string{
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
		Area:                        "standard",
		AreaStacked:                 "stacked",
		AreaPercentStacked:          "percentStacked",
		Area3D:                      "standard",
		Area3DStacked:               "stacked",
		Area3DPercentStacked:        "percentStacked",
		Bar:                         "clustered",
		BarStacked:                  "stacked",
		BarPercentStacked:           "percentStacked",
		Bar3DClustered:              "clustered",
		Bar3DStacked:                "stacked",
		Bar3DPercentStacked:         "percentStacked",
		Bar3DConeClustered:          "clustered",
		Bar3DConeStacked:            "stacked",
		Bar3DConePercentStacked:     "percentStacked",
		Bar3DPyramidClustered:       "clustered",
		Bar3DPyramidStacked:         "stacked",
		Bar3DPyramidPercentStacked:  "percentStacked",
		Bar3DCylinderClustered:      "clustered",
		Bar3DCylinderStacked:        "stacked",
		Bar3DCylinderPercentStacked: "percentStacked",
		Col:                         "clustered",
		ColStacked:                  "stacked",
		ColPercentStacked:           "percentStacked",
		Col3D:                       "standard",
		Col3DClustered:              "clustered",
		Col3DStacked:                "stacked",
		Col3DPercentStacked:         "percentStacked",
		Col3DCone:                   "standard",
		Col3DConeClustered:          "clustered",
		Col3DConeStacked:            "stacked",
		Col3DConePercentStacked:     "percentStacked",
		Col3DPyramid:                "standard",
		Col3DPyramidClustered:       "clustered",
		Col3DPyramidStacked:         "stacked",
		Col3DPyramidPercentStacked:  "percentStacked",
		Col3DCylinder:               "standard",
		Col3DCylinderClustered:      "clustered",
		Col3DCylinderStacked:        "stacked",
		Col3DCylinderPercentStacked: "percentStacked",
		Line:                        "standard",
405 406
	}
	plotAreaChartBarDir = map[string]string{
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		Bar:                         "bar",
		BarStacked:                  "bar",
		BarPercentStacked:           "bar",
		Bar3DClustered:              "bar",
		Bar3DStacked:                "bar",
		Bar3DPercentStacked:         "bar",
		Bar3DConeClustered:          "bar",
		Bar3DConeStacked:            "bar",
		Bar3DConePercentStacked:     "bar",
		Bar3DPyramidClustered:       "bar",
		Bar3DPyramidStacked:         "bar",
		Bar3DPyramidPercentStacked:  "bar",
		Bar3DCylinderClustered:      "bar",
		Bar3DCylinderStacked:        "bar",
		Bar3DCylinderPercentStacked: "bar",
		Col:                         "col",
		ColStacked:                  "col",
		ColPercentStacked:           "col",
		Col3D:                       "col",
		Col3DClustered:              "col",
		Col3DStacked:                "col",
		Col3DPercentStacked:         "col",
		Col3DCone:                   "col",
		Col3DConeStacked:            "col",
		Col3DConeClustered:          "col",
		Col3DConePercentStacked:     "col",
		Col3DPyramid:                "col",
		Col3DPyramidClustered:       "col",
		Col3DPyramidStacked:         "col",
		Col3DPyramidPercentStacked:  "col",
		Col3DCylinder:               "col",
		Col3DCylinderClustered:      "col",
		Col3DCylinderStacked:        "col",
		Col3DCylinderPercentStacked: "col",
		Line:                        "standard",
442
	}
443 444 445 446 447 448 449 450 451 452 453 454
	orientation = map[bool]string{
		true:  "maxMin",
		false: "minMax",
	}
	catAxPos = map[bool]string{
		true:  "t",
		false: "b",
	}
	valAxPos = map[bool]string{
		true:  "r",
		false: "l",
	}
455 456 457 458
	valTickLblPos = map[string]string{
		Contour:          "none",
		WireframeContour: "none",
	}
459 460
)

xurime's avatar
xurime 已提交
461
// parseFormatChartSet provides a function to parse the format settings of the
462
// chart with default value.
463
func parseFormatChartSet(formatSet string) (*formatChart, error) {
464
	format := formatChart{
465 466 467 468
		Dimension: formatChartDimension{
			Width:  480,
			Height: 290,
		},
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
		Format: formatPicture{
			FPrintsWithSheet: true,
			FLocksWithSheet:  false,
			NoChangeAspect:   false,
			OffsetX:          0,
			OffsetY:          0,
			XScale:           1.0,
			YScale:           1.0,
		},
		Legend: formatChartLegend{
			Position:      "bottom",
			ShowLegendKey: false,
		},
		Title: formatChartTitle{
			Name: " ",
		},
		ShowBlanksAs: "gap",
	}
487 488
	err := json.Unmarshal([]byte(formatSet), &format)
	return &format, err
489 490 491 492
}

// AddChart provides the method to add chart in a sheet by given chart format
// set (such as offset, scale, aspect ratio setting and print settings) and
493
// properties set. For example, create 3D clustered column chart with data
494
// Sheet1!$A$29:$D$32:
495 496 497 498 499 500
//
//    package main
//
//    import (
//        "fmt"
//
xurime's avatar
xurime 已提交
501
//        "github.com/360EntSecGroup-Skylar/excelize"
502 503 504
//    )
//
//    func main() {
xurime's avatar
xurime 已提交
505 506
//        categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
//        values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
507
//        f := excelize.NewFile()
xurime's avatar
xurime 已提交
508
//        for k, v := range categories {
509
//            f.SetCellValue("Sheet1", k, v)
xurime's avatar
xurime 已提交
510 511
//        }
//        for k, v := range values {
512 513 514 515 516 517
//            f.SetCellValue("Sheet1", k, v)
//        }
//        err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","dimension":{"width":640,"height":480},"series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D Clustered Column Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero","x_axis":{"reverse_order":true},"y_axis":{"maximum":7.5,"minimum":0.5}}`)
//        if err != nil {
//            fmt.Println(err)
//            return
xurime's avatar
xurime 已提交
518
//        }
519
//        // Save xlsx file by the given path.
520
//        err = xlsx.SaveAs("./Book1.xlsx")
521 522 523 524 525 526 527
//        if err != nil {
//            fmt.Println(err)
//        }
//    }
//
// The following shows the type of chart supported by excelize:
//
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
//     Type                        | Chart
//    -----------------------------+------------------------------
//     area                        | 2D area chart
//     areaStacked                 | 2D stacked area chart
//     areaPercentStacked          | 2D 100% stacked area chart
//     area3D                      | 3D area chart
//     area3DStacked               | 3D stacked area chart
//     area3DPercentStacked        | 3D 100% stacked area chart
//     bar                         | 2D clustered bar chart
//     barStacked                  | 2D stacked bar chart
//     barPercentStacked           | 2D 100% stacked bar chart
//     bar3DClustered              | 3D clustered bar chart
//     bar3DStacked                | 3D stacked bar chart
//     bar3DPercentStacked         | 3D 100% stacked bar chart
//     bar3DConeClustered          | 3D cone clustered bar chart
//     bar3DConeStacked            | 3D cone stacked bar chart
//     bar3DConePercentStacked     | 3D cone percent bar chart
//     bar3DPyramidClustered       | 3D pyramid clustered bar chart
//     bar3DPyramidStacked         | 3D pyramid stacked bar chart
//     bar3DPyramidPercentStacked  | 3D pyramid percent stacked bar chart
//     bar3DCylinderClustered      | 3D cylinder clustered bar chart
//     bar3DCylinderStacked        | 3D cylinder stacked bar chart
//     bar3DCylinderPercentStacked | 3D cylinder percent stacked bar chart
//     col                         | 2D clustered column chart
//     colStacked                  | 2D stacked column chart
//     colPercentStacked           | 2D 100% stacked column chart
//     col3DClustered              | 3D clustered column chart
//     col3D                       | 3D column chart
//     col3DStacked                | 3D stacked column chart
//     col3DPercentStacked         | 3D 100% stacked column chart
//     col3DCone                   | 3D cone column chart
//     col3DConeClustered          | 3D cone clustered column chart
//     col3DConeStacked            | 3D cone stacked column chart
//     col3DConePercentStacked     | 3D cone percent stacked column chart
//     col3DPyramid                | 3D pyramid column chart
//     col3DPyramidClustered       | 3D pyramid clustered column chart
//     col3DPyramidStacked         | 3D pyramid stacked column chart
//     col3DPyramidPercentStacked  | 3D pyramid percent stacked column chart
//     col3DCylinder               | 3D cylinder column chart
//     col3DCylinderClustered      | 3D cylinder clustered column chart
//     col3DCylinderStacked        | 3D cylinder stacked column chart
//     col3DCylinderPercentStacked | 3D cylinder percent stacked column chart
//     doughnut                    | doughnut chart
//     line                        | line chart
//     pie                         | pie chart
//     pie3D                       | 3D pie chart
//     radar                       | radar chart
//     scatter                     | scatter chart
576 577 578
//     surface3D                   | 3D surface chart
//     wireframeSurface3D          | 3D wireframe surface chart
//     contour                     | contour chart
579 580 581
//     wireframeContour            | wireframe contour chart
//     bubble                      | bubble chart
//     bubble3D                    | 3D bubble chart
582 583 584 585 586 587 588 589
//
// In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
//
// The series options that can be set are:
//
//    name
//    categories
//    values
590
//    line
591
//
592
// name: Set the name for the series. The name is displayed in the chart legend and in the formula bar. The name property is optional and if it isn't supplied it will default to Series 1..n. The name can also be a formula such as Sheet1!$A$1
593 594 595 596 597
//
// categories: This sets the chart category labels. The category is more or less the same as the X axis. In most chart types the categories property is optional and the chart will just assume a sequential series from 1..n.
//
// values: This is the most important property of a series and is the only mandatory option for every chart object. This option links the chart with the worksheet data that it displays.
//
598 599
// line: This sets the line format of the line chart. The line property is optional and if it isn't supplied it will default style. The options that can be set is width. The range of width is 0.25pt - 999pt. If the value of width is outside the range, the default width of the line is 2pt.
//
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
// Set properties of the chart legend. The options that can be set are:
//
//    position
//    show_legend_key
//
// position: Set the position of the chart legend. The default legend position is right. The available positions are:
//
//    top
//    bottom
//    left
//    right
//    top_right
//
// show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
//
// Set properties of the chart title. The properties that can be set are:
//
//    title
//
619
// name: Set the name (title) for the chart. The name is displayed above the chart. The name can also be a formula such as Sheet1!$A$1 or a list with a sheetname. The name property is optional. The default is to have no chart title.
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
//
// Specifies how blank cells are plotted on the chart by show_blanks_as. The default value is gap. The options that can be set are:
//
//    gap
//    span
//    zero
//
// gap: Specifies that blank values shall be left as a gap.
//
// sapn: Specifies that blank values shall be spanned with a line.
//
// zero: Specifies that blank values shall be treated as zero.
//
// Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
//
// Set the position of the chart plot area by plotarea. The properties that can be set are:
//
//    show_bubble_size
//    show_cat_name
//    show_leader_lines
//    show_percent
//    show_series_name
//    show_val
//
// show_bubble_size: Specifies the bubble size shall be shown in a data label. The show_bubble_size property is optional. The default value is false.
645
//
646
// show_cat_name: Specifies that the category name shall be shown in the data label. The show_cat_name property is optional. The default value is true.
647
//
648
// show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
649
//
650
// show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
651
//
652
// show_series_name: Specifies that the series name shall be shown in a data label. The show_series_name property is optional. The default value is false.
653
//
654 655
// show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
//
656
// Set the primary horizontal and vertical axis options by x_axis and y_axis. The properties of x_axis that can be set are:
657
//
658 659
//    major_grid_lines
//    minor_grid_lines
xurime's avatar
xurime 已提交
660
//    tick_label_skip
661 662 663 664 665 666 667 668 669
//    reverse_order
//    maximum
//    minimum
//
// The properties of y_axis that can be set are:
//
//    major_grid_lines
//    minor_grid_lines
//    major_unit
670 671 672 673
//    reverse_order
//    maximum
//    minimum
//
674 675 676 677
// major_grid_lines: Specifies major gridlines.
//
// minor_grid_lines: Specifies minor gridlines.
//
678 679 680 681
// major_unit: Specifies the distance between major ticks. Shall contain a positive floating-point number. The major_unit property is optional. The default value is auto.
//
// tick_label_skip: Specifies how many tick labels to skip between label that is drawn. The tick_label_skip property is optional. The default value is auto.
//
682
// reverse_order: Specifies that the categories or values on reverse order (orientation of the chart). The reverse_order property is optional. The default value is false.
xurime's avatar
xurime 已提交
683
//
684
// maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
xurime's avatar
xurime 已提交
685
//
686 687
// minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
//
688 689
// Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
//
690 691 692 693 694
func (f *File) AddChart(sheet, cell, format string) error {
	formatSet, err := parseFormatChartSet(format)
	if err != nil {
		return err
	}
695
	// Read sheet data.
xurime's avatar
xurime 已提交
696 697 698 699
	xlsx, err := f.workSheetReader(sheet)
	if err != nil {
		return err
	}
700 701 702
	if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
		return errors.New("unsupported chart type " + formatSet.Type)
	}
703 704 705 706
	// Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
	drawingID := f.countDrawings() + 1
	chartID := f.countCharts() + 1
	drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
xurime's avatar
xurime 已提交
707
	drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
xurime's avatar
xurime 已提交
708 709
	drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
	drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
710 711 712 713
	err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
	if err != nil {
		return err
	}
714
	f.addChart(formatSet)
xurime's avatar
xurime 已提交
715 716
	f.addContentTypePart(chartID, "chart")
	f.addContentTypePart(drawingID, "drawings")
717
	return err
718 719
}

xurime's avatar
xurime 已提交
720
// countCharts provides a function to get chart files count storage in the
721 722 723 724 725 726 727 728 729 730 731
// folder xl/charts.
func (f *File) countCharts() int {
	count := 0
	for k := range f.XLSX {
		if strings.Contains(k, "xl/charts/chart") {
			count++
		}
	}
	return count
}

xurime's avatar
xurime 已提交
732
// prepareDrawing provides a function to prepare drawing ID and XML by given
733
// drawingID, worksheet name and default drawingXML.
xurime's avatar
xurime 已提交
734 735 736 737 738 739 740 741 742
func (f *File) prepareDrawing(xlsx *xlsxWorksheet, drawingID int, sheet, drawingXML string) (int, string) {
	sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
	if xlsx.Drawing != nil {
		// The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
		sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
		drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
		drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
	} else {
		// Add first picture for given sheet.
743
		sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels"
xurime's avatar
xurime 已提交
744
		rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
xurime's avatar
xurime 已提交
745
		f.addSheetDrawing(sheet, rID)
746
	}
xurime's avatar
xurime 已提交
747
	return drawingID, drawingXML
748 749
}

xurime's avatar
xurime 已提交
750 751
// addChart provides a function to create chart as xl/charts/chart%d.xml by
// given format sets.
752 753 754
func (f *File) addChart(formatSet *formatChart) {
	count := f.countCharts()
	xlsxChartSpace := xlsxChartSpace{
755 756 757 758
		XMLNSc:         NameSpaceDrawingMLChart,
		XMLNSa:         NameSpaceDrawingML,
		XMLNSr:         SourceRelationship,
		XMLNSc16r2:     SourceRelationshipChart201506,
759 760 761
		Date1904:       &attrValBool{Val: boolPtr(false)},
		Lang:           &attrValString{Val: stringPtr("en-US")},
		RoundedCorners: &attrValBool{Val: boolPtr(false)},
762 763 764 765 766 767
		Chart: cChart{
			Title: &cTitle{
				Tx: cTx{
					Rich: &cRich{
						P: aP{
							PPr: &aPPr{
768
								DefRPr: aRPr{
769 770 771 772 773 774 775 776
									Kern:   1200,
									Strike: "noStrike",
									U:      "none",
									Sz:     1400,
									SolidFill: &aSolidFill{
										SchemeClr: &aSchemeClr{
											Val: "tx1",
											LumMod: &attrValInt{
777
												Val: intPtr(65000),
778 779
											},
											LumOff: &attrValInt{
780
												Val: intPtr(35000),
781 782 783
											},
										},
									},
784 785 786 787 788 789 790 791
									Ea: &aEa{
										Typeface: "+mn-ea",
									},
									Cs: &aCs{
										Typeface: "+mn-cs",
									},
									Latin: &aLatin{
										Typeface: "+mn-lt",
792 793 794
									},
								},
							},
795 796 797 798
							R: &aR{
								RPr: aRPr{
									Lang:    "en-US",
									AltLang: "en-US",
799
								},
800
								T: formatSet.Title.Name,
801 802 803 804
							},
						},
					},
				},
805 806 807
				TxPr: cTxPr{
					P: aP{
						PPr: &aPPr{
808
							DefRPr: aRPr{
809 810 811 812 813 814 815 816 817 818
								Kern:   1200,
								U:      "none",
								Sz:     14000,
								Strike: "noStrike",
							},
						},
						EndParaRPr: &aEndParaRPr{
							Lang: "en-US",
						},
					},
819
				},
820
				Overlay: &attrValBool{Val: boolPtr(false)},
821 822
			},
			View3D: &cView3D{
823 824 825 826
				RotX:        &attrValInt{Val: intPtr(chartView3DRotX[formatSet.Type])},
				RotY:        &attrValInt{Val: intPtr(chartView3DRotY[formatSet.Type])},
				Perspective: &attrValInt{Val: intPtr(chartView3DPerspective[formatSet.Type])},
				RAngAx:      &attrValInt{Val: intPtr(chartView3DRAngAx[formatSet.Type])},
827 828
			},
			Floor: &cThicknessSpPr{
829
				Thickness: &attrValInt{Val: intPtr(0)},
830 831
			},
			SideWall: &cThicknessSpPr{
832
				Thickness: &attrValInt{Val: intPtr(0)},
833 834
			},
			BackWall: &cThicknessSpPr{
835
				Thickness: &attrValInt{Val: intPtr(0)},
836 837 838
			},
			PlotArea: &cPlotArea{},
			Legend: &cLegend{
839 840
				LegendPos: &attrValString{Val: stringPtr(chartLegendPosition[formatSet.Legend.Position])},
				Overlay:   &attrValBool{Val: boolPtr(false)},
841
			},
842

843 844 845
			PlotVisOnly:      &attrValBool{Val: boolPtr(false)},
			DispBlanksAs:     &attrValString{Val: stringPtr(formatSet.ShowBlanksAs)},
			ShowDLblsOverMax: &attrValBool{Val: boolPtr(false)},
846 847 848 849
		},
		SpPr: &cSpPr{
			SolidFill: &aSolidFill{
				SchemeClr: &aSchemeClr{Val: "bg1"},
850
			},
851 852 853 854 855
			Ln: &aLn{
				W:    9525,
				Cap:  "flat",
				Cmpd: "sng",
				Algn: "ctr",
856
				SolidFill: &aSolidFill{
857 858
					SchemeClr: &aSchemeClr{Val: "tx1",
						LumMod: &attrValInt{
859
							Val: intPtr(15000),
860 861
						},
						LumOff: &attrValInt{
862
							Val: intPtr(85000),
863 864 865 866
						},
					},
				},
			},
867 868 869 870 871 872 873 874 875
		},
		PrintSettings: &cPrintSettings{
			PageMargins: &cPageMargins{
				B:      0.75,
				L:      0.7,
				R:      0.7,
				T:      0.7,
				Header: 0.3,
				Footer: 0.3,
876 877 878 879
			},
		},
	}
	plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
		Area:                        f.drawBaseChart,
		AreaStacked:                 f.drawBaseChart,
		AreaPercentStacked:          f.drawBaseChart,
		Area3D:                      f.drawBaseChart,
		Area3DStacked:               f.drawBaseChart,
		Area3DPercentStacked:        f.drawBaseChart,
		Bar:                         f.drawBaseChart,
		BarStacked:                  f.drawBaseChart,
		BarPercentStacked:           f.drawBaseChart,
		Bar3DClustered:              f.drawBaseChart,
		Bar3DStacked:                f.drawBaseChart,
		Bar3DPercentStacked:         f.drawBaseChart,
		Bar3DConeClustered:          f.drawBaseChart,
		Bar3DConeStacked:            f.drawBaseChart,
		Bar3DConePercentStacked:     f.drawBaseChart,
		Bar3DPyramidClustered:       f.drawBaseChart,
		Bar3DPyramidStacked:         f.drawBaseChart,
		Bar3DPyramidPercentStacked:  f.drawBaseChart,
		Bar3DCylinderClustered:      f.drawBaseChart,
		Bar3DCylinderStacked:        f.drawBaseChart,
		Bar3DCylinderPercentStacked: f.drawBaseChart,
		Col:                         f.drawBaseChart,
		ColStacked:                  f.drawBaseChart,
		ColPercentStacked:           f.drawBaseChart,
		Col3D:                       f.drawBaseChart,
		Col3DClustered:              f.drawBaseChart,
		Col3DStacked:                f.drawBaseChart,
		Col3DPercentStacked:         f.drawBaseChart,
		Col3DCone:                   f.drawBaseChart,
		Col3DConeClustered:          f.drawBaseChart,
		Col3DConeStacked:            f.drawBaseChart,
		Col3DConePercentStacked:     f.drawBaseChart,
		Col3DPyramid:                f.drawBaseChart,
		Col3DPyramidClustered:       f.drawBaseChart,
		Col3DPyramidStacked:         f.drawBaseChart,
		Col3DPyramidPercentStacked:  f.drawBaseChart,
		Col3DCylinder:               f.drawBaseChart,
		Col3DCylinderClustered:      f.drawBaseChart,
		Col3DCylinderStacked:        f.drawBaseChart,
		Col3DCylinderPercentStacked: f.drawBaseChart,
		Doughnut:                    f.drawDoughnutChart,
		Line:                        f.drawLineChart,
		Pie3D:                       f.drawPie3DChart,
		Pie:                         f.drawPieChart,
		Radar:                       f.drawRadarChart,
		Scatter:                     f.drawScatterChart,
926 927 928 929
		Surface3D:                   f.drawSurface3DChart,
		WireframeSurface3D:          f.drawSurface3DChart,
		Contour:                     f.drawSurfaceChart,
		WireframeContour:            f.drawSurfaceChart,
930 931
		Bubble:                      f.drawBaseChart,
		Bubble3D:                    f.drawBaseChart,
932
	}
933
	xlsxChartSpace.Chart.PlotArea = plotAreaFunc[formatSet.Type](formatSet)
934 935 936

	chart, _ := xml.Marshal(xlsxChartSpace)
	media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
937
	f.saveFileList(media, chart)
938 939
}

xurime's avatar
xurime 已提交
940
// drawBaseChart provides a function to draw the c:plotArea element for bar,
941 942
// and column series charts by given format sets.
func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
xurime's avatar
xurime 已提交
943 944
	c := cCharts{
		BarDir: &attrValString{
945
			Val: stringPtr("col"),
xurime's avatar
xurime 已提交
946 947
		},
		Grouping: &attrValString{
948
			Val: stringPtr("clustered"),
xurime's avatar
xurime 已提交
949 950
		},
		VaryColors: &attrValBool{
951
			Val: boolPtr(true),
xurime's avatar
xurime 已提交
952 953
		},
		Ser:   f.drawChartSeries(formatSet),
954
		Shape: f.drawChartShape(formatSet),
xurime's avatar
xurime 已提交
955 956
		DLbls: f.drawChartDLbls(formatSet),
		AxID: []*attrValInt{
957 958
			{Val: intPtr(754001152)},
			{Val: intPtr(753999904)},
959
		},
960
		Overlap: &attrValInt{Val: intPtr(100)},
961
	}
962
	var ok bool
963
	if *c.BarDir.Val, ok = plotAreaChartBarDir[formatSet.Type]; !ok {
964 965
		c.BarDir = nil
	}
966
	if *c.Grouping.Val, ok = plotAreaChartGrouping[formatSet.Type]; !ok {
967 968
		c.Grouping = nil
	}
969
	if *c.Overlap.Val, ok = plotAreaChartOverlap[formatSet.Type]; !ok {
xurime's avatar
xurime 已提交
970
		c.Overlap = nil
R
Rohan Allison 已提交
971
	}
972 973
	catAx := f.drawPlotAreaCatAx(formatSet)
	valAx := f.drawPlotAreaValAx(formatSet)
xurime's avatar
xurime 已提交
974
	charts := map[string]*cPlotArea{
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
		"area": {
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
		"areaStacked": {
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
		"areaPercentStacked": {
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
		"area3D": {
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
		"area3DStacked": {
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
		"area3DPercentStacked": {
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
1005
		"bar": {
xurime's avatar
xurime 已提交
1006
			BarChart: &c,
R
Rohan Allison 已提交
1007 1008 1009 1010 1011 1012 1013
			CatAx:    catAx,
			ValAx:    valAx,
		},
		"barStacked": {
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
xurime's avatar
xurime 已提交
1014
		},
1015 1016 1017 1018 1019 1020
		"barPercentStacked": {
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
		"bar3DClustered": {
xurime's avatar
xurime 已提交
1021
			Bar3DChart: &c,
R
Rohan Allison 已提交
1022 1023
			CatAx:      catAx,
			ValAx:      valAx,
1024
		},
1025
		"bar3DStacked": {
1026 1027 1028 1029 1030 1031 1032 1033 1034
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DPercentStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
		"bar3DConeClustered": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DConeStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DConePercentStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DPyramidClustered": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DPyramidStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DPyramidPercentStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DCylinderClustered": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DCylinderStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"bar3DCylinderPercentStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
		"col": {
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
		"colStacked": {
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
		"colPercentStacked": {
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
1095
		"col3D": {
1096 1097 1098 1099
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
1100
		"col3DClustered": {
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DPercentStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
		"col3DCone": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DConeClustered": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DConeStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DConePercentStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DPyramid": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DPyramidClustered": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DPyramidStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DPyramidPercentStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DCylinder": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DCylinderClustered": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DCylinderStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
		"col3DCylinderPercentStacked": {
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
		"bubble": {
			BubbleChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
		"bubble3D": {
			BubbleChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
1185
	}
xurime's avatar
xurime 已提交
1186
	return charts[formatSet.Type]
1187 1188
}

xurime's avatar
xurime 已提交
1189
// drawDoughnutChart provides a function to draw the c:plotArea element for
1190 1191 1192 1193 1194
// doughnut chart by given format sets.
func (f *File) drawDoughnutChart(formatSet *formatChart) *cPlotArea {
	return &cPlotArea{
		DoughnutChart: &cCharts{
			VaryColors: &attrValBool{
1195
				Val: boolPtr(true),
1196 1197
			},
			Ser:      f.drawChartSeries(formatSet),
1198
			HoleSize: &attrValInt{Val: intPtr(75)},
1199 1200 1201 1202
		},
	}
}

xurime's avatar
xurime 已提交
1203 1204
// drawLineChart provides a function to draw the c:plotArea element for line
// chart by given format sets.
1205 1206 1207 1208
func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea {
	return &cPlotArea{
		LineChart: &cCharts{
			Grouping: &attrValString{
1209
				Val: stringPtr(plotAreaChartGrouping[formatSet.Type]),
1210 1211
			},
			VaryColors: &attrValBool{
1212
				Val: boolPtr(false),
1213 1214 1215 1216
			},
			Ser:   f.drawChartSeries(formatSet),
			DLbls: f.drawChartDLbls(formatSet),
			Smooth: &attrValBool{
1217
				Val: boolPtr(false),
1218 1219
			},
			AxID: []*attrValInt{
1220 1221
				{Val: intPtr(754001152)},
				{Val: intPtr(753999904)},
1222 1223
			},
		},
1224 1225
		CatAx: f.drawPlotAreaCatAx(formatSet),
		ValAx: f.drawPlotAreaValAx(formatSet),
1226 1227 1228
	}
}

xurime's avatar
xurime 已提交
1229 1230
// drawPieChart provides a function to draw the c:plotArea element for pie
// chart by given format sets.
1231 1232 1233 1234
func (f *File) drawPieChart(formatSet *formatChart) *cPlotArea {
	return &cPlotArea{
		PieChart: &cCharts{
			VaryColors: &attrValBool{
1235
				Val: boolPtr(true),
1236 1237 1238 1239 1240 1241
			},
			Ser: f.drawChartSeries(formatSet),
		},
	}
}

xurime's avatar
xurime 已提交
1242 1243
// drawPie3DChart provides a function to draw the c:plotArea element for 3D
// pie chart by given format sets.
1244 1245 1246 1247
func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
	return &cPlotArea{
		Pie3DChart: &cCharts{
			VaryColors: &attrValBool{
1248
				Val: boolPtr(true),
1249 1250 1251 1252 1253 1254
			},
			Ser: f.drawChartSeries(formatSet),
		},
	}
}

xurime's avatar
xurime 已提交
1255
// drawRadarChart provides a function to draw the c:plotArea element for radar
1256 1257 1258 1259 1260
// chart by given format sets.
func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea {
	return &cPlotArea{
		RadarChart: &cCharts{
			RadarStyle: &attrValString{
1261
				Val: stringPtr("marker"),
1262 1263
			},
			VaryColors: &attrValBool{
1264
				Val: boolPtr(false),
1265 1266 1267 1268
			},
			Ser:   f.drawChartSeries(formatSet),
			DLbls: f.drawChartDLbls(formatSet),
			AxID: []*attrValInt{
1269 1270
				{Val: intPtr(754001152)},
				{Val: intPtr(753999904)},
1271 1272
			},
		},
1273 1274
		CatAx: f.drawPlotAreaCatAx(formatSet),
		ValAx: f.drawPlotAreaValAx(formatSet),
1275 1276 1277
	}
}

xurime's avatar
xurime 已提交
1278 1279
// drawScatterChart provides a function to draw the c:plotArea element for
// scatter chart by given format sets.
1280 1281 1282 1283
func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
	return &cPlotArea{
		ScatterChart: &cCharts{
			ScatterStyle: &attrValString{
1284
				Val: stringPtr("smoothMarker"), // line,lineMarker,marker,none,smooth,smoothMarker
1285 1286
			},
			VaryColors: &attrValBool{
1287
				Val: boolPtr(false),
1288 1289 1290 1291
			},
			Ser:   f.drawChartSeries(formatSet),
			DLbls: f.drawChartDLbls(formatSet),
			AxID: []*attrValInt{
1292 1293
				{Val: intPtr(754001152)},
				{Val: intPtr(753999904)},
1294 1295
			},
		},
1296 1297
		CatAx: f.drawPlotAreaCatAx(formatSet),
		ValAx: f.drawPlotAreaValAx(formatSet),
1298 1299 1300
	}
}

1301 1302 1303 1304 1305 1306 1307
// drawSurface3DChart provides a function to draw the c:surface3DChart element by
// given format sets.
func (f *File) drawSurface3DChart(formatSet *formatChart) *cPlotArea {
	plotArea := &cPlotArea{
		Surface3DChart: &cCharts{
			Ser: f.drawChartSeries(formatSet),
			AxID: []*attrValInt{
1308 1309 1310
				{Val: intPtr(754001152)},
				{Val: intPtr(753999904)},
				{Val: intPtr(832256642)},
1311 1312 1313 1314 1315 1316 1317
			},
		},
		CatAx: f.drawPlotAreaCatAx(formatSet),
		ValAx: f.drawPlotAreaValAx(formatSet),
		SerAx: f.drawPlotAreaSerAx(formatSet),
	}
	if formatSet.Type == WireframeSurface3D {
1318
		plotArea.Surface3DChart.Wireframe = &attrValBool{Val: boolPtr(true)}
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	}
	return plotArea
}

// drawSurfaceChart provides a function to draw the c:surfaceChart element by
// given format sets.
func (f *File) drawSurfaceChart(formatSet *formatChart) *cPlotArea {
	plotArea := &cPlotArea{
		SurfaceChart: &cCharts{
			Ser: f.drawChartSeries(formatSet),
			AxID: []*attrValInt{
1330 1331 1332
				{Val: intPtr(754001152)},
				{Val: intPtr(753999904)},
				{Val: intPtr(832256642)},
1333 1334 1335 1336 1337 1338 1339
			},
		},
		CatAx: f.drawPlotAreaCatAx(formatSet),
		ValAx: f.drawPlotAreaValAx(formatSet),
		SerAx: f.drawPlotAreaSerAx(formatSet),
	}
	if formatSet.Type == WireframeContour {
1340
		plotArea.SurfaceChart.Wireframe = &attrValBool{Val: boolPtr(true)}
1341 1342 1343 1344 1345 1346
	}
	return plotArea
}

// drawChartShape provides a function to draw the c:shape element by given
// format sets.
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
func (f *File) drawChartShape(formatSet *formatChart) *attrValString {
	shapes := map[string]string{
		Bar3DConeClustered:          "cone",
		Bar3DConeStacked:            "cone",
		Bar3DConePercentStacked:     "cone",
		Bar3DPyramidClustered:       "pyramid",
		Bar3DPyramidStacked:         "pyramid",
		Bar3DPyramidPercentStacked:  "pyramid",
		Bar3DCylinderClustered:      "cylinder",
		Bar3DCylinderStacked:        "cylinder",
		Bar3DCylinderPercentStacked: "cylinder",
		Col3DCone:                   "cone",
		Col3DConeClustered:          "cone",
		Col3DConeStacked:            "cone",
		Col3DConePercentStacked:     "cone",
		Col3DPyramid:                "pyramid",
		Col3DPyramidClustered:       "pyramid",
		Col3DPyramidStacked:         "pyramid",
		Col3DPyramidPercentStacked:  "pyramid",
		Col3DCylinder:               "cylinder",
		Col3DCylinderClustered:      "cylinder",
		Col3DCylinderStacked:        "cylinder",
		Col3DCylinderPercentStacked: "cylinder",
	}
	if shape, ok := shapes[formatSet.Type]; ok {
1372
		return &attrValString{Val: stringPtr(shape)}
1373 1374 1375 1376
	}
	return nil
}

xurime's avatar
xurime 已提交
1377 1378
// drawChartSeries provides a function to draw the c:ser element by given
// format sets.
1379 1380
func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
	ser := []cSer{}
1381
	for k := range formatSet.Series {
1382
		ser = append(ser, cSer{
1383 1384
			IDx:   &attrValInt{Val: intPtr(k)},
			Order: &attrValInt{Val: intPtr(k)},
1385 1386
			Tx: &cTx{
				StrRef: &cStrRef{
1387
					F: formatSet.Series[k].Name,
1388 1389
				},
			},
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
			SpPr:       f.drawChartSeriesSpPr(k, formatSet),
			Marker:     f.drawChartSeriesMarker(k, formatSet),
			DPt:        f.drawChartSeriesDPt(k, formatSet),
			DLbls:      f.drawChartSeriesDLbls(formatSet),
			Cat:        f.drawChartSeriesCat(formatSet.Series[k], formatSet),
			Val:        f.drawChartSeriesVal(formatSet.Series[k], formatSet),
			XVal:       f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
			YVal:       f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
			BubbleSize: f.drawCharSeriesBubbleSize(formatSet.Series[k], formatSet),
			Bubble3D:   f.drawCharSeriesBubble3D(formatSet),
1400 1401 1402 1403 1404
		})
	}
	return &ser
}

xurime's avatar
xurime 已提交
1405
// drawChartSeriesSpPr provides a function to draw the c:spPr element by given
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
// format sets.
func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
	spPrScatter := &cSpPr{
		Ln: &aLn{
			W:      25400,
			NoFill: " ",
		},
	}
	spPrLine := &cSpPr{
		Ln: &aLn{
1416
			W:   f.ptToEMUs(formatSet.Series[i].Line.Width),
1417 1418 1419
			Cap: "rnd", // rnd, sq, flat
		},
	}
1420 1421 1422 1423 1424
	if i < 6 {
		spPrLine.Ln.SolidFill = &aSolidFill{
			SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
		}
	}
1425
	chartSeriesSpPr := map[string]*cSpPr{Line: spPrLine, Scatter: spPrScatter}
1426 1427 1428
	return chartSeriesSpPr[formatSet.Type]
}

xurime's avatar
xurime 已提交
1429 1430
// drawChartSeriesDPt provides a function to draw the c:dPt element by given
// data index and format sets.
1431
func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
xurime's avatar
xurime 已提交
1432
	dpt := []*cDPt{{
1433 1434
		IDx:      &attrValInt{Val: intPtr(i)},
		Bubble3D: &attrValBool{Val: boolPtr(false)},
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
		SpPr: &cSpPr{
			SolidFill: &aSolidFill{
				SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
			},
			Ln: &aLn{
				W:   25400,
				Cap: "rnd",
				SolidFill: &aSolidFill{
					SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
				},
			},
			Sp3D: &aSp3D{
				ContourW: 25400,
				ContourClr: &aContourClr{
					SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
				},
			},
		},
	}}
1454
	chartSeriesDPt := map[string][]*cDPt{Pie: dpt, Pie3D: dpt}
1455 1456 1457
	return chartSeriesDPt[formatSet.Type]
}

xurime's avatar
xurime 已提交
1458 1459
// drawChartSeriesCat provides a function to draw the c:cat element by given
// chart series and format sets.
1460 1461 1462 1463 1464 1465
func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
	cat := &cCat{
		StrRef: &cStrRef{
			F: v.Categories,
		},
	}
xurime's avatar
xurime 已提交
1466
	chartSeriesCat := map[string]*cCat{Scatter: nil, Bubble: nil, Bubble3D: nil}
1467
	if _, ok := chartSeriesCat[formatSet.Type]; ok || v.Categories == "" {
1468 1469 1470
		return nil
	}
	return cat
1471 1472
}

xurime's avatar
xurime 已提交
1473 1474
// drawChartSeriesVal provides a function to draw the c:val element by given
// chart series and format sets.
1475 1476 1477 1478 1479 1480
func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
	val := &cVal{
		NumRef: &cNumRef{
			F: v.Values,
		},
	}
xurime's avatar
xurime 已提交
1481
	chartSeriesVal := map[string]*cVal{Scatter: nil, Bubble: nil, Bubble3D: nil}
1482 1483 1484 1485
	if _, ok := chartSeriesVal[formatSet.Type]; ok {
		return nil
	}
	return val
1486 1487
}

xurime's avatar
xurime 已提交
1488 1489
// drawChartSeriesMarker provides a function to draw the c:marker element by
// given data index and format sets.
1490 1491
func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
	marker := &cMarker{
1492 1493
		Symbol: &attrValString{Val: stringPtr("circle")},
		Size:   &attrValInt{Val: intPtr(5)},
1494 1495 1496
	}
	if i < 6 {
		marker.SpPr = &cSpPr{
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
			SolidFill: &aSolidFill{
				SchemeClr: &aSchemeClr{
					Val: "accent" + strconv.Itoa(i+1),
				},
			},
			Ln: &aLn{
				W: 9252,
				SolidFill: &aSolidFill{
					SchemeClr: &aSchemeClr{
						Val: "accent" + strconv.Itoa(i+1),
					},
				},
			},
1510
		}
1511
	}
1512
	chartSeriesMarker := map[string]*cMarker{Scatter: marker}
1513 1514 1515
	return chartSeriesMarker[formatSet.Type]
}

xurime's avatar
xurime 已提交
1516
// drawChartSeriesXVal provides a function to draw the c:xVal element by given
1517 1518 1519 1520 1521 1522 1523
// chart series and format sets.
func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
	cat := &cCat{
		StrRef: &cStrRef{
			F: v.Categories,
		},
	}
1524
	chartSeriesXVal := map[string]*cCat{Scatter: cat}
1525 1526 1527
	return chartSeriesXVal[formatSet.Type]
}

xurime's avatar
xurime 已提交
1528
// drawChartSeriesYVal provides a function to draw the c:yVal element by given
1529 1530 1531 1532 1533 1534 1535
// chart series and format sets.
func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
	val := &cVal{
		NumRef: &cNumRef{
			F: v.Values,
		},
	}
1536
	chartSeriesYVal := map[string]*cVal{Scatter: val, Bubble: val, Bubble3D: val}
1537 1538 1539
	return chartSeriesYVal[formatSet.Type]
}

1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
// drawCharSeriesBubbleSize provides a function to draw the c:bubbleSize
// element by given chart series and format sets.
func (f *File) drawCharSeriesBubbleSize(v formatChartSeries, formatSet *formatChart) *cVal {
	if _, ok := map[string]bool{Bubble: true, Bubble3D: true}[formatSet.Type]; !ok {
		return nil
	}
	return &cVal{
		NumRef: &cNumRef{
			F: v.Values,
		},
	}
}

// drawCharSeriesBubble3D provides a function to draw the c:bubble3D element
// by given format sets.
func (f *File) drawCharSeriesBubble3D(formatSet *formatChart) *attrValBool {
	if _, ok := map[string]bool{Bubble3D: true}[formatSet.Type]; !ok {
		return nil
	}
1559
	return &attrValBool{Val: boolPtr(true)}
1560 1561
}

xurime's avatar
xurime 已提交
1562 1563
// drawChartDLbls provides a function to draw the c:dLbls element by given
// format sets.
1564 1565
func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
	return &cDLbls{
1566 1567 1568 1569 1570 1571 1572
		ShowLegendKey:   &attrValBool{Val: boolPtr(formatSet.Legend.ShowLegendKey)},
		ShowVal:         &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowVal)},
		ShowCatName:     &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowCatName)},
		ShowSerName:     &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowSerName)},
		ShowBubbleSize:  &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowBubbleSize)},
		ShowPercent:     &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowPercent)},
		ShowLeaderLines: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowLeaderLines)},
1573 1574 1575
	}
}

xurime's avatar
xurime 已提交
1576 1577
// drawChartSeriesDLbls provides a function to draw the c:dLbls element by
// given format sets.
1578
func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
xurime's avatar
xurime 已提交
1579
	dLbls := f.drawChartDLbls(formatSet)
1580
	chartSeriesDLbls := map[string]*cDLbls{Scatter: nil, Surface3D: nil, WireframeSurface3D: nil, Contour: nil, WireframeContour: nil, Bubble: nil, Bubble3D: nil}
1581 1582 1583 1584
	if _, ok := chartSeriesDLbls[formatSet.Type]; ok {
		return nil
	}
	return dLbls
1585 1586
}

xurime's avatar
xurime 已提交
1587
// drawPlotAreaCatAx provides a function to draw the c:catAx element.
1588
func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
1589 1590
	min := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Minimum)}
	max := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Maximum)}
1591 1592 1593 1594 1595 1596
	if formatSet.XAxis.Minimum == 0 {
		min = nil
	}
	if formatSet.XAxis.Maximum == 0 {
		max = nil
	}
1597
	axs := []*cAxs{
xurime's avatar
xurime 已提交
1598
		{
1599
			AxID: &attrValInt{Val: intPtr(754001152)},
1600
			Scaling: &cScaling{
1601
				Orientation: &attrValString{Val: stringPtr(orientation[formatSet.XAxis.ReverseOrder])},
1602 1603
				Max:         max,
				Min:         min,
1604
			},
1605 1606
			Delete: &attrValBool{Val: boolPtr(false)},
			AxPos:  &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
1607 1608 1609 1610
			NumFmt: &cNumFmt{
				FormatCode:   "General",
				SourceLinked: true,
			},
1611 1612 1613
			MajorTickMark: &attrValString{Val: stringPtr("none")},
			MinorTickMark: &attrValString{Val: stringPtr("none")},
			TickLblPos:    &attrValString{Val: stringPtr("nextTo")},
1614 1615
			SpPr:          f.drawPlotAreaSpPr(),
			TxPr:          f.drawPlotAreaTxPr(),
1616 1617 1618 1619 1620 1621
			CrossAx:       &attrValInt{Val: intPtr(753999904)},
			Crosses:       &attrValString{Val: stringPtr("autoZero")},
			Auto:          &attrValBool{Val: boolPtr(true)},
			LblAlgn:       &attrValString{Val: stringPtr("ctr")},
			LblOffset:     &attrValInt{Val: intPtr(100)},
			NoMultiLvlLbl: &attrValBool{Val: boolPtr(false)},
1622 1623
		},
	}
1624 1625 1626
	if formatSet.XAxis.MajorGridlines {
		axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1627 1628 1629
	if formatSet.XAxis.MinorGridlines {
		axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1630 1631 1632
	if formatSet.XAxis.TickLabelSkip != 0 {
		axs[0].TickLblSkip = &attrValInt{Val: intPtr(formatSet.XAxis.TickLabelSkip)}
	}
1633
	return axs
1634 1635
}

xurime's avatar
xurime 已提交
1636
// drawPlotAreaValAx provides a function to draw the c:valAx element.
1637
func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
1638 1639
	min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
	max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
1640 1641 1642 1643 1644 1645
	if formatSet.YAxis.Minimum == 0 {
		min = nil
	}
	if formatSet.YAxis.Maximum == 0 {
		max = nil
	}
1646
	axs := []*cAxs{
xurime's avatar
xurime 已提交
1647
		{
1648
			AxID: &attrValInt{Val: intPtr(753999904)},
1649
			Scaling: &cScaling{
1650
				Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
1651 1652
				Max:         max,
				Min:         min,
1653
			},
1654 1655
			Delete: &attrValBool{Val: boolPtr(false)},
			AxPos:  &attrValString{Val: stringPtr(valAxPos[formatSet.YAxis.ReverseOrder])},
1656
			NumFmt: &cNumFmt{
1657
				FormatCode:   chartValAxNumFmtFormatCode[formatSet.Type],
1658 1659
				SourceLinked: true,
			},
1660 1661 1662
			MajorTickMark: &attrValString{Val: stringPtr("none")},
			MinorTickMark: &attrValString{Val: stringPtr("none")},
			TickLblPos:    &attrValString{Val: stringPtr("nextTo")},
1663 1664
			SpPr:          f.drawPlotAreaSpPr(),
			TxPr:          f.drawPlotAreaTxPr(),
1665 1666 1667
			CrossAx:       &attrValInt{Val: intPtr(754001152)},
			Crosses:       &attrValString{Val: stringPtr("autoZero")},
			CrossBetween:  &attrValString{Val: stringPtr(chartValAxCrossBetween[formatSet.Type])},
1668 1669
		},
	}
1670 1671 1672
	if formatSet.YAxis.MajorGridlines {
		axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1673 1674 1675
	if formatSet.YAxis.MinorGridlines {
		axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1676
	if pos, ok := valTickLblPos[formatSet.Type]; ok {
1677
		axs[0].TickLblPos.Val = stringPtr(pos)
1678
	}
1679 1680 1681
	if formatSet.YAxis.MajorUnit != 0 {
		axs[0].MajorUnit = &attrValFloat{Val: float64Ptr(formatSet.YAxis.MajorUnit)}
	}
1682 1683 1684 1685 1686
	return axs
}

// drawPlotAreaSerAx provides a function to draw the c:serAx element.
func (f *File) drawPlotAreaSerAx(formatSet *formatChart) []*cAxs {
1687 1688
	min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
	max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
1689 1690 1691 1692 1693 1694 1695 1696
	if formatSet.YAxis.Minimum == 0 {
		min = nil
	}
	if formatSet.YAxis.Maximum == 0 {
		max = nil
	}
	return []*cAxs{
		{
1697
			AxID: &attrValInt{Val: intPtr(832256642)},
1698
			Scaling: &cScaling{
1699
				Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
1700 1701 1702
				Max:         max,
				Min:         min,
			},
1703 1704 1705
			Delete:     &attrValBool{Val: boolPtr(false)},
			AxPos:      &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
			TickLblPos: &attrValString{Val: stringPtr("nextTo")},
1706 1707
			SpPr:       f.drawPlotAreaSpPr(),
			TxPr:       f.drawPlotAreaTxPr(),
1708
			CrossAx:    &attrValInt{Val: intPtr(753999904)},
1709 1710
		},
	}
1711 1712
}

xurime's avatar
xurime 已提交
1713
// drawPlotAreaSpPr provides a function to draw the c:spPr element.
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
func (f *File) drawPlotAreaSpPr() *cSpPr {
	return &cSpPr{
		Ln: &aLn{
			W:    9525,
			Cap:  "flat",
			Cmpd: "sng",
			Algn: "ctr",
			SolidFill: &aSolidFill{
				SchemeClr: &aSchemeClr{
					Val:    "tx1",
1724 1725
					LumMod: &attrValInt{Val: intPtr(15000)},
					LumOff: &attrValInt{Val: intPtr(85000)},
1726 1727 1728 1729 1730 1731
				},
			},
		},
	}
}

xurime's avatar
xurime 已提交
1732
// drawPlotAreaTxPr provides a function to draw the c:txPr element.
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
func (f *File) drawPlotAreaTxPr() *cTxPr {
	return &cTxPr{
		BodyPr: aBodyPr{
			Rot:              -60000000,
			SpcFirstLastPara: true,
			VertOverflow:     "ellipsis",
			Vert:             "horz",
			Wrap:             "square",
			Anchor:           "ctr",
			AnchorCtr:        true,
		},
		P: aP{
1745
			PPr: &aPPr{
1746
				DefRPr: aRPr{
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
					Sz:       900,
					B:        false,
					I:        false,
					U:        "none",
					Strike:   "noStrike",
					Kern:     1200,
					Baseline: 0,
					SolidFill: &aSolidFill{
						SchemeClr: &aSchemeClr{
							Val:    "tx1",
1757 1758
							LumMod: &attrValInt{Val: intPtr(15000)},
							LumOff: &attrValInt{Val: intPtr(85000)},
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
						},
					},
					Latin: &aLatin{Typeface: "+mn-lt"},
					Ea:    &aEa{Typeface: "+mn-ea"},
					Cs:    &aCs{Typeface: "+mn-cs"},
				},
			},
			EndParaRPr: &aEndParaRPr{Lang: "en-US"},
		},
	}
}

xurime's avatar
xurime 已提交
1771 1772
// drawingParser provides a function to parse drawingXML. In order to solve
// the problem that the label structure is changed after serialization and
xurime's avatar
xurime 已提交
1773 1774
// deserialization, two different structures: decodeWsDr and encodeWsDr are
// defined.
1775
func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
1776 1777 1778 1779 1780
	var (
		err error
		ok  bool
	)

1781 1782 1783 1784
	if f.Drawings[path] == nil {
		content := xlsxWsDr{}
		content.A = NameSpaceDrawingML
		content.Xdr = NameSpaceDrawingMLSpreadSheet
1785
		if _, ok = f.XLSX[path]; ok { // Append Model
1786
			decodeWsDr := decodeWsDr{}
1787 1788 1789 1790
			if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))).
				Decode(&decodeWsDr); err != nil && err != io.EOF {
				log.Printf("xml decode error: %s", err)
			}
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
			content.R = decodeWsDr.R
			for _, v := range decodeWsDr.OneCellAnchor {
				content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
					EditAs:       v.EditAs,
					GraphicFrame: v.Content,
				})
			}
			for _, v := range decodeWsDr.TwoCellAnchor {
				content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
					EditAs:       v.EditAs,
					GraphicFrame: v.Content,
				})
			}
1804
		}
1805
		f.Drawings[path] = &content
1806
	}
xurime's avatar
xurime 已提交
1807 1808
	wsDr := f.Drawings[path]
	return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2
xurime's avatar
xurime 已提交
1809 1810
}

xurime's avatar
xurime 已提交
1811 1812
// addDrawingChart provides a function to add chart graphic frame by given
// sheet, drawingXML, cell, width, height, relationship index and format sets.
1813 1814 1815 1816 1817
func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) error {
	col, row, err := CellNameToCoordinates(cell)
	if err != nil {
		return err
	}
1818 1819 1820
	colIdx := col - 1
	rowIdx := row - 1

xurime's avatar
xurime 已提交
1821 1822
	width = int(float64(width) * formatSet.XScale)
	height = int(float64(height) * formatSet.YScale)
1823 1824
	colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
		f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
1825
	content, cNvPrID := f.drawingParser(drawingXML)
1826
	twoCellAnchor := xdrCellAnchor{}
1827
	twoCellAnchor.EditAs = formatSet.Positioning
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
	from := xlsxFrom{}
	from.Col = colStart
	from.ColOff = formatSet.OffsetX * EMU
	from.Row = rowStart
	from.RowOff = formatSet.OffsetY * EMU
	to := xlsxTo{}
	to.Col = colEnd
	to.ColOff = x2 * EMU
	to.Row = rowEnd
	to.RowOff = y2 * EMU
	twoCellAnchor.From = &from
	twoCellAnchor.To = &to

1841 1842 1843
	graphicFrame := xlsxGraphicFrame{
		NvGraphicFramePr: xlsxNvGraphicFramePr{
			CNvPr: &xlsxCNvPr{
xurime's avatar
xurime 已提交
1844
				ID:   cNvPrID,
1845
				Name: "Chart " + strconv.Itoa(cNvPrID),
1846
			},
1847 1848 1849 1850 1851 1852 1853 1854
		},
		Graphic: &xlsxGraphic{
			GraphicData: &xlsxGraphicData{
				URI: NameSpaceDrawingMLChart,
				Chart: &xlsxChart{
					C:   NameSpaceDrawingMLChart,
					R:   SourceRelationship,
					RID: "rId" + strconv.Itoa(rID),
1855 1856 1857 1858 1859
				},
			},
		},
	}
	graphic, _ := xml.Marshal(graphicFrame)
1860 1861
	twoCellAnchor.GraphicFrame = string(graphic)
	twoCellAnchor.ClientData = &xdrClientData{
1862 1863 1864
		FLocksWithSheet:  formatSet.FLocksWithSheet,
		FPrintsWithSheet: formatSet.FPrintsWithSheet,
	}
1865
	content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
1866
	f.Drawings[drawingXML] = content
1867
	return err
1868
}
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878

// ptToEMUs provides a function to convert pt to EMUs, 1 pt = 12700 EMUs. The
// range of pt is 0.25pt - 999pt. If the value of pt is outside the range, the
// default EMUs will be returned.
func (f *File) ptToEMUs(pt float64) int {
	if 0.25 > pt || pt > 999 {
		return 25400
	}
	return int(12700 * pt)
}