WIP charts2 parts
This commit is contained in:
45 files changed
+1519
-133
No files matched your search
+68
-46
@@ -44,25 +44,39 @@ func (r ChartResult) ValueChanged() bool { return r.value }
|
||||
// Toggled returns the legend series switched in this frame.
|
||||
func (r ChartResult) Toggled() (string, bool) { return r.toggled, r.toggled != "" }
|
||||
|
||||
// chartSpec is one chart's drawing and behavior.
|
||||
type chartSpec struct {
|
||||
// n counts the marks keys step through; 0 is empty.
|
||||
n int
|
||||
keys []ChartLegendItem
|
||||
// toggle lets legend entries hide and show series.
|
||||
toggle bool
|
||||
draw func(p *ui.Painter, r ui.Rect, active int)
|
||||
// pick returns the mark at (x, y), or -1.
|
||||
pick func(r ui.Rect, x, y float32) int
|
||||
readout func(i int) string
|
||||
// move steps from mark i; nil steps in order.
|
||||
move func(i int, key ui.Key) int
|
||||
// activate runs on Enter, Space or a click.
|
||||
activate func(i int)
|
||||
// anchor places mark i's tooltip in plot space; nil uses the plot's top middle.
|
||||
anchor func(r ui.Rect, i int) (x, y float32)
|
||||
// tip writes mark i's tooltip; nil splits the readout.
|
||||
tip func(i int) []ChartTooltipLine
|
||||
// ChartSpec is a chart's marks and drawing, for Chart; r is the chart box in each callback.
|
||||
type ChartSpec struct {
|
||||
// Marks counts what the keys step through; 0 shows the empty state.
|
||||
Marks int
|
||||
// Legend lists series keys above the plot.
|
||||
Legend []ChartLegendItem
|
||||
// Hidden, when set, makes legend keys check boxes switching series off.
|
||||
Hidden *map[string]bool
|
||||
// Draw paints the plot; active is the read mark, or -1.
|
||||
Draw func(p *ui.Painter, r ui.Rect, active int)
|
||||
// Pick returns the mark at (x, y), or -1; required.
|
||||
Pick func(r ui.Rect, x, y float32) int
|
||||
// Readout describes mark i; it is shown, announced and the tooltip's default.
|
||||
Readout func(i int) string
|
||||
// Move steps from mark i for an arrow key; nil steps in order.
|
||||
Move func(i int, key ui.Key) int
|
||||
// Activate runs on Enter, Space or a click on a mark.
|
||||
Activate func(i int)
|
||||
// Anchor places mark i's tooltip for the keyboard; nil uses the top middle.
|
||||
Anchor func(r ui.Rect, i int) (x, y float32)
|
||||
// Tooltip writes mark i's tooltip; nil splits Readout at " · ".
|
||||
Tooltip func(i int) []ChartTooltipLine
|
||||
// Input takes the plot's other input, as a ChartAnnotation's drags.
|
||||
Input func(plot *ui.Element, r ui.Rect)
|
||||
}
|
||||
|
||||
// Chart frames a custom chart: legend, focus, keys, readout, tooltip and empty states.
|
||||
func Chart(c *ui.Context, frame ChartFrame, spec ChartSpec) ChartResult {
|
||||
if spec.Marks < 0 || spec.Draw == nil || spec.Pick == nil || spec.Readout == nil {
|
||||
panic("mujica: Chart needs Marks ≥ 0, Draw, Pick and Readout")
|
||||
}
|
||||
res, _ := newChart(c, frame).build(spec)
|
||||
return res
|
||||
}
|
||||
|
||||
type chartState struct {
|
||||
@@ -92,25 +106,29 @@ func newChart(c *ui.Context, frame ChartFrame) *chart {
|
||||
return ch
|
||||
}
|
||||
|
||||
// toggles returns the legend's hidden set when series can be switched off.
|
||||
func (ch *chart) toggles(on bool) *map[string]bool {
|
||||
if on {
|
||||
return &ch.st.hidden
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// hidden reports whether the legend switched series name off.
|
||||
func (ch *chart) hidden(name string) bool { return ch.st.hidden[name] }
|
||||
|
||||
// build lays out legend, plot and readout; returns the plot.
|
||||
func (ch *chart) build(spec chartSpec) (ChartResult, *ui.Element) {
|
||||
func (ch *chart) build(spec ChartSpec) (ChartResult, *ui.Element) {
|
||||
c, k, st := ch.c, Tokens(ch.c), ch.st
|
||||
u := Density(c).Unit()
|
||||
var plot *ui.Element
|
||||
ch.res.Element.Children(func() {
|
||||
if len(spec.keys) > 0 {
|
||||
var hidden *map[string]bool
|
||||
if spec.toggle {
|
||||
hidden = &st.hidden
|
||||
}
|
||||
if name, ok := ChartLegend(c, hidden, spec.keys, ChartLegendOptions{}).Toggled(); ok {
|
||||
if len(spec.Legend) > 0 {
|
||||
if name, ok := ChartLegend(c, spec.Hidden, spec.Legend, ChartLegendOptions{}).Toggled(); ok {
|
||||
ch.res.toggled = name
|
||||
}
|
||||
}
|
||||
if ch.frame.Status == DataLoading || ch.frame.Status == DataFailed || spec.n == 0 {
|
||||
if ch.frame.Status == DataLoading || ch.frame.Status == DataFailed || spec.Marks == 0 {
|
||||
st.focus = -1
|
||||
ch.res.retry = ChartEmptyState(c, ch.frame.Status, ChartEmptyStateOptions{Height: ch.frame.Height, Error: ch.frame.Error, Empty: ch.frame.Empty}).Changed()
|
||||
ch.res.Point = -1
|
||||
@@ -120,34 +138,38 @@ func (ch *chart) build(spec chartSpec) (ChartResult, *ui.Element) {
|
||||
box := ui.Box(c).Height(ch.frame.Height).FillWidth().MinWidth(0)
|
||||
plot = box.Focusable().Label(ch.frame.Label).Role(ui.RoleGroup)
|
||||
internal.Ring(plot, k.Focus, theme.ControlRadius)
|
||||
st.focus = min(st.focus, spec.n-1)
|
||||
st.focus = min(st.focus, spec.Marks-1)
|
||||
ch.input(plot, spec)
|
||||
if spec.Input != nil && !plot.IsDisabled() {
|
||||
b := plot.Bounds()
|
||||
spec.Input(plot, ui.Rect{W: b.W, H: b.H})
|
||||
}
|
||||
active := -1
|
||||
b := plot.Bounds()
|
||||
local := ui.Rect{W: b.W, H: b.H}
|
||||
x, y, over := plot.PointerPosition()
|
||||
if over && !plot.IsDisabled() {
|
||||
active = spec.pick(local, x, y)
|
||||
active = spec.Pick(local, x, y)
|
||||
}
|
||||
if active < 0 && plot.Focused() {
|
||||
if active < 0 && plot.Focused() && st.focus >= 0 {
|
||||
active = st.focus
|
||||
x, y = local.W/2, 0
|
||||
if spec.anchor != nil {
|
||||
x, y = spec.anchor(local, active)
|
||||
if spec.Anchor != nil {
|
||||
x, y = spec.Anchor(local, active)
|
||||
}
|
||||
}
|
||||
ch.res.Point = active
|
||||
if active >= 0 && b.W > 0 {
|
||||
lines := chartTipLines(spec.readout(active))
|
||||
if spec.tip != nil {
|
||||
lines = spec.tip(active)
|
||||
lines := chartTipLines(spec.Readout(active))
|
||||
if spec.Tooltip != nil {
|
||||
lines = spec.Tooltip(active)
|
||||
}
|
||||
ChartTooltip(c, b.X+x, b.Y+y, lines)
|
||||
}
|
||||
plot.Draw(func(p *ui.Painter, r ui.Rect) { spec.draw(p, r, active) })
|
||||
plot.Draw(func(p *ui.Painter, r ui.Rect) { spec.Draw(p, r, active) })
|
||||
text, col := msg(c, "chart.hint"), k.TextMuted
|
||||
if active >= 0 {
|
||||
text, col = spec.readout(active), k.Text
|
||||
text, col = spec.Readout(active), k.Text
|
||||
}
|
||||
ui.Text(c, text).FontSize(FontSize(c, theme.CaptionSize)).TextColor(col).Role(ui.RoleStatus).SingleLine().Height(u * 5)
|
||||
})
|
||||
@@ -157,7 +179,7 @@ func (ch *chart) build(spec chartSpec) (ChartResult, *ui.Element) {
|
||||
}
|
||||
|
||||
// input moves the keyboard mark, and takes clicks on marks.
|
||||
func (ch *chart) input(plot *ui.Element, spec chartSpec) {
|
||||
func (ch *chart) input(plot *ui.Element, spec ChartSpec) {
|
||||
st := ch.st
|
||||
if plot.IsDisabled() {
|
||||
return
|
||||
@@ -170,19 +192,19 @@ func (ch *chart) input(plot *ui.Element, spec chartSpec) {
|
||||
switch {
|
||||
case st.focus < 0:
|
||||
to = 0
|
||||
case spec.move != nil:
|
||||
to = spec.move(st.focus, key)
|
||||
case spec.Move != nil:
|
||||
to = spec.Move(st.focus, key)
|
||||
case key == ui.KeyLeft || key == ui.KeyUp:
|
||||
to = max(st.focus-1, 0)
|
||||
default:
|
||||
to = min(st.focus+1, spec.n-1)
|
||||
to = min(st.focus+1, spec.Marks-1)
|
||||
}
|
||||
}
|
||||
if plot.Shortcut(0, ui.KeyHome) {
|
||||
to = 0
|
||||
}
|
||||
if plot.Shortcut(0, ui.KeyEnd) {
|
||||
to = spec.n - 1
|
||||
to = spec.Marks - 1
|
||||
}
|
||||
if plot.Shortcut(0, ui.KeyEscape) {
|
||||
to = -1
|
||||
@@ -190,7 +212,7 @@ func (ch *chart) input(plot *ui.Element, spec chartSpec) {
|
||||
if to != st.focus {
|
||||
st.focus = to
|
||||
if to >= 0 {
|
||||
ch.c.Announce(spec.readout(to))
|
||||
ch.c.Announce(spec.Readout(to))
|
||||
}
|
||||
slog.Debug("mujica: chart focus", "label", ch.frame.Label, "mark", to)
|
||||
}
|
||||
@@ -199,12 +221,12 @@ func (ch *chart) input(plot *ui.Element, spec chartSpec) {
|
||||
plot.Focus()
|
||||
x, y, _ := plot.PointerPosition()
|
||||
b := plot.Bounds()
|
||||
if i := spec.pick(ui.Rect{W: b.W, H: b.H}, x, y); i >= 0 {
|
||||
if i := spec.Pick(ui.Rect{W: b.W, H: b.H}, x, y); i >= 0 {
|
||||
st.focus, act = i, true
|
||||
}
|
||||
}
|
||||
if act && spec.activate != nil {
|
||||
spec.activate(st.focus)
|
||||
if act && spec.Activate != nil {
|
||||
spec.Activate(st.focus)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user