docs: update documents

This commit is contained in:
vicanso 2022-06-22 21:04:16 +08:00
parent 92458aece2
commit 706896737b
18 changed files with 83 additions and 18 deletions

View file

@ -32,6 +32,7 @@ type barChart struct {
opt *BarChartOption opt *BarChartOption
} }
// NewBarChart returns a bar chart renderer
func NewBarChart(p *Painter, opt BarChartOption) *barChart { func NewBarChart(p *Painter, opt BarChartOption) *barChart {
if opt.Theme == nil { if opt.Theme == nil {
opt.Theme = defaultTheme opt.Theme = defaultTheme
@ -43,6 +44,7 @@ func NewBarChart(p *Painter, opt BarChartOption) *barChart {
} }
type BarChartOption struct { type BarChartOption struct {
// The theme
Theme ColorPalette Theme ColorPalette
// The font size // The font size
Font *truetype.Font Font *truetype.Font
@ -155,7 +157,7 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
if distance == 0 { if distance == 0 {
distance = 5 distance = 5
} }
text := NewValueLabelFormater(seriesNames, series.Label.Formatter)(index, item.Value, -1) text := NewValueLabelFormatter(seriesNames, series.Label.Formatter)(index, item.Value, -1)
labelStyle := Style{ labelStyle := Style{
FontColor: theme.GetTextColor(), FontColor: theme.GetTextColor(),
FontSize: labelFontSize, FontSize: labelFontSize,

View file

@ -35,11 +35,14 @@ const defaultStrokeWidth = 2.0
var defaultChartWidth = 600 var defaultChartWidth = 600
var defaultChartHeight = 400 var defaultChartHeight = 400
// SetDefaultWidth sets default width of chart
func SetDefaultWidth(width int) { func SetDefaultWidth(width int) {
if width > 0 { if width > 0 {
defaultChartWidth = width defaultChartWidth = width
} }
} }
// SetDefaultHeight sets default height of chart
func SetDefaultHeight(height int) { func SetDefaultHeight(height int) {
if height > 0 { if height > 0 {
defaultChartHeight = height defaultChartHeight = height

View file

@ -34,6 +34,7 @@ type funnelChart struct {
opt *FunnelChartOption opt *FunnelChartOption
} }
// NewFunnelSeriesList returns a series list for funnel
func NewFunnelSeriesList(values []float64) SeriesList { func NewFunnelSeriesList(values []float64) SeriesList {
seriesList := make(SeriesList, len(values)) seriesList := make(SeriesList, len(values))
for index, value := range values { for index, value := range values {
@ -44,6 +45,7 @@ func NewFunnelSeriesList(values []float64) SeriesList {
return seriesList return seriesList
} }
// NewFunnelChart returns a funnel chart renderer
func NewFunnelChart(p *Painter, opt FunnelChartOption) *funnelChart { func NewFunnelChart(p *Painter, opt FunnelChartOption) *funnelChart {
if opt.Theme == nil { if opt.Theme == nil {
opt.Theme = defaultTheme opt.Theme = defaultTheme
@ -55,6 +57,7 @@ func NewFunnelChart(p *Painter, opt FunnelChartOption) *funnelChart {
} }
type FunnelChartOption struct { type FunnelChartOption struct {
// The theme
Theme ColorPalette Theme ColorPalette
// The font size // The font size
Font *truetype.Font Font *truetype.Font

23
grid.go
View file

@ -28,16 +28,25 @@ type gridPainter struct {
} }
type GridPainterOption struct { type GridPainterOption struct {
StrokeWidth float64 // The stroke width
StrokeColor Color StrokeWidth float64
Column int // The stroke color
Row int StrokeColor Color
IgnoreFirstRow bool // The column of grid
IgnoreLastRow bool Column int
// The row of grid
Row int
// Ignore first row
IgnoreFirstRow bool
// Ignore last row
IgnoreLastRow bool
// Ignore first column
IgnoreFirstColumn bool IgnoreFirstColumn bool
IgnoreLastColumn bool // Ignore last column
IgnoreLastColumn bool
} }
// NewGridPainter returns new a grid renderer
func NewGridPainter(p *Painter, opt GridPainterOption) *gridPainter { func NewGridPainter(p *Painter, opt GridPainterOption) *gridPainter {
return &gridPainter{ return &gridPainter{
p: p, p: p,

View file

@ -33,6 +33,7 @@ type horizontalBarChart struct {
} }
type HorizontalBarChartOption struct { type HorizontalBarChartOption struct {
// The theme
Theme ColorPalette Theme ColorPalette
// The font size // The font size
Font *truetype.Font Font *truetype.Font
@ -50,6 +51,7 @@ type HorizontalBarChartOption struct {
Legend LegendOption Legend LegendOption
} }
// NewHorizontalBarChart returns a horizontal bar chart renderer
func NewHorizontalBarChart(p *Painter, opt HorizontalBarChartOption) *horizontalBarChart { func NewHorizontalBarChart(p *Painter, opt HorizontalBarChartOption) *horizontalBarChart {
if opt.Theme == nil { if opt.Theme == nil {
opt.Theme = defaultTheme opt.Theme = defaultTheme

View file

@ -36,6 +36,7 @@ const IconRect = "rect"
const IconLineDot = "lineDot" const IconLineDot = "lineDot"
type LegendOption struct { type LegendOption struct {
// The theme
Theme ColorPalette Theme ColorPalette
// Text array of legend // Text array of legend
Data []string Data []string
@ -60,6 +61,7 @@ type LegendOption struct {
Show *bool Show *bool
} }
// NewLegendOption returns a legend option
func NewLegendOption(labels []string, left ...string) LegendOption { func NewLegendOption(labels []string, left ...string) LegendOption {
opt := LegendOption{ opt := LegendOption{
Data: labels, Data: labels,
@ -70,6 +72,7 @@ func NewLegendOption(labels []string, left ...string) LegendOption {
return opt return opt
} }
// IsEmpty checks legend is empty
func (opt *LegendOption) IsEmpty() bool { func (opt *LegendOption) IsEmpty() bool {
isEmpty := true isEmpty := true
for _, v := range opt.Data { for _, v := range opt.Data {
@ -81,6 +84,7 @@ func (opt *LegendOption) IsEmpty() bool {
return isEmpty return isEmpty
} }
// NewLegendPainter returns a legend renderer
func NewLegendPainter(p *Painter, opt LegendOption) *legendPainter { func NewLegendPainter(p *Painter, opt LegendOption) *legendPainter {
return &legendPainter{ return &legendPainter{
p: p, p: p,

View file

@ -32,6 +32,7 @@ type lineChart struct {
opt *LineChartOption opt *LineChartOption
} }
// NewLineChart returns a line chart render
func NewLineChart(p *Painter, opt LineChartOption) *lineChart { func NewLineChart(p *Painter, opt LineChartOption) *lineChart {
if opt.Theme == nil { if opt.Theme == nil {
opt.Theme = defaultTheme opt.Theme = defaultTheme
@ -43,6 +44,7 @@ func NewLineChart(p *Painter, opt LineChartOption) *lineChart {
} }
type LineChartOption struct { type LineChartOption struct {
// The theme
Theme ColorPalette Theme ColorPalette
// The font size // The font size
Font *truetype.Font Font *truetype.Font

View file

@ -27,6 +27,7 @@ import (
"github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
) )
// NewMarkLine returns a series mark line
func NewMarkLine(markLineTypes ...string) SeriesMarkLine { func NewMarkLine(markLineTypes ...string) SeriesMarkLine {
data := make([]SeriesMarkData, len(markLineTypes)) data := make([]SeriesMarkData, len(markLineTypes))
for index, t := range markLineTypes { for index, t := range markLineTypes {
@ -48,6 +49,7 @@ func (m *markLinePainter) Add(opt markLineRenderOption) {
m.options = append(m.options, opt) m.options = append(m.options, opt)
} }
// NewMarkLinePainter returns a mark line renderer
func NewMarkLinePainter(p *Painter) *markLinePainter { func NewMarkLinePainter(p *Painter) *markLinePainter {
return &markLinePainter{ return &markLinePainter{
p: p, p: p,

View file

@ -27,6 +27,7 @@ import (
"github.com/wcharczuk/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2/drawing"
) )
// NewMarkPoint returns a series mark point
func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint { func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint {
data := make([]SeriesMarkData, len(markPointTypes)) data := make([]SeriesMarkData, len(markPointTypes))
for index, t := range markPointTypes { for index, t := range markPointTypes {
@ -55,6 +56,7 @@ type markPointRenderOption struct {
Points []Point Points []Point
} }
// NewMarkPointPainter returns a mark point renderer
func NewMarkPointPainter(p *Painter) *markPointPainter { func NewMarkPointPainter(p *Painter) *markPointPainter {
return &markPointPainter{ return &markPointPainter{
p: p, p: p,

View file

@ -136,7 +136,7 @@ func PainterWidthHeightOption(width, height int) PainterOption {
} }
} }
// NewPainter creates a new painter // NewPainter creates a painter
func NewPainter(opts PainterOptions, opt ...PainterOption) (*Painter, error) { func NewPainter(opts PainterOptions, opt ...PainterOption) (*Painter, error) {
if opts.Width <= 0 || opts.Height <= 0 { if opts.Width <= 0 || opts.Height <= 0 {
return nil, errors.New("width/height can not be nil") return nil, errors.New("width/height can not be nil")

View file

@ -36,6 +36,7 @@ type pieChart struct {
} }
type PieChartOption struct { type PieChartOption struct {
// The theme
Theme ColorPalette Theme ColorPalette
// The font size // The font size
Font *truetype.Font Font *truetype.Font
@ -51,6 +52,7 @@ type PieChartOption struct {
backgroundIsFilled bool backgroundIsFilled bool
} }
// NewPieChart returns a pie chart renderer
func NewPieChart(p *Painter, opt PieChartOption) *pieChart { func NewPieChart(p *Painter, opt PieChartOption) *pieChart {
if opt.Theme == nil { if opt.Theme == nil {
opt.Theme = defaultTheme opt.Theme = defaultTheme

View file

@ -45,6 +45,7 @@ type RadarIndicator struct {
} }
type RadarChartOption struct { type RadarChartOption struct {
// The theme
Theme ColorPalette Theme ColorPalette
// The font size // The font size
Font *truetype.Font Font *truetype.Font
@ -62,6 +63,7 @@ type RadarChartOption struct {
backgroundIsFilled bool backgroundIsFilled bool
} }
// NewRadarIndicators returns a radar indicator list
func NewRadarIndicators(names []string, values []float64) []RadarIndicator { func NewRadarIndicators(names []string, values []float64) []RadarIndicator {
if len(names) != len(values) { if len(names) != len(values) {
return nil return nil
@ -76,6 +78,7 @@ func NewRadarIndicators(names []string, values []float64) []RadarIndicator {
return indicators return indicators
} }
// NewRadarChart returns a radar chart renderer
func NewRadarChart(p *Painter, opt RadarChartOption) *radarChart { func NewRadarChart(p *Painter, opt RadarChartOption) *radarChart {
if opt.Theme == nil { if opt.Theme == nil {
opt.Theme = defaultTheme opt.Theme = defaultTheme

View file

@ -37,13 +37,19 @@ type axisRange struct {
} }
type AxisRangeOption struct { type AxisRangeOption struct {
Min float64 // The min value of axis
Max float64 Min float64
Size int // The max value of axis
Boundary bool Max float64
// The size of axis
Size int
// Boundary gap
Boundary bool
// The count of divide
DivideCount int DivideCount int
} }
// NewRange returns a axis range
func NewRange(opt AxisRangeOption) axisRange { func NewRange(opt AxisRangeOption) axisRange {
max := opt.Max max := opt.Max
min := opt.Min min := opt.Min
@ -88,6 +94,7 @@ func NewRange(opt AxisRangeOption) axisRange {
} }
} }
// Values returns values of range
func (r axisRange) Values() []string { func (r axisRange) Values() []string {
offset := (r.max - r.min) / float64(r.divideCount) offset := (r.max - r.min) / float64(r.divideCount)
values := make([]string, 0) values := make([]string, 0)
@ -108,10 +115,13 @@ func (r *axisRange) getRestHeight(value float64) int {
return r.size - r.getHeight(value) return r.size - r.getHeight(value)
} }
// GetRange returns a range of index
func (r *axisRange) GetRange(index int) (float64, float64) { func (r *axisRange) GetRange(index int) (float64, float64) {
unit := float64(r.size) / float64(r.divideCount) unit := float64(r.size) / float64(r.divideCount)
return unit * float64(index), unit * float64(index+1) return unit * float64(index), unit * float64(index+1)
} }
// AutoDivide divides the axis
func (r *axisRange) AutoDivide() []int { func (r *axisRange) AutoDivide() []int {
return autoDivide(r.size, r.divideCount) return autoDivide(r.size, r.divideCount)
} }

View file

@ -36,6 +36,7 @@ type SeriesData struct {
Style Style Style Style
} }
// NewSeriesListDataFromValues returns a series list
func NewSeriesListDataFromValues(values [][]float64, chartType ...string) SeriesList { func NewSeriesListDataFromValues(values [][]float64, chartType ...string) SeriesList {
seriesList := make(SeriesList, len(values)) seriesList := make(SeriesList, len(values))
for index, value := range values { for index, value := range values {
@ -44,6 +45,7 @@ func NewSeriesListDataFromValues(values [][]float64, chartType ...string) Series
return seriesList return seriesList
} }
// NewSeriesFromValues returns a series
func NewSeriesFromValues(values []float64, chartType ...string) Series { func NewSeriesFromValues(values []float64, chartType ...string) Series {
s := Series{ s := Series{
Data: NewSeriesDataFromValues(values), Data: NewSeriesDataFromValues(values),
@ -54,6 +56,7 @@ func NewSeriesFromValues(values []float64, chartType ...string) Series {
return s return s
} }
// NewSeriesDataFromValues return a series data
func NewSeriesDataFromValues(values []float64) []SeriesData { func NewSeriesDataFromValues(values []float64) []SeriesData {
data := make([]SeriesData, len(values)) data := make([]SeriesData, len(values))
for index, value := range values { for index, value := range values {
@ -204,13 +207,19 @@ func NewPieSeriesList(values []float64, opts ...PieSeriesOption) SeriesList {
} }
type seriesSummary struct { type seriesSummary struct {
MaxIndex int // The index of max value
MaxValue float64 MaxIndex int
MinIndex int // The max value
MinValue float64 MaxValue float64
// The index of min value
MinIndex int
// The min value
MinValue float64
// THe average value
AverageValue float64 AverageValue float64
} }
// Summary get summary of series
func (s *Series) Summary() seriesSummary { func (s *Series) Summary() seriesSummary {
minIndex := -1 minIndex := -1
maxIndex := -1 maxIndex := -1
@ -237,6 +246,7 @@ func (s *Series) Summary() seriesSummary {
} }
} }
// Names returns the names of series list
func (sl SeriesList) Names() []string { func (sl SeriesList) Names() []string {
names := make([]string, len(sl)) names := make([]string, len(sl))
for index, s := range sl { for index, s := range sl {
@ -245,8 +255,10 @@ func (sl SeriesList) Names() []string {
return names return names
} }
// LabelFormatter label formatter
type LabelFormatter func(index int, value float64, percent float64) string type LabelFormatter func(index int, value float64, percent float64) string
// NewPieLabelFormatter returns a pie label formatter
func NewPieLabelFormatter(seriesNames []string, layout string) LabelFormatter { func NewPieLabelFormatter(seriesNames []string, layout string) LabelFormatter {
if len(layout) == 0 { if len(layout) == 0 {
layout = "{b}: {d}" layout = "{b}: {d}"
@ -254,13 +266,15 @@ func NewPieLabelFormatter(seriesNames []string, layout string) LabelFormatter {
return NewLabelFormatter(seriesNames, layout) return NewLabelFormatter(seriesNames, layout)
} }
func NewValueLabelFormater(seriesNames []string, layout string) LabelFormatter { // NewValueLabelFormatter returns a value formatter
func NewValueLabelFormatter(seriesNames []string, layout string) LabelFormatter {
if len(layout) == 0 { if len(layout) == 0 {
layout = "{c}" layout = "{c}"
} }
return NewLabelFormatter(seriesNames, layout) return NewLabelFormatter(seriesNames, layout)
} }
// NewLabelFormatter returns a label formaatter
func NewLabelFormatter(seriesNames []string, layout string) LabelFormatter { func NewLabelFormatter(seriesNames []string, layout string) LabelFormatter {
return func(index int, value, percent float64) string { return func(index int, value, percent float64) string {
// 如果无percent的则设置为<0 // 如果无percent的则设置为<0

View file

@ -220,6 +220,7 @@ func init() {
SetDefaultTheme(ThemeLight) SetDefaultTheme(ThemeLight)
} }
// SetDefaultTheme sets default theme
func SetDefaultTheme(name string) { func SetDefaultTheme(name string) {
defaultTheme = NewTheme(name) defaultTheme = NewTheme(name)
} }

View file

@ -84,6 +84,7 @@ type titlePainter struct {
opt *TitleOption opt *TitleOption
} }
// NewTitlePainter returns a title renderer
func NewTitlePainter(p *Painter, opt TitleOption) *titlePainter { func NewTitlePainter(p *Painter, opt TitleOption) *titlePainter {
return &titlePainter{ return &titlePainter{
p: p, p: p,

View file

@ -53,6 +53,7 @@ type XAxisOption struct {
const defaultXAxisHeight = 30 const defaultXAxisHeight = 30
// NewXAxisOption returns a x axis option
func NewXAxisOption(data []string, boundaryGap ...*bool) XAxisOption { func NewXAxisOption(data []string, boundaryGap ...*bool) XAxisOption {
opt := XAxisOption{ opt := XAxisOption{
Data: data, Data: data,
@ -89,6 +90,7 @@ func (opt *XAxisOption) ToAxisOption() AxisOption {
return axisOpt return axisOpt
} }
// NewBottomXAxis returns a bottom x axis renderer
func NewBottomXAxis(p *Painter, opt XAxisOption) *axisPainter { func NewBottomXAxis(p *Painter, opt XAxisOption) *axisPainter {
return NewAxisPainter(p, opt.ToAxisOption()) return NewAxisPainter(p, opt.ToAxisOption())
} }

View file

@ -50,6 +50,7 @@ type YAxisOption struct {
isCategoryAxis bool isCategoryAxis bool
} }
// NewYAxisOptions returns a y axis option
func NewYAxisOptions(data []string, others ...[]string) []YAxisOption { func NewYAxisOptions(data []string, others ...[]string) []YAxisOption {
arr := [][]string{ arr := [][]string{
data, data,
@ -95,6 +96,7 @@ func (opt *YAxisOption) ToAxisOption() AxisOption {
return axisOpt return axisOpt
} }
// NewLeftYAxis returns a left y axis renderer
func NewLeftYAxis(p *Painter, opt YAxisOption) *axisPainter { func NewLeftYAxis(p *Painter, opt YAxisOption) *axisPainter {
p = p.Child(PainterPaddingOption(Box{ p = p.Child(PainterPaddingOption(Box{
Bottom: defaultXAxisHeight, Bottom: defaultXAxisHeight,
@ -102,6 +104,7 @@ func NewLeftYAxis(p *Painter, opt YAxisOption) *axisPainter {
return NewAxisPainter(p, opt.ToAxisOption()) return NewAxisPainter(p, opt.ToAxisOption())
} }
// NewRightYAxis returns a right y axis renderer
func NewRightYAxis(p *Painter, opt YAxisOption) *axisPainter { func NewRightYAxis(p *Painter, opt YAxisOption) *axisPainter {
p = p.Child(PainterPaddingOption(Box{ p = p.Child(PainterPaddingOption(Box{
Bottom: defaultXAxisHeight, Bottom: defaultXAxisHeight,