数据可视化——词云制作

觉醒年代豆瓣短评词云生成


关键词提取以及频次统计采用python爬虫,参考博客: Python爬虫

数据存储:Mysql数据库

以下为可视化代码

    <!DOCTYPE html>
    <head>
        <meta charset="UTF-8">
        <center><font size="8"><strong>《觉醒年代》豆瓣评论关键词</strong></font></center>
    </head>
    <body>
    <div id="wc"></div>
  <%@ page contentType="text/html; charset=UTF-8" %> 
  <%@ page language="java" %> 
  <%@ page import="com.mysql.jdbc.Driver" %> 
  <%@ page import="java.sql.*" %> 
    <% 
                            //out.print("

觉醒年代

");
String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPasswd="54.wfy"; String dbName="jxnd"; String tableName1="jxnd"; String label1[]=new String[100]; float rate1[]=new float[100]; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName+"?serverTimezone=GMT%2B8","root","54.wfy"); Statement statement = connection.createStatement(); for(int i=1;i < 101;i++){ String sql="SELECT * FROM "+tableName1+" where id='"+i+"'"; String str=" "; float per=1; ResultSet rs = statement.executeQuery(sql); while(rs.next()){ str=rs.getString(2); per=rs.getFloat(3); } label1[i-1]=str; rate1[i-1]=per; //out.print(label1[i-1]+" "); //out.print(rate1[i-1]); rs.close(); } statement.close(); connection.close(); %> <script src="d3.v3.min.js"></script> <script src="d3.layout.cloud.js"></script> <script> var width=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; var height=window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; w=width*0.98; h=height*3; function wc_click(e){ var evt=e||window.event; var evtSrc=evt.target||evt.srcElement; location.href="word.jsp?w="+evtSrc.textContent; } var fill = d3.scale.category20(); var rate1 = new Array(); //先声明一维 for(var k=0;k<2;k++){ //一维长度为i,i为变量,可以根据实际情况改变 rate1[k]=new Array(); //声明二维,每一个一维数组里面的一个元素都是一个数组; for(var j=0;j<30;j++){ //一维数组里面每个元素数组可以包含的数量p,p也是一个变量; rate1[k][j]=""; //这里将变量初始化,我这边统一初始化为空,后面在用所需的值覆盖里面的值 } } var words=new Array(30); <% for (int i=0;i<100;i++) { %> words[<%=i%>]={name:"<%=label1[i]%>",size:"<%=rate1[i]*1200%>"}; <% } %> d3.layout.cloud() .size([width, height]) .words(words) .padding(-4) .rotate(function() { return ~~(Math.random() * 2) * 90; }) .font("Impact") .fontSize(function(d) { return d.size; }) .on("end", draw) .start(); function draw(words) { d3.select("#wc").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate("+(width/2)+","+(height/2)+")") .selectAll("text") .data(words) .enter().append("text") .style("font-size", function(d) { return d.size + "px"; }) .style("font-family", "Impact") .style("fill", function(d, i) { return fill(i); }) .style("cursor", "pointer") .style("font-weight","bold") .style("font-family","方正小标宋简体") .attr("text-anchor", "middle") .attr("class", "wc") .attr("transform", function(d) { return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; }) .text(function(d) { return d.name; }); var ele=document.getElementsByClassName("wc"); for(var e in ele){ ele[e].onclick=wc_click; } } </script> </body>

结果展示:
数据可视化——词云制作_第1张图片

你可能感兴趣的:(可视化,3d)