go-chart/stacked_bar_chart.go

477 lines
12 KiB
Go
Raw Normal View History

2016-07-29 03:51:55 +02:00
package chart
import (
"errors"
2016-07-30 04:35:18 +02:00
"fmt"
2016-07-29 03:51:55 +02:00
"io"
2016-07-30 04:40:41 +02:00
"math"
2016-07-29 03:51:55 +02:00
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/seq"
util "github.com/wcharczuk/go-chart/util"
2016-07-29 03:51:55 +02:00
)
2017-06-11 23:56:30 +02:00
// Orientation is an orientation for an element.
type Orientation int
const (
// OrientationDefault is the default orientation.
OrientationDefault Orientation = iota
// OrientationLandscape is the landscape orientation.
OrientationLandscape Orientation = iota
// OrientationPortrait is the portrait orientation.
OrientationPortrait Orientation = iota
)
2016-07-29 03:51:55 +02:00
// StackedBar is a bar within a StackedBarChart.
type StackedBar struct {
Name string
Width int
Values []Value
}
// GetWidth returns the width of the bar.
func (sb StackedBar) GetWidth() int {
if sb.Width == 0 {
2016-07-30 04:35:18 +02:00
return 50
2016-07-29 03:51:55 +02:00
}
return sb.Width
}
// StackedBarChart is a chart that draws sections of a bar based on percentages.
type StackedBarChart struct {
Title string
TitleStyle Style
ColorPalette ColorPalette
2016-07-29 03:51:55 +02:00
Width int
Height int
DPI float64
Background Style
Canvas Style
2016-07-30 04:35:18 +02:00
XAxis Style
YAxis Style
2017-06-11 23:56:30 +02:00
BarSpacing int
Orientation Orientation
2016-07-29 03:51:55 +02:00
Font *truetype.Font
defaultFont *truetype.Font
Bars []StackedBar
Elements []Renderable
}
// GetDPI returns the dpi for the chart.
func (sbc StackedBarChart) GetDPI(defaults ...float64) float64 {
if sbc.DPI == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return DefaultDPI
}
return sbc.DPI
}
// GetFont returns the text font.
func (sbc StackedBarChart) GetFont() *truetype.Font {
if sbc.Font == nil {
return sbc.defaultFont
}
return sbc.Font
}
// GetWidth returns the chart width or the default value.
func (sbc StackedBarChart) GetWidth() int {
if sbc.Width == 0 {
return DefaultChartWidth
}
return sbc.Width
}
// GetHeight returns the chart height or the default value.
func (sbc StackedBarChart) GetHeight() int {
if sbc.Height == 0 {
return DefaultChartWidth
}
return sbc.Height
}
// GetBarSpacing returns the spacing between bars.
func (sbc StackedBarChart) GetBarSpacing() int {
if sbc.BarSpacing == 0 {
return 100
}
return sbc.BarSpacing
}
2017-06-11 23:56:30 +02:00
// GetOrientation gets the orientation or the default.
func (sbc StackedBarChart) GetOrientation() Orientation {
switch sbc.Orientation {
case OrientationDefault:
return OrientationPortrait
default:
return sbc.Orientation
}
}
2016-07-29 03:51:55 +02:00
// Render renders the chart with the given renderer to the given io.Writer.
func (sbc StackedBarChart) Render(rp RendererProvider, w io.Writer) error {
if len(sbc.Bars) == 0 {
2017-02-03 20:26:53 +01:00
return errors.New("please provide at least one bar")
2016-07-29 03:51:55 +02:00
}
r, err := rp(sbc.GetWidth(), sbc.GetHeight())
if err != nil {
return err
}
if sbc.Font == nil {
defaultFont, err := GetDefaultFont()
if err != nil {
return err
}
sbc.defaultFont = defaultFont
}
r.SetDPI(sbc.GetDPI(DefaultDPI))
canvasBox := sbc.getAdjustedCanvasBox(r, sbc.getDefaultCanvasBox())
sbc.drawCanvas(r, canvasBox)
2016-07-29 03:51:55 +02:00
sbc.drawBars(r, canvasBox)
2016-07-30 04:35:18 +02:00
sbc.drawXAxis(r, canvasBox)
sbc.drawYAxis(r, canvasBox)
2016-07-29 03:51:55 +02:00
sbc.drawTitle(r)
for _, a := range sbc.Elements {
a(r, canvasBox, sbc.styleDefaultsElements())
}
return r.Save(w)
}
func (sbc StackedBarChart) drawCanvas(r Renderer, canvasBox Box) {
Draw.Box(r, canvasBox, sbc.getCanvasStyle())
}
2016-07-29 03:51:55 +02:00
func (sbc StackedBarChart) drawBars(r Renderer, canvasBox Box) {
2017-06-11 23:56:30 +02:00
var offset int
if sbc.GetOrientation() == OrientationPortrait {
offset = canvasBox.Left
} else {
offset = canvasBox.Top
}
2016-07-29 03:51:55 +02:00
for _, bar := range sbc.Bars {
2017-06-11 23:56:30 +02:00
sbc.drawBar(r, canvasBox, offset, bar)
offset += (sbc.GetBarSpacing() + bar.GetWidth())
2016-07-29 03:51:55 +02:00
}
}
2017-06-11 23:56:30 +02:00
func (sbc StackedBarChart) drawBar(r Renderer, canvasBox Box, offset int, bar StackedBar) int {
2016-07-30 08:13:33 +02:00
barSpacing2 := sbc.GetBarSpacing() >> 1
2016-07-29 03:51:55 +02:00
normalizedBarComponents := Values(bar.Values).Normalize()
2017-06-11 23:56:30 +02:00
if sbc.GetOrientation() == OrientationPortrait {
bxl := offset + barSpacing2
bxr := bxl + bar.GetWidth()
normalizedBarComponents := Values(bar.Values).Normalize()
yoffset := canvasBox.Top
for index, bv := range normalizedBarComponents {
barHeight := int(math.Ceil(bv.Value * float64(canvasBox.Height())))
barBox := Box{
Top: yoffset,
Left: bxl,
Right: bxr,
Bottom: util.Math.MinInt(yoffset+barHeight, canvasBox.Bottom-DefaultStrokeWidth),
}
Draw.Box(r, barBox, bv.Style.InheritFrom(sbc.styleDefaultsStackedBarValue(index)))
yoffset += barHeight
}
return bxr
}
bxt := offset + barSpacing2
bxb := bxt + bar.GetWidth()
xoffset := canvasBox.Left
2016-07-29 03:51:55 +02:00
for index, bv := range normalizedBarComponents {
2016-07-30 04:40:41 +02:00
barHeight := int(math.Ceil(bv.Value * float64(canvasBox.Height())))
2016-07-30 08:13:33 +02:00
barBox := Box{
2017-06-11 23:56:30 +02:00
Top: bxt,
Left: xoffset,
Right: util.Math.MinInt(xoffset+barHeight, canvasBox.Right-DefaultStrokeWidth),
Bottom: bxb,
2016-07-30 08:13:33 +02:00
}
2016-07-30 01:36:29 +02:00
Draw.Box(r, barBox, bv.Style.InheritFrom(sbc.styleDefaultsStackedBarValue(index)))
2017-06-11 23:56:30 +02:00
xoffset += barHeight
2016-07-29 03:51:55 +02:00
}
2017-06-11 23:56:30 +02:00
return bxb
2016-07-29 03:51:55 +02:00
}
2016-07-30 04:35:18 +02:00
func (sbc StackedBarChart) drawXAxis(r Renderer, canvasBox Box) {
if sbc.XAxis.Show {
axisStyle := sbc.XAxis.InheritFrom(sbc.styleDefaultsAxes())
axisStyle.WriteToRenderer(r)
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.Stroke()
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Left, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke()
cursor := canvasBox.Left
for _, bar := range sbc.Bars {
barLabelBox := Box{
Top: canvasBox.Bottom + DefaultXAxisMargin,
Left: cursor,
2016-07-30 08:13:33 +02:00
Right: cursor + bar.GetWidth() + sbc.GetBarSpacing(),
2016-07-30 04:35:18 +02:00
Bottom: sbc.GetHeight(),
}
if len(bar.Name) > 0 {
Draw.TextWithin(r, bar.Name, barLabelBox, axisStyle)
}
axisStyle.WriteToRenderer(r)
r.MoveTo(barLabelBox.Right, canvasBox.Bottom)
r.LineTo(barLabelBox.Right, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke()
2016-07-30 08:13:33 +02:00
cursor += bar.GetWidth() + sbc.GetBarSpacing()
2016-07-30 04:35:18 +02:00
}
}
}
2016-07-29 03:51:55 +02:00
2016-07-30 04:35:18 +02:00
func (sbc StackedBarChart) drawYAxis(r Renderer, canvasBox Box) {
if sbc.YAxis.Show {
axisStyle := sbc.YAxis.InheritFrom(sbc.styleDefaultsAxes())
axisStyle.WriteToRenderer(r)
r.MoveTo(canvasBox.Right, canvasBox.Top)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.Stroke()
r.MoveTo(canvasBox.Right, canvasBox.Bottom)
r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, canvasBox.Bottom)
r.Stroke()
ticks := seq.RangeWithStep(0.0, 1.0, 0.2)
2016-07-30 04:35:18 +02:00
for _, t := range ticks {
axisStyle.GetStrokeOptions().WriteToRenderer(r)
2016-07-30 04:35:18 +02:00
ty := canvasBox.Bottom - int(t*float64(canvasBox.Height()))
r.MoveTo(canvasBox.Right, ty)
r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, ty)
r.Stroke()
axisStyle.GetTextOptions().WriteToRenderer(r)
2016-07-30 04:35:18 +02:00
text := fmt.Sprintf("%0.0f%%", t*100)
tb := r.MeasureText(text)
Draw.Text(r, text, canvasBox.Right+DefaultYAxisMargin+5, ty+(tb.Height()>>1), axisStyle)
2016-07-30 04:35:18 +02:00
}
2016-07-29 03:51:55 +02:00
2016-07-30 04:35:18 +02:00
}
}
2016-07-29 03:51:55 +02:00
2016-07-30 04:35:18 +02:00
func (sbc StackedBarChart) drawTitle(r Renderer) {
if len(sbc.Title) > 0 && sbc.TitleStyle.Show {
r.SetFont(sbc.TitleStyle.GetFont(sbc.GetFont()))
r.SetFontColor(sbc.TitleStyle.GetFontColor(sbc.GetColorPalette().TextColor()))
titleFontSize := sbc.TitleStyle.GetFontSize(DefaultTitleFontSize)
r.SetFontSize(titleFontSize)
textBox := r.MeasureText(sbc.Title)
textWidth := textBox.Width()
textHeight := textBox.Height()
titleX := (sbc.GetWidth() >> 1) - (textWidth >> 1)
titleY := sbc.TitleStyle.Padding.GetTop(DefaultTitleTop) + textHeight
r.Text(sbc.Title, titleX, titleY)
}
}
func (sbc StackedBarChart) getCanvasStyle() Style {
return sbc.Canvas.InheritFrom(sbc.styleDefaultsCanvas())
}
func (sbc StackedBarChart) styleDefaultsCanvas() Style {
return Style{
FillColor: sbc.GetColorPalette().CanvasColor(),
StrokeColor: sbc.GetColorPalette().CanvasStrokeColor(),
StrokeWidth: DefaultCanvasStrokeWidth,
}
}
// GetColorPalette returns the color palette for the chart.
func (sbc StackedBarChart) GetColorPalette() ColorPalette {
if sbc.ColorPalette != nil {
return sbc.ColorPalette
2016-07-29 03:51:55 +02:00
}
return AlternateColorPalette
2016-07-29 03:51:55 +02:00
}
func (sbc StackedBarChart) getDefaultCanvasBox() Box {
return sbc.Box()
}
func (sbc StackedBarChart) getAdjustedCanvasBox(r Renderer, canvasBox Box) Box {
2017-06-11 23:56:30 +02:00
var total int
2016-07-30 08:13:33 +02:00
for _, bar := range sbc.Bars {
2017-06-11 23:56:30 +02:00
total += bar.GetWidth() + sbc.GetBarSpacing()
2016-07-29 03:51:55 +02:00
}
if sbc.XAxis.Show {
2017-06-11 23:56:30 +02:00
if sbc.GetOrientation() == OrientationPortrait {
return Box{
Top: canvasBox.Top,
Left: canvasBox.Left,
Right: canvasBox.Left + total,
Bottom: sbc.GetHeight() - sbc.measurePortraitAxisHeight(r, canvasBox),
}
}
2017-06-11 23:56:30 +02:00
return Box{
Top: canvasBox.Top,
Left: canvasBox.Left,
2017-06-11 23:56:30 +02:00
Right: sbc.GetWidth() - sbc.measureLandscapeAxisWidth(r, canvasBox),
Bottom: canvasBox.Top + total,
}
}
if sbc.GetOrientation() == OrientationPortrait {
return Box{
Top: canvasBox.Top,
Left: canvasBox.Left,
Right: canvasBox.Left,
Bottom: canvasBox.Top + total,
}
}
2016-07-30 04:35:18 +02:00
return Box{
2016-07-29 03:51:55 +02:00
Top: canvasBox.Top,
Left: canvasBox.Left,
2017-06-11 23:56:30 +02:00
Right: canvasBox.Left + total,
2016-07-29 03:51:55 +02:00
Bottom: canvasBox.Bottom,
2016-07-30 04:35:18 +02:00
}
2017-06-11 23:56:30 +02:00
}
func (sbc StackedBarChart) measurePortraitAxisHeight(r Renderer, canvasBox Box) int {
xaxisHeight := DefaultVerticalTickHeight
axisStyle := sbc.XAxis.InheritFrom(sbc.styleDefaultsAxes())
axisStyle.WriteToRenderer(r)
cursor := canvasBox.Left
for _, bar := range sbc.Bars {
if len(bar.Name) > 0 {
barLabelBox := Box{
Top: canvasBox.Bottom + DefaultXAxisMargin,
Left: cursor,
Right: cursor + bar.GetWidth() + sbc.GetBarSpacing(),
Bottom: sbc.GetHeight(),
}
lines := Text.WrapFit(r, bar.Name, barLabelBox.Width(), axisStyle)
linesBox := Text.MeasureLines(r, lines, axisStyle)
xaxisHeight = util.Math.MaxInt(linesBox.Height()+(2*DefaultXAxisMargin), xaxisHeight)
}
}
return xaxisHeight
}
func (sbc StackedBarChart) measureLandscapeAxisWidth(r Renderer, canvasBox Box) int {
axisWidth := DefaultVerticalTickHeight
axisStyle := sbc.XAxis.InheritFrom(sbc.styleDefaultsAxes())
axisStyle.WriteToRenderer(r)
2017-06-11 23:56:30 +02:00
cursor := canvasBox.Top
for _, bar := range sbc.Bars {
if len(bar.Name) > 0 {
barLabelBox := Box{
Top: cursor,
Left: canvasBox.Right + DefaultXAxisMargin,
Right: sbc.GetWidth(),
Bottom: cursor + bar.GetWidth() + sbc.GetBarSpacing(),
}
lines := Text.WrapFit(r, bar.Name, barLabelBox.Width(), axisStyle)
linesBox := Text.MeasureLines(r, lines, axisStyle)
axisWidth = util.Math.MaxInt(linesBox.Width()+(2*DefaultYAxisMargin), axisWidth)
cursor += bar.GetWidth() + sbc.GetBarSpacing()
}
}
return axisWidth
2016-07-29 03:51:55 +02:00
}
// Box returns the chart bounds as a box.
func (sbc StackedBarChart) Box() Box {
2016-07-30 04:35:18 +02:00
dpr := sbc.Background.Padding.GetRight(10)
dpb := sbc.Background.Padding.GetBottom(50)
2016-07-29 03:51:55 +02:00
return Box{
Top: sbc.Background.Padding.GetTop(20),
Left: sbc.Background.Padding.GetLeft(20),
2016-07-29 03:51:55 +02:00
Right: sbc.GetWidth() - dpr,
Bottom: sbc.GetHeight() - dpb,
}
}
func (sbc StackedBarChart) styleDefaultsStackedBarValue(index int) Style {
return Style{
StrokeColor: sbc.GetColorPalette().GetSeriesColor(index),
2016-07-29 03:51:55 +02:00
StrokeWidth: 3.0,
FillColor: sbc.GetColorPalette().GetSeriesColor(index),
2016-07-29 03:51:55 +02:00
}
}
2016-07-30 04:35:18 +02:00
func (sbc StackedBarChart) styleDefaultsTitle() Style {
return sbc.TitleStyle.InheritFrom(Style{
FontColor: DefaultTextColor,
Font: sbc.GetFont(),
FontSize: sbc.getTitleFontSize(),
TextHorizontalAlign: TextHorizontalAlignCenter,
TextVerticalAlign: TextVerticalAlignTop,
TextWrap: TextWrapWord,
})
}
func (sbc StackedBarChart) getTitleFontSize() float64 {
effectiveDimension := util.Math.MinInt(sbc.GetWidth(), sbc.GetHeight())
2016-07-30 04:35:18 +02:00
if effectiveDimension >= 2048 {
return 48
} else if effectiveDimension >= 1024 {
return 24
} else if effectiveDimension >= 512 {
return 18
} else if effectiveDimension >= 256 {
return 12
}
return 10
}
func (sbc StackedBarChart) styleDefaultsAxes() Style {
return Style{
StrokeColor: DefaultAxisColor,
Font: sbc.GetFont(),
FontSize: DefaultAxisFontSize,
FontColor: DefaultAxisColor,
TextHorizontalAlign: TextHorizontalAlignCenter,
TextVerticalAlign: TextVerticalAlignTop,
TextWrap: TextWrapWord,
}
}
2016-07-29 03:51:55 +02:00
func (sbc StackedBarChart) styleDefaultsElements() Style {
return Style{
Font: sbc.GetFont(),
}
}