| 1 |
package charts |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"html/template" |
| 7 |
|
| 8 |
"github.com/go-echarts/go-echarts/v2/datasets" |
| 9 |
"github.com/go-echarts/go-echarts/v2/event" |
| 10 |
"github.com/go-echarts/go-echarts/v2/opts" |
| 11 |
"github.com/go-echarts/go-echarts/v2/render" |
| 12 |
"github.com/go-echarts/go-echarts/v2/types" |
| 13 |
"github.com/go-echarts/go-echarts/v2/util" |
| 14 |
) |
| 15 |
|
| 16 |
var defaultConfigurationVisitor ConfigurationVisitor = BaseConfigurationVisitor{} |
| 17 |
|
| 18 |
|
| 19 |
type GlobalOpts func(bc *BaseConfiguration) |
| 20 |
|
| 21 |
|
| 22 |
type BaseConfiguration struct { |
| 23 |
opts.Legend `json:"legend"` |
| 24 |
opts.Tooltip `json:"tooltip"` |
| 25 |
opts.Toolbox `json:"toolbox"` |
| 26 |
opts.Title `json:"title"` |
| 27 |
opts.Polar `json:"polar"` |
| 28 |
opts.AngleAxis `json:"angleAxis"` |
| 29 |
opts.RadiusAxis `json:"radiusAxis"` |
| 30 |
opts.Brush `json:"brush"` |
| 31 |
*opts.AxisPointer `json:"axisPointer"` |
| 32 |
*opts.Aria `json:"aria"` |
| 33 |
Calendar []*opts.Calendar `json:"calendar"` |
| 34 |
|
| 35 |
render.Renderer `json:"-"` |
| 36 |
opts.Initialization `json:"-"` |
| 37 |
opts.Assets `json:"-"` |
| 38 |
opts.RadarComponent `json:"-"` |
| 39 |
opts.GeoComponent `json:"-"` |
| 40 |
opts.ParallelComponent `json:"-"` |
| 41 |
opts.JSFunctions `json:"-"` |
| 42 |
opts.SingleAxis `json:"-"` |
| 43 |
|
| 44 |
MultiSeries |
| 45 |
XYAxis |
| 46 |
|
| 47 |
opts.XAxis3D |
| 48 |
opts.YAxis3D |
| 49 |
opts.ZAxis3D |
| 50 |
opts.Grid3D |
| 51 |
opts.Grid |
| 52 |
|
| 53 |
legends []string |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Colors []string |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
Animation types.Bool `json:"animation,omitempty"` |
| 62 |
AnimationThreshold types.Int `json:"animationThreshold,omitempty"` |
| 63 |
|
| 64 |
AnimationDuration types.FuncStr `json:"animationDuration,omitempty"` |
| 65 |
AnimationEasing string `json:"animationEasing,omitempty"` |
| 66 |
AnimationDelay types.FuncStr `json:"animationDelay,omitempty"` |
| 67 |
AnimationDurationUpdate types.FuncStr `json:"animationDurationUpdate,omitempty"` |
| 68 |
AnimationEasingUpdate string `json:"animationEasingUpdate,omitempty"` |
| 69 |
AnimationDelayUpdate types.FuncStr `json:"animationDelayUpdate,omitempty"` |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
Progressive types.Int `json:"progressive,omitempty"` |
| 74 |
|
| 75 |
ProgressiveThreshold types.Int `json:"progressiveThreshold,omitempty"` |
| 76 |
|
| 77 |
|
| 78 |
DatasetList []opts.Dataset `json:"dataset,omitempty"` |
| 79 |
|
| 80 |
DataZoomList []opts.DataZoom `json:"datazoom,omitempty"` |
| 81 |
VisualMapList []opts.VisualMap `json:"visualmap,omitempty"` |
| 82 |
|
| 83 |
EventListeners []event.Listener `json:"-"` |
| 84 |
configurationVisitor ConfigurationVisitor |
| 85 |
|
| 86 |
|
| 87 |
ParallelAxisList []opts.ParallelAxis |
| 88 |
|
| 89 |
has3DAxis bool |
| 90 |
hasXYAxis bool |
| 91 |
hasGeo bool |
| 92 |
hasRadar bool |
| 93 |
hasParallel bool |
| 94 |
hasSingleAxis bool |
| 95 |
hasPolar bool |
| 96 |
hasBrush bool |
| 97 |
|
| 98 |
GridList []opts.Grid `json:"grid,omitempty"` |
| 99 |
} |
| 100 |
|
| 101 |
func (bc *BaseConfiguration) Accept(visitor ConfigurationVisitor) { |
| 102 |
bc.configurationVisitor = visitor |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func (bc *BaseConfiguration) JSON() map[string]interface{} { |
| 110 |
return bc.json() |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
func (bc *BaseConfiguration) JSONNotEscaped() template.HTML { |
| 116 |
obj := bc.json() |
| 117 |
buff := bytes.NewBufferString("") |
| 118 |
enc := json.NewEncoder(buff) |
| 119 |
enc.SetEscapeHTML(false) |
| 120 |
_ = enc.Encode(obj) |
| 121 |
|
| 122 |
return template.HTML(buff.String()) |
| 123 |
} |
| 124 |
|
| 125 |
func (bc *BaseConfiguration) json() map[string]interface{} { |
| 126 |
visitor := defaultConfigurationVisitor |
| 127 |
if bc.configurationVisitor != nil { |
| 128 |
visitor = bc.configurationVisitor |
| 129 |
} |
| 130 |
|
| 131 |
obj := map[string]interface{}{ |
| 132 |
"title": visitor.VisitTitleOpt(bc.Title), |
| 133 |
"legend": visitor.VisitLegendOpt(bc.Legend), |
| 134 |
"tooltip": visitor.VisitTooltipOpt(bc.Tooltip), |
| 135 |
"series": visitor.VisitSeriesOpt(bc.MultiSeries), |
| 136 |
} |
| 137 |
|
| 138 |
if bc.Animation != nil { |
| 139 |
obj["animation"] = bc.Animation |
| 140 |
} |
| 141 |
|
| 142 |
if bc.Progressive != nil { |
| 143 |
obj["progressive"] = bc.Animation |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
if bc.ProgressiveThreshold != nil { |
| 148 |
obj["progressiveThreshold"] = bc.ProgressiveThreshold |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
if len(bc.DatasetList) == 1 { |
| 153 |
obj["dataset"] = visitor.VisitDatasets(bc.DatasetList[0]) |
| 154 |
} else if len(bc.DatasetList) > 1 { |
| 155 |
obj["dataset"] = visitor.VisitDatasets(bc.DatasetList...) |
| 156 |
} |
| 157 |
if bc.AxisPointer != nil { |
| 158 |
obj["axisPointer"] = visitor.VisitAxisPointer(bc.AxisPointer) |
| 159 |
} |
| 160 |
|
| 161 |
if bc.Aria != nil { |
| 162 |
obj["aria"] = visitor.VisitAria(bc.Aria) |
| 163 |
} |
| 164 |
|
| 165 |
if bc.hasPolar { |
| 166 |
obj["polar"] = visitor.VisitPolar(bc.Polar) |
| 167 |
obj["angleAxis"] = visitor.VisitAngleAxis(bc.AngleAxis) |
| 168 |
obj["radiusAxis"] = visitor.VisitRadiusAxis(bc.RadiusAxis) |
| 169 |
} |
| 170 |
|
| 171 |
if bc.hasGeo { |
| 172 |
obj["geo"] = visitor.VisitGeo(bc.GeoComponent) |
| 173 |
} |
| 174 |
|
| 175 |
if bc.hasRadar { |
| 176 |
obj["radar"] = visitor.VisitRadar(bc.RadarComponent) |
| 177 |
} |
| 178 |
|
| 179 |
if bc.hasParallel { |
| 180 |
obj["parallel"] = visitor.VisitParallel(bc.ParallelComponent) |
| 181 |
obj["parallelAxis"] = visitor.VisitParallelAxis(bc.ParallelAxisList) |
| 182 |
} |
| 183 |
|
| 184 |
if bc.hasSingleAxis { |
| 185 |
obj["singleAxis"] = visitor.VisitSingleAxis(bc.SingleAxis) |
| 186 |
} |
| 187 |
|
| 188 |
obj["toolbox"] = visitor.VisitToolbox(bc.Toolbox) |
| 189 |
|
| 190 |
if len(bc.DataZoomList) > 0 { |
| 191 |
obj["dataZoom"] = visitor.VisitDataZooms(bc.DataZoomList) |
| 192 |
} |
| 193 |
|
| 194 |
if len(bc.VisualMapList) > 0 { |
| 195 |
obj["visualMap"] = visitor.VisitVisualMaps(bc.VisualMapList) |
| 196 |
} |
| 197 |
|
| 198 |
if bc.hasXYAxis { |
| 199 |
obj["xAxis"] = visitor.VisitXAxis(bc.XAxisList) |
| 200 |
obj["yAxis"] = visitor.VisitYAxis(bc.YAxisList) |
| 201 |
} |
| 202 |
|
| 203 |
if bc.has3DAxis { |
| 204 |
obj["xAxis3D"] = visitor.VisitXAxis3D(bc.XAxis3D) |
| 205 |
obj["yAxis3D"] = visitor.VisitYAxis3D(bc.YAxis3D) |
| 206 |
obj["zAxis3D"] = visitor.VisitZAxis3D(bc.ZAxis3D) |
| 207 |
obj["grid3D"] = visitor.VisitGrid3D(bc.Grid3D) |
| 208 |
} |
| 209 |
|
| 210 |
if bc.Theme == "white" { |
| 211 |
obj["color"] = bc.Colors |
| 212 |
} |
| 213 |
|
| 214 |
if bc.Initialization.BackgroundColor != "" { |
| 215 |
obj["backgroundColor"] = bc.Initialization.BackgroundColor |
| 216 |
} |
| 217 |
|
| 218 |
if len(bc.GridList) > 0 { |
| 219 |
obj["grid"] = visitor.VisitGrid(bc.GridList) |
| 220 |
} |
| 221 |
|
| 222 |
if bc.hasBrush { |
| 223 |
obj["brush"] = visitor.VisitBrush(bc.Brush) |
| 224 |
} |
| 225 |
|
| 226 |
if bc.Calendar != nil { |
| 227 |
obj["calendar"] = visitor.VisitCalendar(bc.Calendar) |
| 228 |
} |
| 229 |
|
| 230 |
visitor.Visit(obj) |
| 231 |
return obj |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
func (bc *BaseConfiguration) GetAssets() opts.Assets { |
| 236 |
return bc.Assets |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
func (bc *BaseConfiguration) AddDataset(dataset ...opts.Dataset) { |
| 241 |
bc.DatasetList = append(bc.DatasetList, dataset...) |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
func (bc *BaseConfiguration) FillDefaultValues() { |
| 246 |
util.SetDefaultValue(bc) |
| 247 |
} |
| 248 |
|
| 249 |
func (bc *BaseConfiguration) initBaseConfiguration() { |
| 250 |
bc.initSeriesColors() |
| 251 |
bc.InitAssets() |
| 252 |
bc.initXYAxis() |
| 253 |
bc.Initialization.Validate() |
| 254 |
bc.FillDefaultValues() |
| 255 |
} |
| 256 |
|
| 257 |
func (bc *BaseConfiguration) initSeriesColors() { |
| 258 |
bc.Colors = []string{ |
| 259 |
"#5470c6", "#91cc75", "#fac858", "#ee6666", "#73c0de", |
| 260 |
"#3ba272", "#fc8452", "#9a60b4", "#ea7ccc", |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
func (bc *BaseConfiguration) setSeriesColors(colors []string) { |
| 265 |
bc.Colors = colors |
| 266 |
} |
| 267 |
|
| 268 |
func (bc *BaseConfiguration) setBaseGlobalOptions(opts ...GlobalOpts) { |
| 269 |
for _, opt := range opts { |
| 270 |
opt(bc) |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
func WithAngleAxisOps(opt opts.AngleAxis) GlobalOpts { |
| 276 |
return func(bc *BaseConfiguration) { |
| 277 |
bc.AngleAxis = opt |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
func WithRadiusAxisOps(opt opts.RadiusAxis) GlobalOpts { |
| 283 |
return func(bc *BaseConfiguration) { |
| 284 |
bc.RadiusAxis = opt |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
func WithBrush(opt opts.Brush) GlobalOpts { |
| 290 |
return func(bc *BaseConfiguration) { |
| 291 |
bc.hasBrush = true |
| 292 |
bc.Brush = opt |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
func WithPolarOps(opt opts.Polar) GlobalOpts { |
| 298 |
return func(bc *BaseConfiguration) { |
| 299 |
bc.Polar = opt |
| 300 |
bc.hasPolar = true |
| 301 |
bc.hasXYAxis = false |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
func WithTitleOpts(opt opts.Title) GlobalOpts { |
| 307 |
return func(bc *BaseConfiguration) { |
| 308 |
bc.Title = opt |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
func WithAnimation(enable bool) GlobalOpts { |
| 314 |
return func(bc *BaseConfiguration) { |
| 315 |
bc.Animation = opts.Bool(enable) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
func WithProgressive(opt int) GlobalOpts { |
| 321 |
return func(bc *BaseConfiguration) { |
| 322 |
bc.Progressive = opts.Int(opt) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
func WithProgressiveThreshold(opt int) GlobalOpts { |
| 328 |
return func(bc *BaseConfiguration) { |
| 329 |
bc.ProgressiveThreshold = opts.Int(opt) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
func WithToolboxOpts(opt opts.Toolbox) GlobalOpts { |
| 335 |
return func(bc *BaseConfiguration) { |
| 336 |
bc.Toolbox = opt |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
func WithSingleAxisOpts(opt opts.SingleAxis) GlobalOpts { |
| 342 |
return func(bc *BaseConfiguration) { |
| 343 |
bc.SingleAxis = opt |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
func WithTooltipOpts(opt opts.Tooltip) GlobalOpts { |
| 349 |
return func(bc *BaseConfiguration) { |
| 350 |
bc.Tooltip = opt |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
func WithLegendOpts(opt opts.Legend) GlobalOpts { |
| 356 |
return func(bc *BaseConfiguration) { |
| 357 |
bc.Legend = opt |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
func WithEventListeners(listeners ...event.Listener) GlobalOpts { |
| 362 |
return func(bc *BaseConfiguration) { |
| 363 |
bc.EventListeners = append(bc.EventListeners, listeners...) |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
func WithInitializationOpts(opt opts.Initialization) GlobalOpts { |
| 369 |
return func(bc *BaseConfiguration) { |
| 370 |
bc.Initialization = opt |
| 371 |
if bc.Initialization.Theme != "" && |
| 372 |
bc.Initialization.Theme != "white" && |
| 373 |
bc.Initialization.Theme != "dark" && |
| 374 |
types.PresetTheme(opt.Theme) { |
| 375 |
bc.JSAssets.Add("themes/" + opt.Theme + ".js") |
| 376 |
} |
| 377 |
bc.Initialization.Validate() |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
func WithDataZoomOpts(opt ...opts.DataZoom) GlobalOpts { |
| 383 |
return func(bc *BaseConfiguration) { |
| 384 |
bc.DataZoomList = append(bc.DataZoomList, opt...) |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
func WithVisualMapOpts(opt ...opts.VisualMap) GlobalOpts { |
| 390 |
return func(bc *BaseConfiguration) { |
| 391 |
bc.VisualMapList = append(bc.VisualMapList, opt...) |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
func WithRadarComponentOpts(opt opts.RadarComponent) GlobalOpts { |
| 397 |
return func(bc *BaseConfiguration) { |
| 398 |
bc.RadarComponent = opt |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
func WithGeoComponentOpts(opt opts.GeoComponent) GlobalOpts { |
| 404 |
return func(bc *BaseConfiguration) { |
| 405 |
bc.GeoComponent = opt |
| 406 |
mapFile, preset := datasets.PresetMapFileNames[opt.Map] |
| 407 |
if preset { |
| 408 |
bc.JSAssets.Add("maps/" + mapFile + ".js") |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
func WithParallelComponentOpts(opt opts.ParallelComponent) GlobalOpts { |
| 415 |
return func(bc *BaseConfiguration) { |
| 416 |
bc.ParallelComponent = opt |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
func WithParallelAxisList(opt []opts.ParallelAxis) GlobalOpts { |
| 422 |
return func(bc *BaseConfiguration) { |
| 423 |
bc.ParallelAxisList = opt |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
func WithColorsOpts(opt opts.Colors) GlobalOpts { |
| 429 |
return func(bc *BaseConfiguration) { |
| 430 |
bc.setSeriesColors(opt) |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
func WithGridOpts(opt ...opts.Grid) GlobalOpts { |
| 436 |
return func(bc *BaseConfiguration) { |
| 437 |
bc.GridList = append(bc.GridList, opt...) |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
func WithAxisPointerOpts(opt *opts.AxisPointer) GlobalOpts { |
| 443 |
return func(bc *BaseConfiguration) { |
| 444 |
bc.AxisPointer = opt |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
func WithAriaOpts(opt *opts.Aria) GlobalOpts { |
| 449 |
return func(bc *BaseConfiguration) { |
| 450 |
bc.Aria = opt |
| 451 |
} |
| 452 |
} |