cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / sheet / chartopts.go
chartopts.go 3.0K
1package sheet23import (4 "math"5 "slices"6 "strings"7)89// ChartOptions are a chart's settings beyond its type and data, as in10// the Customize tab of Sheets' chart editor. The zero value is Sheets'11// default: not stacked, no trend line, an automatic linear value axis12// with gridlines, and the legend at the bottom. Every field is a plain13// value, so charts stay comparable with ==.14type ChartOptions struct {15 // Stack stacks the series of column, bar and area charts.16 Stack ChartStack17 // Trend draws a linear trend line through each series of a scatter18 // chart.19 Trend bool20 // Min and Max fix the ends of the value axis when HasMin and HasMax21 // are set; otherwise the axis fits the data.22 Min, Max float6423 HasMin, HasMax bool24 // Log puts the value axis on a logarithmic scale, leaving out values25 // that aren't positive.26 Log bool27 // NoGrid hides the gridlines.28 NoGrid bool29 // Legend is where the legend goes.30 Legend ChartLegend31}3233// ChartStack is how the series of a chart pile up.34type ChartStack int3536const (37 StackNone ChartStack = iota // side by side38 StackNormal // on top of each other39 StackPercent // on top of each other, as shares of 100%40)4142var chartStackNames = [...]string{StackNone: "", StackNormal: "stacked", StackPercent: "percent"}4344// String is the stacking as files store it, "" for none.45func (s ChartStack) String() string {46 if s < 0 || int(s) >= len(chartStackNames) {47 return ""48 }49 return chartStackNames[s]50}5152// Title is the stacking as the chart editor shows it.53func (s ChartStack) Title() string {54 switch s {55 case StackNormal:56 return "Stacked"57 case StackPercent:58 return "100% stacked"59 }60 return "Not stacked"61}6263// ParseChartStack parses a stacking as String writes it.64func ParseChartStack(s string) (ChartStack, bool) {65 i := slices.Index(chartStackNames[:], strings.ToLower(s))66 return ChartStack(max(i, 0)), i >= 067}6869// Stackable reports whether charts of type t can stack their series.70func (t ChartType) Stackable() bool {71 return t == ChartColumn || t == ChartBar || t == ChartArea72}7374// ChartLegend is where a chart's legend goes.75type ChartLegend int7677const (78 LegendBottom ChartLegend = iota // under the plot, centered79 LegendRight // a column right of the plot80 LegendNone // no legend81)8283var chartLegendNames = [...]string{LegendBottom: "bottom", LegendRight: "right", LegendNone: "none"}8485// String is the position as files store it.86func (l ChartLegend) String() string {87 if l < 0 || int(l) >= len(chartLegendNames) {88 return chartLegendNames[0]89 }90 return chartLegendNames[l]91}9293// ParseChartLegend parses a position as String writes it.94func ParseChartLegend(s string) (ChartLegend, bool) {95 i := slices.Index(chartLegendNames[:], strings.ToLower(s))96 return ChartLegend(max(i, 0)), i >= 097}9899// axisEnd is an optional end of the value axis as files store it: nil100// for automatic.101func axisEnd(v float64, ok bool) *float64 {102 if !ok || math.IsNaN(v) || math.IsInf(v, 0) {103 return nil104 }105 return &v106}