2018-06-20 09:54:27 +08:00
|
|
|
// Copyright 2018 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-06-19 04:36:29 +08:00
|
|
|
package barchart
|
|
|
|
|
|
|
|
// options.go contains configurable options for BarChart.
|
|
|
|
|
|
|
|
import (
|
2019-02-15 12:37:06 +08:00
|
|
|
"fmt"
|
|
|
|
|
2019-02-24 14:13:26 +08:00
|
|
|
"github.com/mum4k/termdash/cell"
|
2020-04-11 03:26:45 +08:00
|
|
|
"github.com/mum4k/termdash/private/draw"
|
2018-06-19 04:36:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Option is used to provide options.
|
|
|
|
type Option interface {
|
|
|
|
// set sets the provided option.
|
|
|
|
set(*options)
|
|
|
|
}
|
|
|
|
|
2018-06-20 09:37:06 +08:00
|
|
|
// option implements Option.
|
|
|
|
type option func(*options)
|
|
|
|
|
|
|
|
// set implements Option.set.
|
|
|
|
func (o option) set(opts *options) {
|
|
|
|
o(opts)
|
|
|
|
}
|
|
|
|
|
2018-06-19 04:36:29 +08:00
|
|
|
// options holds the provided options.
|
|
|
|
type options struct {
|
|
|
|
barChar rune
|
|
|
|
barWidth int
|
|
|
|
barGap int
|
|
|
|
showValues bool
|
|
|
|
barColors []cell.Color
|
|
|
|
labelColors []cell.Color
|
|
|
|
valueColors []cell.Color
|
|
|
|
labels []string
|
|
|
|
}
|
|
|
|
|
2019-02-15 12:37:06 +08:00
|
|
|
// validate validates the provided options.
|
|
|
|
func (o *options) validate() error {
|
|
|
|
if got, min := o.barWidth, 0; got < min {
|
|
|
|
return fmt.Errorf("invalid BarWidth %d, must be %d <= BarWidth", got, min)
|
|
|
|
}
|
|
|
|
if got, min := o.barGap, 0; got < min {
|
|
|
|
return fmt.Errorf("invalid BarGap %d, must be %d <= BarGap", got, min)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-19 04:36:29 +08:00
|
|
|
// newOptions returns options with the default values set.
|
|
|
|
func newOptions() *options {
|
|
|
|
return &options{
|
|
|
|
barChar: DefaultChar,
|
|
|
|
barGap: DefaultBarGap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultChar is the default value for the Char option.
|
|
|
|
const DefaultChar = draw.DefaultRectChar
|
|
|
|
|
|
|
|
// Char sets the rune that is used when drawing the rectangle representing the
|
|
|
|
// bars.
|
|
|
|
func Char(ch rune) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.barChar = ch
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-15 12:37:06 +08:00
|
|
|
// BarWidth sets the width of the bars. If not set, or set to zero, the bars
|
2019-02-15 13:05:45 +08:00
|
|
|
// use all the space available to the widget. Must be a positive or zero
|
2019-02-15 12:37:06 +08:00
|
|
|
// integer.
|
2018-06-19 04:36:29 +08:00
|
|
|
func BarWidth(width int) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.barWidth = width
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultBarGap is the default value for the BarGap option.
|
|
|
|
const DefaultBarGap = 1
|
|
|
|
|
|
|
|
// BarGap sets the width of the space between the bars.
|
2019-02-15 13:05:45 +08:00
|
|
|
// Must be a positive or zero integer.
|
2018-06-19 04:36:29 +08:00
|
|
|
// Defaults to DefaultBarGap.
|
|
|
|
func BarGap(width int) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.barGap = width
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowValues tells the bar chart to display the actual values inside each of the bars.
|
|
|
|
func ShowValues() Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.showValues = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultBarColor is the default color of a bar, unless specified otherwise
|
|
|
|
// via the BarColors option.
|
|
|
|
const DefaultBarColor = cell.ColorRed
|
|
|
|
|
|
|
|
// BarColors sets the colors of each of the bars.
|
|
|
|
// Bars are created on a call to Values(), each value ends up in its own Bar.
|
|
|
|
// The first supplied color applies to the bar displaying the first value.
|
|
|
|
// Any bars that don't have a color specified use the DefaultBarColor.
|
|
|
|
func BarColors(colors []cell.Color) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.barColors = colors
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultLabelColor is the default color of a bar label, unless specified
|
|
|
|
// otherwise via the LabelColors option.
|
|
|
|
const DefaultLabelColor = cell.ColorGreen
|
|
|
|
|
|
|
|
// LabelColors sets the colors of each of the labels under the bars.
|
|
|
|
// Bars are created on a call to Values(), each value ends up in its own Bar.
|
|
|
|
// The first supplied color applies to the label of the bar displaying the
|
|
|
|
// first value. Any labels that don't have a color specified use the
|
|
|
|
// DefaultLabelColor.
|
|
|
|
func LabelColors(colors []cell.Color) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.labelColors = colors
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Labels sets the labels displayed under each bar,
|
|
|
|
// Bars are created on a call to Values(), each value ends up in its own Bar.
|
|
|
|
// The first supplied label applies to the bar displaying the first value.
|
|
|
|
// If not specified, the corresponding bar (or all the bars) don't have a
|
|
|
|
// label.
|
|
|
|
func Labels(labels []string) Option {
|
|
|
|
return option(func(opts *options) {
|
2019-03-21 11:20:16 +08:00
|
|
|
// Copy to avoid external modifications. See #174.
|
|
|
|
opts.labels = make([]string, len(labels))
|
|
|
|
copy(opts.labels, labels)
|
2018-06-19 04:36:29 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultValueColor is the default color of a bar value, unless specified
|
|
|
|
// otherwise via the ValueColors option.
|
|
|
|
const DefaultValueColor = cell.ColorYellow
|
|
|
|
|
|
|
|
// ValueColors sets the colors of each of the values in the bars. Bars are
|
|
|
|
// created on a call to Values(), each value ends up in its own Bar. The first
|
|
|
|
// supplied color applies to the bar displaying the first value. Any values
|
|
|
|
// that don't have a color specified use the DefaultValueColor.
|
|
|
|
func ValueColors(colors []cell.Color) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.valueColors = colors
|
|
|
|
})
|
|
|
|
}
|