Browse Source

Improve compatibility for charts

xuri 6 years ago
parent
commit
eb520ae277
6 changed files with 29 additions and 75 deletions
  1. 17 66
      chart.go
  2. 1 1
      chart_test.go
  3. 1 1
      col.go
  4. 1 1
      picture.go
  5. 8 5
      shape.go
  6. 1 1
      xmlChart.go

+ 17 - 66
chart.go

@@ -179,59 +179,11 @@ var (
 		Contour:                     0,
 		WireframeContour:            0,
 	}
-	chartView3DDepthPercent = map[string]int{
-		Area:                        100,
-		AreaStacked:                 100,
-		AreaPercentStacked:          100,
-		Area3D:                      100,
-		Area3DStacked:               100,
-		Area3DPercentStacked:        100,
-		Bar:                         100,
-		BarStacked:                  100,
-		BarPercentStacked:           100,
-		Bar3DClustered:              100,
-		Bar3DStacked:                100,
-		Bar3DPercentStacked:         100,
-		Bar3DConeClustered:          100,
-		Bar3DConeStacked:            100,
-		Bar3DConePercentStacked:     100,
-		Bar3DPyramidClustered:       100,
-		Bar3DPyramidStacked:         100,
-		Bar3DPyramidPercentStacked:  100,
-		Bar3DCylinderClustered:      100,
-		Bar3DCylinderStacked:        100,
-		Bar3DCylinderPercentStacked: 100,
-		Col:                         100,
-		ColStacked:                  100,
-		ColPercentStacked:           100,
-		Col3D:                       100,
-		Col3DClustered:              100,
-		Col3DStacked:                100,
-		Col3DPercentStacked:         100,
-		Col3DCone:                   100,
-		Col3DConeClustered:          100,
-		Col3DConeStacked:            100,
-		Col3DConePercentStacked:     100,
-		Col3DPyramid:                100,
-		Col3DPyramidClustered:       100,
-		Col3DPyramidStacked:         100,
-		Col3DPyramidPercentStacked:  100,
-		Col3DCylinder:               100,
-		Col3DCylinderClustered:      100,
-		Col3DCylinderStacked:        100,
-		Col3DCylinderPercentStacked: 100,
-		Doughnut:                    100,
-		Line:                        100,
-		Pie:                         100,
-		Pie3D:                       100,
-		Radar:                       100,
-		Scatter:                     100,
-		Surface3D:                   100,
-		WireframeSurface3D:          100,
-		Contour:                     100,
-		WireframeContour:            100,
-		Bubble:                      100,
-		Bubble3D:                    100,
+	plotAreaChartOverlap = map[string]int{
+		BarStacked:        100,
+		BarPercentStacked: 100,
+		ColStacked:        100,
+		ColPercentStacked: 100,
 	}
 	chartView3DPerspective = map[string]int{
 		Contour:          0,
@@ -842,11 +794,10 @@ func (f *File) addChart(formatSet *formatChart) {
 				},
 			},
 			View3D: &cView3D{
-				RotX:         &attrValInt{Val: chartView3DRotX[formatSet.Type]},
-				RotY:         &attrValInt{Val: chartView3DRotY[formatSet.Type]},
-				DepthPercent: &attrValInt{Val: chartView3DDepthPercent[formatSet.Type]},
-				Perspective:  &attrValInt{Val: chartView3DPerspective[formatSet.Type]},
-				RAngAx:       &attrValInt{Val: chartView3DRAngAx[formatSet.Type]},
+				RotX:        &attrValInt{Val: chartView3DRotX[formatSet.Type]},
+				RotY:        &attrValInt{Val: chartView3DRotY[formatSet.Type]},
+				Perspective: &attrValInt{Val: chartView3DPerspective[formatSet.Type]},
+				RAngAx:      &attrValInt{Val: chartView3DRAngAx[formatSet.Type]},
 			},
 			Floor: &cThicknessSpPr{
 				Thickness: &attrValInt{Val: 0},
@@ -980,6 +931,7 @@ func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
 			{Val: 754001152},
 			{Val: 753999904},
 		},
+		Overlap: &attrValInt{Val: 100},
 	}
 	var ok bool
 	if c.BarDir.Val, ok = plotAreaChartBarDir[formatSet.Type]; !ok {
@@ -988,8 +940,8 @@ func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
 	if c.Grouping.Val, ok = plotAreaChartGrouping[formatSet.Type]; !ok {
 		c.Grouping = nil
 	}
-	if strings.HasSuffix(formatSet.Type, "Stacked") {
-		c.Overlap = &attrValInt{Val: 100}
+	if c.Overlap.Val, ok = plotAreaChartOverlap[formatSet.Type]; !ok {
+		c.Overlap = nil
 	}
 	catAx := f.drawPlotAreaCatAx(formatSet)
 	valAx := f.drawPlotAreaValAx(formatSet)
@@ -1485,7 +1437,7 @@ func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *
 			F: v.Categories,
 		},
 	}
