12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034 |
- package excelize
- import (
- "encoding/json"
- "encoding/xml"
- "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.Store(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, newUnsupportChartType(comboChart.Type)
- }
- comboCharts = append(comboCharts, comboChart)
- }
- if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
- return formatSet, comboCharts, newUnsupportChartType(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
- f.Pkg.Range(func(k, v interface{}) bool {
- if strings.Contains(k.(string), "xl/charts/chart") {
- count++
- }
- return true
- })
- return count
- }
- func (f *File) ptToEMUs(pt float64) int {
- if 0.25 > pt || pt > 999 {
- return 25400
- }
- return int(12700 * pt)
- }
|