Skip to content

How to set the font for DataLabel in ChartSeries? #2052

@water160

Description

@water160

Description

When generate Chart, the default FontSize of ChartSeries is 10, which is a little larger. Is there any way that I can change the Font, like: FontSize, FontColor, etc. ?

Below is an example:

package main

import (
	"fmt"

	"github.com/xuri/excelize/v2"
)

func main() {
	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	sheetName := "Sheet1"
	index, err := f.NewSheet(sheetName)
	if err != nil {
		fmt.Println(err)
		return
	}
	f.SetActiveSheet(index)

	data := [][]interface{}{
		{"RL6547E", "Octo", "Nove", nil, "WW46", "WW47", "WW48", "WW49", "WW50"},
		{"Input", 1469, 1190, nil, 200, 300, 123, 243, 324},
		{"Output", 1469, 1190, nil, 200, 300, 123, 243, 324},
		{"Final Yield", 0.9767, 0.9799, nil, 0.9765, 0.9774, 0.9814, 0.9635, 0.9903},
		{"Yield Limit", 0.9500, 0.9500, nil, 0.9500, 0.9500, 0.9500, 0.9500, 0.9500},
	}
	for idx, row := range data {
		cell, err := excelize.CoordinatesToCellName(1, idx+1)
		if err != nil {
			fmt.Println(err)
			return
		}
		if err := f.SetSheetRow("Sheet1", cell, &row); err != nil {
			fmt.Println(err)
			return
		}
	}

	colLen := len(data[0])
	rowLen := len(data)
	maxColAscii := CalcAscii(colLen - 1)
	// maxRowAscii := CalcAscii(rowLen)

	enable, disable := true, false
	barChart := excelize.Chart{
		Dimension: excelize.ChartDimension{
			Height: 350,
			Width:  700,
		},
		Type: excelize.Col,
		Series: []excelize.ChartSeries{
			{
				Name:              fmt.Sprintf("%s!$A$2", sheetName),
				Categories:        fmt.Sprintf("%s!$B$1:$%s$1", sheetName, maxColAscii),
				Values:            fmt.Sprintf("%s!$B$2:$%s$2", sheetName, maxColAscii),
				DataLabelPosition: excelize.ChartDataLabelsPositionAbove,
			},
			{
				Name:              fmt.Sprintf("%s!$A$3", sheetName),
				Categories:        fmt.Sprintf("%s!$B$1:$%s$1", sheetName, maxColAscii),
				Values:            fmt.Sprintf("%s!$B$3:$%s$3", sheetName, maxColAscii),
				DataLabelPosition: excelize.ChartDataLabelsPositionAbove,
			},
		},
		Format: excelize.GraphicOptions{
			ScaleX:          1,
			ScaleY:          1,
			OffsetX:         15,
			OffsetY:         10,
			PrintObject:     &enable,
			LockAspectRatio: false,
			Locked:          &disable,
		},
		Title: []excelize.RichTextRun{
			{
				Text: fmt.Sprintf("%s CP Yield Performance", data[0][0]),
			},
		},
		Legend: excelize.ChartLegend{
			Position:      "bottom",
			ShowLegendKey: false,
		},
		PlotArea: excelize.ChartPlotArea{
			ShowCatName:     false,
			ShowLeaderLines: false,
			ShowPercent:     false,
			ShowSerName:     false,
			ShowVal:         true,
		},
		ShowBlanksAs: "gap",
		YAxis: excelize.ChartAxis{
			MajorGridLines: true,
			Secondary:      true,
			Font: excelize.Font{
				Color: "#000000",
				Size:  10,
			},
		},
	}

	lineChart := excelize.Chart{
		Type: excelize.Line,
		Series: []excelize.ChartSeries{
			{
				Name:              fmt.Sprintf("%s!$A$4", sheetName),
				Categories:        fmt.Sprintf("%s!$B$1:$%s$1", sheetName, maxColAscii),
				Values:            fmt.Sprintf("%s!$B$4:$%s$4", sheetName, maxColAscii),
				DataLabelPosition: excelize.ChartDataLabelsPositionCenter,
				Marker: excelize.ChartMarker{
					Symbol: "none", Size: 9,
				},
			},
			{
				Name:              fmt.Sprintf("%s!$A$5", sheetName),
				Categories:        fmt.Sprintf("%s!$B$1:$%s$1", sheetName, maxColAscii),
				Values:            fmt.Sprintf("%s!$B$5:$%s$5", sheetName, maxColAscii),
				DataLabelPosition: excelize.ChartDataLabelsPositionCenter,
				Marker: excelize.ChartMarker{
					Symbol: "none", Size: 9,
				},
			},
		},
		Legend: excelize.ChartLegend{
			Position:      "bottom",
			ShowLegendKey: false,
		},
		Format: excelize.GraphicOptions{
			ScaleX:          1,
			ScaleY:          1,
			OffsetX:         15,
			OffsetY:         10,
			PrintObject:     &enable,
			LockAspectRatio: false,
			Locked:          &disable,
		},
		PlotArea: excelize.ChartPlotArea{
			ShowCatName:     false,
			ShowLeaderLines: false,
			ShowPercent:     false,
			ShowSerName:     false,
			ShowVal:         true,
			NumFmt: excelize.ChartNumFmt{
				CustomNumFmt: "0.00%",
			},
		},
		ShowBlanksAs: "gap",
		YAxis: excelize.ChartAxis{
			MajorGridLines: true,
			Secondary:      true,
			Font: excelize.Font{
				Color: "#000000",
				Size:  10,
			},
		},
	}

	chartStartCell, err := excelize.CoordinatesToCellName(1, rowLen+1)
	if err != nil {
		fmt.Println(err)
		return
	}

	if err := f.AddChart("Sheet1", chartStartCell,
		&barChart, &lineChart,
	); err != nil {
		fmt.Println(err)
		return
	}

	if err := f.SaveAs("TestCharts.xlsx"); err != nil {
		fmt.Println(err)
	}
}

func CalcAscii(num int) string {
	startAscii := 65 // A
	return string(rune(startAscii + num))
}

Steps to reproduce the issue:

  1. run the code above
  2. observe the chart
  3. the font size is a litter larger

Describe the results you received:
I cannot find any option in ChartSeries

type ChartSeries struct {
	Name              string
	Categories        string
	Values            string
	Sizes             string
	Fill              Fill
	Line              ChartLine
	Marker            ChartMarker
	DataLabelPosition ChartDataLabelPositionType
}

Describe the results you expected:
Change the font like XAxis or YAxis

Output of go version:
1.23.1

Excelize version or commit ID:
github.com/xuri/excelize/v2 v2.9.0

Environment details (OS, Microsoft Excel™ version, physical, etc.):
OS: Windows 11
Excel version: Microsoft Office Home and Student 2019

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions