From 69c70bfdf59515b96b5e0d00a067eb7a272098e1 Mon Sep 17 00:00:00 2001 From: Andre Ziviani Date: Thu, 11 Feb 2021 19:58:47 -0300 Subject: [PATCH 1/9] feat/tree: add tree chart type --- charts/series.go | 14 ++++++++++++++ charts/tree.go | 35 +++++++++++++++++++++++++++++++++++ opts/charts.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ types/charts.go | 1 + 4 files changed, 96 insertions(+) create mode 100644 charts/tree.go diff --git a/charts/series.go b/charts/series.go index 2a2d5908c..035ed7683 100644 --- a/charts/series.go +++ b/charts/series.go @@ -49,6 +49,11 @@ type SingleSeries struct { // Scatter SymbolSize float32 `json:"symbolSize,omitempty"` + // Tree + Orient string `json:"orient,omitempty"` + ExpandAndCollapse bool `json:"expandAndCollapse"` + InitialTreeDepth int `json:"initialTreeDepth,omitempty"` + // WordCloud Shape string `json:"shape,omitempty"` SizeRange []float32 `json:"sizeRange,omitempty"` @@ -195,6 +200,15 @@ func WithBar3DChartOpts(opt opts.Bar3DChart) SeriesOpts { } } +// WithTreeOpts +func WithTreeOpts(opt opts.TreeChart) SeriesOpts { + return func(s *SingleSeries) { + s.Orient = opt.Orient + s.ExpandAndCollapse = opt.ExpandAndCollapse + s.InitialTreeDepth = opt.InitialTreeDepth + } +} + // WithWorldCloudChartOpts func WithWorldCloudChartOpts(opt opts.WordCloudChart) SeriesOpts { return func(s *SingleSeries) { diff --git a/charts/tree.go b/charts/tree.go new file mode 100644 index 000000000..ec60cdf41 --- /dev/null +++ b/charts/tree.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" +) + +// Tree represents a Tree chart. +type Tree struct { + RectChart +} + +// Type returns the chart type. +func (Tree) Type() string { return types.Tree } + +// NewTree creates a new Tree chart instance. +func NewTree() *Tree { + c := &Tree{} + c.initBaseConfiguration() + c.Renderer = render.NewChartRender(c, c.Validate) + return c +} + +func (c *Tree) AddSeries(name string, data []opts.TreeData, options ...SeriesOpts) *Tree { + series := SingleSeries{Name: name, Type: types.Tree, Data: data} + series.configureSeriesOpts(options...) + c.MultiSeries = append(c.MultiSeries, series) + return c +} + +// Validate validates the given configuration. +func (c *Tree) Validate() { + c.Assets.Validate(c.AssetsHost) +} diff --git a/opts/charts.go b/opts/charts.go index 769a6440d..f14936687 100644 --- a/opts/charts.go +++ b/opts/charts.go @@ -516,3 +516,49 @@ type Chart3DData struct { // The style setting of the text label in a single bar. Label *Label `json:"label,omitempty"` } + +type TreeChart struct { + // The layout of the tree, which can be orthogonal and radial. + // * 'orthogonal' refer to the horizontal and vertical direction. + // * 'radial' refers to the view that the root node as the center and each layer of nodes as the ring. + Layout string + + // The direction of the orthogonal layout in the tree diagram. + // * 'from left to right' or 'LR' + // * 'from right to left' or 'RL' + // * 'from top to bottom' or 'TB' + // * 'from bottom to top' or 'BT' + Orient string `json:"orient,omitempty"` + + // Whether to enable mouse zooming and translating. false by default. + // If either zooming or translating is wanted, it can be set to 'scale' or 'move'. + // Otherwise, set it to be true to enable both. + Roam bool + + // Subtree collapses and expands interaction, default true. + ExpandAndCollapse bool `json:"expandAndCollapse"` + + // The initial level (depth) of the tree. The root node is the 0th layer, then the first layer, the second layer, ... , until the leaf node. + // This configuration item is primarily used in conjunction with collapsing and expansion interactions. + // The purpose is to prevent the nodes from obscuring each other. If set as -1 or null or undefined, all nodes are expanded. + InitialTreeDepth int `json:"initialTreeDepth,omitempty"` +} + +type TreeData struct { + // Name of the data item. + Name string `json:"name,omitempty"` + + // Value of the data item. + Value int `json:"value,omitempty"` + + Children []*TreeData `json:"children,omitempty"` + // Symbol of node of this category. + // Icon types provided by ECharts includes + // 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none' + // It can be set to an image with 'image://url' , in which URL is the link to an image, or dataURI of an image. + Symbol string `json:"symbol,omitempty"` + + // node of this category symbol size. It can be set to single numbers like 10, + // or use an array to represent width and height. For example, [20, 10] means symbol width is 20, and height is10. + SymbolSize interface{} `json:"symbolSize,omitempty"` +} diff --git a/types/charts.go b/types/charts.go index e8f2b4810..398cf96e6 100644 --- a/types/charts.go +++ b/types/charts.go @@ -26,4 +26,5 @@ const ( ChartSurface3D = "surface" ChartThemeRiver = "themeRiver" ChartWordCloud = "wordCloud" + Tree = "tree" ) From b7d1c4b65155de7e5bc7625922ea6f451a9aac2c Mon Sep 17 00:00:00 2001 From: Andre Ziviani Date: Fri, 12 Feb 2021 14:13:27 -0300 Subject: [PATCH 2/9] fix/struct: fix json tags --- charts/series.go | 2 +- opts/charts.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/charts/series.go b/charts/series.go index 035ed7683..a7ab222ce 100644 --- a/charts/series.go +++ b/charts/series.go @@ -51,7 +51,7 @@ type SingleSeries struct { // Tree Orient string `json:"orient,omitempty"` - ExpandAndCollapse bool `json:"expandAndCollapse"` + ExpandAndCollapse bool `json:"expandAndCollapse,omitempty"` InitialTreeDepth int `json:"initialTreeDepth,omitempty"` // WordCloud diff --git a/opts/charts.go b/opts/charts.go index f14936687..8c32df7e7 100644 --- a/opts/charts.go +++ b/opts/charts.go @@ -533,10 +533,10 @@ type TreeChart struct { // Whether to enable mouse zooming and translating. false by default. // If either zooming or translating is wanted, it can be set to 'scale' or 'move'. // Otherwise, set it to be true to enable both. - Roam bool + Roam bool `json:"roam"` // Subtree collapses and expands interaction, default true. - ExpandAndCollapse bool `json:"expandAndCollapse"` + ExpandAndCollapse bool `json:"expandAndCollapse,omitempty"` // The initial level (depth) of the tree. The root node is the 0th layer, then the first layer, the second layer, ... , until the leaf node. // This configuration item is primarily used in conjunction with collapsing and expansion interactions. From 9342f4ca0926c22d920983f064410b00deec66e0 Mon Sep 17 00:00:00 2001 From: Andre Ziviani Date: Fri, 12 Feb 2021 15:13:56 -0300 Subject: [PATCH 3/9] feat/tree-leaves: add leaves option --- charts/series.go | 11 ++++++++--- opts/charts.go | 6 ++++++ opts/series.go | 12 ++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/charts/series.go b/charts/series.go index a7ab222ce..401f74230 100644 --- a/charts/series.go +++ b/charts/series.go @@ -50,9 +50,10 @@ type SingleSeries struct { SymbolSize float32 `json:"symbolSize,omitempty"` // Tree - Orient string `json:"orient,omitempty"` - ExpandAndCollapse bool `json:"expandAndCollapse,omitempty"` - InitialTreeDepth int `json:"initialTreeDepth,omitempty"` + Orient string `json:"orient,omitempty"` + ExpandAndCollapse bool `json:"expandAndCollapse,omitempty"` + InitialTreeDepth int `json:"initialTreeDepth,omitempty"` + Leaves interface{} `json:"leaves,omitempty"` // WordCloud Shape string `json:"shape,omitempty"` @@ -203,9 +204,13 @@ func WithBar3DChartOpts(opt opts.Bar3DChart) SeriesOpts { // WithTreeOpts func WithTreeOpts(opt opts.TreeChart) SeriesOpts { return func(s *SingleSeries) { + s.Layout = opt.Layout s.Orient = opt.Orient s.ExpandAndCollapse = opt.ExpandAndCollapse s.InitialTreeDepth = opt.InitialTreeDepth + s.Roam = opt.Roam + s.Label = opt.Label + s.Leaves = opt.Leaves } } diff --git a/opts/charts.go b/opts/charts.go index 8c32df7e7..28c8a7a70 100644 --- a/opts/charts.go +++ b/opts/charts.go @@ -542,6 +542,12 @@ type TreeChart struct { // This configuration item is primarily used in conjunction with collapsing and expansion interactions. // The purpose is to prevent the nodes from obscuring each other. If set as -1 or null or undefined, all nodes are expanded. InitialTreeDepth int `json:"initialTreeDepth,omitempty"` + + // The style setting of the text label in a single bar. + Label *Label `json:"label,omitempty"` + + // Leaf node special configuration, the leaf node and non-leaf node label location is different. + Leaves *TreeLeaves `json:"leaves,omitempty"` } type TreeData struct { diff --git a/opts/series.go b/opts/series.go index 220e2e931..ca1a55b43 100644 --- a/opts/series.go +++ b/opts/series.go @@ -284,6 +284,18 @@ type GraphForce struct { EdgeLength float32 `json:"edgeLength,omitempty"` } +// Leaf node special configuration, the leaf node and non-leaf node label location is different. +type TreeLeaves struct { + // The style setting of the text label in a single bar. + Label *Label `json:"label,omitempty"` + + // ItemStyle settings in this series data. + ItemStyle *ItemStyle `json:"itemStyle,omitempty"` + + // Emphasis settings in this series data. + Emphasis *Emphasis `json:"emphasis,omitempty"` +} + // RGBColor returns the color with RGB format func RGBColor(r, g, b uint16) string { return fmt.Sprintf("rgb(%d,%d,%d)", r, g, b) From 1396fe3b21a2d6068b7415407e8d2c64e5e02a3e Mon Sep 17 00:00:00 2001 From: Andre Ziviani Date: Fri, 12 Feb 2021 15:43:04 -0300 Subject: [PATCH 4/9] feat/label-position: add option to specify label distance from screen border --- charts/series.go | 8 ++++++++ opts/charts.go | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/charts/series.go b/charts/series.go index 401f74230..6083c995f 100644 --- a/charts/series.go +++ b/charts/series.go @@ -54,6 +54,10 @@ type SingleSeries struct { ExpandAndCollapse bool `json:"expandAndCollapse,omitempty"` InitialTreeDepth int `json:"initialTreeDepth,omitempty"` Leaves interface{} `json:"leaves,omitempty"` + Left string `json:"left,omitempty"` + Right string `json:"right,omitempty"` + Top string `json:"top,omitempty"` + Bottom string `json:"bottom,omitempty"` // WordCloud Shape string `json:"shape,omitempty"` @@ -211,6 +215,10 @@ func WithTreeOpts(opt opts.TreeChart) SeriesOpts { s.Roam = opt.Roam s.Label = opt.Label s.Leaves = opt.Leaves + s.Right = opt.Right + s.Left = opt.Left + s.Top = opt.Top + s.Bottom = opt.Bottom } } diff --git a/opts/charts.go b/opts/charts.go index 28c8a7a70..8d3cfdfdc 100644 --- a/opts/charts.go +++ b/opts/charts.go @@ -548,6 +548,14 @@ type TreeChart struct { // Leaf node special configuration, the leaf node and non-leaf node label location is different. Leaves *TreeLeaves `json:"leaves,omitempty"` + + // Distance between tree component and the sides of the container. + // value can be instant pixel value like 20; + // It can also be a percentage value relative to container width like '20%'; + Left string `json:"left,omitempty"` + Right string `json:"right,omitempty"` + Top string `json:"top,omitempty"` + Bottom string `json:"bottom,omitempty"` } type TreeData struct { From c7995989a84fbb523c7b8299d9a9620b13546174 Mon Sep 17 00:00:00 2001 From: Andre Ziviani Date: Fri, 12 Feb 2021 16:20:53 -0300 Subject: [PATCH 5/9] feat/tree-collapsed-chield: add option to collapse a chield --- opts/charts.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/opts/charts.go b/opts/charts.go index 8d3cfdfdc..ca69ab32f 100644 --- a/opts/charts.go +++ b/opts/charts.go @@ -566,6 +566,7 @@ type TreeData struct { Value int `json:"value,omitempty"` Children []*TreeData `json:"children,omitempty"` + // Symbol of node of this category. // Icon types provided by ECharts includes // 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none' @@ -575,4 +576,7 @@ type TreeData struct { // node of this category symbol size. It can be set to single numbers like 10, // or use an array to represent width and height. For example, [20, 10] means symbol width is 20, and height is10. SymbolSize interface{} `json:"symbolSize,omitempty"` + + // If set as `true`, the node is collpased in the initialization. + Collapsed bool `json:"collapsed,omitempty"` } From af99f927d235a9d63a7618119e12303685544b05 Mon Sep 17 00:00:00 2001 From: Koy Date: Mon, 15 Mar 2021 16:46:46 +0800 Subject: [PATCH 6/9] Update charts.go fix conflict. --- opts/charts.go | 1 + 1 file changed, 1 insertion(+) diff --git a/opts/charts.go b/opts/charts.go index 2794aa94c..8e38b6045 100644 --- a/opts/charts.go +++ b/opts/charts.go @@ -608,6 +608,7 @@ type TreeData struct { // If set as `true`, the node is collpased in the initialization. Collapsed bool `json:"collapsed,omitempty"` +} // SunBurstData data type SunBurstData struct { From 052f4a1753a0788ce28fddfab22121ff5d3b4247 Mon Sep 17 00:00:00 2001 From: Koy Date: Mon, 15 Mar 2021 17:39:21 +0800 Subject: [PATCH 7/9] update(type): update Tree name. --- types/charts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/charts.go b/types/charts.go index 1ba92c2fb..2cf035316 100644 --- a/types/charts.go +++ b/types/charts.go @@ -26,6 +26,6 @@ const ( ChartSurface3D = "surface" ChartThemeRiver = "themeRiver" ChartWordCloud = "wordCloud" - Tree = "tree" + ChartTree = "tree" ChartSunburst = "sunburst" ) From dc65b927ae83ea8103baa8961ca0cc06d6069f5c Mon Sep 17 00:00:00 2001 From: Koy Date: Mon, 15 Mar 2021 17:40:05 +0800 Subject: [PATCH 8/9] update: refactor char type. --- charts/tree.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/charts/tree.go b/charts/tree.go index ec60cdf41..a8fe1f991 100644 --- a/charts/tree.go +++ b/charts/tree.go @@ -12,7 +12,7 @@ type Tree struct { } // Type returns the chart type. -func (Tree) Type() string { return types.Tree } +func (Tree) Type() string { return types.ChartTree } // NewTree creates a new Tree chart instance. func NewTree() *Tree { @@ -23,7 +23,7 @@ func NewTree() *Tree { } func (c *Tree) AddSeries(name string, data []opts.TreeData, options ...SeriesOpts) *Tree { - series := SingleSeries{Name: name, Type: types.Tree, Data: data} + series := SingleSeries{Name: name, Type: types.ChartTree, Data: data} series.configureSeriesOpts(options...) c.MultiSeries = append(c.MultiSeries, series) return c From feb00271dc704f7e02dc2784deafa1e1e6b6d2e6 Mon Sep 17 00:00:00 2001 From: Koy Date: Wed, 17 Mar 2021 21:16:22 +0800 Subject: [PATCH 9/9] update: change to BaseConfiguration. --- charts/tree.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/tree.go b/charts/tree.go index a8fe1f991..6ce82c163 100644 --- a/charts/tree.go +++ b/charts/tree.go @@ -8,7 +8,7 @@ import ( // Tree represents a Tree chart. type Tree struct { - RectChart + BaseConfiguration } // Type returns the chart type.