plotcore

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2024 License: BSD-3-Clause Imports: 34 Imported by: 0

Documentation

Overview

Package plotcore provides Cogent Core widgets for viewing and editing plots.

Index

Constants

View Source
const (
	On       = true
	Off      = false
	FixMin   = true
	FloatMin = false
	FixMax   = true
	FloatMax = false
)

Bool constants for PlotEditor.SetColumnOptions.

View Source
const PlotColumnsHeaderN = 2

Variables

This section is empty.

Functions

func MetaMapLower

func MetaMapLower(meta map[string]string, key string) (string, bool)

MetaMapLower tries meta data access by lower-case version of key too

Types

type ColumnOptions added in v0.2.1

type ColumnOptions struct {

	// whether to plot this column
	On bool

	// name of column being plotting
	Column string

	// whether to plot lines; uses the overall plot option if unset
	Lines option.Option[bool]

	// whether to plot points with symbols; uses the overall plot option if unset
	Points option.Option[bool]

	// the width of lines; uses the overall plot option if unset
	LineWidth option.Option[float32]

	// the size of points; uses the overall plot option if unset
	PointSize option.Option[float32]

	// the shape used to draw points; uses the overall plot option if unset
	PointShape option.Option[plots.Shapes]

	// effective range of data to plot -- either end can be fixed
	Range minmax.Range32 `display:"inline"`

	// full actual range of data -- only valid if specifically computed
	FullRange minmax.F32 `display:"inline"`

	// color to use when plotting the line / column
	Color image.Image

	// desired number of ticks
	NTicks int

	// if specified, this is an alternative label to use when plotting
	Label string

	// if column has n-dimensional tensor cells in each row, this is the index within each cell to plot -- use -1 to plot *all* indexes as separate lines
	TensorIndex int

	// specifies a column containing error bars for this column
	ErrColumn string

	// if true this is a string column -- plots as labels
	IsString bool `edit:"-"`

	// our plot, for update method
	Plot *PlotEditor `copier:"-" json:"-" xml:"-" display:"-"`
}

ColumnOptions are options for plotting one column of data.

func (*ColumnOptions) CopyFrom added in v0.2.1

func (co *ColumnOptions) CopyFrom(fr *ColumnOptions)

CopyFrom copies from other column options.

func (*ColumnOptions) Defaults added in v0.2.1

func (co *ColumnOptions) Defaults()

Defaults sets defaults if unset values are present.

func (*ColumnOptions) FromMetaMap added in v0.2.1

func (co *ColumnOptions) FromMetaMap(meta map[string]string)

FromMetaMap sets column options from meta data map.

func (*ColumnOptions) GetLabel added in v0.2.1

func (co *ColumnOptions) GetLabel() string

GetLabel returns the effective label of the column.

type Plot

type Plot struct {
	core.WidgetBase

	// Scale multiplies the plot DPI value, to change the overall scale
	// of the rendered plot.  Larger numbers produce larger scaling.
	// Typically use larger numbers when generating plots for inclusion in
	// documents or other cases where the overall plot size will be small.
	Scale float32

	// Plot is the Plot to display in this widget
	Plot *plot.Plot `set:"-"`
}

Plot is a widget that renders a plot.Plot object. If it is not states.ReadOnly, the user can pan and zoom the graph. See PlotEditor for an interactive interface for selecting columns to view.

func NewPlot

func NewPlot(parent ...tree.Node) *Plot

NewPlot returns a new Plot with the given optional parent: Plot is a widget that renders a plot.Plot object. If it is not states.ReadOnly, the user can pan and zoom the graph. See PlotEditor for an interactive interface for selecting columns to view.

func (*Plot) Init

func (pt *Plot) Init()

func (*Plot) Render

func (pt *Plot) Render()

func (*Plot) SavePNG

func (pt *Plot) SavePNG(filename core.Filename) error

SavePNG saves the current rendered Plot image to an PNG image file.

func (*Plot) SavePlot

func (pt *Plot) SavePlot(filename core.Filename) error

SaveSVG saves the current Plot to an SVG file

func (*Plot) SetPlot

func (pt *Plot) SetPlot(pl *plot.Plot)

