MCPcopy Index your code
hub / github.com/gonum/plot / ExampleBarChart

Function ExampleBarChart

plotter/barchart_example_test.go:18–156  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16)
17
18func ExampleBarChart() {
19 // Create the plot values and labels.
20 values := plotter.Values{0.5, 10, 20, 30}
21 verticalLabels := []string{"A", "B", "C", "D"}
22 horizontalLabels := []string{"Label A", "Label B", "Label C", "Label D"}
23
24 // Create a vertical BarChart
25 p1 := plot.New()
26 verticalBarChart, err := plotter.NewBarChart(values, 0.5*vg.Centimeter)
27 if err != nil {
28 log.Panic(err)
29 }
30 p1.Add(verticalBarChart)
31 p1.NominalX(verticalLabels...)
32 err = p1.Save(100, 100, "testdata/verticalBarChart.png")
33 if err != nil {
34 log.Panic(err)
35 }
36
37 // Create a horizontal BarChart
38 p2 := plot.New()
39 horizontalBarChart, err := plotter.NewBarChart(values, 0.5*vg.Centimeter)
40 horizontalBarChart.Horizontal = true // Specify a horizontal BarChart.
41 if err != nil {
42 log.Panic(err)
43 }
44 p2.Add(horizontalBarChart)
45 p2.NominalY(horizontalLabels...)
46 err = p2.Save(100, 100, "testdata/horizontalBarChart_"+runtime.GOARCH+".png")
47 if err != nil {
48 log.Panic(err)
49 }
50
51 // Now, make a different type of BarChart.
52 groupA := plotter.Values{20, 35, 30, 35, 27}
53 groupB := plotter.Values{25, 32, 34, 20, 25}
54 groupC := plotter.Values{12, 28, 15, 21, 8}
55 groupD := plotter.Values{30, 42, 6, 9, 12}
56
57 p := plot.New()
58 p.Title.Text = "Bar chart"
59 p.Y.Label.Text = "Heights"
60
61 w := vg.Points(8)
62
63 barsA, err := plotter.NewBarChart(groupA, w)
64 if err != nil {
65 log.Panic(err)
66 }
67 barsA.Color = color.RGBA{R: 255, A: 255}
68 barsA.Offset = -w / 2
69
70 barsB, err := plotter.NewBarChart(groupB, w)
71 if err != nil {
72 log.Panic(err)
73 }
74 barsB.Color = color.RGBA{R: 196, G: 196, A: 255}
75 barsB.Offset = w / 2

Callers

nothing calls this directly

Calls 9

StackOnMethod · 0.95
NewFunction · 0.92
NewBarChartFunction · 0.92
PointsFunction · 0.92
NewGlyphBoxesFunction · 0.92
NominalXMethod · 0.80
SaveMethod · 0.80
NominalYMethod · 0.80
AddMethod · 0.45

Tested by

no test coverage detected