intimate_view/src/ChartsCount.js

144 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-08-21 11:36:05 +00:00
import React from 'react';
import ReactEcharts from 'echarts-for-react';
2020-08-23 16:23:43 +00:00
import apihost from './Var.js';
function parseData(cw = {}) {
2020-08-21 11:36:05 +00:00
var legendData = [];
var seriesData = [];
var selected = {};
2020-08-23 16:23:43 +00:00
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++) {
2020-08-21 11:36:05 +00:00
// legendData.push(name);
2020-08-23 16:23:43 +00:00
var taginfo = cw[i];
2020-08-21 11:36:05 +00:00
seriesData.push({
name: taginfo.Name,
value: taginfo.Value
})
selected[taginfo.Name] = false;
2020-08-23 16:23:43 +00:00
}
2020-08-21 11:36:05 +00:00
2020-08-23 16:23:43 +00:00
seriesData.sort((a, b) => {
2020-08-21 11:36:05 +00:00
return b.value - a.value
2020-08-23 16:23:43 +00:00
})
2020-08-21 11:36:05 +00:00
2020-08-23 16:23:43 +00:00
for (var i = 0; i < seriesData.length; i++) {
2020-08-21 11:36:05 +00:00
var o = seriesData[i];
legendData.push(o.name);
selected[o.name] = i <= 20;
2020-08-23 16:23:43 +00:00
}
2020-08-21 11:36:05 +00:00
2020-08-23 16:23:43 +00:00
return { legendData: legendData, seriesData: seriesData, selected: selected }
2020-08-21 11:36:05 +00:00
}
2020-08-23 16:23:43 +00:00
function getOption(state = {}) {
2020-08-21 11:36:05 +00:00
const { platform, data } = state;
const option = {
title: {
text: `${platform}同名数量统计`,
subtext: '数据最新仅供参考',
left: 'center'
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b} : {c} ({d}%)'
},
legend: {
type: 'scroll',
orient: 'vertical',
2020-08-23 16:23:43 +00:00
right: 10,
2020-08-21 11:36:05 +00:00
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;
}
2020-08-23 16:23:43 +00:00
class ChartsCount extends React.Component {
2020-08-21 11:36:05 +00:00
state = {
option: {},
platform: this.props.platform,
data: {}
}
changePlatform = (p) => {
2020-08-23 16:23:43 +00:00
this.setState({ platform: p }, () => {
2020-08-21 11:36:05 +00:00
this.updateData();
2020-08-23 16:23:43 +00:00
});
2020-08-21 11:36:05 +00:00
};
updateData = () => {
const { platform } = this.state;
2020-08-23 16:23:43 +00:00
fetch(`${apihost}/tag/count?platform=${platform}`, {
2020-08-21 11:36:05 +00:00
"mode": "cors"
}).then(
response => {
2020-08-23 16:23:43 +00:00
response.json().then(value => {
2020-08-21 11:36:05 +00:00
this.setState({
data: parseData(value)
2020-08-23 16:23:43 +00:00
}, () => {
2020-08-21 11:36:05 +00:00
var ins = this.echarts_react.getEchartsInstance();
2020-08-23 16:23:43 +00:00
ins.setOption(getOption(this.state));
});
2020-08-21 11:36:05 +00:00
2020-08-23 16:23:43 +00:00
});
2020-08-21 11:36:05 +00:00
}
)
2020-08-23 16:23:43 +00:00
};
2020-08-21 11:36:05 +00:00
2020-08-23 16:23:43 +00:00
componentDidMount() {
2020-08-21 11:36:05 +00:00
this.updateData();
2020-08-23 16:23:43 +00:00
}
2020-08-21 11:36:05 +00:00
render() {
2020-08-23 16:23:43 +00:00
return (<div style={{ padding: 24, background: '#fff', minHeight: 780 }} >
<ReactEcharts
notMerge={true}
ref={(e) => { this.echarts_react = e; }}
option={getOption(this.state)}
style={{ height: '600px', width: '100%' }}
className='react_for_echarts' />
</div>
2020-08-21 11:36:05 +00:00
);
}
};
export default ChartsCount;
2020-08-23 16:23:43 +00:00
2020-08-21 11:36:05 +00:00