1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-27 13:48:49 +08:00

The LineChart widget now supports zoom.

This commit is contained in:
Jakub Sobon 2019-02-18 01:28:30 -05:00
parent 4a7c5d9f48
commit 72b3ac4ff9
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
4 changed files with 288 additions and 34 deletions

View File

@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
"roll" through the linechart.
- The LineChart widget now has a method that returns the observed capacity of
the LineChart the last time Draw was called.
- The LineChart widget now supports zoom of the content triggered by mouse
events.
- The Text widget now has a Write option that atomically replaces the entire
text content.

View File

@ -31,6 +31,7 @@ import (
"github.com/mum4k/termdash/terminalapi"
"github.com/mum4k/termdash/widgetapi"
"github.com/mum4k/termdash/widgets/linechart/axes"
"github.com/mum4k/termdash/widgets/linechart/zoom"
)
// seriesValues represent values stored in the series.
@ -70,6 +71,10 @@ func newSeriesValues(values []float64) *seriesValues {
// The Y axis will be sized so that it can conveniently accommodate the largest
// value among all the labeled line charts. This determines the used scale.
//
// LineChart supports mouse based zoom, zooming is achieved by either
// highlighting an area on the graph (left mouse clicking and dragging) or by
// using the mouse scroll button.
//
// Implements widgetapi.Widget. This object is thread-safe.
type LineChart struct {
// mu protects the LineChart widget.
@ -91,6 +96,9 @@ type LineChart struct {
// xLabels that were provided on a call to Series.
xLabels map[int]string
// zoom tracks the zooming of the X axis.
zoom *zoom.Tracker
}
// New returns a new line chart widget.
@ -339,23 +347,18 @@ func (lc *LineChart) drawAxes(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.YD
return nil
}
// brailleCvs returns a braille canvas sized so that it fits between the axes
// and the canvas borders.
func (lc *LineChart) brailleCvs(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.YDetails) (*braille.Canvas, error) {
// The area available to the graph.
graphAr := image.Rect(yd.Start.X+1, yd.Start.Y, cvs.Area().Max.X, xd.End.Y)
bc, err := braille.New(graphAr)
if err != nil {
return nil, fmt.Errorf("braille.New => %v", err)
}
return bc, nil
// graphAr returns the area available for the graph itself sized so that it
// fits between the axes and the canvas borders.
func (lc *LineChart) graphAr(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.YDetails) image.Rectangle {
return image.Rect(yd.Start.X+1, yd.Start.Y, cvs.Area().Max.X, xd.End.Y)
}
// drawSeries draws the graph representing the stored series.
// Returns XDetails that might be adjusted to not start at zero value if some
// of the series didn't fit the graphs and XAxisUnscaled was provided.
func (lc *LineChart) drawSeries(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.YDetails) (*axes.XDetails, error) {
bc, err := lc.brailleCvs(cvs, xd, yd)
graphAr := lc.graphAr(cvs, xd, yd)
bc, err := braille.New(graphAr)
if err != nil {
return nil, err
}
@ -365,6 +368,19 @@ func (lc *LineChart) drawSeries(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.
return nil, err
}
if lc.zoom == nil {
z, err := zoom.New(xdForCap, cvs.Area(), graphAr, zoom.ScrollStep(lc.opts.zoomStepPercent))
if err != nil {
return nil, err
}
lc.zoom = z
} else {
if err := lc.zoom.Update(xdForCap, cvs.Area(), graphAr); err != nil {
return nil, err
}
}
xdZoomed := lc.zoom.Zoom()
var names []string
for name := range lc.series {
names = append(names, name)
@ -383,7 +399,7 @@ func (lc *LineChart) drawSeries(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.
var prev float64
for i := 1; i < len(sv.values); i++ {
prev = sv.values[i-1]
if i < int(xdForCap.Scale.Min.Value)+1 || i > int(xdForCap.Scale.Max.Value) {
if i < int(xdZoomed.Scale.Min.Value)+1 || i > int(xdZoomed.Scale.Max.Value) {
// Don't draw lines for values that aren't supposed to be visible.
// These are either values outside of the current zoom or
// values at the beginning of a series that falls before athe
@ -392,13 +408,13 @@ func (lc *LineChart) drawSeries(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.
continue
}
startX, err := xdForCap.Scale.ValueToPixel(i - 1)
startX, err := xdZoomed.Scale.ValueToPixel(i - 1)
if err != nil {
return nil, fmt.Errorf("failure for series %v[%d], xdForCap.Scale.ValueToPixel => %v", name, i-1, err)
return nil, fmt.Errorf("failure for series %v[%d], xdZoomed.Scale.ValueToPixel => %v", name, i-1, err)
}
endX, err := xdForCap.Scale.ValueToPixel(i)
endX, err := xdZoomed.Scale.ValueToPixel(i)
if err != nil {
return nil, fmt.Errorf("failure for series %v[%d], xdForCap.Scale.ValueToPixel => %v", name, i, err)
return nil, fmt.Errorf("failure for series %v[%d], xdZoomed.Scale.ValueToPixel => %v", name, i, err)
}
startY, err := yd.Scale.ValueToPixel(prev)
@ -420,10 +436,24 @@ func (lc *LineChart) drawSeries(cvs *canvas.Canvas, xd *axes.XDetails, yd *axes.
}
}
}
if highlight, hRange := lc.zoom.Highlight(); highlight {
if err := lc.highlightRange(bc, hRange); err != nil {
return nil, err
}
}
if err := bc.CopyTo(cvs); err != nil {
return nil, fmt.Errorf("bc.Apply => %v", err)
}
return xdForCap, nil
return xdZoomed, nil
}
// highlightRange highlights the range of X columns on the braille canvas.
func (lc *LineChart) highlightRange(bc *braille.Canvas, hRange *zoom.Range) error {
cellAr := bc.CellArea()
ar := image.Rect(hRange.Start, cellAr.Min.Y, hRange.End, cellAr.Max.Y)
return bc.SetAreaCellOpts(ar, cell.BgColor(lc.opts.zoomHightlightColor))
}
// Keyboard implements widgetapi.Widget.Keyboard.
@ -433,7 +463,10 @@ func (lc *LineChart) Keyboard(k *terminalapi.Keyboard) error {
// Mouse implements widgetapi.Widget.Mouse.
func (lc *LineChart) Mouse(m *terminalapi.Mouse) error {
return errors.New("the LineChart widget doesn't support mouse events")
if lc.zoom == nil {
return nil
}
return lc.zoom.Mouse(m)
}
// minSize determines the minimum required size to draw the line chart.
@ -457,6 +490,7 @@ func (lc *LineChart) Options() widgetapi.Options {
return widgetapi.Options{
MinimumSize: lc.minSize(),
WantMouse: true,
}
}

View File

@ -26,7 +26,9 @@ import (
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/draw"
"github.com/mum4k/termdash/draw/testdraw"
"github.com/mum4k/termdash/mouse"
"github.com/mum4k/termdash/terminal/faketerm"
"github.com/mum4k/termdash/terminalapi"
"github.com/mum4k/termdash/widgetapi"
)
@ -42,6 +44,22 @@ func TestLineChartDraws(t *testing.T) {
wantWriteErr bool
wantDrawErr bool
}{
{
desc: "fails with scroll step too low",
canvas: image.Rect(0, 0, 3, 4),
opts: []Option{
ZoomStepPercent(0),
},
wantErr: true,
},
{
desc: "fails with scroll step too high",
canvas: image.Rect(0, 0, 3, 4),
opts: []Option{
ZoomStepPercent(101),
},
wantErr: true,
},
{
desc: "fails with custom scale where min is NaN",
canvas: image.Rect(0, 0, 3, 4),
@ -1136,6 +1154,155 @@ func TestLineChartDraws(t *testing.T) {
return ft
},
},
{
desc: "highlights area for zoom",
canvas: image.Rect(0, 0, 20, 10),
writes: func(lc *LineChart) error {
if err := lc.Series("first", []float64{0, 100}); err != nil {
return err
}
// Draw once so zoom tracker is initialized.
cvs := testcanvas.MustNew(image.Rect(0, 0, 20, 10))
if err := lc.Draw(cvs); err != nil {
return err
}
return lc.Mouse(&terminalapi.Mouse{
Position: image.Point{6, 5},
Button: mouse.ButtonLeft,
})
},
wantCapacity: 28,
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
c := testcanvas.MustNew(ft.Area())
// Y and X axis.
lines := []draw.HVLine{
{Start: image.Point{5, 0}, End: image.Point{5, 8}},
{Start: image.Point{5, 8}, End: image.Point{19, 8}},
}
testdraw.MustHVLines(c, lines)
// Value labels.
testdraw.MustText(c, "0", image.Point{4, 7})
testdraw.MustText(c, "51.68", image.Point{0, 3})
testdraw.MustText(c, "0", image.Point{6, 9})
testdraw.MustText(c, "1", image.Point{19, 9})
// Braille line.
graphAr := image.Rect(6, 0, 20, 8)
bc := testbraille.MustNew(graphAr)
testdraw.MustBrailleLine(bc, image.Point{0, 31}, image.Point{26, 0})
// Highlighted area for zoom.
testbraille.MustSetAreaCellOpts(bc, image.Rect(0, 0, 1, 8), cell.BgColor(cell.ColorNumber(235)))
testbraille.MustCopyTo(bc, c)
testcanvas.MustApply(c, ft)
return ft
},
},
{
desc: "highlights area for zoom to a custom color",
opts: []Option{
ZoomHightlightColor(cell.ColorNumber(13)),
},
canvas: image.Rect(0, 0, 20, 10),
writes: func(lc *LineChart) error {
if err := lc.Series("first", []float64{0, 100}); err != nil {
return err
}
// Draw once so zoom tracker is initialized.
cvs := testcanvas.MustNew(image.Rect(0, 0, 20, 10))
if err := lc.Draw(cvs); err != nil {
return err
}
return lc.Mouse(&terminalapi.Mouse{
Position: image.Point{6, 5},
Button: mouse.ButtonLeft,
})
},
wantCapacity: 28,
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
c := testcanvas.MustNew(ft.Area())
// Y and X axis.
lines := []draw.HVLine{
{Start: image.Point{5, 0}, End: image.Point{5, 8}},
{Start: image.Point{5, 8}, End: image.Point{19, 8}},
}
testdraw.MustHVLines(c, lines)
// Value labels.
testdraw.MustText(c, "0", image.Point{4, 7})
testdraw.MustText(c, "51.68", image.Point{0, 3})
testdraw.MustText(c, "0", image.Point{6, 9})
testdraw.MustText(c, "1", image.Point{19, 9})
// Braille line.
graphAr := image.Rect(6, 0, 20, 8)
bc := testbraille.MustNew(graphAr)
testdraw.MustBrailleLine(bc, image.Point{0, 31}, image.Point{26, 0})
// Highlighted area for zoom.
testbraille.MustSetAreaCellOpts(bc, image.Rect(0, 0, 1, 8), cell.BgColor(cell.ColorNumber(13)))
testbraille.MustCopyTo(bc, c)
testcanvas.MustApply(c, ft)
return ft
},
},
{
desc: "zooms in on scroll up",
opts: []Option{
ZoomStepPercent(50),
},
canvas: image.Rect(0, 0, 20, 10),
writes: func(lc *LineChart) error {
if err := lc.Series("first", []float64{0, 25, 75, 100}); err != nil {
return err
}
// Draw once so zoom tracker is initialized.
cvs := testcanvas.MustNew(image.Rect(0, 0, 20, 10))
if err := lc.Draw(cvs); err != nil {
return err
}
return lc.Mouse(&terminalapi.Mouse{
Position: image.Point{8, 5},
Button: mouse.ButtonWheelUp,
})
},
wantCapacity: 28,
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
c := testcanvas.MustNew(ft.Area())
// Y and X axis.
lines := []draw.HVLine{
{Start: image.Point{5, 0}, End: image.Point{5, 8}},
{Start: image.Point{5, 8}, End: image.Point{19, 8}},
}
testdraw.MustHVLines(c, lines)
// Value labels.
testdraw.MustText(c, "0", image.Point{4, 7})
testdraw.MustText(c, "51.68", image.Point{0, 3})
testdraw.MustText(c, "0", image.Point{6, 9})
testdraw.MustText(c, "1", image.Point{12, 9})
testdraw.MustText(c, "2", image.Point{19, 9})
// Braille line.
graphAr := image.Rect(6, 0, 20, 8)
bc := testbraille.MustNew(graphAr)
testdraw.MustBrailleLine(bc, image.Point{0, 31}, image.Point{13, 23})
testdraw.MustBrailleLine(bc, image.Point{13, 23}, image.Point{27, 8})
testbraille.MustCopyTo(bc, c)
testcanvas.MustApply(c, ft)
return ft
},
},
}
for _, tc := range tests {
@ -1198,6 +1365,26 @@ func TestLineChartDraws(t *testing.T) {
}
}
func TestKeyboard(t *testing.T) {
lc, err := New()
if err != nil {
t.Fatalf("New => unexpected error: %v", err)
}
if err := lc.Keyboard(&terminalapi.Keyboard{}); err == nil {
t.Errorf("Keyboard => got nil err, wanted one")
}
}
func TestMouseDoesNothingWithoutZoomTracker(t *testing.T) {
lc, err := New()
if err != nil {
t.Fatalf("New => unexpected error: %v", err)
}
if err := lc.Mouse(&terminalapi.Mouse{}); err != nil {
t.Errorf("Mouse => unexpected error: %v", err)
}
}
func TestOptions(t *testing.T) {
tests := []struct {
desc string
@ -1210,6 +1397,7 @@ func TestOptions(t *testing.T) {
desc: "reserves space for axis without series",
want: widgetapi.Options{
MinimumSize: image.Point{3, 4},
WantMouse: true,
},
},
{
@ -1219,6 +1407,7 @@ func TestOptions(t *testing.T) {
},
want: widgetapi.Options{
MinimumSize: image.Point{5, 4},
WantMouse: true,
},
},
{
@ -1228,6 +1417,7 @@ func TestOptions(t *testing.T) {
},
want: widgetapi.Options{
MinimumSize: image.Point{6, 4},
WantMouse: true,
},
},
{
@ -1240,6 +1430,7 @@ func TestOptions(t *testing.T) {
},
want: widgetapi.Options{
MinimumSize: image.Point{4, 5},
WantMouse: true,
},
},
{
@ -1252,6 +1443,7 @@ func TestOptions(t *testing.T) {
},
want: widgetapi.Options{
MinimumSize: image.Point{5, 7},
WantMouse: true,
},
},
}

View File

@ -20,6 +20,7 @@ import (
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/widgets/linechart/axes"
"github.com/mum4k/termdash/widgets/linechart/zoom"
)
// options.go contains configurable options for LineChart.
@ -32,33 +33,39 @@ type Option interface {
// options stores the provided options.
type options struct {
axesCellOpts []cell.Option
xLabelCellOpts []cell.Option
xLabelOrientation axes.LabelOrientation
yLabelCellOpts []cell.Option
xAxisUnscaled bool
yAxisMode axes.YScaleMode
yAxisCustomScale *customScale
axesCellOpts []cell.Option
xLabelCellOpts []cell.Option
xLabelOrientation axes.LabelOrientation
yLabelCellOpts []cell.Option
xAxisUnscaled bool
yAxisMode axes.YScaleMode
yAxisCustomScale *customScale
zoomHightlightColor cell.Color
zoomStepPercent int
}
// validate validates the provided options.
func (o *options) validate() error {
if o.yAxisCustomScale == nil {
return nil
if o.yAxisCustomScale != nil {
if math.IsNaN(o.yAxisCustomScale.min) || math.IsNaN(o.yAxisCustomScale.max) {
return fmt.Errorf("both the min(%v) and the max(%v) provided as custom Y scale must be valid numbers", o.yAxisCustomScale.min, o.yAxisCustomScale.max)
}
if o.yAxisCustomScale.min >= o.yAxisCustomScale.max {
return fmt.Errorf("the min(%v) must be less than the max(%v) provided as custom Y scale", o.yAxisCustomScale.min, o.yAxisCustomScale.max)
}
}
if math.IsNaN(o.yAxisCustomScale.min) || math.IsNaN(o.yAxisCustomScale.max) {
return fmt.Errorf("both the min(%v) and the max(%v) provided as custom Y scale must be valid numbers", o.yAxisCustomScale.min, o.yAxisCustomScale.max)
}
if o.yAxisCustomScale.min >= o.yAxisCustomScale.max {
return fmt.Errorf("the min(%v) must be less than the max(%v) provided as custom Y scale", o.yAxisCustomScale.min, o.yAxisCustomScale.max)
if got, min, max := o.zoomStepPercent, 1, 100; got < min || got > max {
return fmt.Errorf("invalid ZoomStepPercent %d, must be in range %d <= value <= %d", got, min, max)
}
return nil
}
// newOptions returns a new options instance.
func newOptions(opts ...Option) *options {
opt := &options{}
opt := &options{
zoomHightlightColor: cell.ColorNumber(235),
zoomStepPercent: zoom.DefaultScrollStep,
}
for _, o := range opts {
o.set(opt)
}
@ -168,3 +175,22 @@ func XAxisUnscaled() Option {
opts.xAxisUnscaled = true
})
}
// ZoomHightlightColor sets the background color of the area that is selected
// with mouse in order to zoom the linechart.
// Defaults to color number 235.
func ZoomHightlightColor(c cell.Color) Option {
return option(func(opts *options) {
opts.zoomHightlightColor = c
})
}
// ZoomStepPercent sets the zooming step on each mouse scroll event as the
// percentage of the size of the X axis.
// The value must be in range 0 < value <= 100.
// Defaults to zoom.DefaultScrollStep.
func ZoomStepPercent(perc int) Option {
return option(func(opts *options) {
opts.zoomStepPercent = perc
})
}