import React from 'react'; import ReactEcharts from 'echarts-for-react'; import apihost from './Var.js'; import { Row, Col, Button, Divider } from 'antd'; function parseData(cw = {}) { var legendData = []; var seriesData = []; var selected = {}; if (cw == null) { seriesData.push({ name: "没有数据", value: 1 }) legendData.push(seriesData[0].name) selected[seriesData[0].name] = true; return { legendData: legendData, seriesData: seriesData, selected: selected } } for (var i = 0; i < cw.length; i++) { // legendData.push(name); var taginfo = cw[i]; seriesData.push({ name: taginfo.Name, value: taginfo.Value }) selected[taginfo.Name] = false; } seriesData.sort((a, b) => { return b.value - a.value }) for (i = 0; i < seriesData.length; i++) { var o = seriesData[i]; legendData.push(o.name); selected[o.name] = i <= 20; } return { legendData: legendData, seriesData: seriesData, selected: selected } } function getOption(state = {}) { const { platform, data, title } = state; const option = { title: { text: `${platform}${title}`, subtext: '数据最新仅供参考', left: 'center' }, tooltip: { trigger: 'item', formatter: '{a}
{b} : {c} ({d}%)' }, legend: { type: 'scroll', orient: 'vertical', right: 10, top: 10, bottom: 10, data: data.legendData, selected: data.selected }, series: [ { name: '标签', type: 'pie', radius: '55%', center: ['40%', '50%'], data: data.seriesData, emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ], }; return option; } class ChartsCount extends React.Component { constructor(props) { // platform super(props) this.state = { option: {}, platform: this.props.platform, data: {}, countkey: "count", title: "与数量关系", } } changePlatform = (platform) => { this.updateData({ platform: platform, countkey: this.state.countkey }) }; updateData = (params = {}) => { // const {platform, countkey, title} = params const platform = params.platform?params.platform:this.state.platform const countkey = params.countkey?params.countkey:this.state.countkey const title = params.title?params.title:this.state.title fetch(`${apihost}/tag/count?platform=${platform}&countkey=${countkey}`, { "mode": "cors" }).then( response => { response.json().then(value => { this.setState({ title: title , platform: platform , countkey: countkey , data: parseData(value) }, () => { var ins = this.echarts_react.getEchartsInstance(); ins.setOption(getOption(this.state)); }); }); } ) }; countMethod(countkey, title) { this.updateData({countkey: countkey, title: title}); } render() { return (
{ this.echarts_react = e; }} option={getOption(this.state)} style={{ height: '600px', width: '100%' }} className='react_for_echarts' />
); } }; export default ChartsCount;