SetPlot sets the plot to given Plot, and calls UpdatePlot to ensure it is drawn at the current size of this widget

func (*Plot) SetScale

func (t *Plot) SetScale(v float32) *Plot

SetScale sets the [Plot.Scale]: Scale multiplies the plot DPI value, to change the overall scale of the rendered plot. Larger numbers produce larger scaling. Typically use larger numbers when generating plots for inclusion in documents or other cases where the overall plot size will be small.

func (*Plot) SizeFinal

func (pt *Plot) SizeFinal()

func (*Plot) UpdatePlot

func (pt *Plot) UpdatePlot()

UpdatePlot draws the current plot at the size of the current widget, and triggers a Render so the widget will be rendered.

func (*Plot) WidgetTooltip

func (pt *Plot) WidgetTooltip(pos image.Point) (string, image.Point)

type PlotEditor

type PlotEditor struct {
	core.Frame

	// Table is the table of data being plotted.
	Table *table.IndexView `set:"-"`

	// Options are the overall plot options.
	Options PlotOptions

	// Columns are the options for each column of the table.
	Columns []*ColumnOptions `set:"-"`

	// Plot is the plot object.
	Plot *plot.Plot `set:"-" edit:"-" json:"-" xml:"-"`

	// ConfigPlotFunc is a function to call to configure [PlotEditor.Plot], the plot.Plot that
	// actually does the plotting. It is called after [Plot] is generated, and properties
	// of [Plot] can be modified in it. Properties of [Plot] should not be modified outside
	// of this function, as doing so will have no effect.
	ConfigPlotFunc func() `json:"-" xml:"-"`

	// current svg file
	SVGFile core.Filename

	// current csv data file
	DataFile core.Filename

	// currently doing a plot
	InPlot bool `set:"-" edit:"-" json:"-" xml:"-"`
}

PlotEditor is a widget that provides an interactive 2D plot of selected columns of tabular data, represented by a table.IndexView into a table.Table. Other types of tabular data can be converted into this format. The user can change various options for the plot and also modify the underlying data.

func NewPlotEditor

func NewPlotEditor(parent ...tree.Node) *PlotEditor

NewPlotEditor returns a new PlotEditor with the given optional parent: PlotEditor is a widget that provides an interactive 2D plot of selected columns of tabular data, represented by a table.IndexView into a table.Table. Other types of tabular data can be converted into this format. The user can change various options for the plot and also modify the underlying data.

func NewSubPlot

func NewSubPlot(parent ...tree.Node) *PlotEditor

NewSubPlot returns a PlotEditor with its own separate Toolbar, suitable for a tab or other element that is not the main plot.

func (*PlotEditor) ColumnOptions added in v0.2.1

func (pl *PlotEditor) ColumnOptions(column string) *ColumnOptions

ColumnOptions returns the current column options by name (to access by index, just use Columns directly). It returns nil if not found.

func (*PlotEditor) ColumnOptionsTry added in v0.2.1

func (pl *PlotEditor) ColumnOptionsTry(column string) (*ColumnOptions, error)

ColumnOptionsTry returns the current column options by name (to access by index, just use Columns directly). It returns an error message if not found.

func (*PlotEditor) ColumnsFrame

func (pl *PlotEditor) ColumnsFrame() *core.Frame

func (*PlotEditor) ColumnsFromMetaMap

func (pl *PlotEditor) ColumnsFromMetaMap(meta map[string]string)

ColumnsFromMetaMap updates all the column settings from given meta map

func (*PlotEditor) ColumnsListUpdate

func (pl *PlotEditor) ColumnsListUpdate()

ColumnsListUpdate updates the list of columns

func (*PlotEditor) ConfigPlot

func (pl *PlotEditor) ConfigPlot(plt *plot.Plot)

ConfigPlot configures the given plot based on the plot options.

func (*PlotEditor) CopyFieldsFrom

func (pl *PlotEditor) CopyFieldsFrom(frm tree.Node)

func (*PlotEditor) GenPlot

func (pl *PlotEditor) GenPlot()

GenPlot generates the plot and renders it to SVG It surrounds operation with InPlot true / false to prevent multiple updates

func (*PlotEditor) GenPlotBar

