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

Way to get "pretty" y-axis number ticks

Open grokify opened this issue 4 years ago • 3 comments

I have a bar chart with the following auto-generated y-axis ticks.

Is there a way to make it more visually appealing including:

  1. nice rounded number ticks: e,g. 10,000,000, 20,000,000, 30,000,000 ... 120,000,000, 140,000,000
  2. right aligned numbers so the commas line up

For the first item, I'm looking into a way to auto-generate those ticks myself, but was wondering if a solution was already available.

wchart_y-axis

grokify avatar Mar 06 '20 21:03 grokify

I now have a initial implementation here:

https://godoc.org/github.com/grokify/gotilla/math/mathutil#PrettyTicks

This gives me the following:

yaxis_commify

I also have an abbreviation function now for the following:

https://godoc.org/github.com/grokify/gotilla/strconv/strconvutil#Int64Abbreviation

yaxis_abbreviations

grokify avatar Mar 08 '20 20:03 grokify

this is great

117 avatar Mar 25 '20 06:03 117

I use this to get nice ticks:

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
)
YAxis: chart.YAxis{
	ValueFormatter: func(v interface{}) string {
		if vf, isFloat := v.(float64); isFloat {
			p := message.NewPrinter(language.English)
			vi := int64(vf)
			return p.Sprintf("%d", vi)
		}
		return ""
	},
}

coalaura avatar Sep 30 '20 09:09 coalaura