Skip to content

Pie charts

Sebastien Binet requested to merge benoitmasson:pie into master

Created by: benoitmasson

This introduces a PieChart plotter, in response to Issue #125 (closed).

The question of the relevance of such a plotter was raised in this issue, but as for me, pie charts (as a graphical representation of a list of values) are very similar to bar charts and deserve to be included. Clearly, in most cases, axes will be hidden with p.HideAxes().

Since I needed them for one of my projects, I added the file to the lib, and I now share it. If you think it's worth it, let me know so I can add tests and documentation (examples) to finalize this PR.

I attach 2 examples of what the plotter can do (complete documentation in the plot/plotter/pie.go file):

  • BASIC USAGE
	// Initialise chart
	p, err := plot.New()
	if err != nil {
		return nil, err
	}
	p.HideAxes()

	// Setup pie chart
	pie, err := plotters.NewPieChart(plotter.Values{1, 2, 3, 2, 4})
	if err != nil {
		return nil, err
	}
	pie.Color = color.RGBA{255, 0, 0, 255}
	p.Add(pie)

basic

  • ADVANCED USAGE
	// Initialise chart
	p, err := plot.New()
	if err != nil {
		return nil, err
	}
	p.Legend.Top = true
	p.HideAxes()

	// Setup pie charts
	pie1, err := plotters.NewPieChart(plotter.Values{1, 2})
	if err != nil {
		return nil, err
	}
	pie1.Color = color.RGBA{255, 0, 0, 255}
	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 := plotters.NewPieChart(plotter.Values{3, 2})
	if err != nil {
		return nil, err
	}
	pie2.Color = color.RGBA{0, 255, 0, 255}
	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 := plotters.NewPieChart(plotter.Values{4})
	if err != nil {
		return nil, err
	}
	pie3.Color = color.RGBA{0, 0, 255, 255}
	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

Merge request reports