func (pl *PlotEditor) GenPlotBar()

GenPlotBar generates a Bar plot, setting GPlot variable

func (*PlotEditor) GenPlotXY

func (pl *PlotEditor) GenPlotXY()

GenPlotXY generates an XY (lines, points) plot, setting Plot variable

func (*PlotEditor) GoUpdatePlot

func (pl *PlotEditor) GoUpdatePlot()

GoUpdatePlot updates the display based on current IndexView into table. this version can be called from go routines. It does Sequential() on the table.IndexView, under the assumption that it is used for tracking a the latest updates of a running process.

func (*PlotEditor) Init

func (pl *PlotEditor) Init()

func (*PlotEditor) MakeToolbar

func (pl *PlotEditor) MakeToolbar(p *tree.Plan)

func (*PlotEditor) OpenCSV

func (pl *PlotEditor) OpenCSV(filename core.Filename, delim table.Delims)

OpenCSV opens the Table data from a csv (comma-separated values) file (or any delim)

func (*PlotEditor) OpenFS

func (pl *PlotEditor) OpenFS(fsys fs.FS, filename core.Filename, delim table.Delims)

OpenFS opens the Table data from a csv (comma-separated values) file (or any delim) from the given filesystem.

func (*PlotEditor) PlotChild

func (pl *PlotEditor) PlotChild() *Plot

func (*PlotEditor) PlotXAxis

func (pl *PlotEditor) PlotXAxis(plt *plot.Plot, ixvw *table.IndexView) (xi int, xview *table.IndexView, err error)

PlotXAxis processes the XAxis and returns its index

func (*PlotEditor) SaveAll

func (pl *PlotEditor) SaveAll(fname core.Filename)

SaveAll saves the current plot to a png, svg, and the data to a tsv -- full save Any extension is removed and appropriate extensions are added

func (*PlotEditor) SaveCSV

func (pl *PlotEditor) SaveCSV(fname core.Filename, delim table.Delims)

SaveCSV saves the Table data to a csv (comma-separated values) file with headers (any delim)

func (*PlotEditor) SavePNG

func (pl *PlotEditor) SavePNG(fname core.Filename)

SavePNG saves the current plot to a png, capturing current render

func (*PlotEditor) SaveSVG

func (pl *PlotEditor) SaveSVG(fname core.Filename)

SaveSVG saves the plot to an svg -- first updates to ensure that plot is current

func (*PlotEditor) SetAllColumns

func (pl *PlotEditor) SetAllColumns(on bool)

SetAllColumns turns all Columns on or off (except X axis)

func (*PlotEditor) SetColumnOptions added in v0.2.1

func (pl *PlotEditor) SetColumnOptions(column string, on bool, fixMin bool, min float32, fixMax bool, max float32) *ColumnOptions

SetColumnOptions sets the main parameters for one column.

func (*PlotEditor) SetColumnsByName

func (pl *PlotEditor) SetColumnsByName(nameContains string, on bool)

SetColumnsByName turns cols On or Off if their name contains given string

func (*PlotEditor) SetConfigPlotFunc

func (t *PlotEditor) SetConfigPlotFunc(v func()) *PlotEditor

SetConfigPlotFunc sets the [PlotEditor.ConfigPlotFunc]: ConfigPlotFunc is a function to call to configure [PlotEditor.Plot], the plot.Plot that actually does the plotting. It is called after Plot is generated, and properties of Plot can be modified in it. Properties of Plot should not be modified outside of this function, as doing so will have no effect.

func (*PlotEditor) SetDataFile

func (t *PlotEditor) SetDataFile(v core.Filename) *PlotEditor

SetDataFile sets the [PlotEditor.DataFile]: current csv data file

func (*PlotEditor) SetIndexView

func (pl *PlotEditor) SetIndexView(tab *table.IndexView) *PlotEditor

SetIndexView sets the table to view and does Update to update the Column list, which will also trigger a Layout and updating of the plot on next render pass. This is safe to call from a different goroutine.

func (*PlotEditor) SetOptions added in v0.2.1

func (t *PlotEditor) SetOptions(v PlotOptions) *PlotEditor

SetOptions sets the [PlotEditor.Options]: Options are the overall plot options.

