Skip to content

Commit

Permalink
Feat: basic sunburst chart (#158)
Browse files Browse the repository at this point in the history
* Feat: basic sunburst chart

* chore: add commnet and small tweak

* chore: use `BaseConfiguration` for sunburst
  • Loading branch information
susiwen8 committed Mar 12, 2021
1 parent 5f31fd4 commit 012bf0e
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 8 deletions.
32 changes: 32 additions & 0 deletions charts/series.go
Expand Up @@ -54,12 +54,27 @@ type SingleSeries struct {
SizeRange []float32 `json:"sizeRange,omitempty"`
RotationRange []float32 `json:"rotationRange,omitempty"`

// Sunburst
NodeClick string `json:"nodeClick,omitempty"`
Sort string `json:"sort,omitempty"`
RenderLabelForZeroData bool `json:"renderLabelForZeroData"`
SelectedMode bool `json:"selectedMode"`
Animation bool `json:"animation"`
AnimationThreshold int `json:"animationThreshold,omitempty"`
AnimationDuration int `json:"animationDuration,omitempty"`
AnimationEasing string `json:"animationEasing,omitempty"`
AnimationDelay int `json:"animationDelay,omitempty"`
AnimationDurationUpdate int `json:"animationDurationUpdate,omitempty"`
AnimationEasingUpdate string `json:"animationEasingUpdate,omitempty"`
AnimationDelayUpdate int `json:"animationDelayUpdate,omitempty"`

// series data
Data interface{} `json:"data"`

// series options
*opts.ItemStyle `json:"itemStyle,omitempty"`
*opts.Label `json:"label,omitempty"`
*opts.LabelLine `json:"labelLine,omitempty"`
*opts.Emphasis `json:"emphasis,omitempty"`
*opts.MarkLines `json:"markLine,omitempty"`
*opts.MarkPoints `json:"markPoint,omitempty"`
Expand Down Expand Up @@ -130,6 +145,23 @@ func WithBarChartOpts(opt opts.BarChart) SeriesOpts {
}
}

func WithSunburstOpts(opt opts.SunburstChart) SeriesOpts {
return func(s *SingleSeries) {
s.NodeClick = opt.NodeClick
s.Sort = opt.Sort
s.RenderLabelForZeroData = opt.RenderLabelForZeroData
s.SelectedMode = opt.SelectedMode
s.Animation = opt.Animation
s.AnimationThreshold = opt.AnimationThreshold
s.AnimationDuration = opt.AnimationDuration
s.AnimationEasing = opt.AnimationEasing
s.AnimationDelay = opt.AnimationDelay
s.AnimationDurationUpdate = opt.AnimationDurationUpdate
s.AnimationEasingUpdate = opt.AnimationEasingUpdate
s.AnimationDelayUpdate = opt.AnimationDelayUpdate
}
}

// WithGraphChartOpts
func WithGraphChartOpts(opt opts.GraphChart) SeriesOpts {
return func(s *SingleSeries) {
Expand Down
42 changes: 42 additions & 0 deletions charts/sunburst.go
@@ -0,0 +1,42 @@
package charts

import (
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/render"
"github.com/go-echarts/go-echarts/v2/types"
)

// Sunburst represents a sunburst chart.
type Sunburst struct {
BaseConfiguration
}

// Type returns the chart type.
func (Sunburst) Type() string { return types.ChartSunburst }

// NewSunburst creates a new sunburst chart instance.
func NewSunburst() *Sunburst {
c := &Sunburst{}
c.initBaseConfiguration()
c.Renderer = render.NewChartRender(c, c.Validate)
return c
}

// AddSeries adds new data sets.
func (c *Sunburst) AddSeries(name string, data []opts.SunBurstData, options ...SeriesOpts) *Sunburst {
series := SingleSeries{Name: name, Type: types.ChartSunburst, Data: data}
series.configureSeriesOpts(options...)
c.MultiSeries = append(c.MultiSeries, series)
return c
}

// SetGlobalOptions sets options for the Pie instance.
func (c *Sunburst) SetGlobalOptions(options ...GlobalOpts) *Sunburst {
c.BaseConfiguration.setBaseGlobalOptions(options...)
return c
}

// Validate validates the given configuration.
func (c *Sunburst) Validate() {
c.Assets.Validate(c.AssetsHost)
}
39 changes: 39 additions & 0 deletions opts/charts.go
Expand Up @@ -35,6 +35,35 @@ type BarChart struct {
CoordSystem string
}

// SunburstChart
// https://echarts.apache.org/en/option.html#series-sunburst
type SunburstChart struct {
// The action of clicking a sector
NodeClick string `json:"nodeClick,omitempty"`
// Sorting method that sectors use based on value
Sort string `json:"sort,omitempty"`
// If there is no name, whether need to render it.
RenderLabelForZeroData bool `json:"renderLabelForZeroData"`
// Selected mode
SelectedMode bool `json:"selectedMode"`
// Whether to enable animation.
Animation bool `json:"animation"`
// Whether to set graphic number threshold to animation
AnimationThreshold int `json:"animationThreshold,omitempty"`
// Duration of the first animation
AnimationDuration int `json:"animationDuration,omitempty"`
// Easing method used for the first animation
AnimationEasing string `json:"animationEasing,omitempty"`
// Delay before updating the first animation
AnimationDelay int `json:"animationDelay,omitempty"`
// Time for animation to complete
AnimationDurationUpdate int `json:"animationDurationUpdate,omitempty"`
// Easing method used for animation.
AnimationEasingUpdate string `json:"animationEasingUpdate,omitempty"`
// Delay before updating animation
AnimationDelayUpdate int `json:"animationDelayUpdate,omitempty"`
}

// BarData
// https://echarts.apache.org/en/option.html#series-bar.data
type BarData struct {
Expand Down Expand Up @@ -516,3 +545,13 @@ type Chart3DData struct {
// The style setting of the text label in a single bar.
Label *Label `json:"label,omitempty"`
}

// SunBurstData data
type SunBurstData struct {
// Name of data item.
Name string `json:"name,omitempty"`
// Value of data item.
Value float64 `json:"value,omitempty"`
// sub item of data item
Children []*SunBurstData `json:"children,omitempty"`
}
32 changes: 25 additions & 7 deletions opts/global.go
Expand Up @@ -477,6 +477,13 @@ type AxisLabel struct {
// Set this to false to prevent the axis label from appearing.
Show bool `json:"show,omitempty"`

// Interval of Axis label, which is available in category axis.
// It uses a strategy that labels do not overlap by default.
// You may set it to be 0 to display all labels compulsively.
// If it is set to be 1, it means that labels are shown once after one label.
// And if it is set to be 2, it means labels are shown once after two labels, and so on.
Interval string `json:"interval,omitempty"`

// Set this to true so the axis labels face the inside direction.
Inside bool `json:"inside,omitempty"`

Expand All @@ -487,13 +494,6 @@ type AxisLabel struct {
// The margin between the axis label and the axis line.
Margin float64 `json:"margin,omitempty"`

// Interval of Axis label, which is available in category axis.
// It uses a strategy that labels do not overlap by default.
// You may set it to be 0 to display all labels compulsively.
// If it is set to be 1, it means that labels are shown once after one label.
// And if it is set to be 2, it means labels are shown once after two labels, and so on.
Interval string `json:"interval,omitempty"`

// Formatter of axis label, which supports string template and callback function.
//
// Example:
Expand All @@ -515,6 +515,9 @@ type AxisLabel struct {
//}
Formatter string `json:"formatter,omitempty"`

ShowMinLabel bool `json:"showMinLabel"`
ShowMaxLabel bool `json:"showMaxLabel"`

// Color of axis label is set to be axisLine.lineStyle.color by default. Callback function is supported,
// in the following format:
//
Expand All @@ -527,6 +530,21 @@ type AxisLabel struct {
// }
// }
Color string `json:"color,omitempty"`

// axis label font style
FontStyle string `json:"fontStyle,omitempty"`
// axis label font weight
FontWeight string `json:"fontWeight,omitempty"`
// axis label font family
FontFamily string `json:"fontFamily,omitempty"`
// axis label font size
FontSize string `json:"fontSize,omitempty"`
// Horizontal alignment of axis label
Align string `json:"align,omitempty"`
// Vertical alignment of axis label
VerticalAlign string `json:"verticalAlign,omitempty"`
// Line height of the axis label
LineHeight string `json:"lineHeight,omitempty"`
}

// XAxis is the option set for X axis.
Expand Down
18 changes: 17 additions & 1 deletion opts/series.go
Expand Up @@ -6,7 +6,7 @@ import "fmt"
// https://echarts.apache.org/en/option.html#series-line.label
type Label struct {
// Whether to show label.
Show bool `json:"show,omitempty"`
Show bool `json:"show"`

// Color is the text color.
// If set as "auto", the color will assigned as visual color, such as series color.
Expand Down Expand Up @@ -48,6 +48,22 @@ type Label struct {
Formatter string `json:"formatter,omitempty"`
}

// LabelLine Configuration of label guide line.
type LabelLine struct {
// Whether to show the label guide line.
Show bool `json:"show"`
// Whether to show the label guide line above the corresponding element.
ShowAbove bool `json:"showAbove"`
// The length of the second segment of guide line.
Length2 float64 `json:"length2,omitempty"`
// smoothness of guide line.
Smooth bool `json:"smooth"`
// Minimum turn angle between two segments of guide line
MinTurnAngle float64 `json:"minTurnAngle,omitempty"`
// The style of label line
LineStyle *LineStyle `json:"lineStyle,omitempty"`
}

// Emphasis is the style when it is highlighted, like being hovered by mouse, or highlighted via legend connect.
type Emphasis struct {
// the emphasis style of label
Expand Down
1 change: 1 addition & 0 deletions types/charts.go
Expand Up @@ -26,4 +26,5 @@ const (
ChartSurface3D = "surface"
ChartThemeRiver = "themeRiver"
ChartWordCloud = "wordCloud"
ChartSunburst = "sunburst"
)

0 comments on commit 012bf0e

Please sign in to comment.