@@ -69,7 +69,7 @@ title: 自定义插件化图表
6969
7070> 需求定义: 制作一个散点图,包含一个或多个的维度列数据,两个指标列数据作为散点图的X、Y轴。
7171
72- 完整代码及说明如下:
72+ 完整代码及说明如下:
7373
7474``` javascript
7575/**
@@ -96,64 +96,64 @@ function D3JSScatterChart({ dHelper }) {
9696 config: {
9797 datas: [
9898 {
99- label: ' metrics ' ,
100- key: ' metrics ' ,
101- required: true ,
102- type: ' group' ,
99+ label: " dimension " ,
100+ key: " dimension " ,
101+ required: false ,
102+ type: " group" ,
103103 },
104104 {
105- label: ' deminsion ' ,
106- key: ' deminsion ' ,
105+ label: " metrics " ,
106+ key: " metrics " ,
107107 required: true ,
108- type: ' aggregate' ,
108+ type: " aggregate" ,
109109 },
110110 ],
111111 styles: [
112112 {
113- label: ' common.title' ,
114- key: ' scatter' ,
115- comType: ' group' ,
113+ label: " common.title" ,
114+ key: " scatter" ,
115+ comType: " group" ,
116116 rows: [
117117 {
118- label: ' common.color' ,
119- key: ' color' ,
120- comType: ' fontColor' ,
118+ label: " common.color" ,
119+ key: " color" ,
120+ comType: " fontColor" ,
121121 },
122122 ],
123123 },
124124 ],
125125 i18ns: [
126126 {
127- lang: ' zh-CN' ,
127+ lang: " zh-CN" ,
128128 translation: {
129129 common: {
130- title: ' 散点图配置' ,
131- color: ' 气泡颜色' ,
130+ title: " 散点图配置" ,
131+ color: " 气泡颜色" ,
132132 },
133133 },
134134 },
135135 {
136- lang: ' en ' ,
136+ lang: " en " ,
137137 translation: {
138138 common: {
139- title: ' Scatter Setting' ,
140- color: ' Bubble Color' ,
139+ title: " Scatter Setting" ,
140+ color: " Bubble Color" ,
141141 },
142142 },
143143 },
144144 ],
145145 },
146146
147- isISOContainer: ' demo-d3js-scatter-chart' ,
148-
147+ isISOContainer: " demo-d3js-scatter-chart" ,
148+
149149 // 【必须】加载D3JS绘图引擎,此处需给出CDN链接或者服务端相对资源地址即可
150- dependency: [' https://d3js.org/d3.v5.min.js' ],
150+ dependency: [" https://d3js.org/d3.v5.min.js" ],
151151
152152 // 【必须】设置图表的基本信息,icon可从Datart Icon图标中选取,暂时不支持自定义
153153 meta: {
154- id: ' demo-d3js-scatter-chart' ,
155- name: ' [Plugin Demo] D3JS 散点图' ,
156- icon: ' sandiantu' ,
154+ id: " demo-d3js-scatter-chart" ,
155+ name: " [Plugin Demo] D3JS 散点图" ,
156+ icon: " sandiantu" ,
157157 requirements: [
158158 {
159159 group: [0 , 999 ],
@@ -176,11 +176,11 @@ function D3JSScatterChart({ dHelper }) {
176176 // 初始化D3JS绘图区域
177177 this .chart = context .window .d3
178178 .select (host)
179- .append (' svg' )
180- .attr (' width' , width + margin .left + margin .right )
181- .attr (' height' , height + margin .top + margin .bottom )
182- .append (' g ' )
183- .attr (' transform' , ' translate(' + margin .left + ' , ' + margin .top + ' ) ' );
179+ .append (" svg" )
180+ .attr (" width" , width + margin .left + margin .right )
181+ .attr (" height" , height + margin .top + margin .bottom )
182+ .append (" g " )
183+ .attr (" transform" , " translate(" + margin .left + " , " + margin .top + " ) " );
184184 },
185185
186186 onUpdated (options , context ) {
@@ -205,8 +205,8 @@ function D3JSScatterChart({ dHelper }) {
205205 .range ([0 , width]); // This is the corresponding value I want in Pixel
206206
207207 this .chart
208- .append (' g ' )
209- .attr (' transform' , ' translate(0,' + height + ' ) ' )
208+ .append (" g " )
209+ .attr (" transform" , " translate(0," + height + " ) " )
210210 .call (context .window .d3 .axisBottom (x));
211211
212212 // X scale and Axis
@@ -215,24 +215,24 @@ function D3JSScatterChart({ dHelper }) {
215215 .domain ([0 , 100 ]) // This is the min and the max of the data: 0 to 100 if percentages
216216 .range ([height, 0 ]); // This is the corresponding value I want in Pixel
217217
218- this .chart .append (' g ' ).call (context .window .d3 .axisLeft (y));
218+ this .chart .append (" g " ).call (context .window .d3 .axisLeft (y));
219219
220220 // Add 3 dots for 0, 50 and 100%
221221 this .chart
222- .selectAll (' whatever' )
222+ .selectAll (" whatever" )
223223 .data (data)
224224 .enter ()
225- .append (' circle' )
226- .attr (' cx ' , function (d ) {
225+ .append (" circle" )
226+ .attr (" cx " , function (d ) {
227227 return x (d .x );
228228 })
229- .attr (' cy ' , function (d ) {
229+ .attr (" cy " , function (d ) {
230230 return y (d .y );
231231 })
232- .style (' fill' , style .color )
233- .attr (' r ' , 7 );
232+ .style (" fill" , style .color )
233+ .attr (" r " , 7 );
234234
235- this .chart .selectAll (' whatever' ).style (' color' , ' blue' );
235+ this .chart .selectAll (" whatever" ).style (" color" , " blue" );
236236 },
237237
238238 getOptions (dataset , config ) {
@@ -242,52 +242,53 @@ function D3JSScatterChart({ dHelper }) {
242242 // 获取样式配置信息
243243 const styleConfigs = config .styles ;
244244 const groupConfigs = dataConfigs
245- .filter (c => c .type === ' group' )
246- .flatMap (config => config .rows || []);
245+ .filter (( c ) => c .type === " group" )
246+ .flatMap (( config ) => config .rows || []);
247247
248248 // 获取指标类型配置信息
249249 const aggregateConfigs = dataConfigs
250- .filter (c => c .type === ' aggregate' )
251- .flatMap (config => config .rows || []);
250+ .filter (( c ) => c .type === " aggregate" )
251+ .flatMap (( config ) => config .rows || []);
252252
253253 // 数据转换,根据Datart提供了Helper转换工具
254- const objDataColumns = dHelper .transfromToObjectArray (
254+ const objDataColumns = dHelper .transformToObjectArray (
255255 dataset .rows ,
256- dataset .columns ,
256+ dataset .columns
257257 );
258-
259- const data = objDataColumns .map (dc => {
258+ const data = objDataColumns .map ((dc ) => {
260259 return {
261260 x: dc[dHelper .getValueByColumnKey (aggregateConfigs[0 ])],
262261 y: dc[dHelper .getValueByColumnKey (aggregateConfigs[1 ])],
263262 };
264263 });
265264
266- var xMinValue = Math .min (... data .map (o => o .x ));
267- var xMaxValue = Math .max (... data .map (o => o .y ));
265+ var xMinValue = Math .min (... data .map (( o ) => o .x ));
266+ var xMaxValue = Math .max (... data .map (( o ) => o .y ));
268267
269- var yMinValue = Math .min (... data .map (o => o .y ));
270- var yMaxValue = Math .max (... data .map (o => o .y ));
268+ var yMinValue = Math .min (... data .map (( o ) => o .y ));
269+ var yMaxValue = Math .max (... data .map (( o ) => o .y ));
271270
272271 // 获取用户配置
273272 const color = dHelper .getStyleValueByGroup (
274273 styleConfigs,
275- ' scatter' ,
276- ' color' ,
274+ " scatter" ,
275+ " color"
277276 );
278277
279278 return {
280279 style: {
281280 color,
282281 },
283- data: data .map (d => {
282+ data: data .map (( d ) => {
284283 return {
285284 x: ((d .x || xMinValue - xMinValue) * 100 ) / (xMaxValue - xMinValue),
286285 y: ((d .y || yMinValue - yMinValue) * 100 ) / (yMaxValue - yMinValue),
287286 };
288287 }),
289288 };
290289 },
290+
291+ onUnMount () {},
291292 };
292293}
293294```
0 commit comments