plot icon indicating copy to clipboard operation
plot copied to clipboard

Tick display precision should be configurable

Open DavidGamba opened this issue 8 years ago • 4 comments

Commit https://github.com/gonum/plot/commit/2651c0e3640a62df0ece92952610e4a5579cfd95 added a displayPrecision variable in line 18 (https://github.com/gonum/plot/commit/2651c0e3640a62df0ece92952610e4a5579cfd95#diff-d7f174bedb1bfb5e7a82248d40e9b46fR18). This variable is used to format float64 ticks. The default value of 4 causes a small-ish number like 20000 to be rendered as 2e+04. The value should be configurable to allow the user to define if he ever wants scientific notation or not.

I can submit a PR that exposes DefaultDisplayPrecision as a global variable to plot, by changing the const to an exported var but you might have something else in mind. Please let me know how you would like to expose access to that precision without forcing the user to implement his own ticks logic just for the precision. The approach from the wiki example on the comma separated ticks marks based on the default ticks doesn't work because at that point the tick string has already been generated using the displayPrecision.

DavidGamba avatar Feb 09 '17 05:02 DavidGamba

I like this idea, although I'm not a huge fan of using a global variable, as a user may want different precision for different plots or axes.

Would it be better to change the Ticker interface to the following:

type Ticker interface {
    // Ticks returns Ticks in a specified range
    Ticks(min, max float64, format func(v float64, prec int) string) []Tick
}

and then add a field to Axis.Tick that allows for the specification of a custom format function? (If the custom format function was nil, then the default would be used.)

ctessum avatar Feb 09 '17 16:02 ctessum

Sounds like a plan. I'll give it a shot during the weekend hopefully.

DavidGamba avatar Feb 09 '17 16:02 DavidGamba

This also allows the possibility of exporting some of these helpers.

kortschak avatar Feb 09 '17 23:02 kortschak

I created PR #362. I expanded the Ticker interface but did not add the field to Axis because the places where the format was used had no knowledge of Axis so Axis couldn't be checked there. I might be missing something.

DavidGamba avatar May 25 '17 06:05 DavidGamba