Skip to content

Commit

Permalink
update: support for multiple datasets with AddDataset and strip '//' …
Browse files Browse the repository at this point in the history
…comments from function options (#285)

* Add support for multiple datasets with AddDataset

eCharts supports Chart's `dataset` as an array and indexing it
in a Series with `datasetIndex`.

This replaces the `Dataset` Chart config field with the
function `AddDataset`.  It also adds `WithDatasetIndex` as
an option for `SeriesOpts`

The `DatasetList` is JSON rendered as a single object, as
before.  But multiple Datasets are rendered as Array.

This is a breaking change:

```
// old
chart.Dataset = opts.Dataset{Source: test}
// new
chart.AddDataset(opts.Dataset{Source: test})
```

* Strip '//' comments from function options, add FuncNoStripOpts to skip this

* Add lots of missing fields

Also some whitespace changes per go fmt for doc comments

   * Tooltip.ValueFormatter
   * AxisPointer.Show
   * AxisPointer.Label
   * AxisLabel.BackgroundColor
   * XAxis.AxisPointer
   * YAxis.AxisPointer
   * DataZoom.LabelFormatter

* Restored `FuncOpts` as default and now have `FuncStripCommentOpts`

renamed `funcPat` to `newlineTabPat` for clarity

* AxisPointer::Show is not `omitempty` due to default conflicts
  • Loading branch information
neomantra committed May 14, 2023
1 parent beec2e8 commit 5762dc7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
16 changes: 14 additions & 2 deletions charts/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type BaseConfiguration struct {
opts.Tooltip `json:"tooltip"`
opts.Toolbox `json:"toolbox"`
opts.Title `json:"title"`
opts.Dataset `json:"dataset"`
opts.Polar `json:"polar"`
opts.AngleAxis `json:"angleAxis"`
opts.RadiusAxis `json:"radiusAxis"`
Expand Down Expand Up @@ -58,6 +57,9 @@ type BaseConfiguration struct {
// Animation whether enable the animation, default true
Animation bool `json:"animation" default:"true"`

// Array of datasets, managed by AddDataset()
DatasetList []opts.Dataset `json:"dataset,omitempty"`

DataZoomList []opts.DataZoom `json:"datazoom,omitempty"`
VisualMapList []opts.VisualMap `json:"visualmap,omitempty"`

Expand Down Expand Up @@ -119,9 +121,14 @@ func (bc *BaseConfiguration) json() map[string]interface{} {
"animation": bc.Animation,
"tooltip": bc.Tooltip,
"series": bc.MultiSeries,
"dataset": bc.Dataset,
}
// if only one item, use it directly instead of an Array
if len(bc.DatasetList) == 1 {
obj["dataset"] = bc.DatasetList[0]
} else if len(bc.DatasetList) > 1 {
obj["dataset"] = bc.DatasetList

}
if bc.AxisPointer != nil {
obj["axisPointer"] = bc.AxisPointer
}
Expand Down Expand Up @@ -197,6 +204,11 @@ func (bc *BaseConfiguration) GetAssets() opts.Assets {
return bc.Assets
}

// AddDataset adds a Dataset to this chart
func (bc *BaseConfiguration) AddDataset(dataset ...opts.Dataset) {
bc.DatasetList = append(bc.DatasetList, dataset...)
}

// FillDefaultValues fill default values for chart options.
func (bc *BaseConfiguration) FillDefaultValues() {
opts.SetDefaultValue(bc)
Expand Down
2 changes: 1 addition & 1 deletion charts/rectangle.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (rc *RectChart) SetGlobalOptions(options ...GlobalOpts) *RectChart {
return rc
}

//SetDispatchActions sets actions for the RectChart instance.
// SetDispatchActions sets actions for the RectChart instance.
func (rc *RectChart) SetDispatchActions(options ...GlobalActions) *RectChart {
rc.RectConfiguration.setRectGlobalActions(options...)
return rc
Expand Down
10 changes: 9 additions & 1 deletion charts/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ type SingleSeries struct {
AnimationDelayUpdate int `json:"animationDelayUpdate,omitempty"`

// series data
Data interface{} `json:"data"`
Data interface{} `json:"data,omitempty"`
DatasetIndex int `json:"datasetIndex,omitempty"`

// series options
*opts.Encode `json:"encode,omitempty"`
Expand Down Expand Up @@ -534,3 +535,10 @@ func WithEncodeOpts(opt opts.Encode) SeriesOpts {
s.Encode = &opt
}
}

// WithDatasetIndex sets the datasetIndex option.
func WithDatasetIndex(index int) SeriesOpts {
return func(s *SingleSeries) {
s.DatasetIndex = index
}
}
45 changes: 41 additions & 4 deletions opts/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ type Tooltip struct {
// }
Formatter string `json:"formatter,omitempty"`

ValueFormatter string `json:"valueFormatter,omitempty"`

// Configuration item for axisPointer
AxisPointer *AxisPointer `json:"axisPointer,omitempty"`
}
Expand All @@ -374,6 +376,10 @@ type AxisPointer struct {
Link []AxisPointerLink `json:"link,omitempty"`

Axis string `json:"axis,omitempty"`

Show bool `json:"show"`

Label *Label `json:"label,omitempty"`
}

type AxisPointerLink struct {
Expand Down Expand Up @@ -617,6 +623,8 @@ type AxisLabel struct {
VerticalAlign string `json:"verticalAlign,omitempty"`
// Line height of the axis label
LineHeight string `json:"lineHeight,omitempty"`

BackgroundColor string `json:"backgroundColor,omitempty"`
}

type AxisTick struct {
Expand Down Expand Up @@ -732,6 +740,9 @@ type XAxis struct {

// Settings related to axis tick.
AxisTick *AxisTick `json:"axisTick,omitempty"`

// Settings related to axis pointer.
AxisPointer *AxisPointer `json:"axisPointer,omitempty"`
}

// YAxis is the option set for Y axis.
Expand Down Expand Up @@ -795,6 +806,9 @@ type YAxis struct {

// Settings related to axis line.
AxisLine *AxisLine `json:"axisLine,omitempty"`

// Settings related to axis pointer.
AxisPointer *AxisPointer `json:"axisPointer,omitempty"`
}

// TextStyle is the option set for a text style component.
Expand Down Expand Up @@ -974,6 +988,20 @@ type DataZoom struct {
// If it is set as a single number, one axis is controlled, while if it is set as an Array ,
// multiple axes are controlled.
YAxisIndex interface{} `json:"yAxisIndex,omitempty"`

// LabelFormatter is the formatter tool for the label.
//
// If it is a string, it will be a template. For instance, aaaa{value}bbbb, where {value} will be replaced by the value of actual data value.
// It can also be a callback function. For example:
//
// /** @param {*} value If axis.type is 'category', `value` is the index of axis.data.
// * else `value` is current value.
// * @param {string} valueStr Inner formatted string.
// * @return {string} Returns the label formatted.
// labelFormatter: function (value, valueStr) {
// return 'aaa' + value + 'bbb';
// }
LabelFormatter string `json:"labelFormatter,omitempty"`
}

// SingleAxis is the option set for single axis.
Expand Down Expand Up @@ -1183,7 +1211,8 @@ type RadiusAxis struct {
Inverse bool `json:"inverse,omitempty"`
}

var funcPat = regexp.MustCompile(`\n|\t`)
var newlineTabPat = regexp.MustCompile(`\n|\t`)
var commentPat = regexp.MustCompile(`(//.*)\n`)

const funcMarker = "__f__"

Expand All @@ -1194,18 +1223,26 @@ type JSFunctions struct {
// AddJSFuncs adds a new JS function.
func (f *JSFunctions) AddJSFuncs(fn ...string) {
for i := 0; i < len(fn); i++ {
f.Fns = append(f.Fns, funcPat.ReplaceAllString(fn[i], ""))
f.Fns = append(f.Fns, newlineTabPat.ReplaceAllString(fn[i], ""))
}
}

// FuncOpts is the option set for handling function type.
// FuncOpts returns a string suitable for options expecting JavaScript code.
func FuncOpts(fn string) string {
return replaceJsFuncs(fn)
}

// FuncStripCommentsOpts returns a string suitable for options expecting JavaScript code,
// stripping '//' comments.
func FuncStripCommentsOpts(fn string) string {
fn = commentPat.ReplaceAllString(fn, "")
return replaceJsFuncs(fn)
}

// replace and clear up js functions string
func replaceJsFuncs(fn string) string {
return fmt.Sprintf("%s%s%s", funcMarker, funcPat.ReplaceAllString(fn, ""), funcMarker)
fn = newlineTabPat.ReplaceAllString(fn, "")
return fmt.Sprintf("%s%s%s", funcMarker, fn, funcMarker)
}

type Colors []string
Expand Down

0 comments on commit 5762dc7

Please sign in to comment.