1package ui23import (4 "strconv"5 "strings"67 "src.rest/cat/012/internal/sheet"8)910// The chart editor's axis bar: the ends of the value axis, typed on the11// context line (blank for automatic), a log scale, gridlines and where12// the legend goes. Enter and Esc go back to the main bar, keeping the13// changes; Esc there undoes them all.1415// Axis bar keys, also the chips' mouse targets.16const (17 axisMin = "n"18 axisMax = "x"19 axisLog = "l"20 axisGrid = "g"21 axisLegend = "p"22)2324func (e *chartEditor) axisParts() []editorPart {25 c := e.chart()26 end := func(name string, v float64, set bool, key string) editorPart {27 text := name + " auto"28 if set {29 text = name + " " + strconv.FormatFloat(v, 'g', 10, 64)30 }31 return e.chip(set, text, key)32 }33 return []editorPart{34 end("Min", c.Min, c.HasMin, axisMin),35 end("Max", c.Max, c.HasMax, axisMax),36 e.chip(c.Log, "Log", axisLog),37 e.chip(!c.NoGrid, "Gridlines", axisGrid),38 e.chip(c.Legend != sheet.LegendNone, "Legend "+c.Legend.String(), axisLegend),39 }40}4142func (e *chartEditor) axisKey(key string) {43 switch key {44 case "enter", "esc":45 e.axes = false46 case axisMin:47 e.editAxisEnd("Axis minimum (blank for auto):", func(c *sheet.Chart) (*float64, *bool) { return &c.Min, &c.HasMin })48 case axisMax:49 e.editAxisEnd("Axis maximum (blank for auto):", func(c *sheet.Chart) (*float64, *bool) { return &c.Max, &c.HasMax })50 case axisLog:51 e.set(func(c *sheet.Chart) { c.Log = !c.Log })52 case axisGrid:53 e.set(func(c *sheet.Chart) { c.NoGrid = !c.NoGrid })54 case axisLegend:55 e.set(func(c *sheet.Chart) { c.Legend = (c.Legend + 1) % 3 })56 }57}5859// editAxisEnd asks for an end of the value axis, as a number the way60// cells take one ("500", "$1,200", "10%"); blank makes it automatic.61func (e *chartEditor) editAxisEnd(label string, end func(*sheet.Chart) (*float64, *bool)) {62 c := e.chart()63 v, set := end(&c)64 initial := ""65 if *set {66 initial = strconv.FormatFloat(*v, 'g', -1, 64)67 }68 e.m.closeOverlay()69 e.askAxisEnd(label, label, initial, end)70}7172// askAxisEnd asks with the prompt label, and asks again, saying so, until73// the answer is a number or blank.74func (e *chartEditor) askAxisEnd(label, question, initial string, end func(*sheet.Chart) (*float64, *bool)) {75 e.m.askText(label, initial, func(text string) {76 text = strings.TrimSpace(text)77 n, _, ok := sheet.ParseValue(text)78 switch {79 case text == "":80 e.set(func(c *sheet.Chart) { _, set := end(c); *set = false })81 case !ok:82 e.askAxisEnd("Not a number. "+question, question, text, end)83 return84 default:85 e.set(func(c *sheet.Chart) { v, set := end(c); *v, *set = n, true })86 }87 e.reopen()88 }, e.reopen)89}9091func (e *chartEditor) axisStatus() string {92 width, _ := e.m.size()93 return fitHints(e.m.styles(), width, []string{"N", "min", "X", "max", "L", "log", "G", "grid", "P", "legend", "Esc", "back"})94}