Skip to content

Commit

Permalink
fix: fix base actions issue. (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koooooo-7 committed Mar 31, 2023
1 parent 7b2bc0e commit fb0b97b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions render/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"html/template"
"io"
"reflect"
"regexp"

tpls "github.com/go-echarts/go-echarts/v2/templates"
Expand Down Expand Up @@ -86,12 +87,29 @@ func (r *chartRender) Render(w io.Writer) error {
return err
}

// isSet check if the field exist in the chart instance
// Shamed copy from https://stackoverflow.com/questions/44675087/golang-template-variable-isset
func isSet(name string, data interface{}) bool {
v := reflect.ValueOf(data)

if v.Kind() == reflect.Ptr {
v = v.Elem()
}

if v.Kind() != reflect.Struct {
return false
}

return v.FieldByName(name).IsValid()
}

// MustTemplate creates a new template with the given name and parsed contents.
func MustTemplate(name string, contents []string) *template.Template {
tpl := template.Must(template.New(name).Parse(contents[0])).Funcs(template.FuncMap{
"safeJS": func(s interface{}) template.JS {
return template.JS(fmt.Sprint(s))
},
"isSet": isSet,
})

for _, cont := range contents[1:] {
Expand Down
4 changes: 4 additions & 0 deletions templates/base.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package templates

// We should check the BaseActions field before call the JSONNotEscapedAction since the BaseActions only exist in RectCharts

var BaseTpl = `
{{- define "base" }}
<div class="container">
Expand All @@ -10,7 +12,9 @@ var BaseTpl = `
"use strict";
let goecharts_{{ .ChartID | safeJS }} = echarts.init(document.getElementById('{{ .ChartID | safeJS }}'), "{{ .Theme }}");
let option_{{ .ChartID | safeJS }} = {{ .JSONNotEscaped | safeJS }};
{{ if isSet "BaseActions" . }}
let action_{{ .ChartID | safeJS }} = {{ .JSONNotEscapedAction | safeJS }};
{{ end }}
goecharts_{{ .ChartID | safeJS }}.setOption(option_{{ .ChartID | safeJS }});
goecharts_{{ .ChartID | safeJS }}.dispatchAction(action_{{ .ChartID | safeJS }});
Expand Down

0 comments on commit fb0b97b

Please sign in to comment.