-	chartSeriesCat := map[string]*cCat{Scatter: nil}
+	chartSeriesCat := map[string]*cCat{Scatter: nil, Bubble: nil, Bubble3D: nil}
 	if _, ok := chartSeriesCat[formatSet.Type]; ok {
 		return nil
 	}
@@ -1500,7 +1452,7 @@ func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *
 			F: v.Values,
 		},
 	}
-	chartSeriesVal := map[string]*cVal{Scatter: nil}
+	chartSeriesVal := map[string]*cVal{Scatter: nil, Bubble: nil, Bubble3D: nil}
 	if _, ok := chartSeriesVal[formatSet.Type]; ok {
 		return nil
 	}
@@ -1783,7 +1735,6 @@ func (f *File) drawPlotAreaTxPr() *cTxPr {
 // deserialization, two different structures: decodeWsDr and encodeWsDr are
 // defined.
 func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
-	cNvPrID := 1
 	if f.Drawings[path] == nil {
 		content := xlsxWsDr{}
 		content.A = NameSpaceDrawingML
@@ -1793,7 +1744,6 @@ func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
 			decodeWsDr := decodeWsDr{}
 			_ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(path)), &decodeWsDr)
 			content.R = decodeWsDr.R
-			cNvPrID = len(decodeWsDr.OneCellAnchor) + len(decodeWsDr.TwoCellAnchor) + 1
 			for _, v := range decodeWsDr.OneCellAnchor {
 				content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
 					EditAs:       v.EditAs,
@@ -1809,7 +1759,8 @@ func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
 		}
 		f.Drawings[path] = &content
 	}
-	return f.Drawings[path], cNvPrID
+	wsDr := f.Drawings[path]
+	return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2
 }
 
 // addDrawingChart provides a function to add chart graphic frame by given
