-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhighcharts-column-basic-chart.js
189 lines (181 loc) · 6.8 KB
/
highcharts-column-basic-chart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* @Author: cui<devcui@outlook.com>
* @LastEditors: cui<devcui@outlook.com>
* @Date: 2022-09-22 17:01:15
* @LastEditTime: 2022-09-26 16:17:24
* @FilePath: \custom-chart-plugins\highcharts-column-basic-chart.js
* @Description:
*
* Copyright (c) 2022 by cui<devcui@outlook.com>, All Rights Reserved.
*/
function HighChartsColumnBasicChart({ dHelper }) {
let instance = null;
return {
config: {
datas: [
{ label: "dimension", key: "dimension", type: "group", allowSameField: false },
{ label: "metrics", key: "metrics", type: "aggregate", allowSameField: false }
],
styles: [
{
label: '标题',
key: 'title',
comType: 'input',
},
{
label: '描述',
key: 'desc',
comType: 'input',
}
],
i18ns: [],
},
isISOContainer: 'highcharts',
dependency: [
'/custom-chart-plugins/common/highcharts/code/highcharts.js',
'/custom-chart-plugins/common/highcharts/code/highcharts-3d.js',
'/custom-chart-plugins/common/highcharts/code/highcharts-more.js',
'/custom-chart-plugins/common/highcharts/code/css/highcharts.css',
],
meta: {
id: 'highcharts-column-basic-chart',
name: '[Highcharts][Column][Basic][Chart]',
icon: 'chart',
requirements: [
{
group: null,
aggregate: null,
},
],
},
onMount(options, context) {
if (!options.containerId || !context.document) return;
if (!options.containerId || !context.document) return;
const { window: { Highcharts } } = context
const { containerId } = options
instance = Highcharts.chart(containerId, {
chart: {
type: 'column'
},
title: {
text: '月平均降雨量'
},
subtitle: {
text: '数据来源: WorldClimate.com'
},
xAxis: {
categories: [
'一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'
],
crosshair: true
},
yAxis: {
min: 0,
title: {
text: '降雨量 (mm)'
}
},
tooltip: {
// head + 每个 point + footer 拼接成完整的 table
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
borderWidth: 0
}
},
series: [{
name: '东京',
data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
name: '纽约',
data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]
}, {
name: '伦敦',
data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]
}, {
name: '柏林',
data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]
}]
});
},
onUpdated(options, context) {
if (!options.containerId || !context.document) return;
const { config, dataset, containerId } = options;
if (!dataset || !dataset.rows || dataset.rows.length === 0) return
const { window: { Highcharts } } = context
const styleConfigs = config.styles;
// 标题
let title = styleConfigs.filter((style) => style.key === 'title')
title = title.length > 0 ? title[0].value : 'title'
// 描述
let desc = styleConfigs.filter((style) => style.key === 'desc')
desc = desc.length > 0 ? desc[0].value : 'desc'
// 获取类型
const categories = dataset.rows.map(d => d[0])
const seriesNames = dataset.rows.map(d => d[1])
// 去重
let filteredCategories = []
categories.forEach((c) => {
if (!filteredCategories.includes(c)) filteredCategories.push(c)
})
let filteredSeriesNames = []
seriesNames.forEach((c) => {
if (!filteredSeriesNames.includes(c)) filteredSeriesNames.push(c)
})
// 构造指标
let series = []
filteredSeriesNames.forEach((s) => {
const columnSerie = {
name: s,
data: [],
}
dataset.rows.forEach((r) => {
if (r[1] === s) {
columnSerie.data.push(r[2])
}
})
series.push(columnSerie)
})
instance = Highcharts.chart(containerId, {
chart: {
type: 'column'
},
title: {
text: title
},
subtitle: {
text: desc
},
xAxis: {
categories: filteredCategories,
crosshair: true
},
yAxis: {
min: 0,
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
borderWidth: 0
}
},
series: series
});
},
onUnMount() { },
onResize(options, context) { }
}
}