piechart

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: GPL-3.0 Imports: 10 Imported by: 2

README

Licence Go Reference

Pie chart

Pie chart plotter for gonum.org/v1/plot package.

Import "github.com/benoitmasson/plotters/piechart" in your Go file, then create a new chart with piechart.NewPieChart(plotter.Values{...}).

See documentation for a complete feature overview.

Examples

Such plots need to call p.HideAxes(), since axes are not relevant for this kind of chart.

Basic usage
	// Initialize chart
	p := plot.New()
	p.HideAxes()

	// Setup pie chart
	pie, err := piechart.NewPieChart(plotter.Values{1, 2, 3, 2, 4})
	if err != nil {
		panic(err)
	}
	pie.Color = color.RGBA{200, 100, 100, 255} // red
	p.Add(pie)

basic

Advanced usage
	// Initialize chart
	p := plot.New()
	p.Legend.Top = true
	p.HideAxes()

	// Setup pie charts
	pie1, err := piechart.NewPieChart(plotter.Values{1, 2})
	if err != nil {
		panic(err)
	}
	pie1.Color = color.RGBA{200, 100, 100, 255} // red
	pie1.Total = 12
	pie1.Labels.Nominal = []string{"one", "two"}
	pie1.Labels.Values.Show = true
	pie1.Labels.Values.Percentage = true
	p.Add(pie1)
	p.Legend.Add("sample 1", pie1)

	pie2, err := piechart.NewPieChart(plotter.Values{3, 2})
	if err != nil {
		panic(err)
	}
	pie2.Color = color.RGBA{100, 200, 100, 255} // green
	pie2.Offset.Value = 3
	pie2.Total = 12
	pie2.Labels.Nominal = []string{"three", "four"}
	pie2.Labels.Values.Show = true
	pie2.Labels.Values.Percentage = true
	p.Add(pie2)
	p.Legend.Add("sample 2", pie2)

	pie3, err := piechart.NewPieChart(plotter.Values{4})
	if err != nil {
		panic(err)
	}
	pie3.Color = color.RGBA{100, 100, 200, 255} // blue
	pie3.Offset.Value = 8
	pie3.Total = 12
	pie3.Offset.X = vg.Length(10)
	pie3.Offset.Y = vg.Length(-15)
	pie3.Labels.Position = 1.1
	pie3.Labels.Nominal = []string{"five"}
	pie3.Labels.Values.Show = true
	pie3.Labels.Values.Percentage = true
	p.Add(pie3)
	p.Legend.Add("sample 3", pie3)

advanced

License

GNU General Public License v3.0 or later.

See COPYING to see the full text.

Documentation

Overview

Package piechart implements a pie chart plotter for package gonum.org/v1/plot.

Pies rendering is fully customizable (see below). In particular, multiple pies can be combined on one chart, thanks to the Offset and Total attributes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PieChart

type PieChart struct {
	plotter.Values

	// Radius is the radius of the pie with respect to
	// the chart size. If 0, the pie is a simple dot, if 1, it fills the chart
	// (given that the axes range from -1 to 1).
	// Default: 0.8
	Radius float64

	// Color is the fill color of the pie slices.
	Color color.Color

	// LineStyle is the style of the outline of the pie slices.
	// In particular, LineStyle.Width may be set to change the spacing
	// between slices (defaults to vg.Length(2)).
	draw.LineStyle

	Offset struct {
		// Value is the value added to the position of each slice in the pie,
		// as if there was an invisible slice with value Value before.
		// When this Value offset is zero (default), the slices are drawn one after
		// the other, counter-clockwise, starting from the right side.
		Value float64

		// X is the length added to the X position of the pie.
		// When the X offset is zero, the pie is centered horizontally.
		X vg.Length

		// Y is the length added to the Y position the pie.
		// When the Y offset is zero, the pie is centered vertically.
		Y vg.Length
	}

	// Total is the total of values required to fill the pie, it defaults to
	// the sum of all Values.
	// If changed to be greater than this sum, the slices will not fill the pie.
	// If less, it will be reset to the default and fill the pie (slices
	// can not overlay each other).
	Total float64

	Labels struct {
		// Show (default: true) is used to determine whether labels should be
		// displayed in each slice of the pie chart
		Show bool

		// Position is the location of the label:
		// 0 is for the center of the pie;
		// 1 is for the side of the slice.
		// A value greater than 1 is outside of the pie (default: 0.8)
		Position float64

		// TextStyle is the style of the slices label text.
		draw.TextStyle

		// Nominal can be used to override the displayed labels (if not used,
		// regular indexes are displayed).
		// Useful in particular in combination with Offset.Value, to offset
		// manually the displayed indexes.
		Nominal []string

		Values struct {
			// Show (default: false) sets whether values should be displayed
			// along with indexes.
			Show bool

			// Percentage (default: false) is to display the percent value
			// of each slice instead of the real value.
			// The percentage is computed with respect to the given Total
			// value (if any).
			Percentage bool
		}
	}
	// contains filtered or unexported fields
}

PieChart presents data values as slices in a pie, with area proportional to the data values.

func NewPieChart

func NewPieChart(vs plotter.Valuer) (*PieChart, error)

NewPieChart returns a new pie chart with a single slice for each value. The slice areas correspond to the values.

Since this chart is not related to axes, this should probably be used in conjunction with p.HideAxes().

func (*PieChart) DataRange

func (p *PieChart) DataRange() (float64, float64, float64, float64)

DataRange implements the https://godoc.org/gonum.org/v1/plot#DataRanger interface.

A pie chart is always defined in the range (-1, -1) to (1, 1). If something different is required, change the Offset.X/Y or Radius attributes of the PieChart to move or resize it.

func (*PieChart) Plot

func (p *PieChart) Plot(c draw.Canvas, plt *plot.Plot)

Plot implements the https://godoc.org/gonum.org/v1/plot#Plotter interface.

func (*PieChart) Thumbnail

func (p *PieChart) Thumbnail(c *draw.Canvas)

Thumbnail fulfills the https://godoc.org/gonum.org/v1/plot#Thumbnailer interface.

Jump to

Keyboard shortcuts

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