Skip to content

Commit

Permalink
plotter: add FillColor field and logic for BoxPlot
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerwelin authored Feb 24, 2021
1 parent 0101d32 commit 875edf3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
21 changes: 17 additions & 4 deletions plotter/boxplot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package plotter

import (
"errors"
"image/color"
"math"
"sort"

Expand Down Expand Up @@ -64,6 +65,10 @@ type BoxPlot struct {
// GlyphStyle is the style of the outside point glyphs.
GlyphStyle draw.GlyphStyle

// FillColor is the color used to fill the box.
// The default is no fill.
FillColor color.Color

// BoxStyle is the line style for the box.
BoxStyle draw.LineStyle

Expand Down Expand Up @@ -206,13 +211,17 @@ func (b *BoxPlot) Plot(c draw.Canvas, plt *plot.Plot) {
aLow := trY(b.AdjLow)
aHigh := trY(b.AdjHigh)

box := c.ClipLinesY([]vg.Point{
pts := []vg.Point{
{X: x - b.Width/2, Y: q1},
{X: x - b.Width/2, Y: q3},
{X: x + b.Width/2, Y: q3},
{X: x + b.Width/2, Y: q1},
{X: x - b.Width/2 - b.BoxStyle.Width/2, Y: q1},
})
}
box := c.ClipLinesY(pts)
if b.FillColor != nil {
c.FillPolygon(b.FillColor, c.ClipPolygonY(pts))
}
c.StrokeLines(b.BoxStyle, box...)

medLine := c.ClipLinesY([]vg.Point{
Expand Down Expand Up @@ -334,13 +343,17 @@ func (b horizBoxPlot) Plot(c draw.Canvas, plt *plot.Plot) {
aLow := trX(b.AdjLow)
aHigh := trX(b.AdjHigh)

box := c.ClipLinesX([]vg.Point{
pts := []vg.Point{
{X: q1, Y: y - b.Width/2},
{X: q3, Y: y - b.Width/2},
{X: q3, Y: y + b.Width/2},
{X: q1, Y: y + b.Width/2},
{X: q1, Y: y - b.Width/2 - b.BoxStyle.Width/2},
})
}
box := c.ClipLinesX(pts)
if b.FillColor != nil {
c.FillPolygon(b.FillColor, c.ClipPolygonX(pts))
}
c.StrokeLines(b.BoxStyle, box...)

medLine := c.ClipLinesX([]vg.Point{
Expand Down
4 changes: 4 additions & 0 deletions plotter/boxplot_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package plotter_test

import (
"fmt"
"image/color"
"log"

"golang.org/x/exp/rand"
Expand Down Expand Up @@ -37,14 +38,17 @@ func ExampleBoxPlot() {
if err != nil {
log.Panic(err)
}
uniBox.FillColor = color.RGBA{127, 188, 165, 1}
normBox, err := plotter.NewBoxPlot(vg.Points(20), 1, normal)
if err != nil {
log.Panic(err)
}
normBox.FillColor = color.RGBA{127, 188, 165, 1}
expBox, err := plotter.NewBoxPlot(vg.Points(20), 2, expon)
if err != nil {
log.Panic(err)
}
expBox.FillColor = color.RGBA{127, 188, 165, 1}

// Make a vertical box plot.
uniLabels, err := uniBox.OutsideLabels(uniform)
Expand Down
Binary file modified plotter/testdata/horizontalBoxPlot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/verticalBoxPlot_golden.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 875edf3

Please sign in to comment.