go-chart icon indicating copy to clipboard operation
go-chart copied to clipboard

Fix bar chart y label

Open eduardhasanaj opened this issue 3 years ago • 0 comments

This pull request fixes the following issues:

  1. YAxis Label not drawn at all for BarChart.
  2. Add support for label padding

Instead of using BarChart.drawYAxis we can use YAxis.Render method. I am unclear why this was not used in the first place otherwise we have code duplication.

Please note that this branch contains features of an open pull request. PM me after it is approved so I can make any required changes. After the fix we can see the label output

code

package main

//go:generate go run main.go

import (
	"os"

	"github.com/wcharczuk/go-chart/v2"
)

func main() {
	graph := chart.BarChart{
		Title: "Test Bar Chart",
		Background: chart.Style{
			Padding: chart.Box{
				Top: 40,
			},
		},
		Height:   512,
		BarWidth: 60,
		YAxis: chart.YAxis{
			Name: "Label Y",
			NameStyle: chart.Style{
				FontSize:            14,
				Padding:             chart.NewBox(0, 40, 20, 0),
				TextRotationDegrees: 0,
			},
		},
		Bars: []chart.Value{
			{Value: 5.25, Label: "Blue"},
			{Value: 4.88, Label: "Green"},
			{Value: 4.74, Label: "Gray"},
			{Value: 3.22, Label: "Orange"},
			{Value: 3, Label: "Test"},
			{Value: 2.27, Label: "??"},
			{Value: 1, Label: "!!"},
		},
	}

	f, _ := os.Create("output.png")
	defer f.Close()
	graph.Render(chart.PNG, f)
}

eduardhasanaj avatar Oct 29 '21 14:10 eduardhasanaj