/* * 封装的饼图 * @zjf-2021-06-30 * */ import React, { Component, Fragment } from 'react'; import global from '@/global.less'; import DataSet from '@antv/data-set'; import { noDataPlaceholder } from '@/utils/utils'; import { Chart, Geom, Axis, Tooltip, Coord, Label, Legend, } from 'bizcharts'; export default class Pie extends Component { constructor(props) { super(props); this.state = { cols: { month: { range: [0, 1], }, }, pieData: props.data, dv: '', colsPie: { percent: { formatter: val => { val = (val * 100).toFixed(2) + '%'; return val; }, }, }, scaleWidth: window.innerWidth, axisType: props.axisType === 'amount' ? props.axisType : 'number', }; } componentDidMount() { this.initData(); window.addEventListener('resize', () => { this.setState({ scaleWidth: 0 }, () => { this.setState({ scaleWidth: window.innerWidth }); }); }); } componentWillReceiveProps(nextProps, nextContext) { let _this = this; if (JSON.stringify(nextProps.data) != JSON.stringify(this.props.data)) { this.setState({ pieData: nextProps.data, axisType: nextProps.axisType }, () => { _this.initData(); }); } } initData = () => { let { pieData } = this.state; const { DataView } = DataSet; let dv = new DataView(); if (pieData.length > 0) { dv.source(pieData).transform({ type: 'percent', field: 'count', dimension: 'item', as: 'percent', }); } else { dv = ''; } this.setState({ dv, }); }; render() { const { colsPie, dv, scaleWidth, axisType } = this.state; const { tipTitle, showNumPrecision } = this.props; return ( {dv ?
{ function getXY(c, { index: idx = 0, field = 'percent', radius = 0.5 }) { const d = c.get('data'); if (idx > d.length) return; const scales = c.get('scales'); let sum = 0; for (let i = 0; i < idx + 1; i++) { let val = d[i][field]; if (i === idx) { val = val / 2; } sum += val; } const pt = { y: scales[field].scale(sum), x: radius, }; const coord = c.get('coord'); let xy = coord.convert(pt); return xy; } const xy = getXY(c, { index: 0 }); c.showTooltip(xy); }} > { return { title: tipTitle ? tipTitle : '销售额', name: item, value: `${axisType === 'amount' ? '¥' : ''}${count.toFixed(showNumPrecision != undefined ? showNumPrecision : 2)}` + (percent * 100).toFixed(2) + '%', }; }, ]} style={{ lineWidth: 1, stroke: '#fff', }} >
: noDataPlaceholder()}
); } }