数据可视化在作业当中的应用 Posted on 2020-06-16 Words count in article: 357 | Reading time ≈ 1 数据可视化在作业当中的应用数据可视化我用了d3去实现 home.js12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import React from 'react';import Layout from '../components/layout';import './style.scss';import Api from '../../js/Api';import Util from '../../js/Util';import { connect } from 'react-redux';import WordCloud from 'react-d3-cloud';//这里运用了d3中的词云工具,下面是定义每条信息的大小的一个函数const fontSizeMapper = (word) => Math.log2(word.value) * 5;class Home extends React.Component { constructor(props) { super(props); this.state = { list: [], }; } componentDidMount() { this.initTableData(); } //我们要做的就是随机生成豆瓣中的简短介绍,然后加入到一个数组当中 //再利用worldcould组件来生成词云 initTableData(current, params) { const token = Util.getToken(); Api.get(`/douban/getAll`, { headers: { Authorization: `Bearer ${token}` }, }).then((res) => { if (res.data) { const { data } = res.data || {}; const result = []; data.forEach((element) => { //把每条信息插入,信息源时豆瓣表格中的quote,value是其大小 result.push({ text: element.quote, value: parseInt(Math.random() * 100) }); }); this.setState({ list: result, }); } } ); }//render中渲染出词云组件 render() { const { list } = this.state; const { userData } = this.props; // const { name } = userData; return ( <div> <Layout> <div className="word-cloud"> <WordCloud width={1200} data={list} fontSizeMapper={fontSizeMapper} rotate={0} />, </div> </Layout> </div> ); }}const mapStateToProps = (state) => { return { userData: state.userData.data, };};export default connect(mapStateToProps, null)(Home); 我们可以看到每次的都是不一样的 -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/06/16/%E6%95%B0%E6%8D%AE%E5%8F%AF%E8%A7%86%E5%8C%96%E5%9C%A8%E4%BD%9C%E4%B8%9A%E5%BD%93%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.