Skip to content

Commit

Permalink
Refactor: 重新设计接口
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Feb 3, 2019
1 parent e8cef2a commit 552e523
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 104 deletions.
35 changes: 19 additions & 16 deletions .gitignore
@@ -1,16 +1,19 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# IDE
.idea/
.vscode/
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# IDE
.idea/
.vscode/

# render file
example/html/*.html
10 changes: 9 additions & 1 deletion charts/bar.go
Expand Up @@ -9,13 +9,17 @@ type Bar struct {
isXYReversal bool
}

func (Bar) chartType() string { return "bar" }

// Bar series options
type BarOpts struct {
Stack string
XAxisIndex int
YAxisIndex int
}

func (BarOpts) markSeries() {}

func (opt *BarOpts) setChartOpt(s *singleSeries) {
s.Stack = opt.Stack
s.XAxisIndex = opt.XAxisIndex
Expand All @@ -34,7 +38,7 @@ func (c *Bar) AddXAxis(xAxis interface{}) *Bar {
return c
}

func (c *Bar) AddYAxis(name string, yAxis interface{}, options ...interface{}) *Bar {
func (c *Bar) AddYAxis(name string, yAxis interface{}, options ...seriesOptser) *Bar {
series := singleSeries{Name: name, Type: "bar", Data: yAxis}
series.setSingleSeriesOpts(options...)
c.Series = append(c.Series, series)
Expand All @@ -53,6 +57,10 @@ func (c *Bar) validateOpts() {
c.YAxisOptsList[0].Data = c.xAxisData
c.XAxisOptsList[0].Data = nil
}
// 确保 Y 轴数标签正确显示
for i := 0; i < len(c.YAxisOptsList); i++ {
c.YAxisOptsList[i].AxisLabel.Show = true
}
c.validateAssets(c.AssetsHost)
}

Expand Down
45 changes: 27 additions & 18 deletions charts/base.go
@@ -1,5 +1,9 @@
package charts

type globalOptser interface {
markGlobal()
}

// 图形初始化配置项
type InitOpts struct {
// 生成的 HTML 页面标题
Expand All @@ -16,6 +20,8 @@ type InitOpts struct {
Theme string `default:"white"`
}

func (InitOpts) markGlobal() {}

// 静态资源配置项
type AssetsOpts struct {
JSAssets, CSSAssets orderedSet
Expand Down Expand Up @@ -90,32 +96,35 @@ func (f *JSFunctions) AddJSFuncs(fn ...string) {
// 全局颜色配置项
type ColorOpts []string

func (ColorOpts) markGlobal() {}
func (ColorOpts) markSeries() {}

// 所有图表都拥有的基本配置项
type BaseOpts struct {
InitOpts // 图形初始化配置项
LegendOpts // 图例组件配置项
TooltipOpts // 提示框组件配置项
ToolboxOpts // 工具箱组件配置项
TitleOpts // 标题组件配置项
AssetsOpts // 静态资源配置项
Colors []string // 全局颜色列表
appendColor []string // 追加全局颜色列表
HTTPRouters // 路由列表
DataZoomOptsList // 区域缩放组件配置项列表
VisualMapOptsList // 视觉映射组件配置项列表
GeoOpts // 地理坐标系组件配置项

JSFunctions // JS 函数列表
HasXYAxis bool // 图形是否拥有 XY 轴
InitOpts // 图形初始化配置项
LegendOpts // 图例组件配置项
TooltipOpts // 提示框组件配置项
ToolboxOpts // 工具箱组件配置项
TitleOpts // 标题组件配置项
AssetsOpts // 静态资源配置项
Colors []string // 全局颜色列表
appendColor []string // 追加全局颜色列表
HTTPRouters // 路由列表
DataZoomOptsList // 区域缩放组件配置项列表
VisualMapOptsList // 视觉映射组件配置项列表
GeoOpts // 地理坐标系组件配置项

JSFunctions // JS 函数列表
HasXYAxis bool // 图形是否拥有 XY 轴
}

// 设置全局颜色
func (opt *BaseOpts) setColor(options ...interface{}) {
func (opt *BaseOpts) setColor(options ...seriesOptser) {
for i := 0; i < len(options); i++ {
option := options[i]
switch option.(type) {
case ColorOpts:
opt.appendColor = append(opt.appendColor, option.(ColorOpts)...)
opt.insertSeriesColors(option.(ColorOpts))
}
}
}
Expand Down Expand Up @@ -150,7 +159,7 @@ func (opt *BaseOpts) insertSeriesColors(s []string) {
}

// 设置 BaseOptions 全局配置项
func (opt *BaseOpts) setBaseGlobalConfig(options ...interface{}) {
func (opt *BaseOpts) setBaseGlobalOptions(options ...globalOptser) {
for i := 0; i < len(options); i++ {
option := options[i]
switch option.(type) {
Expand Down
38 changes: 37 additions & 1 deletion charts/components.go
Expand Up @@ -34,6 +34,8 @@ type TitleOpts struct {
Right string `json:"right,omitempty"`
}

func (TitleOpts) markGlobal() {}

// 图例组件配置项
type LegendOpts struct {
// 是否显示图例
Expand All @@ -56,6 +58,8 @@ type LegendOpts struct {
Bottom string `json:"bottom,omitempty"`
}

func (LegendOpts) markGlobal() {}

// 提示框组件配置项
type TooltipOpts struct {
// 是否显示提示框
Expand Down Expand Up @@ -94,13 +98,17 @@ type TooltipOpts struct {
Formatter string `json:"formatter,omitempty"`
}

func (TooltipOpts) markGlobal() {}

// 工具箱组件配置项
type ToolboxOpts struct {
// 是否显示工具栏组件
Show bool `json:"show"`
TBFeature `json:"feature"`
}

func (ToolboxOpts) markGlobal() {}

type TBFeature struct {
// 保存为图片
SaveAsImage struct{} `json:"saveAsImage"`
Expand All @@ -125,6 +133,8 @@ type TextStyleOpts struct {
Normal *TextStyleOpts `json:"normal,omitempty"`
}

func (TextStyleOpts) markSeries() {}

// 线风格配置项
type LineStyleOpts struct {
// 线的颜色
Expand All @@ -142,6 +152,8 @@ type LineStyleOpts struct {
Curveness float32 `json:"curveness,omitempty"`
}

func (LineStyleOpts) markSeries() {}

// 区域风格配置项
type AreaStyleOpts struct {
// 填充区域的颜色
Expand All @@ -150,6 +162,8 @@ type AreaStyleOpts struct {
Opacity float32 `json:"opacity,omitempty"`
}

func (AreaStyleOpts) markSeries() {}

// 区域缩放组件配置项
type DataZoomOpts struct {
// 缩放类型,可选 "inside", "slider"
Expand All @@ -175,6 +189,8 @@ type DataZoomOpts struct {
YAxisIndex interface{} `json:"yAxisIndex,omitempty"`
}

func (DataZoomOpts) markGlobal() {}

type DataZoomOptsList []DataZoomOpts

func (dz DataZoomOptsList) Len() int {
Expand All @@ -197,9 +213,11 @@ type VisualMapOpts struct {
// 两端的文本,如 ['High', 'Low']
Text []string `json:"text,omitempty"`
// 定义在选中范围中的视觉元素
VMInRange `json:"inRange,omitempty"`
InRange VMInRange `json:"inRange,omitempty"`
}

func (VisualMapOpts) markGlobal() {}

type VisualMapOptsList []VisualMapOpts

func (vm VisualMapOptsList) Len() int {
Expand Down Expand Up @@ -265,6 +283,8 @@ type XAxisOpts struct {
SplitLine SplitLineOpts `json:"splitLine,,omitempty"`
}

func (XAxisOpts) markGlobal() {}

// Y 轴配置项组件
type YAxisOpts struct {
// Y 轴名称
Expand All @@ -276,6 +296,20 @@ type YAxisOpts struct {
Type string `json:"type,omitempty"`
// 是否显示 Y 轴
Show bool `json:"show,omitempty"`
// 刻度标签的内容格式器,支持字符串模板和回调函数两种形式
// 1.使用字符串模板,模板变量为刻度默认标签 {value}
// formatter: '{value} kg'
// 2.使用函数模板,函数参数分别为刻度数值(类目),刻度的索引
// formatter: function (value, index) {
// // 格式化成月/日,只在第一个刻度显示年份
// var date = new Date(value);
// var texts = [(date.getMonth() + 1), date.getDate()];
// if (index === 0) {
// texts.unshift(date.getYear());
// }
// return texts.join('/');
// }
AxisLabel LabelTextOpts `json:"axisLabel,omitempty"`
// Y 轴数据项
Data interface{} `json:"data,omitempty"`
// Y 坐标轴的分割段数,需要注意的是这个分割段数只是个预估值,
Expand All @@ -302,6 +336,8 @@ type YAxisOpts struct {
SplitLine SplitLineOpts `json:"splitLine,,omitempty"`
}

func (YAxisOpts) markGlobal() {}

// 地理坐标系组件配置项
type GeoOpts struct {
Map string `json:"map,omitempty"`
Expand Down
6 changes: 5 additions & 1 deletion charts/effectscatter.go
Expand Up @@ -4,6 +4,8 @@ type EffectScatter struct {
RectChart
}

func (EffectScatter) chartType() string { return "effectScatter" }

// EffectScatter series options
type EffectScatterChartOpts struct {
XAxisIndex int
Expand All @@ -23,6 +25,8 @@ type RippleEffectOpts struct {
BrushType string `json:"brushType,omitempty"`
}

func (RippleEffectOpts) markSeries() {}

func NewEffectScatter(routers ...HTTPRouter) *EffectScatter {
chart := new(EffectScatter)
chart.initBaseOpts(true, routers...)
Expand All @@ -35,7 +39,7 @@ func (c *EffectScatter) AddXAxis(xAxis interface{}) *EffectScatter {
return c
}

func (c *EffectScatter) AddYAxis(name string, yAxis interface{}, options ...interface{}) *EffectScatter {
func (c *EffectScatter) AddYAxis(name string, yAxis interface{}, options ...seriesOptser) *EffectScatter {
series := singleSeries{Name: name, Type: "effectScatter", Data: yAxis}
series.setSingleSeriesOpts(options...)
c.Series = append(c.Series, series)
Expand Down
8 changes: 5 additions & 3 deletions charts/funnel.go
Expand Up @@ -9,13 +9,15 @@ type Funnel struct {
Series
}

func (Funnel) chartType() string { return "funnel" }

func NewFunnel(routers ...HTTPRouter) *Funnel {
chart := new(Funnel)
chart.initBaseOpts(false, routers...)
return chart
}

func (c *Funnel) Add(name string, data map[string]interface{}, options ...interface{}) *Funnel {
func (c *Funnel) Add(name string, data map[string]interface{}, options ...seriesOptser) *Funnel {
nvs := make([]nameValueItem, 0)
for k, v := range data {
nvs = append(nvs, nameValueItem{k, v})
Expand All @@ -27,8 +29,8 @@ func (c *Funnel) Add(name string, data map[string]interface{}, options ...interf
return c
}

func (c *Funnel) SetGlobalConfig(options ...interface{}) *Funnel {
c.BaseOpts.setBaseGlobalConfig(options...)
func (c *Funnel) SetGlobalOptions(options ...globalOptser) *Funnel {
c.BaseOpts.setBaseGlobalOptions(options...)
return c
}

Expand Down
9 changes: 5 additions & 4 deletions charts/gauge.go
Expand Up @@ -9,26 +9,27 @@ type Gauge struct {
Series
}

func (Gauge) chartType() string { return "gauge" }

func NewGauge(routers ...HTTPRouter) *Gauge {
chart := new(Gauge)
chart.initBaseOpts(false, routers...)
return chart
}

func (c *Gauge) Add(name string, data map[string]interface{}, options ...interface{}) *Gauge {
func (c *Gauge) Add(name string, data map[string]interface{}, options ...seriesOptser) *Gauge {
nvs := make([]nameValueItem, 0)
for k, v := range data {
nvs = append(nvs, nameValueItem{k, v})
}
series := singleSeries{Name: name, Type: "gauge", Data: nvs}
series.setSingleSeriesOpts(options...)
c.Series = append(c.Series, series)
c.setColor(options...)
return c
}

func (c *Gauge) SetGlobalConfig(options ...interface{}) *Gauge {
c.BaseOpts.setBaseGlobalConfig(options...)
func (c *Gauge) SetGlobalOptions(options ...globalOptser) *Gauge {
c.BaseOpts.setBaseGlobalOptions(options...)
return c
}

Expand Down
8 changes: 5 additions & 3 deletions charts/geo.go
Expand Up @@ -11,6 +11,8 @@ type Geo struct {
Series
}

func (Geo) chartType() string { return "geo" }

var geoFormatter = `function (params) {
return params.name + ' : ' + params.value[2];
}`
Expand All @@ -23,7 +25,7 @@ func NewGeo(mapType string, routers ...HTTPRouter) *Geo {
return chart
}

func (c *Geo) Add(name, geoType string, data map[string]float32, options ...interface{}) *Geo {
func (c *Geo) Add(name, geoType string, data map[string]float32, options ...seriesOptser) *Geo {
nvs := make([]nameValueItem, 0)
for k, v := range data {
nvs = append(nvs, nameValueItem{k, c.extendValue(k, v)})
Expand All @@ -41,8 +43,8 @@ func (c *Geo) extendValue(region string, v float32) []float32 {
return res
}

func (c *Geo) SetGlobalConfig(options ...interface{}) *Geo {
c.BaseOpts.setBaseGlobalConfig(options...)
func (c *Geo) SetGlobalOptions(options ...globalOptser) *Geo {
c.BaseOpts.setBaseGlobalOptions(options...)
return c
}

Expand Down

0 comments on commit 552e523

Please sign in to comment.