Skip to content

Commit

Permalink
Feat: add polar bar (#157)
Browse files Browse the repository at this point in the history
* Feat: add polar bar

* Feat: seperate polar bar from bar

* Feat: add `enable` func to enable polar bar

* Fix: avoid  unnecessary option added into global

* chore: typo

* Fix: built in theme don't need extra js

* chore: typo
  • Loading branch information
susiwen8 committed Feb 9, 2021
1 parent fa2cd12 commit bf525d1
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@

> If a language can be used to build web scrapers, it definitely needs to provide a graceful data visualization library. --- by dongdong.
In the Golang ecosystem, there are not many choices for data visualization libraries. The development of [go-echarts](https://github.com/go-echarts/go-echarts) aims to provide a simple yet powerful data visualization library for Golang. [Echarts](https://echarts.apache.org/) is an outstanding charting and visualization library opensourced by Baidu, it supports adorable chart types and various interactive features. There are many language bindings for Echarts, for example, [pyecharts](https://github.com/pyecharts/pyecharts). go-echarts learns from pyecharts and has evolved a lot.
In the Golang ecosystem, there are not many choices for data visualization libraries. The development of [go-echarts](https://github.com/go-echarts/go-echarts) aims to provide a simple yet powerful data visualization library for Golang. [Apache ECharts](https://echarts.apache.org/) is an outstanding charting and visualization library, it supports adorable chart types and various interactive features. There are many language bindings for Echarts, for example, [pyecharts](https://github.com/pyecharts/pyecharts). go-echarts learns from pyecharts and has evolved a lot.

[中文 README](README_CN.md)

Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Expand Up @@ -27,7 +27,7 @@

> 如果一门语言可以用来写爬虫,那么它就需要一个优雅的数据可视化库。 --- by dongdong
在 Golang 这门语言中,目前数据可视化的第三方库还是特别少,[go-echarts](https://github.com/go-echarts/go-echarts) 的开发就是为了填补这部分的空隙。[Echarts](http://echarts.apche.com/) 是百度开源的非常优秀的可视化图表库,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。也有其他语言为其实现了相应语言版本的接口,如 Python 的 [pyecharts](https://github.com/pyecharts/pyecharts),go-echarts 也是借鉴了 pyecharts 的一些设计思想。
在 Golang 这门语言中,目前数据可视化的第三方库还是特别少,[go-echarts](https://github.com/go-echarts/go-echarts) 的开发就是为了填补这部分的空隙。[Apache ECharts](http://echarts.apache.org/) 是非常优秀的可视化图表库,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。也有其他语言为其实现了相应语言版本的接口,如 Python 的 [pyecharts](https://github.com/pyecharts/pyecharts),go-echarts 也是借鉴了 pyecharts 的一些设计思想。


### 🔰 安装
Expand Down
7 changes: 7 additions & 0 deletions charts/bar.go
Expand Up @@ -25,6 +25,13 @@ func NewBar() *Bar {
return c
}

// EnablePolarType enable polar bar
func (c *Bar) EnablePolarType() *Bar {
c.hasXYAxis = false
c.hasPolar = true
return c
}

// SetXAxis sets the X axis.
func (c *Bar) SetXAxis(x interface{}) *Bar {
c.xAxisData = x
Expand Down
47 changes: 40 additions & 7 deletions charts/base.go
Expand Up @@ -11,10 +11,13 @@ type GlobalOpts func(bc *BaseConfiguration)

// BaseConfiguration represents an option set needed by all chart types.
type BaseConfiguration struct {
opts.Legend `json:"legend"`
opts.Tooltip `json:"tooltip"`
opts.Toolbox `json:"toolbox"`
opts.Title `json:"title"`
opts.Legend `json:"legend"`
opts.Tooltip `json:"tooltip"`
opts.Toolbox `json:"toolbox"`
opts.Title `json:"title"`
opts.Polar `json:"polar"`
opts.AngleAxis `json:"angleAxis"`
opts.RadiusAxis `json:"radiusAxis"`

render.Renderer `json:"-"`
opts.Initialization `json:"-"`
Expand Down Expand Up @@ -52,6 +55,7 @@ type BaseConfiguration struct {
hasRadar bool
hasParallel bool
hasSingleAxis bool
hasPolar bool
}

// JSON wraps all the options to a map so that it could be used in the base template
Expand All @@ -66,6 +70,12 @@ func (bc *BaseConfiguration) JSON() map[string]interface{} {
"series": bc.MultiSeries,
}

if bc.hasPolar {
obj["polar"] = bc.Polar
obj["angleAxis"] = bc.AngleAxis
obj["radiusAxis"] = bc.RadiusAxis
}

if bc.hasGeo {
obj["geo"] = bc.GeoComponent
}
Expand Down Expand Up @@ -132,8 +142,8 @@ func (bc *BaseConfiguration) initBaseConfiguration() {

func (bc *BaseConfiguration) initSeriesColors() {
bc.Colors = []string{
"#c23531", "#2f4554", "#61a0a8", "#d48265", "#91c7ae",
"#749f83", "#ca8622", "#bda29a", "#6e7074", "#546570",
"#5470c6", "#91cc75", "#fac858", "#ee6666", "#73c0de",
"#3ba272", "#fc8452", "#9a60b4", "#ea7ccc",
}
}

Expand All @@ -152,6 +162,27 @@ func (bc *BaseConfiguration) setBaseGlobalOptions(opts ...GlobalOpts) {
}
}

// WithPolarOps set angleAxis
func WithAngleAxisOps(opt opts.AngleAxis) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.AngleAxis = opt
}
}

// WithPolarOps set radiusAxis
func WithRadiusAxisOps(opt opts.RadiusAxis) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.RadiusAxis = opt
}
}

// WithPolarOps set polar
func WithPolarOps(opt opts.Polar) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.Polar = opt
}
}

// WithTitleOpts
func WithTitleOpts(opt opts.Title) GlobalOpts {
return func(bc *BaseConfiguration) {
Expand Down Expand Up @@ -191,7 +222,9 @@ func WithLegendOpts(opt opts.Legend) GlobalOpts {
func WithInitializationOpts(opt opts.Initialization) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.Initialization = opt
if bc.Initialization.Theme != "" {
if bc.Initialization.Theme != "" &&
bc.Initialization.Theme != "white" &&
bc.Initialization.Theme != "dark" {
bc.JSAssets.Add("themes/" + opt.Theme + ".js")
}
bc.Initialization.Validate()
Expand Down
6 changes: 6 additions & 0 deletions charts/series.go
Expand Up @@ -14,6 +14,8 @@ type SingleSeries struct {
// Bar
BarGap string `json:"barGap,omitempty"`
BarCategoryGap string `json:"barCategoryGap,omitempty"`
ShowBackground bool `json:"showBackground,omitempty"`
RoundCap bool `json:"roundCap,omitempty"`

// Bar3D
Shading string `json:"shading,omitempty"`
Expand Down Expand Up @@ -121,6 +123,10 @@ func WithBarChartOpts(opt opts.BarChart) SeriesOpts {
s.BarCategoryGap = opt.BarCategoryGap
s.XAxisIndex = opt.XAxisIndex
s.YAxisIndex = opt.YAxisIndex
s.ShowBackground = opt.ShowBackground
s.RoundCap = opt.RoundCap
s.CoordSystem = opt.CoordSystem
s.Type = opt.Type
}
}

Expand Down
5 changes: 5 additions & 0 deletions opts/charts.go
Expand Up @@ -3,6 +3,7 @@ package opts
// BarChart
// https://echarts.apache.org/en/option.html#series-bar
type BarChart struct {
Type string
// Name of stack. On the same category axis, the series with the
// same stack name would be put on top of each other.
Stack string
Expand All @@ -28,6 +29,10 @@ type BarChart struct {

// Index of y axis to combine with, which is useful for multiple y axes in one chart.
YAxisIndex int

ShowBackground bool
RoundCap bool
CoordSystem string
}

// BarData
Expand Down
43 changes: 43 additions & 0 deletions opts/global.go
Expand Up @@ -915,6 +915,49 @@ type ParallelAxis struct {
Data interface{} `json:"data,omitempty"`
}

// Polar Bar options
type Polar struct {
ID string `json:"id,omitempty"`
Zlevel int `json:"zlevel,omitempty"`
Z int `json:"z,omitempty"`
Center [2]string `json:"center,omitempty"`
Radius [2]string `json:"radius,omitempty"`
Tooltip Tooltip `json:"tooltip,omitempty"`
}

type PolarAxisBase struct {
ID string `json:"id,omitempty"`
PolarIndex int `json:"polarIndex,omitempty"`
StartAngle float64 `json:"startAngle,omitempty"`
Type string `json:"type,omitempty"`
BoundaryGap bool `json:"boundaryGap,omitempty"`
Min float64 `json:"min,omitempty"`
Max float64 `json:"max,omitempty"`
Scale bool `json:"scale,omitempty"`
SplitNumber int `json:"splitNumber,omitempty"`
MinInterval float64 `json:"minInterval,omitempty"`
MaxInterval float64 `json:"maxInterval,omitempty"`
Interval float64 `json:"interval,omitempty"`
LogBase float64 `json:"logBase,omitempty"`
Silent bool `json:"silent,omitempty"`
TriggerEvent bool `json:"triggerEvent,omitempty"`
}

type AngleAxis struct {
PolarAxisBase
Clockwise bool `json:"clockwise,omitempty"`
}

type RadiusAxis struct {
PolarAxisBase
Name string `json:"name,omitempty"`
NameLocation string `json:"nameLocation,omitempty"`
NameTextStyle TextStyle `json:"nameTextStyle,omitempty"`
NameGap float64 `json:"nameGap,omitempty"`
NameRadius float64 `json:"nameRotate,omitempty"`
Inverse bool `json:"inverse,omitempty"`
}

var funcPat = regexp.MustCompile(`\n|\t`)

const funcMarker = "__f__"
Expand Down

0 comments on commit bf525d1

Please sign in to comment.