func (*PlotEditor) SetSVGFile

func (t *PlotEditor) SetSVGFile(v core.Filename) *PlotEditor

SetSVGFile sets the [PlotEditor.SVGFile]: current svg file

func (*PlotEditor) SetTable

func (pl *PlotEditor) SetTable(tab *table.Table) *PlotEditor

SetTable sets the table to view and does Update to update the Column list, which will also trigger a Layout and updating of the plot on next render pass. This is safe to call from a different goroutine.

func (*PlotEditor) SizeFinal

func (pt *PlotEditor) SizeFinal()

func (*PlotEditor) UpdatePlot

func (pl *PlotEditor) UpdatePlot()

UpdatePlot updates the display based on current IndexView into table. This version can only be called within main goroutine for window eventloop -- use GoUpdateUplot for other-goroutine updates. It does not automatically update the table.IndexView unless it is nil or out date.

func (*PlotEditor) XLabel

func (pl *PlotEditor) XLabel() string

XLabel returns the X-axis label

func (*PlotEditor) YLabel

func (pl *PlotEditor) YLabel() string

YLabel returns the Y-axis label

type PlotOptions added in v0.2.1

type PlotOptions struct {

	// optional title at top of plot
	Title string

	// type of plot to generate.  For a Bar plot, items are plotted ordinally by row and the XAxis is optional
	Type PlotTypes

	// whether to plot lines
	Lines bool `default:"true"`

	// whether to plot points with symbols
	Points bool

	// width of lines
	LineWidth float32 `default:"1"`

	// size of points
	PointSize float32 `default:"3"`

	// the shape used to draw points
	PointShape plots.Shapes

	// width of bars for bar plot, as fraction of available space (1 = no gaps)
	BarWidth float32 `min:"0.01" max:"1" default:"0.8"`

	// if true, draw lines that connect points with a negative X-axis direction;
	// otherwise there is a break in the line.
	// default is false, so that repeated series of data across the X axis
	// are plotted separately.
	NegativeXDraw bool

	// Scale multiplies the plot DPI value, to change the overall scale
	// of the rendered plot.  Larger numbers produce larger scaling.
	// Typically use larger numbers when generating plots for inclusion in
	// documents or other cases where the overall plot size will be small.
	Scale float32 `default:"1,2"`

	// what column to use for the common X axis. if empty or not found,
	// the row number is used.  This optional for Bar plots, if present and
	// LegendColumn is also present, then an extra space will be put between X values.
	XAxisColumn string

	// optional column for adding a separate colored / styled line or bar
	// according to this value, and acts just like a separate Y variable,
	// crossed with Y variables.
	LegendColumn string

	// position of the Legend
	LegendPosition plot.LegendPosition `display:"inline"`

	// rotation of the X Axis labels, in degrees
	XAxisRotation float32

	// optional label to use for XAxis instead of column name
	XAxisLabel string

	// optional label to use for YAxis -- if empty, first column name is used
	YAxisLabel string

	// our plot, for update method
	Plot *PlotEditor `copier:"-" json:"-" xml:"-" display:"-"`
}

PlotOptions are options for the overall plot.

func (*PlotOptions) CopyFrom added in v0.2.1

func (po *PlotOptions) CopyFrom(fr *PlotOptions)

CopyFrom copies from other plot options.

func (*PlotOptions) Defaults added in v0.2.1

func (po *PlotOptions) Defaults()

Defaults sets defaults if unset values are present.

func (*PlotOptions) FromMeta added in v0.2.1

func (po *PlotOptions) FromMeta(dt *table.Table)

FromMeta sets plot options from meta data.

func (*PlotOptions) FromMetaMap added in v0.2.1

func (po *PlotOptions) FromMetaMap(meta map[string]string)

FromMetaMap sets plot options from meta data map.

type PlotTypes

type PlotTypes int32 //enums:enum

PlotTypes are different types of plots.

const (
	// XY is a standard line / point plot.
	XY PlotTypes = iota

	// Bar plots vertical bars.
	Bar
)
const PlotTypesN PlotTypes = 2

PlotTypesN is the highest valid value for type PlotTypes, plus one.

func PlotTypesValues

