2016-11-10 21:25:53 -07:00
// Copyright 2016 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2015-04-17 16:03:22 -06:00
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
package termui
import (
"fmt"
)
// This is the implemetation of multi-colored or stacked bar graph. This is different from default barGraph which is implemented in bar.go
// Multi-Colored-BarChart creates multiple bars in a widget:
/ *
bc := termui . NewMBarChart ( )
data := make ( [ ] [ ] int , 2 )
data [ 0 ] := [ ] int { 3 , 2 , 5 , 7 , 9 , 4 }
data [ 1 ] := [ ] int { 7 , 8 , 5 , 3 , 1 , 6 }
bclabels := [ ] string { "S0" , "S1" , "S2" , "S3" , "S4" , "S5" }
2016-10-14 05:48:50 -06:00
bc . BorderLabel = "Bar Chart"
2015-04-17 16:03:22 -06:00
bc . Data = data
bc . Width = 26
bc . Height = 10
bc . DataLabels = bclabels
bc . TextColor = termui . ColorGreen
bc . BarColor = termui . ColorRed
bc . NumColor = termui . ColorYellow
* /
type MBarChart struct {
Block
BarColor [ NumberofColors ] Attribute
TextColor Attribute
NumColor [ NumberofColors ] Attribute
Data [ NumberofColors ] [ ] int
DataLabels [ ] string
BarWidth int
BarGap int
labels [ ] [ ] rune
dataNum [ NumberofColors ] [ ] [ ] rune
numBar int
scale float64
max int
minDataLen int
numStack int
2015-04-27 18:27:11 -06:00
ShowScale bool
maxScale [ ] rune
2015-04-17 16:03:22 -06:00
}
// NewBarChart returns a new *BarChart with current theme.
func NewMBarChart ( ) * MBarChart {
bc := & MBarChart { Block : * NewBlock ( ) }
2015-10-07 12:25:59 -06:00
bc . BarColor [ 0 ] = ThemeAttr ( "mbarchart.bar.bg" )
bc . NumColor [ 0 ] = ThemeAttr ( "mbarchart.num.fg" )
bc . TextColor = ThemeAttr ( "mbarchart.text.fg" )
2015-04-17 16:03:22 -06:00
bc . BarGap = 1
bc . BarWidth = 3
return bc
}
func ( bc * MBarChart ) layout ( ) {
2015-10-07 12:25:59 -06:00
bc . numBar = bc . innerArea . Dx ( ) / ( bc . BarGap + bc . BarWidth )
2015-04-17 16:03:22 -06:00
bc . labels = make ( [ ] [ ] rune , bc . numBar )
DataLen := 0
LabelLen := len ( bc . DataLabels )
2015-04-27 18:27:11 -06:00
bc . minDataLen = 9999 //Set this to some very hight value so that we find the minimum one We want to know which array among data[][] has got the least length
2015-04-17 16:03:22 -06:00
// We need to know how many stack/data array data[0] , data[1] are there
for i := 0 ; i < len ( bc . Data ) ; i ++ {
if bc . Data [ i ] == nil {
break
}
DataLen ++
}
bc . numStack = DataLen
2015-04-27 18:27:11 -06:00
//We need to know what is the mimimum size of data array data[0] could have 10 elements data[1] could have only 5, so we plot only 5 bar graphs
2015-04-17 16:03:22 -06:00
for i := 0 ; i < DataLen ; i ++ {
if bc . minDataLen > len ( bc . Data [ i ] ) {
bc . minDataLen = len ( bc . Data [ i ] )
}
}
if LabelLen > bc . minDataLen {
LabelLen = bc . minDataLen
}
2015-04-17 22:38:59 -06:00
for i := 0 ; i < LabelLen && i < bc . numBar ; i ++ {
2015-04-17 16:03:22 -06:00
bc . labels [ i ] = trimStr2Runes ( bc . DataLabels [ i ] , bc . BarWidth )
}
for i := 0 ; i < bc . numStack ; i ++ {
bc . dataNum [ i ] = make ( [ ] [ ] rune , len ( bc . Data [ i ] ) )
//For each stack of bar calcualte the rune
2015-04-17 22:38:59 -06:00
for j := 0 ; j < LabelLen && i < bc . numBar ; j ++ {
2015-04-17 16:03:22 -06:00
n := bc . Data [ i ] [ j ]
s := fmt . Sprint ( n )
bc . dataNum [ i ] [ j ] = trimStr2Runes ( s , bc . BarWidth )
}
//If color is not defined by default then populate a color that is different from the prevous bar
if bc . BarColor [ i ] == ColorDefault && bc . NumColor [ i ] == ColorDefault {
if i == 0 {
bc . BarColor [ i ] = ColorBlack
} else {
bc . BarColor [ i ] = bc . BarColor [ i - 1 ] + 1
if bc . BarColor [ i ] > NumberofColors {
bc . BarColor [ i ] = ColorBlack
}
}
bc . NumColor [ i ] = ( NumberofColors + 1 ) - bc . BarColor [ i ] //Make NumColor opposite of barColor for visibility
}
}
//If Max value is not set then we have to populate, this time the max value will be max(sum(d1[0],d2[0],d3[0]) .... sum(d1[n], d2[n], d3[n]))
if bc . max == 0 {
bc . max = - 1
}
for i := 0 ; i < bc . minDataLen && i < LabelLen ; i ++ {
var dsum int
for j := 0 ; j < bc . numStack ; j ++ {
dsum += bc . Data [ j ] [ i ]
}
if dsum > bc . max {
bc . max = dsum
}
}
2015-04-27 18:27:11 -06:00
//Finally Calculate max sale
if bc . ShowScale {
s := fmt . Sprintf ( "%d" , bc . max )
bc . maxScale = trimStr2Runes ( s , len ( s ) )
2015-10-07 12:25:59 -06:00
bc . scale = float64 ( bc . max ) / float64 ( bc . innerArea . Dy ( ) - 2 )
2015-04-27 18:27:11 -06:00
} else {
2015-10-07 12:25:59 -06:00
bc . scale = float64 ( bc . max ) / float64 ( bc . innerArea . Dy ( ) - 1 )
2015-04-27 18:27:11 -06:00
}
2015-04-17 16:03:22 -06:00
}
func ( bc * MBarChart ) SetMax ( max int ) {
if max > 0 {
bc . max = max
}
}
// Buffer implements Bufferer interface.
2015-10-07 12:25:59 -06:00
func ( bc * MBarChart ) Buffer ( ) Buffer {
buf := bc . Block . Buffer ( )
2015-04-17 16:03:22 -06:00
bc . layout ( )
var oftX int
for i := 0 ; i < bc . numBar && i < bc . minDataLen && i < len ( bc . DataLabels ) ; i ++ {
ph := 0 //Previous Height to stack up
2015-04-27 18:27:11 -06:00
oftX = i * ( bc . BarWidth + bc . BarGap )
2015-04-17 16:03:22 -06:00
for i1 := 0 ; i1 < bc . numStack ; i1 ++ {
h := int ( float64 ( bc . Data [ i1 ] [ i ] ) / bc . scale )
// plot bars
for j := 0 ; j < bc . BarWidth ; j ++ {
for k := 0 ; k < h ; k ++ {
2015-10-07 12:25:59 -06:00
c := Cell {
Ch : ' ' ,
Bg : bc . BarColor [ i1 ] ,
}
2015-04-17 16:03:22 -06:00
if bc . BarColor [ i1 ] == ColorDefault { // when color is default, space char treated as transparent!
2015-10-07 12:25:59 -06:00
c . Bg |= AttrReverse
2015-04-17 16:03:22 -06:00
}
2015-10-07 12:25:59 -06:00
x := bc . innerArea . Min . X + i * ( bc . BarWidth + bc . BarGap ) + j
y := bc . innerArea . Min . Y + bc . innerArea . Dy ( ) - 2 - k - ph
buf . Set ( x , y , c )
2015-04-17 16:03:22 -06:00
}
}
ph += h
}
// plot text
for j , k := 0 , 0 ; j < len ( bc . labels [ i ] ) ; j ++ {
w := charWidth ( bc . labels [ i ] [ j ] )
2015-10-07 12:25:59 -06:00
c := Cell {
Ch : bc . labels [ i ] [ j ] ,
Bg : bc . Bg ,
Fg : bc . TextColor ,
}
y := bc . innerArea . Min . Y + bc . innerArea . Dy ( ) - 1
x := bc . innerArea . Max . X + oftX + ( ( bc . BarWidth - len ( bc . labels [ i ] ) ) / 2 ) + k
buf . Set ( x , y , c )
2015-04-17 16:03:22 -06:00
k += w
}
// plot num
ph = 0 //re-initialize previous height
for i1 := 0 ; i1 < bc . numStack ; i1 ++ {
h := int ( float64 ( bc . Data [ i1 ] [ i ] ) / bc . scale )
2015-04-17 22:38:59 -06:00
for j := 0 ; j < len ( bc . dataNum [ i1 ] [ i ] ) && h > 0 ; j ++ {
2015-10-07 12:25:59 -06:00
c := Cell {
Ch : bc . dataNum [ i1 ] [ i ] [ j ] ,
Fg : bc . NumColor [ i1 ] ,
Bg : bc . BarColor [ i1 ] ,
}
2015-04-17 16:03:22 -06:00
if bc . BarColor [ i1 ] == ColorDefault { // the same as above
2015-10-07 12:25:59 -06:00
c . Bg |= AttrReverse
2015-04-17 16:03:22 -06:00
}
if h == 0 {
2015-10-07 12:25:59 -06:00
c . Bg = bc . Bg
2015-04-17 16:03:22 -06:00
}
2015-10-07 12:25:59 -06:00
x := bc . innerArea . Min . X + oftX + ( bc . BarWidth - len ( bc . dataNum [ i1 ] [ i ] ) ) / 2 + j
y := bc . innerArea . Min . Y + bc . innerArea . Dy ( ) - 2 - ph
buf . Set ( x , y , c )
2015-04-17 16:03:22 -06:00
}
ph += h
}
}
2015-04-27 18:27:11 -06:00
if bc . ShowScale {
//Currently bar graph only supprts data range from 0 to MAX
//Plot 0
2015-10-07 12:25:59 -06:00
c := Cell {
Ch : '0' ,
Bg : bc . Bg ,
Fg : bc . TextColor ,
}
y := bc . innerArea . Min . Y + bc . innerArea . Dy ( ) - 2
x := bc . X
buf . Set ( x , y , c )
2015-04-27 18:27:11 -06:00
//Plot the maximum sacle value
for i := 0 ; i < len ( bc . maxScale ) ; i ++ {
2015-10-07 12:25:59 -06:00
c := Cell {
Ch : bc . maxScale [ i ] ,
Bg : bc . Bg ,
Fg : bc . TextColor ,
}
y := bc . innerArea . Min . Y
x := bc . X + i
buf . Set ( x , y , c )
2015-04-27 18:27:11 -06:00
}
}
2015-10-07 12:25:59 -06:00
return buf
2015-04-17 16:03:22 -06:00
}