// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
packagetermui
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"}
bc.Border.Label="Bar Chart"
bc.Data=data
bc.Width=26
bc.Height=10
bc.DataLabels=bclabels
bc.TextColor=termui.ColorGreen
bc.BarColor=termui.ColorRed
bc.NumColor=termui.ColorYellow
*/
typeMBarChartstruct{
Block
BarColor[NumberofColors]Attribute
TextColorAttribute
NumColor[NumberofColors]Attribute
Data[NumberofColors][]int
DataLabels[]string
BarWidthint
BarGapint
labels[][]rune
dataNum[NumberofColors][][]rune
numBarint
scalefloat64
maxint
minDataLenint
numStackint
}
// NewBarChart returns a new *BarChart with current theme.
funcNewMBarChart()*MBarChart{
bc:=&MBarChart{Block:*NewBlock()}
bc.BarColor[0]=theme.MBarChartBar
bc.NumColor[0]=theme.MBarChartNum
bc.TextColor=theme.MBarChartText
bc.BarGap=1
bc.BarWidth=3
returnbc
}
func(bc*MBarChart)layout(){
bc.numBar=bc.innerWidth/(bc.BarGap+bc.BarWidth)
bc.labels=make([][]rune,bc.numBar)
DataLen:=0
LabelLen:=len(bc.DataLabels)
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 lenght
// We need to know how many stack/data array data[0] , data[1] are there
fori:=0;i<len(bc.Data);i++{
ifbc.Data[i]==nil{
break
}
DataLen++
}
bc.numStack=DataLen
//We need to 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