Plot
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/plot.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"math"
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
)
func init() {
registerSection("plot", "charts", func() core.Widget {
var wave, damped []kit.PlotPoint
for i := 0; i <= 200; i++ {
x := float64(i) / 10
wave = append(wave, kit.PlotPoint{X: x, Y: math.Sin(x)})
damped = append(damped, kit.PlotPoint{X: x, Y: math.Exp(-x/8) * math.Cos(x*1.3)})
}
lines := kit.Plot(kit.PlotSeries{Name: "sin(x)", Points: wave}, kit.PlotSeries{Name: demoText("Damped oscillation", "衰减振荡"), Points: damped}).
Lines().Title(demoText("Scroll to zoom · Drag to pan · Double-click to reset", "滚轮缩放 · 拖动平移 · 双击复位"))
var scatter []kit.PlotPoint
for i := 0; i < 120; i++ {
x := float64(i%40) + float64(i)/7
scatter = append(scatter, kit.PlotPoint{X: x, Y: 30 + x*1.8 + 12*math.Sin(float64(i)*1.7)})
}
dots := kit.Plot(kit.PlotSeries{Name: demoText("Order amount vs quantity", "订单金额 vs 数量"), Points: scatter}).Title(demoText("Scatter plot: hover for values", "散点:悬停查看数值")).Height(200)
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(16).W(el.Dp(620)).MaxW(el.Full).Child(lines.Render(cx), dots.Render(cx))
}))
})
}
Numerical x / y plots for interactive exploration of data.
p := kit.Plot(
kit.PlotSeries{Name: "sin(x)", Points: wave},
kit.PlotSeries{Name: "Measured", Points: samples},
).Lines().Title("Signal")
- Mouse: The scroll wheel zooms with the pointer position as the center; drag and pan; double-click or click "Reset" to restore the display of all data.
- Keyboard: After getting focus, + / − zoom, arrow keys pan, 0 reset.
- When hovering, the nearest data point within the 12dp range will be selected, enlarged and displayed, and a prompt box will pop up to display the name and coordinates.
- The default is to draw scattered points;
Lines()uses 2dp lines to connect the points and only magnifies the selected point. The color of the dot istheme.Chart, and there is a stroke of the background color outside, which can be distinguished even when overlapping. View()/SetView()reads or sets the current visible range,Reset()adapts to all data,SetSeriesreplaces data, andFormat(fn)sets the numerical writing method.
Agent: Role figure; "Reset" is a button.
Verify: go run ./examples/components -section plot, add -theme dark to check the dark theme.
Construction and SetSeries copy series and point slices; points containing NaN/Inf coordinates do not participate in fitting, picking, or drawing, and the polyline breaks there. Empty data and completely invalid data display an empty status. SetView Ignore non-finite or inverted ranges.
Zooming keeps the endpoints limited and orderly, and stops zooming in after reaching the lower limit of relative accuracy (approximately 10⁻¹² of the current coordinate scale); if panning or zooming out will overflow, the original range will be retained. The scroll wheel single magnification limit is 0.01–100. Scientific notation is used by default for very large or very small values.
SetDisabled(true) disables the pointer, reset and keyboard; canceling dragging or disabling the area during dragging will return to the view where dragging started. Replacing data will clear old hover selections. Titles and legends wrap in narrow windows so that the tip width does not exceed the plot area.
Custom charts can use the independent ui/plot package, see Public Drawing Basics.