| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"math" |
| 8 |
|
| 9 |
"github.com/golang/freetype/truetype" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
type BarChart struct { |
| 14 |
Title string |
| 15 |
TitleStyle Style |
| 16 |
|
| 17 |
ColorPalette ColorPalette |
| 18 |
|
| 19 |
Width int |
| 20 |
Height int |
| 21 |
DPI float64 |
| 22 |
|
| 23 |
BarWidth int |
| 24 |
|
| 25 |
Background Style |
| 26 |
Canvas Style |
| 27 |
|
| 28 |
XAxis Style |
| 29 |
YAxis YAxis |
| 30 |
|
| 31 |
BarSpacing int |
| 32 |
|
| 33 |
UseBaseValue bool |
| 34 |
BaseValue float64 |
| 35 |
|
| 36 |
Font *truetype.Font |
| 37 |
defaultFont *truetype.Font |
| 38 |
|
| 39 |
Bars []Value |
| 40 |
Elements []Renderable |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func (bc BarChart) GetDPI() float64 { |
| 45 |
if bc.DPI == 0 { |
| 46 |
return DefaultDPI |
| 47 |
} |
| 48 |
return bc.DPI |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
func (bc BarChart) GetFont() *truetype.Font { |
| 53 |
if bc.Font == nil { |
| 54 |
return bc.defaultFont |
| 55 |
} |
| 56 |
return bc.Font |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func (bc BarChart) GetWidth() int { |
| 61 |
if bc.Width == 0 { |
| 62 |
return DefaultChartWidth |
| 63 |
} |
| 64 |
return bc.Width |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (bc BarChart) GetHeight() int { |
| 69 |
if bc.Height == 0 { |
| 70 |
return DefaultChartHeight |
| 71 |
} |
| 72 |
return bc.Height |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
func (bc BarChart) GetBarSpacing() int { |
| 77 |
if bc.BarSpacing == 0 { |
| 78 |
return DefaultBarSpacing |
| 79 |
} |
| 80 |
return bc.BarSpacing |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (bc BarChart) GetBarWidth() int { |
| 85 |
if bc.BarWidth == 0 { |
| 86 |
return DefaultBarWidth |
| 87 |
} |
| 88 |
return bc.BarWidth |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (bc BarChart) Render(rp RendererProvider, w io.Writer) error { |
| 93 |
if len(bc.Bars) == 0 { |
| 94 |
return errors.New("please provide at least one bar") |
| 95 |
} |
| 96 |
|
| 97 |
r, err := rp(bc.GetWidth(), bc.GetHeight()) |
| 98 |
if err != nil { |
| 99 |
return err |
| 100 |
} |
| 101 |
|
| 102 |
if bc.Font == nil { |
| 103 |
defaultFont, err := GetDefaultFont() |
| 104 |
if err != nil { |
| 105 |
return err |
| 106 |
} |
| 107 |
bc.defaultFont = defaultFont |
| 108 |
} |
| 109 |
r.SetDPI(bc.GetDPI()) |
| 110 |
|
| 111 |
bc.drawBackground(r) |
| 112 |
|
| 113 |
var canvasBox Box |
| 114 |
var yt []Tick |
| 115 |
var yr Range |
| 116 |
var yf ValueFormatter |
| 117 |
|
| 118 |
canvasBox = bc.getDefaultCanvasBox() |
| 119 |
yr = bc.getRanges() |
| 120 |
if yr.GetMax()-yr.GetMin() == 0 { |
| 121 |
return fmt.Errorf("invalid data range; cannot be zero") |
| 122 |
} |
| 123 |
yr = bc.setRangeDomains(canvasBox, yr) |
| 124 |
yf = bc.getValueFormatters() |
| 125 |
|
| 126 |
if bc.hasAxes() { |
| 127 |
yt = bc.getAxesTicks(r, yr, yf) |
| 128 |
canvasBox = bc.getAdjustedCanvasBox(r, canvasBox, yr, yt) |
| 129 |
yr = bc.setRangeDomains(canvasBox, yr) |
| 130 |
} |
| 131 |
bc.drawCanvas(r, canvasBox) |
| 132 |
bc.drawBars(r, canvasBox, yr) |
| 133 |
bc.drawXAxis(r, canvasBox) |
| 134 |
bc.drawYAxis(r, canvasBox, yr, yt) |
| 135 |
|
| 136 |
bc.drawTitle(r) |
| 137 |
for _, a := range bc.Elements { |
| 138 |
a(r, canvasBox, bc.styleDefaultsElements()) |
| 139 |
} |
| 140 |
|
| 141 |
return r.Save(w) |
| 142 |
} |
| 143 |
|
| 144 |
func (bc BarChart) drawCanvas(r Renderer, canvasBox Box) { |
| 145 |
Draw.Box(r, canvasBox, bc.getCanvasStyle()) |
| 146 |
} |
| 147 |
|
| 148 |
func (bc BarChart) getRanges() Range { |
| 149 |
var yrange Range |
| 150 |
if bc.YAxis.Range != nil && !bc.YAxis.Range.IsZero() { |
| 151 |
yrange = bc.YAxis.Range |
| 152 |
} else { |
| 153 |
yrange = &ContinuousRange{} |
| 154 |
} |
| 155 |
|
| 156 |
if !yrange.IsZero() { |
| 157 |
return yrange |
| 158 |
} |
| 159 |
|
| 160 |
if len(bc.YAxis.Ticks) > 0 { |
| 161 |
tickMin, tickMax := math.MaxFloat64, -math.MaxFloat64 |
| 162 |
for _, t := range bc.YAxis.Ticks { |
| 163 |
tickMin = math.Min(tickMin, t.Value) |
| 164 |
tickMax = math.Max(tickMax, t.Value) |
| 165 |
} |
| 166 |
yrange.SetMin(tickMin) |
| 167 |
yrange.SetMax(tickMax) |
| 168 |
return yrange |
| 169 |
} |
| 170 |
|
| 171 |
min, max := math.MaxFloat64, -math.MaxFloat64 |
| 172 |
for _, b := range bc.Bars { |
| 173 |
min = math.Min(b.Value, min) |
| 174 |
max = math.Max(b.Value, max) |
| 175 |
} |
| 176 |
|
| 177 |
yrange.SetMin(min) |
| 178 |
yrange.SetMax(max) |
| 179 |
|
| 180 |
return yrange |
| 181 |
} |
| 182 |
|
| 183 |
func (bc BarChart) drawBackground(r Renderer) { |
| 184 |
Draw.Box(r, Box{ |
| 185 |
Right: bc.GetWidth(), |
| 186 |
Bottom: bc.GetHeight(), |
| 187 |
}, bc.getBackgroundStyle()) |
| 188 |
} |
| 189 |
|
| 190 |
func (bc BarChart) drawBars(r Renderer, canvasBox Box, yr Range) { |
| 191 |
xoffset := canvasBox.Left |
| 192 |
|
| 193 |
width, spacing, _ := bc.calculateScaledTotalWidth(canvasBox) |
| 194 |
bs2 := spacing >> 1 |
| 195 |
|
| 196 |
var barBox Box |
| 197 |
var bxl, bxr, by int |
| 198 |
for index, bar := range bc.Bars { |
| 199 |
bxl = xoffset + bs2 |
| 200 |
bxr = bxl + width |
| 201 |
|
| 202 |
by = canvasBox.Bottom - yr.Translate(bar.Value) |
| 203 |
|
| 204 |
if bc.UseBaseValue { |
| 205 |
barBox = Box{ |
| 206 |
Top: by, |
| 207 |
Left: bxl, |
| 208 |
Right: bxr, |
| 209 |
Bottom: canvasBox.Bottom - yr.Translate(bc.BaseValue), |
| 210 |
} |
| 211 |
} else { |
| 212 |
barBox = Box{ |
| 213 |
Top: by, |
| 214 |
Left: bxl, |
| 215 |
Right: bxr, |
| 216 |
Bottom: canvasBox.Bottom, |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
Draw.Box(r, barBox, bar.Style.InheritFrom(bc.styleDefaultsBar(index))) |
| 221 |
|
| 222 |
xoffset += width + spacing |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
func (bc BarChart) drawXAxis(r Renderer, canvasBox Box) { |
| 227 |
if !bc.XAxis.Hidden { |
| 228 |
axisStyle := bc.XAxis.InheritFrom(bc.styleDefaultsAxes()) |
| 229 |
axisStyle.WriteToRenderer(r) |
| 230 |
|
| 231 |
width, spacing, _ := bc.calculateScaledTotalWidth(canvasBox) |
| 232 |
|
| 233 |
r.MoveTo(canvasBox.Left, canvasBox.Bottom) |
| 234 |
r.LineTo(canvasBox.Right, canvasBox.Bottom) |
| 235 |
r.Stroke() |
| 236 |
|
| 237 |
r.MoveTo(canvasBox.Left, canvasBox.Bottom) |
| 238 |
r.LineTo(canvasBox.Left, canvasBox.Bottom+DefaultVerticalTickHeight) |
| 239 |
r.Stroke() |
| 240 |
|
| 241 |
cursor := canvasBox.Left |
| 242 |
for index, bar := range bc.Bars { |
| 243 |
barLabelBox := Box{ |
| 244 |
Top: canvasBox.Bottom + DefaultXAxisMargin, |
| 245 |
Left: cursor, |
| 246 |
Right: cursor + width + spacing, |
| 247 |
Bottom: bc.GetHeight(), |
| 248 |
} |
| 249 |
|
| 250 |
if len(bar.Label) > 0 { |
| 251 |
Draw.TextWithin(r, bar.Label, barLabelBox, axisStyle) |
| 252 |
} |
| 253 |
|
| 254 |
axisStyle.WriteToRenderer(r) |
| 255 |
if index < len(bc.Bars)-1 { |
| 256 |
r.MoveTo(barLabelBox.Right, canvasBox.Bottom) |
| 257 |
r.LineTo(barLabelBox.Right, canvasBox.Bottom+DefaultVerticalTickHeight) |
| 258 |
r.Stroke() |
| 259 |
} |
| 260 |
cursor += width + spacing |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func (bc BarChart) drawYAxis(r Renderer, canvasBox Box, yr Range, ticks []Tick) { |
| 266 |
if !bc.YAxis.Style.Hidden { |
| 267 |
bc.YAxis.Render(r, canvasBox, yr, bc.styleDefaultsAxes(), ticks) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
func (bc BarChart) drawTitle(r Renderer) { |
| 272 |
if len(bc.Title) > 0 && !bc.TitleStyle.Hidden { |
| 273 |
r.SetFont(bc.TitleStyle.GetFont(bc.GetFont())) |
| 274 |
r.SetFontColor(bc.TitleStyle.GetFontColor(bc.GetColorPalette().TextColor())) |
| 275 |
titleFontSize := bc.TitleStyle.GetFontSize(bc.getTitleFontSize()) |
| 276 |
r.SetFontSize(titleFontSize) |
| 277 |
|
| 278 |
textBox := r.MeasureText(bc.Title) |
| 279 |
|
| 280 |
textWidth := textBox.Width() |
| 281 |
textHeight := textBox.Height() |
| 282 |
|
| 283 |
titleX := (bc.GetWidth() >> 1) - (textWidth >> 1) |
| 284 |
titleY := bc.TitleStyle.Padding.GetTop(DefaultTitleTop) + textHeight |
| 285 |
|
| 286 |
r.Text(bc.Title, titleX, titleY) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func (bc BarChart) getCanvasStyle() Style { |
| 291 |
return bc.Canvas.InheritFrom(bc.styleDefaultsCanvas()) |
| 292 |
} |
| 293 |
|
| 294 |
func (bc BarChart) styleDefaultsCanvas() Style { |
| 295 |
return Style{ |
| 296 |
FillColor: bc.GetColorPalette().CanvasColor(), |
| 297 |
StrokeColor: bc.GetColorPalette().CanvasStrokeColor(), |
| 298 |
StrokeWidth: DefaultCanvasStrokeWidth, |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
func (bc BarChart) hasAxes() bool { |
| 303 |
return !bc.YAxis.Style.Hidden |
| 304 |
} |
| 305 |
|
| 306 |
func (bc BarChart) setRangeDomains(canvasBox Box, yr Range) Range { |
| 307 |
yr.SetDomain(canvasBox.Height()) |
| 308 |
return yr |
| 309 |
} |
| 310 |
|
| 311 |
func (bc BarChart) getDefaultCanvasBox() Box { |
| 312 |
return bc.box() |
| 313 |
} |
| 314 |
|
| 315 |
func (bc BarChart) getValueFormatters() ValueFormatter { |
| 316 |
if bc.YAxis.ValueFormatter != nil { |
| 317 |
return bc.YAxis.ValueFormatter |
| 318 |
} |
| 319 |
return FloatValueFormatter |
| 320 |
} |
| 321 |
|
| 322 |
func (bc BarChart) getAxesTicks(r Renderer, yr Range, yf ValueFormatter) (yticks []Tick) { |
| 323 |
if !bc.YAxis.Style.Hidden { |
| 324 |
yticks = bc.YAxis.GetTicks(r, yr, bc.styleDefaultsAxes(), yf) |
| 325 |
} |
| 326 |
return |
| 327 |
} |
| 328 |
|
| 329 |
func (bc BarChart) calculateEffectiveBarSpacing(canvasBox Box) int { |
| 330 |
totalWithBaseSpacing := bc.calculateTotalBarWidth(bc.GetBarWidth(), bc.GetBarSpacing()) |
| 331 |
if totalWithBaseSpacing > canvasBox.Width() { |
| 332 |
lessBarWidths := canvasBox.Width() - (len(bc.Bars) * bc.GetBarWidth()) |
| 333 |
if lessBarWidths > 0 { |
| 334 |
return int(math.Ceil(float64(lessBarWidths) / float64(len(bc.Bars)))) |
| 335 |
} |
| 336 |
return 0 |
| 337 |
} |
| 338 |
return bc.GetBarSpacing() |
| 339 |
} |
| 340 |
|
| 341 |
func (bc BarChart) calculateEffectiveBarWidth(canvasBox Box, spacing int) int { |
| 342 |
totalWithBaseWidth := bc.calculateTotalBarWidth(bc.GetBarWidth(), spacing) |
| 343 |
if totalWithBaseWidth > canvasBox.Width() { |
| 344 |
totalLessBarSpacings := canvasBox.Width() - (len(bc.Bars) * spacing) |
| 345 |
if totalLessBarSpacings > 0 { |
| 346 |
return int(math.Ceil(float64(totalLessBarSpacings) / float64(len(bc.Bars)))) |
| 347 |
} |
| 348 |
return 0 |
| 349 |
} |
| 350 |
return bc.GetBarWidth() |
| 351 |
} |
| 352 |
|
| 353 |
func (bc BarChart) calculateTotalBarWidth(barWidth, spacing int) int { |
| 354 |
return len(bc.Bars) * (barWidth + spacing) |
| 355 |
} |
| 356 |
|
| 357 |
func (bc BarChart) calculateScaledTotalWidth(canvasBox Box) (width, spacing, total int) { |
| 358 |
spacing = bc.calculateEffectiveBarSpacing(canvasBox) |
| 359 |
width = bc.calculateEffectiveBarWidth(canvasBox, spacing) |
| 360 |
total = bc.calculateTotalBarWidth(width, spacing) |
| 361 |
return |
| 362 |
} |
| 363 |
|
| 364 |
func (bc BarChart) getAdjustedCanvasBox(r Renderer, canvasBox Box, yrange Range, yticks []Tick) Box { |
| 365 |
axesOuterBox := canvasBox.Clone() |
| 366 |
|
| 367 |
_, _, totalWidth := bc.calculateScaledTotalWidth(canvasBox) |
| 368 |
|
| 369 |
if !bc.XAxis.Hidden { |
| 370 |
xaxisHeight := DefaultVerticalTickHeight |
| 371 |
|
| 372 |
axisStyle := bc.XAxis.InheritFrom(bc.styleDefaultsAxes()) |
| 373 |
axisStyle.WriteToRenderer(r) |
| 374 |
|
| 375 |
cursor := canvasBox.Left |
| 376 |
for _, bar := range bc.Bars { |
| 377 |
if len(bar.Label) > 0 { |
| 378 |
barLabelBox := Box{ |
| 379 |
Top: canvasBox.Bottom + DefaultXAxisMargin, |
| 380 |
Left: cursor, |
| 381 |
Right: cursor + bc.GetBarWidth() + bc.GetBarSpacing(), |
| 382 |
Bottom: bc.GetHeight(), |
| 383 |
} |
| 384 |
lines := Text.WrapFit(r, bar.Label, barLabelBox.Width(), axisStyle) |
| 385 |
linesBox := Text.MeasureLines(r, lines, axisStyle) |
| 386 |
|
| 387 |
xaxisHeight = MinInt(linesBox.Height()+(2*DefaultXAxisMargin), xaxisHeight) |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
xbox := Box{ |
| 392 |
Top: canvasBox.Top, |
| 393 |
Left: canvasBox.Left, |
| 394 |
Right: canvasBox.Left + totalWidth, |
| 395 |
Bottom: bc.GetHeight() - xaxisHeight, |
| 396 |
} |
| 397 |
|
| 398 |
axesOuterBox = axesOuterBox.Grow(xbox) |
| 399 |
} |
| 400 |
|
| 401 |
if !bc.YAxis.Style.Hidden { |
| 402 |
axesBounds := bc.YAxis.Measure(r, canvasBox, yrange, bc.styleDefaultsAxes(), yticks) |
| 403 |
axesOuterBox = axesOuterBox.Grow(axesBounds) |
| 404 |
} |
| 405 |
|
| 406 |
return canvasBox.OuterConstrain(bc.box(), axesOuterBox) |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
func (bc BarChart) box() Box { |
| 411 |
dpr := bc.Background.Padding.GetRight(10) |
| 412 |
dpb := bc.Background.Padding.GetBottom(50) |
| 413 |
|
| 414 |
return Box{ |
| 415 |
Top: bc.Background.Padding.GetTop(20), |
| 416 |
Left: bc.Background.Padding.GetLeft(20), |
| 417 |
Right: bc.GetWidth() - dpr, |
| 418 |
Bottom: bc.GetHeight() - dpb, |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
func (bc BarChart) getBackgroundStyle() Style { |
| 423 |
return bc.Background.InheritFrom(bc.styleDefaultsBackground()) |
| 424 |
} |
| 425 |
|
| 426 |
func (bc BarChart) styleDefaultsBackground() Style { |
| 427 |
return Style{ |
| 428 |
FillColor: bc.GetColorPalette().BackgroundColor(), |
| 429 |
StrokeColor: bc.GetColorPalette().BackgroundStrokeColor(), |
| 430 |
StrokeWidth: DefaultStrokeWidth, |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
func (bc BarChart) styleDefaultsBar(index int) Style { |
| 435 |
return Style{ |
| 436 |
StrokeColor: bc.GetColorPalette().GetSeriesColor(index), |
| 437 |
StrokeWidth: 3.0, |
| 438 |
FillColor: bc.GetColorPalette().GetSeriesColor(index), |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
func (bc BarChart) styleDefaultsTitle() Style { |
| 443 |
return bc.TitleStyle.InheritFrom(Style{ |
| 444 |
FontColor: bc.GetColorPalette().TextColor(), |
| 445 |
Font: bc.GetFont(), |
| 446 |
FontSize: bc.getTitleFontSize(), |
| 447 |
TextHorizontalAlign: TextHorizontalAlignCenter, |
| 448 |
TextVerticalAlign: TextVerticalAlignTop, |
| 449 |
TextWrap: TextWrapWord, |
| 450 |
}) |
| 451 |
} |
| 452 |
|
| 453 |
func (bc BarChart) getTitleFontSize() float64 { |
| 454 |
effectiveDimension := MinInt(bc.GetWidth(), bc.GetHeight()) |
| 455 |
if effectiveDimension >= 2048 { |
| 456 |
return 48 |
| 457 |
} else if effectiveDimension >= 1024 { |
| 458 |
return 24 |
| 459 |
} else if effectiveDimension >= 512 { |
| 460 |
return 18 |
| 461 |
} else if effectiveDimension >= 256 { |
| 462 |
return 12 |
| 463 |
} |
| 464 |
return 10 |
| 465 |
} |
| 466 |
|
| 467 |
func (bc BarChart) styleDefaultsAxes() Style { |
| 468 |
return Style{ |
| 469 |
StrokeColor: bc.GetColorPalette().AxisStrokeColor(), |
| 470 |
Font: bc.GetFont(), |
| 471 |
FontSize: DefaultAxisFontSize, |
| 472 |
FontColor: bc.GetColorPalette().TextColor(), |
| 473 |
TextHorizontalAlign: TextHorizontalAlignCenter, |
| 474 |
TextVerticalAlign: TextVerticalAlignTop, |
| 475 |
TextWrap: TextWrapWord, |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
func (bc BarChart) styleDefaultsElements() Style { |
| 480 |
return Style{ |
| 481 |
Font: bc.GetFont(), |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
func (bc BarChart) GetColorPalette() ColorPalette { |
| 487 |
if bc.ColorPalette != nil { |
| 488 |
return bc.ColorPalette |
| 489 |
} |
| 490 |
return AlternateColorPalette |
| 491 |
} |