JavaScript连接MySql数据库中的六级单词数据并进行可视化
注意:要事先安装tomcat以及相应版本的mysql-connector-java的jar包,将jar移动到lib文件中
数据库数据如下
可视化代码:
<!DOCTYPE html>
<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("<center><h1><font color=blue>Matrix Word Cloud English Learning</h1></center>");
String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPasswd="123456";
String dbName="flask";
String tableName="map_enword";
int count[]=new int[26];
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName+"?serverTimezone=GMT%2B8","root","你的密码");
Statement statement = connection.createStatement();
for(int i=0;i<26;i++){
int aa=Integer.valueOf('a')+i;
char cha = (char) aa;
String sql="SELECT * FROM "+tableName+" where english like '"+cha+"%' "+"order by english";
ResultSet rs = statement.executeQuery(sql);
String str;
int j=0;
while(rs.next()) {
str=(rs.getString(2)).substring(0,1);
//out.print(str+" ");
j++;
}
count[i]=j;
//out.print(" "+count[i]+" <br>");
rs.close();
}
statement.close();
connection.close();
%>
<script src="d3.v3.min.js"></script>
<script>
var w=window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
w=w*0.98;
h=h*2.3;
var color=d3.scale.category10();
var dataset=new Array(26);
var name=new Array(26);
var name1=new Array(26);
<% for (int i=0;i<26;i++){ %>
dataset[<%=i%>]="<%=count[i]%>";
<% } %>
name1=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var name=name1.join('');
var ww=w/dataset.length;
var svg=d3.select("body")
.append("svg")
.attr("width",w)
.attr("height",h);
var rect=svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x",function(d,i){return i*w/dataset.length})
.attr("y",function(d){return h-d*0.6-90})
.attr("width",ww*0.95)
.attr("height",function(d){return d*0.6})
.attr("fill",function(d,i){return color(i)});
var value=svg.selectAll("text.value")
.data(dataset)
.enter()
.append("text")
.attr("x",function(d,i){return i*w/dataset.length})
.attr("y",function(d){return h-d*0.6-90})
.text(function(d){return d})
.attr("dx",ww/2)
.attr("dy","-1em")
.attr("text-anchor","middle")
.attr("fill",function(d,i){return color(i)});
var title=svg.selectAll("text.title")
.data(name)
.enter()
.append("text")
.attr("x",function(d,i){return i*w/name.length})
.attr("y",function(d){return h-60})
.text(function(d){return d})
.attr("dx",ww/2)
.attr("dy","-lem")
.attr("text-anchor","middle")
.attr("fill",function(d,i){return color(i)});
//饼图
/* var pie=d3.layout.pie()
.value(function(d){return d;});
var piedata=pie(dataset);
var arc=d3.svg.arc()
.innerRadius(100)
.outerRadius(300)
svg.selectAll("path")
.data(piedata)
.enter()
.append("path")
.attr("d",function(d){
return arc(d)
})
.attr("fill",function(d,i){return color(i);})
.attr("transform","translate("+w/2+","+(h/2-400)+")");
*/
var dataset2 =new Array();
for(var i=0;i<26;i++){
dataset2[i]=new Array();
for(var j=0;j<2;j++){
dataset2[i][j]=1;
}
}
var datas=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
dataset2[0][0]='a'; dataset2[1][0]='b'; dataset2[2][0]='c'; dataset2[3][0]='d'; dataset2[4][0]='e'; dataset2[5][0]='f';
dataset2[6][0]='g'; dataset2[7][0]='h'; dataset2[8][0]='i'; dataset2[9][0]='j'; dataset2[10][0]='k'; dataset2[11][0]='l';
dataset2[12][0]='m'; dataset2[13][0]='n'; dataset2[14][0]='o'; dataset2[15][0]='p'; dataset2[16][0]='q'; dataset2[17][0]='r';
dataset2[18][0]='s'; dataset2[19][0]='t'; dataset2[20][0]='u'; dataset2[21][0]='v'; dataset2[22][0]='w'; dataset2[23][0]='x';
dataset2[24][0]='y'; dataset2[25][0]='z';
<%for(int i=0;i <count.length;i++){%>dataset2[<%=i%>][1]=" <%=count[i]%> ";<%}%>
var pie = d3.layout.pie()
.value(function (d) { return d[1]; });
innerRadius=30
var arcPath=d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(function (d) {
var value=d.value;
return value*0.6+ innerRadius;});
var piedata = pie(dataset2);
var arcs=svg.selectAll("path")
.data(piedata).enter()
.append("path")
.attr("d", function (d) { return arcPath(d); })
.attr("fill", function(d,i){return color(i);})
.attr("transform", "translate("+ w / 2 + "," + h / 3 +")");
arcs.append("path")
.attr("fill",function(d,i){
return color(i);})
.attr("d",arcPath)
var rect=svg.selectAll("rect")
.data(dataset2)
.enter()
.append("rect")
.attr("x",function(d,i){return i*w/dataset2.length})
.attr("y",function(d){return h-d[1]-30})
.attr("width",ww*0.95)
.attr("height",function(d){return d[1]})
.attr("fill",function(d,i){return color(i)});
bar1 = svg.append("g").attr("class", "bar1");
bar1.selectAll("text")
.data(piedata)
.enter()
.append("text")
.attr("fill","white")
.attr("transform",function(d){
var x=arcPath.centroid(d)[0];
var y=arcPath.centroid(d)[1];
return "translate("+(w/2+x)+","+(y+h/3)+")";})
.attr("text-anchor","middle")
.attr("font-size",function(d){
return d.data[1]*0.05+"px";})
.text(function(d){return d.data[0]});
</script>
</body>
可视化结果如下:
JavaScript连接MySql数据库中的疫苗接种情况数据并进行可视化
疫苗接种百分比(前二十)数据
每一百人疫苗接种次数数据
疫苗接种百分比(前二十)和每一百人疫苗接种次数的数据
可视化代码:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</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("<center><h1><font color=black>疫苗接种人数</h1></center>");
String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPasswd="54.wfy";
String dbName="yimiao";
String tableName1="renshu";
String tableName2="baifenbi";
String tableName3="douyou";
String country1[]=new String[25];
double data1[]=new double[25];
String country2[]=new String[25];
double data2[]=new double[25];
String country3[]=new String[25];
double data31[]=new double[25];
double data32[]=new double[25];
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 < 21;i++){
String sql="SELECT * FROM "+tableName1+" where id='"+i+"'";
String str=" ";
double per=1;
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
str=rs.getString(2);
per=rs.getDouble(3);
}
country1[i-1]=str;
data1[i-1]=per;
//out.print(country[i-1]+" ");
//out.print(data[i-1]);
rs.close();
}
for(int i=1;i < 21;i++){
String sql="SELECT * FROM "+tableName2+" where id='"+i+"'";
String str=" ";
double per=1;
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
str=rs.getString(2);
per=rs.getDouble(3);
}
country2[i-1]=str;
data2[i-1]=per;
//out.print(country[i-1]+" ");
//out.print(data[i-1]);
rs.close();
}
for(int i=1;i < 17;i++){
String sql="SELECT * FROM "+tableName3+" where id='"+i+"'";
String str=" ";
double per1=1;
double per2=1;
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
str=rs.getString(2);
per1=rs.getDouble(3);
per2=rs.getDouble(4);
}
country3[i-1]=str;
data31[i-1]=per1;
data32[i-1]=per2;
//out.print(country[i-1]+" ");
//out.print(data[i-1]);
rs.close();
}
statement.close();
connection.close();
%>
<div style="position: absolute; bottom: 2150px; right: 365px;"><font size="8"><strong>COVID-19疫苗接种人数(单位:百万)</strong></font></div>
<div style="position: absolute; bottom: 1400px; right: 340px;"><font size="8"><strong>COVID-19疫苗接种人次(单位:次/百人)</strong></font></div>
<div style="position: absolute; bottom: 680px; right: 340px;"><font size="8"><strong>COVID-19疫苗接种人数与人次对比</strong></font></div>
<div style="position: absolute; bottom: 2100px; right: 365px;"><font size="2"><strong>数据来源:Our World in Data</strong></font></div>
<script src="d3.v3.min.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;
var color=d3.scale.category10();
var dataset1=new Array(20);
var country1=new Array(20);
var dataset2=new Array(20);
var country2=new Array(20);
var dataset31=new Array(16);
var dataset32=new Array(16);
var country3=new Array(16);
<% for (int i=0;i<20;i++){ %>
dataset1[<%=i%>]= "<%=data1[i]%>";
country1[<%=i%>]= "<%=country1[i]%>";
<% } %>
<% for (int i=0;i<20;i++){ %>
dataset2[<%=i%>]= "<%=data2[i]%>";
country2[<%=i%>]= "<%=country2[i]%>";
<% } %>
<% for (int i=0;i<16;i++){ %>
dataset31[<%=i%>]= "<%=data31[i]%>";
dataset32[<%=i%>]= "<%=data32[i]%>";
country3[<%=i%>]= "<%=country3[i]%>";
<% } %>
var w1=w/dataset1.length;
var svg=d3.select("body")
.append("svg")
.attr("width",w)
.attr("height",h);
var rect=svg.selectAll("rect1")
.data(dataset1)
.enter()
.append("rect")
.attr("x",function(d,i){return i*w/dataset1.length})
.attr("y",function(d){return h-2*height-d*3-80})
.attr("width",w1*0.95)
.attr("height",function(d){return d*3})
.attr("fill",function(d,i){return color(i)});
var value=svg.selectAll("text.value1")
.data(dataset1)
.enter()
.append("text")
.attr("x",function(d,i){return i*w/dataset1.length})
.attr("y",function(d){return h-2*height-d*3-80})
.text(function(d){return d})
.attr("dx",w1/2)
.attr("dy","-1em")
.attr("text-anchor","middle")
.attr("fill",function(d,i){return color(i)});
svg.selectAll("text.title1")
.data(country1)
.enter()
.append("text")
.attr("font-size","13px")
.attr("x",function(d,i){
return (w/dataset1.length)*i;
})
.attr("y",h-2*height-70)
.attr("dx",(w/dataset1.length)/2-35)
.attr("dy","13px")
.text(function(d){
return d;
})
.attr("fill",function(d,i){return color(i)});
var w2=w/dataset2.length;
var rect=svg.selectAll("rect2")
.data(dataset2)
.enter()
.append("rect")
.attr("x",function(d,i){return i*w/dataset2.length})
.attr("y",function(d){return h-height-d*3-80})
.attr("width",w2*0.95)
.attr("height",function(d){return d*3})
.attr("fill",function(d,i){return color(i)});
var value=svg.selectAll("text.value2")
.data(dataset2)
.enter()
.append("text")
.attr("x",function(d,i){return i*w/dataset2.length})
.attr("y",function(d){return h-height-d*3-80})
.text(function(d){return d})
.attr("dx",w2/2)
.attr("dy","-1em")
.attr("text-anchor","middle")
.attr("fill",function(d,i){return color(i)});
svg.selectAll("text.title2")
.data(country2)
.enter()
.append("text")
.attr("font-size","13px")
.attr("x",function(d,i){
return (w/dataset2.length)*i;
})
.attr("y",h-height-70)
.attr("dx",(w/dataset2.length)/2-35)
.attr("dy","13px")
.text(function(d){
return d;
})
.attr("fill",function(d,i){return color(i)});
var w3=w/dataset31.length;
var rect=svg.selectAll("rect31")
.data(dataset31)
.enter()
.append("rect")
.attr("x",function(d,i){return i*w/dataset31.length+4;})
.attr("y",function(d){return h-d*3-80})
.attr("width",w3*0.4)
.attr("height",function(d){return d*3})
.attr("fill",function(d,i){return color(i)});
var value=svg.selectAll("text.value31")
.data(dataset31)
.enter()
.append("text")
.attr("x",function(d,i){return i*w/dataset31.length-w3/2+20;})
.attr("y",function(d){return h-d*3-80})
.text(function(d){return d})
.attr("dx",w3/2)
.attr("dy","-1em")
.attr("text-anchor","middle")
.attr("fill",function(d,i){return color(i)});
var rect=svg.selectAll("rect32")
.data(dataset32)
.enter()
.append("rect")
.attr("x",function(d,i){return i*w/dataset32.length+w3/2-4;})
.attr("y",function(d){return h-d*3-80})
.attr("width",w3*0.4)
.attr("height",function(d){return d*3})
.attr("fill",function(d,i){return color(i)});
var value=svg.selectAll("text.value32")
.data(dataset32)
.enter()
.append("text")
.attr("x",function(d,i){return i*w/dataset32.length+w3/2-30;})
.attr("y",function(d){return h-d*3-80})
.text(function(d){return d})
.attr("dx",w3/2)
.attr("dy","-1em")
.attr("text-anchor","middle")
.attr("fill",function(d,i){return color(i)});
svg.selectAll("text.title3")
.data(country3)
.enter()
.append("text")
.attr("font-size","13px")
.attr("x",function(d,i){
return (w/dataset31.length)*i;
})
.attr("y",h-70)
.attr("dx",(w/dataset31.length)/2-25)
.attr("dy","13px")
.text(function(d){
return d;
})
.attr("fill",function(d,i){return color(i)});
</script>
</body>
可视化结果: