Skip to content

Commit

Permalink
Added bar chart example with positive and negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
ctessum committed Feb 26, 2017
1 parent e6157b7 commit 855031c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions plotter/barchart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package plotter
import (
"image/color"
"log"
"math/rand"
"testing"

"github.com/gonum/plot"
Expand Down Expand Up @@ -172,3 +173,102 @@ func TestBarChart(t *testing.T) {
"horizontalBarChart.png", "barChart2.png",
"stackedBarChart.png")
}

// This example shows a bar chart with both positive and negative values.
func ExampleBarChart_positiveNegative() {
rnd := rand.New(rand.NewSource(1))

// Create random data points between -1 and 1.
const n = 6
data1 := make(Values, n)
data2 := make(Values, n)
net := make(XYs, n) // net = data1 + data2
for i := 0; i < n; i++ {
data1[i] = rnd.Float64()*2 - 1
data2[i] = rnd.Float64()*2 - 1
net[i].X = data1[i] + data2[i]
net[i].Y = float64(i)
}

// posNeg splits an array into two arrays containing the positive and
// negative values, respectively, from the original array.
posNeg := func(d Values) (pos, neg Values) {
pos = make(Values, len(d))
neg = make(Values, len(d))
for i, v := range d {
if v > 0 {
pos[i] = v
} else {
neg[i] = v
}
}
return
}

data1Pos, data1Neg := posNeg(data1)
data2Pos, data2Neg := posNeg(data2)

const barWidth = 0.3 * vg.Centimeter
pos1, err := NewBarChart(data1Pos, barWidth)
if err != nil {
log.Panic(err)
}
pos2, err := NewBarChart(data2Pos, barWidth)
if err != nil {
log.Panic(err)
}
neg1, err := NewBarChart(data1Neg, barWidth)
if err != nil {
log.Panic(err)
}
neg2, err := NewBarChart(data2Neg, barWidth)
if err != nil {
log.Panic(err)
}

netDots, err := NewScatter(net)
if err != nil {
log.Panic(err)
}
netDots.Radius = vg.Points(1.25)

pos2.StackOn(pos1) // Specify that pos2 goes on top of pos1.
neg2.StackOn(neg1) // Specify that neg2 goes on top of neg1.

color1 := color.NRGBA{R: 112, G: 22, B: 0, A: 255}
color2 := color.NRGBA{R: 91, G: 194, B: 54, A: 100}

pos1.Color, neg1.Color = color1, color1
pos2.Color, neg2.Color = color2, color2

// Specify that we want a horizontal bar chart.
pos1.Horizontal, pos2.Horizontal, neg1.Horizontal, neg2.Horizontal = true, true, true, true

// Create a line at zero.
zero, err := NewLine(XYs{{0, 0}, {0, 5}})
if err != nil {
log.Panic(err)
}

p, err := plot.New()
if err != nil {
log.Panic(err)
}
p.Add(zero, pos1, pos2, neg1, neg2, netDots)
p.NominalY("Alpha", "Bravo", "Charlie", "Echo", "Foxtrot", "Golf")

p.Legend.Add("1", pos1)
p.Legend.Add("2", pos2)
p.Legend.Add("Sum", netDots)
p.Legend.Left = true
p.Legend.ThumbnailWidth = 2 * vg.Millimeter

err = p.Save(100, 100, "testdata/barChart_positiveNegative.png")
if err != nil {
log.Panic(err)
}
}

func TestBarChart_positiveNegative(t *testing.T) {
cmpimg.CheckPlot(ExampleBarChart_positiveNegative, t, "barChart_positiveNegative.png")
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 855031c

Please sign in to comment.