From 579fa0fde95c98ae4831344ec4242205df90e0be Mon Sep 17 00:00:00 2001 From: susiwen8 Date: Sun, 7 Feb 2021 23:20:43 +0800 Subject: [PATCH 1/3] Feat: basic sunburst chart --- charts/series.go | 32 ++++++++++++++++++++++++++++++++ charts/sunburst.go | 35 +++++++++++++++++++++++++++++++++++ opts/charts.go | 22 ++++++++++++++++++++++ opts/global.go | 25 ++++++++++++++++++------- opts/series.go | 11 ++++++++++- types/charts.go | 1 + 6 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 charts/sunburst.go diff --git a/charts/series.go b/charts/series.go index 78831cd85..4af05a760 100644 --- a/charts/series.go +++ b/charts/series.go @@ -52,12 +52,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"` @@ -124,6 +139,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) { diff --git a/charts/sunburst.go b/charts/sunburst.go new file mode 100644 index 000000000..603336a2c --- /dev/null +++ b/charts/sunburst.go @@ -0,0 +1,35 @@ +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 { + RectChart +} + +// Type returns the chart type. +func (Sunburst) Type() string { return types.Sunburst } + +// NewSunburst creates a new sunburst chart instance. +func NewSunburst() *Sunburst { + c := &Sunburst{} + c.initBaseConfiguration() + c.Renderer = render.NewChartRender(c, c.Validate) + return c +} + +func (c *Sunburst) AddSeries(name string, data []opts.SunBurstData, options ...SeriesOpts) *Sunburst { + series := SingleSeries{Name: name, Type: types.Sunburst, Data: data} + series.configureSeriesOpts(options...) + c.MultiSeries = append(c.MultiSeries, series) + return c +} + +// Validate validates the given configuration. +func (c *Sunburst) Validate() { + c.Assets.Validate(c.AssetsHost) +} diff --git a/opts/charts.go b/opts/charts.go index 6fc3a197e..83b92233f 100644 --- a/opts/charts.go +++ b/opts/charts.go @@ -30,6 +30,21 @@ type BarChart struct { YAxisIndex int } +type SunburstChart struct { + 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"` +} + // BarData // https://echarts.apache.org/en/option.html#series-bar.data type BarData struct { @@ -511,3 +526,10 @@ 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 string `json:"name,omitempty"` + Value float64 `json:"value,omitempty"` + Children []*SunBurstData `json:"children,omitempty"` +} diff --git a/opts/global.go b/opts/global.go index 01998969d..5263ae15c 100644 --- a/opts/global.go +++ b/opts/global.go @@ -456,6 +456,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"` @@ -466,13 +473,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: @@ -494,6 +494,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: // @@ -506,6 +509,14 @@ type AxisLabel struct { // } // } Color string `json:"color,omitempty"` + + FontStyle string `json:"fontStyle,omitempty"` + FontWeight string `json:"fontWeight,omitempty"` + FontFamily string `json:"fontFamily,omitempty"` + FontSize string `json:"fontSize,omitempty"` + Align string `json:"align,omitempty"` + VerticalAlign string `json:"verticalAlign,omitempty"` + LineHeight string `json:"lineHeight,omitempty"` } // XAxis is the option set for X axis. diff --git a/opts/series.go b/opts/series.go index 220e2e931..258b52ba4 100644 --- a/opts/series.go +++ b/opts/series.go @@ -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. @@ -48,6 +48,15 @@ type Label struct { Formatter string `json:"formatter,omitempty"` } +type LabelLine struct { + Show bool `json:"show"` + ShowAbove bool `json:"showAbove"` + Length2 float64 `json:"length2,omitempty"` + Smooth bool `json:"smooth"` + MinTurnAngle float64 `json:"minTurnAngle,omitempty"` + LineStyle +} + // 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 diff --git a/types/charts.go b/types/charts.go index e8f2b4810..e9210542c 100644 --- a/types/charts.go +++ b/types/charts.go @@ -26,4 +26,5 @@ const ( ChartSurface3D = "surface" ChartThemeRiver = "themeRiver" ChartWordCloud = "wordCloud" + Sunburst = "sunburst" ) From c7fcf47e5977edf9b4d75991f020ee4663e0c6f9 Mon Sep 17 00:00:00 2001 From: susiwen8 Date: Wed, 10 Mar 2021 20:51:34 +0800 Subject: [PATCH 2/3] chore: add commnet and small tweak --- charts/sunburst.go | 4 ++-- opts/charts.go | 45 +++++++++++++++++++++++++++++++-------------- opts/global.go | 19 +++++++++++++------ opts/series.go | 17 ++++++++++++----- types/charts.go | 2 +- 5 files changed, 59 insertions(+), 28 deletions(-) diff --git a/charts/sunburst.go b/charts/sunburst.go index 603336a2c..f512775a3 100644 --- a/charts/sunburst.go +++ b/charts/sunburst.go @@ -12,7 +12,7 @@ type Sunburst struct { } // Type returns the chart type. -func (Sunburst) Type() string { return types.Sunburst } +func (Sunburst) Type() string { return types.ChartSunburst } // NewSunburst creates a new sunburst chart instance. func NewSunburst() *Sunburst { @@ -23,7 +23,7 @@ func NewSunburst() *Sunburst { } func (c *Sunburst) AddSeries(name string, data []opts.SunBurstData, options ...SeriesOpts) *Sunburst { - series := SingleSeries{Name: name, Type: types.Sunburst, Data: data} + series := SingleSeries{Name: name, Type: types.ChartSunburst, Data: data} series.configureSeriesOpts(options...) c.MultiSeries = append(c.MultiSeries, series) return c diff --git a/opts/charts.go b/opts/charts.go index 859b4ffc6..d24bc03d0 100644 --- a/opts/charts.go +++ b/opts/charts.go @@ -35,19 +35,33 @@ type BarChart struct { CoordSystem string } +// SunburstChart +// https://echarts.apache.org/en/option.html#series-sunburst type SunburstChart struct { - 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"` + // 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 @@ -534,7 +548,10 @@ type Chart3DData struct { // SunBurstData data type SunBurstData struct { - Name string `json:"name,omitempty"` - Value float64 `json:"value,omitempty"` + // 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"` } diff --git a/opts/global.go b/opts/global.go index a99f75993..314821ef2 100644 --- a/opts/global.go +++ b/opts/global.go @@ -531,13 +531,20 @@ type AxisLabel struct { // } Color string `json:"color,omitempty"` - FontStyle string `json:"fontStyle,omitempty"` - FontWeight string `json:"fontWeight,omitempty"` - FontFamily string `json:"fontFamily,omitempty"` - FontSize string `json:"fontSize,omitempty"` - Align string `json:"align,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"` - LineHeight string `json:"lineHeight,omitempty"` + // Line height of the axis label + LineHeight string `json:"lineHeight,omitempty"` } // XAxis is the option set for X axis. diff --git a/opts/series.go b/opts/series.go index 258b52ba4..25c33d7d4 100644 --- a/opts/series.go +++ b/opts/series.go @@ -48,13 +48,20 @@ type Label struct { Formatter string `json:"formatter,omitempty"` } +// LabelLine Configuration of label guide line. type LabelLine struct { - Show bool `json:"show"` - ShowAbove bool `json:"showAbove"` - Length2 float64 `json:"length2,omitempty"` - Smooth bool `json:"smooth"` + // 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"` - LineStyle + // 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. diff --git a/types/charts.go b/types/charts.go index e9210542c..1a46264d7 100644 --- a/types/charts.go +++ b/types/charts.go @@ -26,5 +26,5 @@ const ( ChartSurface3D = "surface" ChartThemeRiver = "themeRiver" ChartWordCloud = "wordCloud" - Sunburst = "sunburst" + ChartSunburst = "sunburst" ) From f117812021c4dc330b45209faa791567f69669a0 Mon Sep 17 00:00:00 2001 From: susiwen8 Date: Thu, 11 Mar 2021 23:46:34 +0800 Subject: [PATCH 3/3] chore: use `BaseConfiguration` for sunburst --- charts/sunburst.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/charts/sunburst.go b/charts/sunburst.go index f512775a3..9f92dd6e2 100644 --- a/charts/sunburst.go +++ b/charts/sunburst.go @@ -8,7 +8,7 @@ import ( // Sunburst represents a sunburst chart. type Sunburst struct { - RectChart + BaseConfiguration } // Type returns the chart type. @@ -22,6 +22,7 @@ func NewSunburst() *Sunburst { 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...) @@ -29,6 +30,12 @@ func (c *Sunburst) AddSeries(name string, data []opts.SunBurstData, options ...S 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)