func PlotTypesValues() []PlotTypes

PlotTypesValues returns all possible values for the type PlotTypes.

func (PlotTypes) Desc

func (i PlotTypes) Desc() string

Desc returns the description of the PlotTypes value.

func (PlotTypes) Int64

func (i PlotTypes) Int64() int64

Int64 returns the PlotTypes value as an int64.

func (PlotTypes) MarshalText

func (i PlotTypes) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*PlotTypes) SetInt64

func (i *PlotTypes) SetInt64(in int64)

SetInt64 sets the PlotTypes value from an int64.

func (*PlotTypes) SetString

func (i *PlotTypes) SetString(s string) error

SetString sets the PlotTypes value from its string representation, and returns an error if the string is invalid.

func (PlotTypes) String

func (i PlotTypes) String() string

String returns the string representation of this PlotTypes value.

func (*PlotTypes) UnmarshalText

func (i *PlotTypes) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (PlotTypes) Values

func (i PlotTypes) Values() []enums.Enum

Values returns all possible values for the type PlotTypes.

type TableXY

type TableXY struct {

	// the index view of data table to plot from
	Table *table.IndexView

	// the indexes of the tensor columns to use for the X and Y data, respectively
	XColumn, YColumn int

	// numer of elements in each row of data -- 1 for scalar, > 1 for multi-dimensional
	XRowSz, YRowSz int

	// the indexes of the element within each tensor cell if cells are n-dimensional, respectively
	XIndex, YIndex int

	// the column to use for returning a label using Label interface -- for string cols
	LabelColumn int

	// the column to use for returning errorbars (+/- given value) -- if YColumn is tensor then this must also be a tensor and given YIndex used
	ErrColumn int

	// range constraints on Y values
	YRange minmax.Range32
}

TableXY selects two columns from a table.Table data table to plot in a gonum plot, satisfying the plot/plots.XYer and .Valuer interfaces (for bar charts). For Tensor-valued cells, Index's specify tensor cell. Also satisfies the plot/plots.Labeler interface for labels attached to a line, and plot/plots.YErrorer for error bars.

func NewTableXY

func NewTableXY(dt *table.IndexView, xcol, xtsrIndex, ycol, ytsrIndex int, yrng minmax.Range32) (*TableXY, error)

NewTableXY returns a new XY plot view onto the given IndexView of table.Table (makes a copy), from given column indexes, and tensor indexes within each cell. Column indexes are enforced to be valid, with an error message if they are not.

func NewTableXYName

func NewTableXYName(dt *table.IndexView, xi, xtsrIndex int, ycol string, ytsrIndex int, yrng minmax.Range32) (*TableXY, error)

NewTableXYName returns a new XY plot view onto the given IndexView of table.Table (makes a copy), from given column name and tensor indexes within each cell. Column indexes are enforced to be valid, with an error message if they are not.

func (*TableXY) FilterValues

func (txy *TableXY) FilterValues()

FilterValues removes items with NaN values, and out of Y range

func (*TableXY) Label

func (txy *TableXY) Label(row int) string

Label returns a label for given row in table, using plot/plots.Labeler interface

func (*TableXY) Len

func (txy *TableXY) Len() int

Len returns the number of rows in the view of table

func (*TableXY) TRowValue

func (txy *TableXY) TRowValue(row int) float32

TRowValue returns the y value at given true table row in table

func (*TableXY) TRowXValue

func (txy *TableXY) TRowXValue(row int) float32

TRowXValue returns an x value at given actual row in table

func (*TableXY) Validate

func (txy *TableXY) Validate() error

Validate returns error message if column indexes are invalid, else nil it also sets column indexes to 0 so nothing crashes.

func (*TableXY) Value

func (txy *TableXY) Value(row int) float32

Value returns the y value at given row in table

func (*TableXY) XValue

func (txy *TableXY) XValue(row int) float32

XValue returns an x value at given row in table

func (*TableXY) XY

func (txy *TableXY) XY(row int) (x, y float32)

XY returns an x, y pair at given row in table

func (*TableXY) YError

func (txy *TableXY) YError(row int) (float32, float32)

YError returns a error bars using plot/plots.YErrorer interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL