unichart

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 9 Imported by: 35

README

unichart

unichart is a native Golang charting library. Its primary purpose is to integrate with UniPDF and other products in the UniDoc ecosystem in order to provide charting capabilities.

Installation

go get -u github.com/unidoc/unichart

Examples

For usage and output samples, see the examples directory.

Supported chart types

Line chart

Sample chart output

Bar chart

Sample bar chart output

Stacked bar chart

Sample stacked bar chart output

Pie chart

Sample pie chart output

Donut chart

Sample donut chart output

Progress Bar

Sample progress bar

License

This project is licensed under the MIT license. See LICENSE for more details.

A commercial license is required in order to use this project with UniPDF. There are two options:

Credits

This is a modified fork of wcharczuk/go-chart.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Legend

func Legend(c *Chart, userDefaults ...render.Style) render.Renderable

Legend returns a legend renderable function.

func LegendLeft

func LegendLeft(c *Chart, userDefaults ...render.Style) render.Renderable

LegendLeft is a legend that is designed for longer series lists.

func LegendThin

func LegendThin(c *Chart, userDefaults ...render.Style) render.Renderable

LegendThin is a legend that doesn't obscure the chart area.

Types

type Axis

type Axis interface {
	GetName() string
	SetName(name string)

	GetStyle() render.Style
	SetStyle(style render.Style)

	GetTicks() []Tick
	GenerateTicks(r render.Renderer, ra sequence.Range, vf dataset.ValueFormatter) []Tick

	// GenerateGridLines returns the gridlines for the axis.
	GetGridLines(ticks []Tick) []GridLine

	// Measure should return an absolute box for the axis.
	// This is used when auto-fitting the canvas to the background.
	Measure(r render.Renderer, canvasBox render.Box, ra sequence.Range, style render.Style, ticks []Tick) render.Box

	// Render renders the axis.
	Render(r render.Renderer, canvasBox render.Box, ra sequence.Range, style render.Style, ticks []Tick)
}

Axis is a chart feature detailing what values happen where.

type BarChart

type BarChart struct {
	Title      string
	TitleStyle render.Style

	Font         render.Font
	Background   render.Style
	Canvas       render.Style
	ColorPalette render.ColorPalette

	XAxis render.Style
	YAxis YAxis

	BarWidth     int
	BarSpacing   int
	IsHorizontal bool

	UseBaseValue bool
	BaseValue    float64

	Bars     []dataset.Value
	Elements []render.Renderable
	// contains filtered or unexported fields
}

BarChart is a chart that draws bars on a range.

func (*BarChart) DPI

func (bc *BarChart) DPI() float64

DPI returns the DPI for the chart.

func (*BarChart) GetBarSpacing

func (bc *BarChart) GetBarSpacing() int

GetBarSpacing returns the spacing between bars.

func (*BarChart) GetBarWidth

func (bc *BarChart) GetBarWidth() int

GetBarWidth returns the default bar width.

func (*BarChart) GetColorPalette

func (bc *BarChart) GetColorPalette() render.ColorPalette

GetColorPalette returns the color palette for the chart.

func (*BarChart) GetFont

func (bc *BarChart) GetFont() render.Font

GetFont returns the text font.

func (*BarChart) Height

func (bc *BarChart) Height() int

Height returns the chart height or the default value.

func (*BarChart) Render

func (bc *BarChart) Render(rp render.RendererProvider, w io.Writer) error

Render renders the chart with the given renderer to the given io.Writer.

func (*BarChart) SetDPI

func (bc *BarChart) SetDPI(dpi float64)

SetDPI sets the DPI for the chart.

func (*BarChart) SetHeight

func (bc *BarChart) SetHeight(height int)

SetHeight sets the chart height.

func (*BarChart) SetWidth

func (bc *BarChart) SetWidth(width int)

SetWidth sets the chart width.

func (*BarChart) Width

func (bc *BarChart) Width() int

Width returns the chart width or the default value.

type Chart

type Chart struct {
	Title      string
	TitleStyle render.Style

	Font         render.Font
	Background   render.Style
	Canvas       render.Style
	ColorPalette render.ColorPalette

	XAxis          XAxis
	YAxis          YAxis
	YAxisSecondary YAxis

	Series   []dataset.Series
	Elements []render.Renderable
	// contains filtered or unexported fields
}

Chart represents a line, curve or histogram chart.

func (*Chart) Box

func (c *Chart) Box() render.Box

Box returns the chart bounds as a box.

func (*Chart) DPI

func (c *Chart) DPI(defaults ...float64) float64

DPI returns the DPI for the chart.

func (*Chart) GetColorPalette

func (c *Chart) GetColorPalette() render.ColorPalette

GetColorPalette returns the color palette for the chart.

func (*Chart) GetFont

func (c *Chart) GetFont() render.Font

GetFont returns the text font.

func (*Chart) Height

func (c *Chart) Height() int

Height returns the chart height.

func (*Chart) Render

func (c *Chart) Render(rp render.RendererProvider, w io.Writer) error

Render renders the chart with the given renderer to the given io.Writer.

func (*Chart) SetDPI

func (c *Chart) SetDPI(dpi float64)

SetDPI sets the DPI for the chart.

func (*Chart) SetHeight

func (c *Chart) SetHeight(height int)

SetHeight sets the chart height.

func (*Chart) SetWidth

func (c *Chart) SetWidth(width int)

SetWidth sets the chart width.

func (*Chart) Width

func (c *Chart) Width() int

Width returns the chart width.

type CircularProgressBar added in v0.2.0

type CircularProgressBar struct {
	// BackgroundStyle is the style for the background bar.
	BackgroundStyle render.Style

	// ForegroundStyle is the style for the foreground bar.
	ForegroundStyle render.Style

	// LabelStyle is the style for the label that will displayed in the center of the progress bar.
	LabelStyle render.Style

	// ColorPalette is the color pallete that could be used to add colors in this progress bar
	ColorPalette render.ColorPalette

	// Reversed is a flag where if the value is true then the progress bar would rendered counter clockwise.
	Reversed bool
	// contains filtered or unexported fields
}

CircularProgressBar is a component that will render circular progress bar component.

func (*CircularProgressBar) DPI added in v0.2.0

func (cp *CircularProgressBar) DPI() float64

DPI returns the DPI of the progress bar.

func (*CircularProgressBar) GetColorPalette added in v0.2.0

func (cp *CircularProgressBar) GetColorPalette() render.ColorPalette

GetColorPalette returns the color palette for the chart.

func (*CircularProgressBar) GetLabel added in v0.2.0

func (cp *CircularProgressBar) GetLabel() string

GetLabel returns the label displayed in the center of the progress bar.

func (*CircularProgressBar) GetProgress added in v0.2.0

func (cp *CircularProgressBar) GetProgress() float64

GetProgress returns the progress represented by this chart. Returned value should be a float number between 0.0 - 1.0.

func (*CircularProgressBar) Height added in v0.2.0

func (cp *CircularProgressBar) Height() int

Height returns the chart height.

func (*CircularProgressBar) Render added in v0.2.0

Render renders the progress bar with the given renderer to the given io.Writer.

func (*CircularProgressBar) SetDPI added in v0.2.0

func (cp *CircularProgressBar) SetDPI(dpi float64)

SetDPI sets the DPI for the progress bar.

func (*CircularProgressBar) SetHeight added in v0.2.0

func (cp *CircularProgressBar) SetHeight(height int)

SetHeight method is exists to fuifill the requirements of render.ChartRenderable interface. To set width or height of this circular progress bar, use SetSize instead.

func (*CircularProgressBar) SetLabel added in v0.2.0

func (cp *CircularProgressBar) SetLabel(label string)

SetLabel sets the label that would be displayed in the center of the progress bar.

func (*CircularProgressBar) SetProgress added in v0.2.0

func (cp *CircularProgressBar) SetProgress(progress float64)

SetProgress set the progress that will represented by this chart. Expected value should be float number between 0.0 - 1.0.

func (*CircularProgressBar) SetSize added in v0.2.0

func (cp *CircularProgressBar) SetSize(size int)

SetSize sets the chart size.

func (*CircularProgressBar) SetWidth added in v0.2.0

func (cp *CircularProgressBar) SetWidth(width int)

SetWidth method is exists to fuifill the requirements of render.ChartRenderable interface. To set width or height of this circular progress bar, use SetSize instead.

func (*CircularProgressBar) Size added in v0.2.0

func (cp *CircularProgressBar) Size() int

Size returns the chart size or the default value.

func (*CircularProgressBar) Width added in v0.2.0

func (cp *CircularProgressBar) Width() int

Width returns the chart width.

type DonutChart

type DonutChart struct {
	Title      string
	TitleStyle render.Style

	Font         render.Font
	Background   render.Style
	Canvas       render.Style
	SliceStyle   render.Style
	ColorPalette render.ColorPalette

	Values   []dataset.Value
	Elements []render.Renderable
	// contains filtered or unexported fields
}

DonutChart is a chart that draws sections of a circle based on percentages with an hole.

func (*DonutChart) Box

func (pc *DonutChart) Box() render.Box

Box returns the chart bounds as a box.

func (*DonutChart) DPI

func (pc *DonutChart) DPI(defaults ...float64) float64

DPI returns the DPI for the chart.

func (*DonutChart) GetColorPalette

func (pc *DonutChart) GetColorPalette() render.ColorPalette

GetColorPalette returns the color palette for the chart.

func (*DonutChart) GetFont

func (pc *DonutChart) GetFont() render.Font

GetFont returns the text font.

func (*DonutChart) Height

func (pc *DonutChart) Height() int

Height returns the chart height or the default value.

func (*DonutChart) Render

func (pc *DonutChart) Render(rp render.RendererProvider, w io.Writer) error

Render renders the chart with the given renderer to the given io.Writer.

func (*DonutChart) SetDPI

func (pc *DonutChart) SetDPI(dpi float64)

SetDPI sets the DPI for the chart.

func (*DonutChart) SetHeight

func (pc *DonutChart) SetHeight(height int)

SetHeight sets the chart height.

func (*DonutChart) SetWidth

func (pc *DonutChart) SetWidth(width int)

SetWidth sets the chart width.

func (*DonutChart) Width

func (pc *DonutChart) Width() int

Width returns the chart width or the default value.

type GridLine

type GridLine struct {
	IsMinor bool
	Style   render.Style
	Value   float64
}

GridLine is a line on a graph canvas.

func GenerateGridLines

func GenerateGridLines(ticks []Tick, majorStyle, minorStyle render.Style) []GridLine

GenerateGridLines generates grid lines.

func (GridLine) Major

func (gl GridLine) Major() bool

Major returns if the gridline is a `major` line.

func (GridLine) Minor

func (gl GridLine) Minor() bool

Minor returns if the gridline is a `minor` line.

func (GridLine) Render

func (gl GridLine) Render(r render.Renderer, canvasBox render.Box, ra sequence.Range, isVertical bool, defaults render.Style)

Render renders the gridline

type GridLineProvider

type GridLineProvider interface {
	GetGridLines(ticks []Tick, isVertical bool, majorStyle, minorStyle render.Style) []GridLine
}

GridLineProvider is a type that provides grid lines.

type LinearProgressBar added in v0.2.0

type LinearProgressBar struct {
	// BackgroundStyle is the style for the background bar.
	BackgroundStyle render.Style

	// ForegroundStyle is the style for the foreground bar.
	ForegroundStyle render.Style

	// LabelStyle is the style for the label that will displayed on the progress bar.
	LabelStyle render.Style

	// ColorPalette is the color pallete that could be used to add colors in this progress bar
	ColorPalette render.ColorPalette

	// RoundedEdgeStart is a flag to enable rounded edge at the start of the bar.
	RoundedEdgeStart bool

	// RoundedEdgeEnd us a flag to enable rounded edge at the end of the bar.
	RoundedEdgeEnd bool

	// CustomTopInfo defines a user provided function to draw a custom info above the progress bar.
	// The callback function should return the height occupied by the top info, which would be used
	// to shift down the progress bar position.
	CustomTopInfo func(r render.Renderer, x int) int

	// CustomBottomInfo defines a user provided function to draw a custom info under the progress bar.
	// The callback function should return the height occupied by the bottom info, which would be used
	// to calculate this progress bar total height.
	CustomBottomInfo func(r render.Renderer, x int) int
	// contains filtered or unexported fields
}

LinearProgressBar is a component that will render progress bar component.

func (*LinearProgressBar) DPI added in v0.2.0

func (lp *LinearProgressBar) DPI() float64

DPI returns the DPI of the progress bar.

func (*LinearProgressBar) GetColorPalette added in v0.2.0

func (lp *LinearProgressBar) GetColorPalette() render.ColorPalette

GetColorPalette returns the color palette for the chart.

func (*LinearProgressBar) GetLabel added in v0.2.0

func (lp *LinearProgressBar) GetLabel() string

GetLabel would returns label that rendered on the progress bar.

func (*LinearProgressBar) GetProgress added in v0.2.0

func (lp *LinearProgressBar) GetProgress() float64

GetProgress returns the progress represented by this chart. Returned value should be a float number between 0.0 - 1.0.

func (*LinearProgressBar) Height added in v0.2.0

func (lp *LinearProgressBar) Height() int

Height returns the chart height or the default value.

func (*LinearProgressBar) Render added in v0.2.0

Render renders the progrss bar with the given renderer to the given io.Writer.

func (*LinearProgressBar) SetDPI added in v0.2.0

func (lp *LinearProgressBar) SetDPI(dpi float64)

SetDPI sets the DPI for the progrss bar.

func (*LinearProgressBar) SetHeight added in v0.2.0

func (lp *LinearProgressBar) SetHeight(height int)

SetHeight sets the chart height.

func (*LinearProgressBar) SetLabel added in v0.2.0

func (lp *LinearProgressBar) SetLabel(label string)

SetLabel sets the labe that would be rendered on the progress bar.

func (*LinearProgressBar) SetProgress added in v0.2.0

func (lp *LinearProgressBar) SetProgress(progress float64)

SetProgress set the progress that will represented by this chart. Expected value should be float number between 0.0 - 1.0.

func (*LinearProgressBar) SetWidth added in v0.2.0

func (lp *LinearProgressBar) SetWidth(width int)

SetWidth sets the chart width.

func (*LinearProgressBar) Width added in v0.2.0

func (lp *LinearProgressBar) Width() int

Width returns the chart width or the default value.

type PieChart

type PieChart struct {
	Title      string
	TitleStyle render.Style

	Font         render.Font
	Background   render.Style
	Canvas       render.Style
	SliceStyle   render.Style
	ColorPalette render.ColorPalette

	Values   []dataset.Value
	Elements []render.Renderable
	// contains filtered or unexported fields
}

PieChart is a chart that draws sections of a circle based on percentages.

func (*PieChart) Box

func (pc *PieChart) Box() render.Box

Box returns the chart bounds as a box.

func (*PieChart) DPI

func (pc *PieChart) DPI(defaults ...float64) float64

DPI returns the DPI for the chart.

func (*PieChart) GetColorPalette

func (pc *PieChart) GetColorPalette() render.ColorPalette

GetColorPalette returns the color palette for the chart.

func (*PieChart) GetFont

func (pc *PieChart) GetFont() render.Font

GetFont returns the text font.

func (*PieChart) Height

func (pc *PieChart) Height() int

Height returns the chart height.

func (*PieChart) Render

func (pc *PieChart) Render(rp render.RendererProvider, w io.Writer) error

Render renders the chart with the given renderer to the given io.Writer.

func (*PieChart) SetDPI

func (pc *PieChart) SetDPI(dpi float64)

SetDPI sets the DPI for the chart.

func (*PieChart) SetHeight

func (pc *PieChart) SetHeight(height int)

SetHeight sets the chart height.

func (*PieChart) SetWidth

func (pc *PieChart) SetWidth(width int)

SetWidth sets the chart width.

func (*PieChart) Width

func (pc *PieChart) Width() int

Width returns the chart width.

type StackedBar

type StackedBar struct {
	Name   string
	Width  int
	Values []dataset.Value
}

StackedBar is a bar within a StackedBarChart.

func (StackedBar) GetWidth

func (sb StackedBar) GetWidth() int

GetWidth returns the width of the bar.

type StackedBarChart

type StackedBarChart struct {
	Title      string
	TitleStyle render.Style

	Font         render.Font
	Background   render.Style
	Canvas       render.Style
	ColorPalette render.ColorPalette

	XAxis render.Style
	YAxis render.Style

	BarSpacing   int
	IsHorizontal bool

	Bars     []StackedBar
	Elements []render.Renderable
	// contains filtered or unexported fields
}

StackedBarChart is a chart that draws sections of a bar based on percentages.

func (StackedBarChart) Box

func (sbc StackedBarChart) Box() render.Box

Box returns the chart bounds as a box.

func (StackedBarChart) DPI

func (sbc StackedBarChart) DPI(defaults ...float64) float64

DPI returns the DPI for the chart.

func (StackedBarChart) GetBarSpacing

func (sbc StackedBarChart) GetBarSpacing() int

GetBarSpacing returns the spacing between bars.

func (StackedBarChart) GetColorPalette

func (sbc StackedBarChart) GetColorPalette() render.ColorPalette

GetColorPalette returns the color palette for the chart.

func (StackedBarChart) GetFont

func (sbc StackedBarChart) GetFont() render.Font

GetFont returns the text font.

func (StackedBarChart) Height

func (sbc StackedBarChart) Height() int

Height returns the chart height or the default value.

func (StackedBarChart) Render

Render renders the chart with the given renderer to the given io.Writer.

func (*StackedBarChart) SetDPI

func (sbc *StackedBarChart) SetDPI(dpi float64)

SetDPI sets the DPI for the chart.

func (*StackedBarChart) SetHeight

func (sbc *StackedBarChart) SetHeight(height int)

SetHeight sets the chart height.

func (*StackedBarChart) SetWidth

func (sbc *StackedBarChart) SetWidth(width int)

SetWidth sets the chart width.

func (StackedBarChart) Width

func (sbc StackedBarChart) Width() int

Width returns the chart width or the default value.

type Tick

type Tick struct {
	Value float64
	Label string
}

Tick represents a label on an axis.

type TickPosition

type TickPosition int

TickPosition is an enumeration of possible tick drawing positions.

const (
	// TickPositionUnset means to use the default tick position.
	TickPositionUnset TickPosition = 0

	// TickPositionBetweenTicks draws the labels for a tick between the previous and current tick.
	TickPositionBetweenTicks TickPosition = 1

	// TickPositionUnderTick draws the tick below the tick.
	TickPositionUnderTick TickPosition = 2
)

type TicksProvider

type TicksProvider interface {
	GetTicks(r render.Renderer, defaults render.Style, vf dataset.ValueFormatter) []Tick
}

TicksProvider is a type that provides ticks.

type XAxis

type XAxis struct {
	Name      string
	NameStyle render.Style

	Style          render.Style
	ValueFormatter dataset.ValueFormatter
	Range          sequence.Range

	TickStyle    render.Style
	Ticks        []Tick
	TickPosition TickPosition

	GridLines      []GridLine
	GridMajorStyle render.Style
	GridMinorStyle render.Style
}

XAxis represents the horizontal axis.

func (XAxis) GetGridLines

func (xa XAxis) GetGridLines(ticks []Tick) []GridLine

GetGridLines returns the gridlines for the axis.

func (XAxis) GetName

func (xa XAxis) GetName() string

GetName returns the name.

func (XAxis) GetStyle

func (xa XAxis) GetStyle() render.Style

GetStyle returns the style.

func (XAxis) GetTickPosition

func (xa XAxis) GetTickPosition(defaults ...TickPosition) TickPosition

GetTickPosition returns the tick position option for the axis.

func (XAxis) GetTicks

func (xa XAxis) GetTicks(r render.Renderer, ra sequence.Range, defaults render.Style, vf dataset.ValueFormatter) []Tick

GetTicks returns the ticks for a series. The coalesce priority is:

  • User Supplied Ticks (i.e. Ticks array on the axis itself).
  • Range ticks (i.e. if the range provides ticks).
  • Generating continuous ticks based on minimum spacing and canvas width.

func (XAxis) GetValueFormatter

func (xa XAxis) GetValueFormatter() dataset.ValueFormatter

GetValueFormatter returns the value formatter for the axis.

func (XAxis) Measure

func (xa XAxis) Measure(r render.Renderer, canvasBox render.Box, ra sequence.Range, defaults render.Style, ticks []Tick) render.Box

Measure returns the bounds of the axis.

func (XAxis) Render

func (xa XAxis) Render(r render.Renderer, canvasBox render.Box, ra sequence.Range, defaults render.Style, ticks []Tick)

Render renders the axis

type YAxis

type YAxis struct {
	Name      string
	NameStyle render.Style
	Style     render.Style

	Zero      GridLine
	AxisType  dataset.YAxisType
	Ascending bool

	ValueFormatter dataset.ValueFormatter
	Range          sequence.Range

	TickStyle render.Style
	Ticks     []Tick

	GridLines      []GridLine
	GridMajorStyle render.Style
	GridMinorStyle render.Style
}

YAxis is a veritcal rule of the range. There can be (2) y-axes; a primary and secondary.

func (YAxis) GetGridLines

func (ya YAxis) GetGridLines(ticks []Tick) []GridLine

GetGridLines returns the gridlines for the axis.

func (YAxis) GetName

func (ya YAxis) GetName() string

GetName returns the name.

func (YAxis) GetNameStyle

func (ya YAxis) GetNameStyle() render.Style

GetNameStyle returns the name style.

func (YAxis) GetStyle

func (ya YAxis) GetStyle() render.Style

GetStyle returns the style.

func (YAxis) GetTickStyle

func (ya YAxis) GetTickStyle() render.Style

GetTickStyle returns the tick style.

func (YAxis) GetTicks

func (ya YAxis) GetTicks(r render.Renderer, ra sequence.Range, defaults render.Style, vf dataset.ValueFormatter) []Tick

GetTicks returns the ticks for a series. The coalesce priority is:

  • User Supplied Ticks (i.e. Ticks array on the axis itself).
  • Range ticks (i.e. if the range provides ticks).
  • Generating continuous ticks based on minimum spacing and canvas width.

func (YAxis) GetValueFormatter

func (ya YAxis) GetValueFormatter() dataset.ValueFormatter

GetValueFormatter returns the value formatter for the axis.

func (YAxis) Measure

func (ya YAxis) Measure(r render.Renderer, canvasBox render.Box, ra sequence.Range, defaults render.Style, ticks []Tick) render.Box

Measure returns the bounds of the axis.

func (YAxis) Render

func (ya YAxis) Render(r render.Renderer, canvasBox render.Box, ra sequence.Range, defaults render.Style, ticks []Tick)

Render renders the axis.

func (YAxis) RenderAxisLine added in v0.3.0

func (ya YAxis) RenderAxisLine(r render.Renderer, canvasBox render.Box, ra sequence.Range, defaults render.Style, ticks []Tick)

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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