import * as React from 'react'; import './circleChart.scss'; import { taker, getApiUrl2 } from '&/helper' import Tab from '../../common/tab'; import NoData from '&/components/common/noDataView'; interface Props{ } interface labelPosition{ x:number, y:number, } const COLORS = ['#31bbff', '#ffad62', '#29ead0', '#8cd0ff', '#c0c0ff', '#02e0f0', '#fe96b7', '#88cda3', '#f9ce94', '#8db4f5', '#daa5ed', '#fe77a0', '#f8ad98', '#c5dceb', '#91afbf', '#cbe796']; const tabItems = [{ text: '打赏分布图', value:1 },{ text: '获赏分布图', value:2 }] const RADIAN = Math.PI / 180; const cns = ['left-top','right-top','left-bottom','right-bottom'] interface item { name:string, count:number } export default class CircleChart extends React.Component{ state:{ Rechart:any, type:number, data:Array, loaded:boolean } refs:any constructor(p:Props) { super(p); this.loadChart(); this.state = { Rechart:undefined, type:1, data:[], loaded:false }; this.labelPosition=[]; this.labelData=[]; this.getData = this.getData.bind(this); } labelPosition:Array labelData:Array loadChart() { import(/* webpackChunkName: "recharts" */ "recharts").then(Rechart => { this.setState({ Rechart }); }); } async getData(value?:number){ // GET /api/v2/{tenant_id}/{user_id}/reward/diagram // 赏金分布图 type:1:打赏的金额 2:收赏的金额 const url = getApiUrl2(`reward/diagram?type=${value || this.state.type}`) const response = await taker(url); response.Data.map((res:any,k:number)=>{ res.value = res.count }) this.setState({ type:value, data: response.Data, loaded:false }) } componentWillMount(){ this.getData() } handleTabChange=(value:number,k:number)=>{ this.getData(value); // this.setState({type:value, loaded:false}); } renderCustomizedLabel = (data:any) => { const { cx, cy, midAngle, innerRadius, outerRadius, percent } = data; const radius = innerRadius + (outerRadius - innerRadius) * 2; const x = cx + radius * Math.cos(-midAngle * RADIAN); const y = cy + radius * Math.sin(-midAngle * RADIAN); !this.state.loaded && this.labelPosition.push({x,y}) && this.labelData.push({name:data.payload.name ,persent:(percent * 100).toFixed(0) + '%'}); if(data.index == this.state.data.length-1 && !this.state.loaded){ //最后一个 this.setState({loaded:true}) } return ( cx ? 'start' : 'end'} dominantBaseline="central"> {`${data.name} ${(percent * 100).toFixed(0)}%`} ); }; getTipPosition = (position:any) => { let x = position.x; let y = position.y; const container = this.refs.containner; const conWidth = container.state.containerWidth/2; const conHeight = container.state.containerHeight/2; //return { left:x,top:y } let style = { left:position.x-=85, top:position.y+=20 } let positions; if(x<=conWidth && y< conHeight){ // 图表左上方 style.left -= 10; positions =0; }else if(x>conWidth && y< conHeight){ //图表右上方 style.left += 80; positions=1; }else if(x<=conWidth && y>= conHeight){ //图表左下方 style.top -= 10; positions=2; } else{ // 图表右下方 style.left += 60; style.top -= 10; positions=3; } if(style.top > 225){ style.top = 215 } return {style:style,positions:positions}; } getLabelStyle = (positions:number) => { switch(positions){ case 0: //左上 return {top:'-30px'}; case 1: // 右上 return { textAlign:'right',top: '-30px' }; case 2: // 左下 return {top:'-15px'}; default: // 右下 return {top:'-15px',textAlign:'right'}; } } render(){ const { Rechart, data, loaded } = this.state; // const data = [{name: "同事", count: 5, value: 5},{name: "同事1", count: 5, value: 5},{name: "同事2", count: 5, value: 5},{name: "同事3", count: 5, value: 5}] if(!Rechart) return null; return
{ !data.length ? :
{ data.map((entry:any, index:number) => ) } {loaded && this.labelPosition.reverse().slice(0,data.length).map((position:labelPosition,k:number)=>{ const temp = this.labelData[this.labelData.length-k-1]; const { style, positions } = this.getTipPosition(position); return { temp.name } { temp.persent } })}
}
} }