Documentation
¶
Overview ¶
Package ntcharts3d provides Z-up 3D charts for Bubble Tea. A Model combines named scatter, surface, bar, or custom series with one camera. Axes provide shared names, numeric ticks, categories, and optional fixed ranges. Charts support plot proportions, back-plane grids, and hover highlights.
A Palette supplies colors; a color domain maps values to those colors. A ColorLegend displays the mapping. RenderMode selects WebGPU, software, or wireframe rendering. ntcharts handles terminal image and glyph output.
Index ¶
- Constants
- type Arrow
- type Axes
- type Axis
- type AxisFrame
- type AxisTick
- type Bars
- type Box
- type Camera
- type ColorDomainSeries
- type ColorLegend
- type ColorLegendMode
- type Emphasis
- type EmphasisFrame
- type Frame
- type Geometry
- type Grid
- type Light
- type Lines
- type Material
- type Model
- func (m *Model) Axes() Axes
- func (m *Model) Bounds() math3d.AABB
- func (m *Model) Camera() Camera
- func (m *Model) Clear() tea.Cmd
- func (m *Model) Close() error
- func (m *Model) ColorDomain() *Range
- func (m *Model) ColorLegendMode() ColorLegendMode
- func (m *Model) Emphasis() Emphasis
- func (m *Model) Err() error
- func (m *Model) Grid() Grid
- func (m *Model) ID() uint64
- func (m *Model) Init() tea.Cmd
- func (m *Model) Pick(x, y int) *PickMsg
- func (m *Model) PictureMode() picture.PictureMode
- func (m *Model) PlotAspect() math3d.Vec3
- func (m *Model) PlotBounds() math3d.AABB
- func (m *Model) RenderMode() RenderMode
- func (m *Model) RequestedRenderMode() RenderMode
- func (m *Model) SetAxes(a Axes) tea.Cmd
- func (m *Model) SetBars(name string, d Bars) tea.Cmd
- func (m *Model) SetCamera(c Camera) tea.Cmd
- func (m *Model) SetColorDomain(r *Range) tea.Cmd
- func (m *Model) SetColorLegend(title string) tea.Cmd
- func (m *Model) SetColorLegendMode(mode ColorLegendMode) tea.Cmd
- func (m *Model) SetColorLegendVisible(visible bool) tea.Cmd
- func (m *Model) SetEmphasis(e Emphasis) tea.Cmd
- func (m *Model) SetGrid(g Grid) tea.Cmd
- func (m *Model) SetLight(l Light) tea.Cmd
- func (m *Model) SetLines(name string, d Lines) tea.Cmd
- func (m *Model) SetPalette(cs []color.Color) tea.Cmd
- func (m *Model) SetPlotAspect(v math3d.Vec3) tea.Cmd
- func (m *Model) SetRenderMode(l RenderMode) tea.Cmd
- func (m *Model) SetScatter(name string, d Scatter) tea.Cmd
- func (m *Model) SetSeries(s Series) tea.Cmd
- func (m *Model) SetSeriesTexture(name string, texture *Texture) tea.Cmd
- func (m *Model) SetSharedColorDomain(enabled bool) tea.Cmd
- func (m *Model) SetSize(w, h int) tea.Cmd
- func (m *Model) SetSurface(name string, d Surface) tea.Cmd
- func (m *Model) SetVectorField(name string, d VectorField) tea.Cmd
- func (m *Model) Snapshot(width, height int) (image.Image, RenderMode, error)
- func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd)
- func (m *Model) View() tea.View
- type Option
- func WithAutoRotate(on bool) Option
- func WithAxes(a Axes) Option
- func WithBackground(c color.Color) Option
- func WithColorDomain(r Range) Option
- func WithColorLegend(title string) Option
- func WithColorLegendMode(mode ColorLegendMode) Option
- func WithEmphasis(e Emphasis) Option
- func WithGrid(g Grid) Option
- func WithKittyID(id int) Option
- func WithKittyMedium(medium picture.KittyMedium) Option
- func WithMaxPoints(n int) Option
- func WithOrtho() Option
- func WithPlotAspect(v math3d.Vec3) Option
- func WithProjection(p Projection) Option
- func WithRenderMode(l RenderMode) Option
- func WithRenderer(r Renderer) Option
- func WithSharedColorDomain(enabled bool) Option
- func WithZoneManager(z *zone.Manager) Option
- type Palette
- type PickMsg
- type Point
- type Projection
- type Range
- type RenderMode
- type Renderer
- type Scatter
- type Series
- type Surface
- type Texture
- type UV
- type VectorField
- type Vertex
Constants ¶
const ( MaxMeshTriangles = (128 << 20) / 48 / 3 MaxMeshIndices = 3 * MaxMeshTriangles )
MaxMeshIndices is the most indices a series' mesh may have, three to a triangle. The GPU renderer keeps a 48-byte record for each index, vertices shared or not, in a storage buffer; 128 MiB is the largest binding that WebGPU promises, and so the largest mesh that can be drawn wherever WebGPU can. MaxMeshTriangles is the same limit, counted in faces.
const MaxLineSegments = 512 * 512
const MaxPickPoints = 200_000
const MaxPoints = 500_000
const MaxVectors = 100_000
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arrow ¶
Arrow is an unlit, screen-facing shaft and triangular head between two world positions. Width is in render pixels (zero defaults to 1). HeadSize is the head length in pixels, 0..64; zero draws only the shaft. Short projections shrink the head to at most 45% of the projected length. End-on arrows appear as a square of Width pixels. Datum identifies the original vector for picking.
type Axes ¶
type Axes struct{ X, Y, Z Axis }
Axes configures the X, Y, and Z axes shared by all series.
type Axis ¶
type Axis struct {
Range *Range // Optional fixed numeric bounds. Geometry outside them is clipped.
Name string
Categories []string // Names at coordinates 0, 1, ...; at most 512 entries.
TickCount int // Target numeric tick count, 2–64; zero uses five.
// Format formats numeric tick values. It runs synchronously when a frame is
// prepared and may run again after camera or size changes. Nil uses numbers.
Format func(float64) string
Hidden, HideName, HideTicks, HideLabels bool
}
Axis configures one shared coordinate axis. The zero value shows a numeric axis with automatic ticks. An empty Name uses X, Y, or Z.
type AxisFrame ¶
AxisFrame is a resolved axis for rendering. Tick labels are already formatted. Renderers must not mutate its ticks. Axes in Frame are ordered X, Y, Z.
type Bars ¶
Bars holds a category grid stored by row. Values are signed heights from zero; Axis.Categories names the columns and rows. SetBars copies all slices.
type Camera ¶
type Camera struct {
Alpha, Beta, Distance float64
Target math3d.Vec3
Projection Projection
AutoRotate bool
ResumeAfter float64 // idle seconds before rotation resumes after input
}
Camera uses degrees for Alpha (elevation) and Beta (azimuth). Distance and Target use normalized units: geometry is centered at the origin and its longest extent spans two units.
func DefaultCamera ¶
func DefaultCamera() Camera
DefaultCamera returns a Camera initialized with some typical default values.
type ColorDomainSeries ¶
type ColorDomainSeries interface {
GeometryWithColorDomain(Palette, float32, float32) (Geometry, error)
}
ColorDomainSeries lets a custom Series map values against a shared color domain.
type ColorLegend ¶
ColorLegend describes a palette and its numeric range for rendering.
type ColorLegendMode ¶
type ColorLegendMode uint8
ColorLegendMode selects a legend in the image or as terminal text beside it.
const ( ColorLegendImage ColorLegendMode = iota ColorLegendText )
type Emphasis ¶
Emphasis controls the foreground highlight for the hovered datum. The zero value draws a point ring or bar outline without a label.
type EmphasisFrame ¶
type EmphasisFrame struct {
Position math3d.Vec3
Radius float32 // Point radius in pixels, before the ring's padding.
Box *Box
Label string
Color color.RGBA
}
EmphasisFrame describes a resolved hover highlight. Box is nil for a point ring.
type Frame ¶
type Frame struct {
Width, Height int
Matrix math3d.Mat4
Revision uint64
TextureRevision uint64
Bounds math3d.AABB // Plot bounds after fixed axis ranges.
Axes [3]AxisFrame // X, Y, Z; separate from cached series geometry.
Geometry []Geometry // Clipped geometry; mesh normals account for plot proportions.
GridLines []Vertex // Dynamic pairs of back-plane grid endpoints.
Emphasis *EmphasisFrame
ColorLegends []ColorLegend
Background color.RGBA
Light Light
// contains filtered or unexported fields
}
Frame is a read-only snapshot. Mesh data changes only when Revision changes; materials may also change when TextureRevision changes.
type Geometry ¶
type Geometry struct {
UV []UV // One per vertex when textured.
Material *Material // Applies to indexed triangles only.
Points []Point
Vertices []Vertex
Indices []uint32
Boxes []Box
Lines []Vertex
LineWidth float32 // Render pixels for Lines; zero defaults to 1.
Arrows []Arrow
Labels []string
Bounds math3d.AABB
ColorMin float32
ColorMax float32
HasColorRange bool
}
Geometry contains points, indexed triangles, boxes, line pairs, and arrow instances. Custom Series must not mutate geometry after returning it to the model.
type Lines ¶
type Lines struct {
Start, End []math3d.Vec3
Width float32
Color []uint32
ColorValue []float32
Labels []string
}
Lines contains independent Start/End pairs. Width is in render pixels (1..32); zero defaults to 1. Segments have butt caps and no joins. Repeat adjacent endpoints to form a polyline. Optional columns have one entry per segment. Color uses 0xRRGGBBAA (alpha ignored). Without Color, the palette maps ColorValue, or each segment's midpoint Z when ColorValue is absent.
type Material ¶
Material controls triangle appearance. Texture replaces vertex colors. Unlit preserves colors without applying the scene light.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is a Bubble Tea chart containing named series in a shared 3D space.
func (*Model) Close ¶
Close releases GPU resources and pending shared-memory images. Call it after the Bubble Tea program exits. The GPU renderer waits for any active render.
func (*Model) ColorDomain ¶
ColorDomain returns a copy of the fixed domain, or nil for automatic domains.
func (*Model) ColorLegendMode ¶
func (m *Model) ColorLegendMode() ColorLegendMode
func (*Model) Pick ¶
Pick finds a datum at cell coordinates relative to the plot, or returns nil. Points use projected radii with a minimum tolerance based on cell size. Above MaxPickPoints total points, line segments, and arrows, only bars and surface vertices are tested. Lines and arrows use their projected strokes. Bars use ray/box intersections; surfaces use nearby grid vertices.
func (*Model) PictureMode ¶
func (m *Model) PictureMode() picture.PictureMode
func (*Model) PlotAspect ¶
PlotAspect returns the configured relative plot lengths.
func (*Model) PlotBounds ¶
PlotBounds returns data bounds with fixed axis ranges applied. Bounds returns the data bounds.
func (*Model) RenderMode ¶
func (m *Model) RenderMode() RenderMode
RenderMode returns the active rendering mode, including automatic fallback.
func (*Model) RequestedRenderMode ¶
func (m *Model) RequestedRenderMode() RenderMode
RequestedRenderMode returns the last selected mode, defaulting to WebGPU. Fallback does not change it.
func (*Model) SetAxes ¶
SetAxes replaces the shared axes and copies category slices. Invalid settings leave the previous axes intact and set Err. Return the command from Update.
func (*Model) SetColorDomain ¶
SetColorDomain fixes the palette domain and clamps values to its endpoints. Nil restores automatic domains, respecting SetSharedColorDomain. Custom mapped series must implement ColorDomainSeries. The range is copied.
func (*Model) SetColorLegend ¶
SetColorLegend sets the legend title. An empty title uses series names, or "value" for a shared domain. SetColorLegendVisible controls visibility.
func (*Model) SetColorLegendMode ¶
func (m *Model) SetColorLegendMode(mode ColorLegendMode) tea.Cmd
SetColorLegendMode switches between image and terminal-text legends.
func (*Model) SetEmphasis ¶
SetEmphasis replaces the hover highlight settings.
func (*Model) SetLines ¶
SetLines copies all slices. Invalid input leaves the existing series intact.
func (*Model) SetPalette ¶
SetPalette copies color stops and recompiles series using the new palette. Empty palettes are ignored; invalid colors leave the existing data intact. Execute the returned command when calling this after Init.
func (*Model) SetPlotAspect ¶
SetPlotAspect sets relative plot lengths, for example (1, 1, .5). All components must be positive and finite, or all zero to preserve data proportions.
func (*Model) SetRenderMode ¶
func (m *Model) SetRenderMode(l RenderMode) tea.Cmd
SetRenderMode selects both the requested and active mode. Selecting WebGPU after fallback retries GPU initialization. Invalid modes are ignored.
func (*Model) SetSeries ¶
SetSeries compiles geometry synchronously. Call it before Init or from Update. A matching name replaces the existing series in place; a new name appends it. Providers must not mutate returned geometry. Err reports validation errors.
func (*Model) SetSeriesTexture ¶
SetSeriesTexture replaces the image of an already textured series without resampling or rebuilding its mesh. Invalid input leaves the series intact. Execute the returned command after Init. Err reports errors.
func (*Model) SetSharedColorDomain ¶
SetSharedColorDomain maps series with color ranges against their combined minimum and maximum. Custom series must implement ColorDomainSeries.
func (*Model) SetVectorField ¶
func (m *Model) SetVectorField(name string, d VectorField) tea.Cmd
SetVectorField copies all slices and compiles GPU arrow instances. Camera changes do not rebuild or upload them. Return the command from Update.
func (*Model) Snapshot ¶ added in v0.2.0
Snapshot draws the chart as it stands into an image of width by height pixels. It needs no terminal: the program need not be running, and the chart's size in cells plays no part. The picture's own shape sets the camera's aspect. Its area may not exceed 4096×2160 pixels.
The chart is drawn in its requested render mode, and the mode returned is the one that drew it. WebGPU falls back to software if the GPU is unavailable or fails, as it does for the terminal, but a snapshot's fallback does not change how the chart is drawn on screen. A software snapshot draws every point, triangle, bar, line, and arrow; frames for the terminal sample large series to stay prompt. Wireframe draws text, and has no image to give.
Snapshot returns when the image is drawn. Like the chart's other methods, it must not be called while another goroutine changes the chart.
type Option ¶
type Option func(*Model)
func WithAutoRotate ¶
func WithBackground ¶
func WithColorDomain ¶
WithColorDomain sets a fixed domain for all palette-mapped series.
func WithColorLegend ¶
WithColorLegend sets the legend title. An empty title uses series names, or "value" for a shared domain.
func WithColorLegendMode ¶
func WithColorLegendMode(mode ColorLegendMode) Option
func WithEmphasis ¶
WithEmphasis sets the initial hover highlight settings.
func WithKittyID ¶
func WithKittyMedium ¶
func WithKittyMedium(medium picture.KittyMedium) Option
func WithMaxPoints ¶
func WithPlotAspect ¶
WithPlotAspect sets relative X, Y, Z plot lengths. Zero preserves data proportions.
func WithProjection ¶
func WithProjection(p Projection) Option
func WithRenderMode ¶
func WithRenderMode(l RenderMode) Option
WithRenderMode selects the initial requested and active rendering mode. WebGPU is the default; invalid modes are ignored.
func WithRenderer ¶
func WithSharedColorDomain ¶
WithSharedColorDomain maps series with color ranges against their combined minimum and maximum. The domain updates when data or palettes change.
func WithZoneManager ¶
WithZoneManager uses a parent's zone manager. The parent must Scan its full View. By default, the chart owns a manager and starts at the terminal origin.
type PickMsg ¶
type PickMsg struct {
ModelID uint64
SeriesIndex int
SeriesName string
Datum int
Position math3d.Vec3
Label string
}
PickMsg identifies a clicked datum. SeriesIndex is the zero-based series position at the time of the pick. SeriesName comes from the setter or Series.Name.
type Projection ¶
type Projection uint8
const ( Orthographic Projection = iota Perspective )
func (Projection) String ¶
func (p Projection) String() string
type Range ¶
type Range struct{ Min, Max float32 }
Range is a fixed numeric interval. Min and Max must be finite, with Min < Max.
type RenderMode ¶
type RenderMode uint8
RenderMode selects WebGPU, software, or wireframe rendering. Kitty/glyph output is a separate setting.
const ( WebGPU RenderMode = iota Software Wireframe )
func (RenderMode) String ¶
func (l RenderMode) String() string
type Renderer ¶
Renderer draws a Frame into a Go image. Implementations must serialize Render and Close and reuse mesh data while Frame.Revision is unchanged. Texture bindings must also be refreshed when Frame.TextureRevision changes.
type Scatter ¶
type Scatter struct {
X, Y, Z []float32
Color []uint32
ColorValue, Size []float32
Category []int
Labels []string
}
Scatter holds X/Y/Z columns of equal length. SetScatter copies all slices. Optional columns must be empty or match that length. Color uses 0xRRGGBBAA; without it, the palette maps ColorValue (or Z). Size is the point radius in pixels, defaults to 2, and is clamped to [1,32] before rendering.
type Series ¶
Series supplies a named dataset as geometry. Geometry is compiled when data, palettes, or shared-domain settings change, never on camera movement.
type Surface ¶
type Surface struct {
Material *Material
UV []UV // Optional; defaults to north-up XY mapping.
ColorLegend bool // Include elevation in palette domains and legends even when textured.
NX, NY int
Z []float32
Sampler func(float64, float64) float64
MinX, MaxX, MinY, MaxY float64
Wireframe bool
}
Surface is a regular XY grid stored by row. Sampler overrides Z and runs when geometry is compiled; an update may sample the grid more than once. Equal bounds on either horizontal axis default to [-1,1].
type Texture ¶
type Texture struct {
// contains filtered or unexported fields
}
Texture owns an immutable snapshot of opaque image pixels. The zero value is invalid. GPU resources are owned by each renderer, not by Texture.
func NewTexture ¶
NewTexture snapshots src, including images with nonzero bounds. Images must be opaque and at most 8192 pixels per side. Channels are treated as sRGB-encoded values, with filtering and lighting performed directly in that encoded space.
func (*Texture) ColorModel ¶
ColorModel implements image.Image without exposing mutable storage.
type UV ¶
type UV struct{ U, V float32 }
UV is a normalized image coordinate; (0,0) is the top-left corner.
type VectorField ¶
type VectorField struct {
Origins, Vectors []math3d.Vec3
Scale float32
Normalize bool
Width, HeadSize float32
Color []uint32
ColorValue []float32
Labels []string
}
VectorField pairs Origins with Vectors. The tip is origin + vector * Scale. Scale defaults to 1 and must be positive. Normalize makes every nonzero vector Scale world units long; colors still use the original magnitude. Zero vectors have no arrow. Width defaults to 2 pixels, HeadSize to 8 (range 1..64). Optional Color (0xRRGGBBAA), ColorValue, and Labels have one entry per vector. Without Color, the palette maps ColorValue or the original vector magnitude.
Source Files
¶
- axis.go
- axis_layout.go
- axis_ticks.go
- bars.go
- camera.go
- clipping.go
- doc.go
- emphasis.go
- geometry.go
- grid.go
- interaction.go
- legend.go
- legend_image.go
- lines.go
- model.go
- options.go
- overlay.go
- palette.go
- pick.go
- plot.go
- render_gpu.go
- render_software.go
- render_texture.go
- render_wireframe.go
- renderer.go
- scatter.go
- schedule.go
- series.go
- snapshot.go
- stroke.go
- surface.go
- text.go
- texture.go
- update.go
- vector_field.go
- view.go
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
wasm-build
command
wasm-build builds the gallery with Go and TinyGo and assembles the Pages site.
|
wasm-build builds the gallery with Go and TinyGo and assembles the Pages site. |
|
examples
|
|
|
gallery
module
|
|
|
Package math3d provides vectors, matrices, rays, and axis-aligned bounds.
|
Package math3d provides vectors, matrices, rays, and axis-aligned bounds. |