intimate_view/src/ChartsCount.js

162 lines
4.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';
2020-08-24 16:44:25 +00:00
import { Row, Col, Button, Divider } from 'antd';
2020-08-23 16:23:43 +00:00
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
for (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-24 11:07:02 +00:00
const { platform, data, title } = state;
2020-08-21 11:36:05 +00:00
const option = {
title: {
2020-08-24 11:07:02 +00:00
text: `${platform}${title}`,
2020-08-21 11:36:05 +00:00
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-31 10:10:38 +00:00
constructor(props) {
super(props)
this.state = {
option: {},
platform: this.props.platform,
data: {},
countkey: "count",
title: "与数量关系",
}
}
2020-08-21 11:36:05 +00:00
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 = () => {
2020-08-24 11:07:02 +00:00
const { countkey, platform } = this.state;
fetch(`${apihost}/tag/count?platform=${platform}&countkey=${countkey}`, {
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
2020-08-24 11:07:02 +00:00
countMethod(countkey, title) {
2020-08-26 01:54:57 +00:00
this.setState({ countkey: countkey, title: title }, () => { this.updateData() });
2020-08-24 11:07:02 +00:00
}
2020-08-21 11:36:05 +00:00
render() {
2020-08-24 11:07:02 +00:00
return (
<div style={{ padding: 24, background: '#fff', minHeight: 780 }} >
2020-08-24 16:44:25 +00:00
<div>
2020-08-26 01:54:57 +00:00
<Row justify="start" gutter={{ xs: 8, sm: 16, md: 24, lg: 32 }}>
<Col span={4}> <Button type="primary" onClick={(e) => { this.countMethod("count", e.target.innerText) }} >与数量关系</Button> </Col>
<Col span={4}> <Button type="primary" onClick={(e) => { this.countMethod("followers", e.target.innerText) }}>与关注关系</Button> </Col>
<Col span={4}> <Button type="primary" onClick={(e) => { this.countMethod("gratuity", e.target.innerText) }} >与赏币关系</Button> </Col>
2020-08-24 11:07:02 +00:00
</Row>
</div>
2020-08-24 16:44:25 +00:00
<Divider />
2020-08-24 11:07:02 +00:00
<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