首页引入g2,饼图动态
Showing
2 changed files
with
157 additions
and
2 deletions
| ... | @@ -12,6 +12,7 @@ | ... | @@ -12,6 +12,7 @@ |
| 12 | "lint": "vue-cli-service lint" | 12 | "lint": "vue-cli-service lint" |
| 13 | }, | 13 | }, |
| 14 | "dependencies": { | 14 | "dependencies": { |
| 15 | "@antv/g2": "^4.1.7", | ||
| 15 | "@antv/g6": "^4.0.3", | 16 | "@antv/g6": "^4.0.3", |
| 16 | "axios": "^0.19.2", | 17 | "axios": "^0.19.2", |
| 17 | "core-js": "^2.6.12", | 18 | "core-js": "^2.6.12", | ... | ... |
| ... | @@ -88,8 +88,8 @@ | ... | @@ -88,8 +88,8 @@ |
| 88 | <div slot="header" class="clearfix"> | 88 | <div slot="header" class="clearfix"> |
| 89 | <span><i class="iconfont iconbingtu"></i>权利类型占比</span> | 89 | <span><i class="iconfont iconbingtu"></i>权利类型占比</span> |
| 90 | </div> | 90 | </div> |
| 91 | <div class="enter_box" ref="enterBox"> | 91 | <div class="enter_box" ref="enterBox" id="qllxzbid"> |
| 92 | <img src="../../assets/images/cglbzb.png" :style="{'height':imgHeight + 'px'}" alt=""> | 92 | <!-- <img src="../../assets/images/cglbzb.png" :style="{'height':imgHeight + 'px'}" alt=""> --> |
| 93 | </div> | 93 | </div> |
| 94 | </el-card> | 94 | </el-card> |
| 95 | <el-card class="cghj"> | 95 | <el-card class="cghj"> |
| ... | @@ -114,6 +114,7 @@ | ... | @@ -114,6 +114,7 @@ |
| 114 | </template> | 114 | </template> |
| 115 | 115 | ||
| 116 | <script> | 116 | <script> |
| 117 | import { Chart } from '@antv/g2'; | ||
| 117 | import {getDbxList} from "@api/dbx"; | 118 | import {getDbxList} from "@api/dbx"; |
| 118 | export default { | 119 | export default { |
| 119 | components:{}, | 120 | components:{}, |
| ... | @@ -181,6 +182,7 @@ export default { | ... | @@ -181,6 +182,7 @@ export default { |
| 181 | this.imgHeight = this.$refs.enterBox.offsetHeight - 10 | 182 | this.imgHeight = this.$refs.enterBox.offsetHeight - 10 |
| 182 | }); | 183 | }); |
| 183 | this.getDbxCount(); | 184 | this.getDbxCount(); |
| 185 | this.initG2(); | ||
| 184 | }, | 186 | }, |
| 185 | methods: { | 187 | methods: { |
| 186 | download(row,column,event){ | 188 | download(row,column,event){ |
| ... | @@ -243,6 +245,158 @@ export default { | ... | @@ -243,6 +245,158 @@ export default { |
| 243 | }) | 245 | }) |
| 244 | } | 246 | } |
| 245 | }) | 247 | }) |
| 248 | }, | ||
| 249 | initG2(){ | ||
| 250 | const data = [ | ||
| 251 | { item: '国有建设用地使用权', count: 40, percent: 0.4 }, | ||
| 252 | { item: '集体建设用地使用权', count: 21, percent: 0.21 }, | ||
| 253 | { item: '宅基地使用权', count: 17, percent: 0.17 }, | ||
| 254 | { item: '土地承包经营权', count: 13, percent: 0.13 }, | ||
| 255 | { item: '林地使用权', count: 9, percent: 0.09 }, | ||
| 256 | ]; | ||
| 257 | const container = document.getElementById('qllxzbid'); | ||
| 258 | |||
| 259 | const chart = new Chart({ | ||
| 260 | container: 'qllxzbid', | ||
| 261 | autoFit: true, | ||
| 262 | height: 500, | ||
| 263 | }); | ||
| 264 | |||
| 265 | chart.coordinate('theta', { | ||
| 266 | radius: 0.75, | ||
| 267 | innerRadius: 0.7, | ||
| 268 | }); | ||
| 269 | |||
| 270 | chart.data(data); | ||
| 271 | |||
| 272 | chart.scale('percent', { | ||
| 273 | formatter: val => { | ||
| 274 | val = val * 100 + '%'; | ||
| 275 | return val; | ||
| 276 | }, | ||
| 277 | }); | ||
| 278 | |||
| 279 | chart.tooltip(false); | ||
| 280 | |||
| 281 | // 声明需要进行自定义图例字段: 'item' | ||
| 282 | chart.legend('item', { | ||
| 283 | position: 'right', // 配置图例显示位置 | ||
| 284 | offsetX: -50, | ||
| 285 | custom: true, // 关键字段,告诉 G2,要使用自定义的图例 | ||
| 286 | items: data.map((obj, index) => { | ||
| 287 | return { | ||
| 288 | name: obj.item, // 对应 itemName | ||
| 289 | value: obj.percent, // 对应 itemValue | ||
| 290 | marker: { | ||
| 291 | symbol: 'square', // marker 的形状 | ||
| 292 | style: { | ||
| 293 | r: 5, // marker 图形半径 | ||
| 294 | fill: chart.getTheme().colors10[index], // marker 颜色,使用默认颜色,同图形对应 | ||
| 295 | }, | ||
| 296 | }, // marker 配置 | ||
| 297 | }; | ||
| 298 | }), | ||
| 299 | itemValue: {//百分比的样式 | ||
| 300 | style: { | ||
| 301 | fill: '#999', | ||
| 302 | }, // 配置 itemValue 样式 | ||
| 303 | formatter: val => `${val * 100}%` // 格式化 itemValue 内容 | ||
| 304 | }, | ||
| 305 | }); | ||
| 306 | |||
| 307 | chart | ||
| 308 | .interval() | ||
| 309 | .adjust('stack') | ||
| 310 | .position('percent') | ||
| 311 | .color('item') | ||
| 312 | .style({ | ||
| 313 | fillOpacity: 1, | ||
| 314 | }) | ||
| 315 | .state({ | ||
| 316 | active: { | ||
| 317 | style: element => { | ||
| 318 | const shape = element.shape; | ||
| 319 | return { | ||
| 320 | lineWidth: 10, | ||
| 321 | stroke: shape.attr('fill'), | ||
| 322 | strokeOpacity: shape.attr('fillOpacity'), | ||
| 323 | }; | ||
| 324 | }, | ||
| 325 | }, | ||
| 326 | }); | ||
| 327 | |||
| 328 | // 移除图例点击过滤交互 | ||
| 329 | chart.removeInteraction('legend-filter'); | ||
| 330 | chart.interaction('element-active'); | ||
| 331 | |||
| 332 | chart.render(); | ||
| 333 | |||
| 334 | // 监听 element 上状态的变化来动态更新 Annotation 信息 | ||
| 335 | chart.on('element:statechange', (ev) => { | ||
| 336 | const { state, stateStatus, element } = ev.gEvent.originalEvent; | ||
| 337 | |||
| 338 | // 本示例只需要监听 active 的状态变化 | ||
| 339 | if (state === 'active') { | ||
| 340 | const data = element.getData(); | ||
| 341 | if (stateStatus) { | ||
| 342 | // 更新 Annotation | ||
| 343 | updateAnnotation(data); | ||
| 344 | } else { | ||
| 345 | // 隐藏 Annotation | ||
| 346 | clearAnnotation(); | ||
| 347 | } | ||
| 348 | } | ||
| 349 | }); | ||
| 350 | // 绘制 annotation | ||
| 351 | let lastItem; | ||
| 352 | function updateAnnotation(data) { | ||
| 353 | if (data.item !== lastItem) { | ||
| 354 | chart.annotation().clear(true); | ||
| 355 | chart | ||
| 356 | .annotation() | ||
| 357 | .text({ | ||
| 358 | position: ['50%', '50%'], | ||
| 359 | content: data.item, | ||
| 360 | style: { | ||
| 361 | fontSize: 18, | ||
| 362 | fill: '#8c8c8c', | ||
| 363 | textAlign: 'center', | ||
| 364 | }, | ||
| 365 | offsetY: -20, | ||
| 366 | }) | ||
| 367 | .text({ | ||
| 368 | position: ['50%', '50%'], | ||
| 369 | content: data.count, | ||
| 370 | style: { | ||
| 371 | fontSize: 18, | ||
| 372 | fill: '#8c8c8c', | ||
| 373 | textAlign: 'center', | ||
| 374 | }, | ||
| 375 | offsetX: -10, | ||
| 376 | offsetY: 20, | ||
| 377 | }) | ||
| 378 | .text({ | ||
| 379 | position: ['50%', '50%'], | ||
| 380 | content: '件', | ||
| 381 | style: { | ||
| 382 | fontSize: 18, | ||
| 383 | fill: '#8c8c8c', | ||
| 384 | textAlign: 'center', | ||
| 385 | }, | ||
| 386 | offsetY: 20, | ||
| 387 | offsetX: 20, | ||
| 388 | }); | ||
| 389 | chart.render(true); | ||
| 390 | lastItem = data.item; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | // 清空 annotation | ||
| 395 | function clearAnnotation() { | ||
| 396 | chart.annotation().clear(true); | ||
| 397 | chart.render(true); | ||
| 398 | lastItem = null; | ||
| 399 | } | ||
| 246 | } | 400 | } |
| 247 | }, | 401 | }, |
| 248 | }; | 402 | }; | ... | ... |
-
Please register or sign in to post a comment