Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
water160 opened this issue Dec 26, 2024 · 3 comments
Closed

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

water160 opened this issue Dec 26, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@water160
Copy link

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

@xuri xuri added enhancement New feature or request in progress Working in progress labels Dec 26, 2024
@xuri xuri moved this to Features in Excelize v2.9.1 Dec 28, 2024
@xuri xuri closed this as completed in 3f6ecff Dec 29, 2024
@xuri
Copy link
Member

xuri commented Dec 29, 2024

Thanks for your issue. I added a new field DataLabel in the ChartSeries data type to support to sets the format of the chart series data label. For 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:  15,
            },
        },
    }

    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,
                },
+               DataLabel: excelize.ChartDataLabel{
+                   Font: excelize.Font{
+                       Size: 22,
+                   },
+                   Fill: excelize.Fill{Type: "pattern", Color: []string{"C7EECF"}, Pattern: 1},
+               },
            },
            {
                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))
}

This feature will be released on the next version.

@xuri xuri removed the in progress Working in progress label Dec 29, 2024
@water160
Copy link
Author

water160 commented Jan 5, 2025

This feature really helps a lot, Thanks!

@xuri
Copy link
Member

xuri commented Jan 6, 2025

You're welcome. If you have enjoyed the productivity of using my open-source projects, consider sponsorship though GitHub sponsors, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Status: Features
Development

No branches or pull requests

2 participants