1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030 |
- package excelize
- import (
- "encoding/json"
- "encoding/xml"
- "errors"
- "fmt"
- "strconv"
- "strings"
- )
- const (
- 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"
- PieOfPieChart = "pieOfPie"
- BarOfPieChart = "barOfPie"
- Radar = "radar"
- Scatter = "scatter"
- Surface3D = "surface3D"
- WireframeSurface3D = "wireframeSurface3D"
- Contour = "contour"
- WireframeContour = "wireframeContour"
- Bubble = "bubble"
- Bubble3D = "bubble3D"
- )
- var (
- chartView3DRotX = map[string]int{
- 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,
- PieOfPieChart: 0,
- BarOfPieChart: 0,
- Radar: 0,
- Scatter: 0,
- Surface3D: 15,
- WireframeSurface3D: 15,
- Contour: 90,
- WireframeContour: 90,
- }
- chartView3DRotY = map[string]int{
- 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,
- PieOfPieChart: 0,
- BarOfPieChart: 0,
- Radar: 0,
- Scatter: 0,
- Surface3D: 20,
- WireframeSurface3D: 20,
- Contour: 0,
- WireframeContour: 0,
- }
- plotAreaChartOverlap = map[string]int{
- BarStacked: 100,
- BarPercentStacked: 100,
- ColStacked: 100,
- ColPercentStacked: 100,
- }
- chartView3DPerspective = map[string]int{
- Contour: 0,
- WireframeContour: 0,
- }
- chartView3DRAngAx = map[string]int{
- 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,
- PieOfPieChart: 0,
- BarOfPieChart: 0,
- Radar: 0,
- Scatter: 0,
- Surface3D: 0,
- WireframeSurface3D: 0,
- Contour: 0,
- Bubble: 0,
- Bubble3D: 0,
- }
- chartLegendPosition = map[string]string{
- "bottom": "b",
- "left": "l",
- "right": "r",
- "top": "t",
- "top_right": "tr",
- }
- chartValAxNumFmtFormatCode = map[string]string{
- 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",
- PieOfPieChart: "General",
- BarOfPieChart: "General",
- Radar: "General",
- Scatter: "General",
- Surface3D: "General",
- WireframeSurface3D: "General",
- Contour: "General",
- WireframeContour: "General",
- Bubble: "General",
- Bubble3D: "General",
- }
- chartValAxCrossBetween = map[string]string{
- 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",
- PieOfPieChart: "between",
- BarOfPieChart: "between",
- Radar: "between",
- Scatter: "between",
- Surface3D: "midCat",
- WireframeSurface3D: "midCat",
- Contour: "midCat",
- WireframeContour: "midCat",
- Bubble: "midCat",
- Bubble3D: "midCat",
- }
- plotAreaChartGrouping = map[string]string{
- 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",
- }
- plotAreaChartBarDir = map[string]string{
- 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",
- }
- orientation = map[bool]string{
- true: "maxMin",
- false: "minMax",
- }
- catAxPos = map[bool]string{
- true: "t",
- false: "b",
- }
- valAxPos = map[bool]string{
- true: "r",
- false: "l",
- }
- valTickLblPos = map[string]string{
- Contour: "none",
- WireframeContour: "none",
- }
- )
- func parseFormatChartSet(formatSet string) (*formatChart, error) {
- format := formatChart{
- Dimension: formatChartDimension{
- Width: 480,
- Height: 290,
- },
- 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: " ",
- },
- VaryColors: true,
- ShowBlanksAs: "gap",
- }
- err := json.Unmarshal([]byte(formatSet), &format)
- return &format, err
- }
- func (f *File) AddChart(sheet, cell, format string, combo ...string) error {
-
- ws, err := f.workSheetReader(sheet)
- if err != nil {
- return err
- }
- formatSet, comboCharts, err := f.getFormatChart(format, combo)
- if err != nil {
- return err
- }
-
- drawingID := f.countDrawings() + 1
- chartID := f.countCharts() + 1
- drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
- drawingID, drawingXML = f.prepareDrawing(ws, drawingID, sheet, drawingXML)
- drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
- drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
- err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
- if err != nil {
- return err
- }
- f.addChart(formatSet, comboCharts)
- f.addContentTypePart(chartID, "chart")
- f.addContentTypePart(drawingID, "drawings")
- f.addSheetNameSpace(sheet, SourceRelationship)
- return err
- }
- func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
-
- if f.GetSheetIndex(sheet) != -1 {
- return ErrExistsWorksheet
- }
- formatSet, comboCharts, err := f.getFormatChart(format, combo)
- if err != nil {
- return err
- }
- cs := xlsxChartsheet{
- SheetViews: &xlsxChartsheetViews{
- SheetView: []*xlsxChartsheetView{{ZoomScaleAttr: 100, ZoomToFitAttr: true}},
- },
- }
- f.SheetCount++
- wb := f.workbookReader()
- sheetID := 0
- for _, v := range wb.Sheets.Sheet {
- if v.SheetID > sheetID {
- sheetID = v.SheetID
- }
- }
- sheetID++
- path := "xl/chartsheets/sheet" + strconv.Itoa(sheetID) + ".xml"
- f.sheetMap[trimSheetName(sheet)] = path
- f.Sheet[path] = nil
- drawingID := f.countDrawings() + 1
- chartID := f.countCharts() + 1
- drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
- f.prepareChartSheetDrawing(&cs, drawingID, sheet)
- drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
- drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
- f.addSheetDrawingChart(drawingXML, drawingRID, &formatSet.Format)
- f.addChart(formatSet, comboCharts)
- f.addContentTypePart(chartID, "chart")
- f.addContentTypePart(sheetID, "chartsheet")
- f.addContentTypePart(drawingID, "drawings")
-
- rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipChartsheet, fmt.Sprintf("/xl/chartsheets/sheet%d.xml", sheetID), "")
-
- f.setWorkbook(sheet, sheetID, rID)
- chartsheet, _ := xml.Marshal(cs)
- f.addSheetNameSpace(sheet, NameSpaceSpreadSheet)
- f.saveFileList(path, replaceRelationshipsBytes(f.replaceNameSpaceBytes(path, chartsheet)))
- return err
- }
- func (f *File) getFormatChart(format string, combo []string) (*formatChart, []*formatChart, error) {
- comboCharts := []*formatChart{}
- formatSet, err := parseFormatChartSet(format)
- if err != nil {
- return formatSet, comboCharts, err
- }
- for _, comboFormat := range combo {
- comboChart, err := parseFormatChartSet(comboFormat)
- if err != nil {
- return formatSet, comboCharts, err
- }
- if _, ok := chartValAxNumFmtFormatCode[comboChart.Type]; !ok {
- return formatSet, comboCharts, errors.New("unsupported chart type " + comboChart.Type)
- }
- comboCharts = append(comboCharts, comboChart)
- }
- if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
- return formatSet, comboCharts, errors.New("unsupported chart type " + formatSet.Type)
- }
- return formatSet, comboCharts, err
- }
- func (f *File) DeleteChart(sheet, cell string) (err error) {
- col, row, err := CellNameToCoordinates(cell)
- if err != nil {
- return
- }
- col--
- row--
- ws, err := f.workSheetReader(sheet)
- if err != nil {
- return
- }
- if ws.Drawing == nil {
- return
- }
- drawingXML := strings.Replace(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl", -1)
- return f.deleteDrawing(col, row, drawingXML, "Chart")
- }
- func (f *File) countCharts() int {
- count := 0
- for k := range f.XLSX {
- if strings.Contains(k, "xl/charts/chart") {
- count++
- }
- }
- return count
- }
- func (f *File) ptToEMUs(pt float64) int {
- if 0.25 > pt || pt > 999 {
- return 25400
- }
- return int(12700 * pt)
- }
|