@@ -1845,7 +1796,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI
 	graphicFrame := xlsxGraphicFrame{
 		NvGraphicFramePr: xlsxNvGraphicFramePr{
 			CNvPr: &xlsxCNvPr{
-				ID:   len(content.OneCellAnchor) + len(content.TwoCellAnchor) + 2,
+				ID:   cNvPrID,
 				Name: "Chart " + strconv.Itoa(cNvPrID),
 			},
 		},

+ 1 - 1
chart_test.go

@@ -126,7 +126,7 @@ func TestAddChart(t *testing.T) {
 	assert.NoError(t, f.AddChart("Sheet1", "X1", `{"type":"colStacked","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"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":"left","show_legend_key":false},"title":{"name":"2D Stacked 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"}`))
 	assert.NoError(t, f.AddChart("Sheet1", "P16", `{"type":"colPercentStacked","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"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":"left","show_legend_key":false},"title":{"name":"100% Stacked 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"}`))
 	assert.NoError(t, f.AddChart("Sheet1", "X16", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"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":"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"}`))
-	assert.NoError(t, f.AddChart("Sheet1", "P30", `{"type":"col3DStacked","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"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":"left","show_legend_key":false},"title":{"name":"3D 100% Stacked Bar 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"}`))
+	assert.NoError(t, f.AddChart("Sheet1", "P30", `{"type":"col3DStacked","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"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":"left","show_legend_key":false},"title":{"name":"3D Stacked 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"}`))
 	assert.NoError(t, f.AddChart("Sheet1", "X30", `{"type":"col3DPercentStacked","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"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":"left","show_legend_key":false},"title":{"name":"3D 100% Stacked 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"}`))
 	assert.NoError(t, f.AddChart("Sheet1", "AF1", `{"type":"col3DConeStacked","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"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":"left","show_legend_key":false},"title":{"name":"3D Column Cone Stacked 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"}`))
 	assert.NoError(t, f.AddChart("Sheet1", "AF16", `{"type":"col3DConeClustered","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"},{"name":"Sheet1!$A$31","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$31:$D$31"},{"name":"Sheet1!$A$32","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$32:$D$32"},{"name":"Sheet1!$A$33","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$33:$D$33"},{"name":"Sheet1!$A$34","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$34:$D$34"},{"name":"Sheet1!$A$35","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$35:$D$35"},{"name":"Sheet1!$A$36","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$36:$D$36"},{"name":"Sheet1!$A$37","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$37:$D$37"}],"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":"left","show_legend_key":false},"title":{"name":"3D Column Cone Clustered 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"}`))

+ 1 - 1
col.go

@@ -113,7 +113,7 @@ func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
 	for c := range xlsx.Cols.Col {
 		colData := &xlsx.Cols.Col[c]
 		if colData.Min <= colNum && colNum <= colData.Max {
-			level = colData.OutlineLevel + 1
+			level = colData.OutlineLevel
 		}
 	}
 	return level, err

+ 1 - 1
picture.go

@@ -272,7 +272,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
 	twoCellAnchor.To = &to
 	pic := xlsxPic{}
 	pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspect = formatSet.NoChangeAspect
-	pic.NvPicPr.CNvPr.ID = len(content.OneCellAnchor) + len(content.TwoCellAnchor) + 2
+	pic.NvPicPr.CNvPr.ID = cNvPrID
 	pic.NvPicPr.CNvPr.Descr = file
 	pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID)
 	if hyperlinkRID != 0 {

+ 8 - 5
shape.go

@@ -411,11 +411,6 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
 					U:       u,
 					Sz:      p.Font.Size * 100,
 					Latin:   &aLatin{Typeface: p.Font.Family},
-					SolidFill: &aSolidFill{
-						SrgbClr: &attrValString{
-							Val: strings.Replace(strings.ToUpper(p.Font.Color), "#", "", -1),
-						},
-					},
 				},
 				T: text,
 			},
@@ -423,6 +418,14 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
 				Lang: "en-US",
 			},
 		}
+		srgbClr := strings.Replace(strings.ToUpper(p.Font.Color), "#", "", -1)
+		if len(srgbClr) == 6 {
+			paragraph.R.RPr.SolidFill = &aSolidFill{
+				SrgbClr: &attrValString{
+					Val: srgbClr,
+				},
+			}
+		}
 		shape.TxBody.P = append(shape.TxBody.P, paragraph)
 	}
 	twoCellAnchor.Sp = &shape

+ 1 - 1
xmlChart.go

@@ -332,8 +332,8 @@ type cCharts struct {
 	VaryColors   *attrValBool   `xml:"varyColors"`
 	Wireframe    *attrValBool   `xml:"wireframe"`
 	Ser          *[]cSer        `xml:"ser"`
-	Shape        *attrValString `xml:"shape"`
 	DLbls        *cDLbls        `xml:"dLbls"`
+	Shape        *attrValString `xml:"shape"`
 	HoleSize     *attrValInt    `xml:"holeSize"`
 	Smooth       *attrValBool   `xml:"smooth"`
 	Overlap      *attrValInt    `xml:"overlap"`