gpt4 book ai didi

javascript - JSP x 谷歌图表 X MySQL : Multiple Charts Not Displaying Entire Information

转载 作者:行者123 更新时间:2023-11-30 22:17:17 26 4
gpt4 key购买 nike

早上好。我正在尝试用 Java 创建一个动态 Web 项目,该项目连接到 MySQL 数据库以检索我需要的某些信息并使用 Google Charts 在图表中显示它。在这种情况下,我试图从表格帖子中获取两个计数值:按性别(男性或女性)分类的帖子数和按位置分类(东部或西部)的帖子数。因为我对这个 API 比较陌生,所以我通过显示一个饼图来练习,其中包含按性别分类的帖子数并且它有效。它显示帖子的数量,无论性别是男性还是女性。但是,当我尝试显示两个图表时(一个饼图按性别显示帖子数量,一个柱形图按位置显示帖子数量),它们会加载但不会显示全部信息。 Instad 获取两种性别的帖子数量和两个位置的帖子数量,我只获得一种性别(“男性”)的帖子数量和一个位置(“西方”)的帖子数量。

这是一张图片,说明了我试图获得的东西以及我得到的东西:https://s31.postimg.org/40c3skgmz/output.jpg

我在 JSP 代码中添加了一些打印行,以确保它检索到我需要的所有信息并且没有问题。但是,当我在 HTML 中传递数据时情况并非如此。

这是用于检索数据的 JSP 代码(使用 JSON 库):

<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@page import="org.json.JSONObject" %>

<%
Connection con= null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sentimentposts?autoReconnect=true&useSSL=false","root","root");

ResultSet rs1 = null;
ResultSet rs2 = null;

List opdetails = new LinkedList();
JSONObject responseObj = new JSONObject();

String query1 = "select poster_gender, count(*) as gender_count from post group by poster_gender";
PreparedStatement pstm1= con.prepareStatement(query1);

String query2 = "select poster_location, count(*) as location_count from post group by poster_location";
PreparedStatement pstm2= con.prepareStatement(query2);

rs1 = pstm1.executeQuery();
rs2 = pstm2.executeQuery();

JSONObject opObj = new JSONObject();

while (rs1.next()) {
String gender = rs1.getString("poster_gender");
System.out.println(gender);
int count = rs1.getInt("gender_count");
System.out.println(count);
opObj.put("gender", gender);
opObj.put("count", count);
}

while(rs2.next()){
String location = rs2.getString("poster_location");
System.out.println(location);
int count2 = rs2.getInt("location_count");
System.out.println(count2);
opObj.put("location", location);
opObj.put("count2", count2);
}
opdetails.add(opObj);
responseObj.put("opdetails", opdetails);
out.print(responseObj.toString());
}
catch(Exception e){
e.printStackTrace();
}finally{
if(con!= null){
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
%>

显示页面的HTML代码:

    <!DOCTYPE html>
<html>
<head>
<title>Opinion Chart Multi</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>

<script type="text/javascript">
google.load("visualization", "1.0", {packages:["corechart", "bar"], callback:drawCharts});
//google.setOnLoadCallback(drawCharts);

var queryObject="";
var queryObjectLen="";
$.ajax({
url : 'getdata.jsp',
dataType:'json',
success : function(data) {
queryObject = eval('(' + JSON.stringify(data) + ')');
queryObjectLen = queryObject.opdetails.length;
},
error : function(xhr, type) {
alert('server error occoured')
}
});

function drawCharts() {

var data = new google.visualization.DataTable();
data.addColumn('string', 'gender');
data.addColumn('number', 'gender_count');
for(var i=0;i<queryObjectLen;i++)
{
var gender = queryObject.opdetails[i].gender;
var count = queryObject.opdetails[i].count;
data.addRows([
[gender,parseInt(count)]
]);
}

var data2 = new google.visualization.DataTable();
data2.addColumn('string', 'location');
data2.addColumn('number', 'location_count');
for(var i=0;i<queryObjectLen;i++)
{
var location = queryObject.opdetails[i].location;
var count2 = queryObject.opdetails[i].count2;
data2.addRows([
[location,parseInt(count2)]
]);
}

var options = {
title: 'Posts By Gender',
};

var options2 = {
title: 'Posts by Location',
colors: ['green','yellow'],
hAxis: {
title: 'Location'
},
vAxis: {
title: 'No. of Posts'
}
};

var chart1 = new google.visualization.PieChart(document.getElementById('chart1'));
chart1.draw(data,options);

var chart2 = new google.visualization.ColumnChart(document.getElementById('chart2'));
chart2.draw(data2,options2);
}
</script>
</head>
<body>

<table class="columns">
<tr>
<td><div id="chart1"></div></td>
<td><div id="chart2"></div></td>
</tr>
</table>

</body>
</html>

最后,我使用的 SQL 表的格式:

表格:帖子

列:

消息 varchar(45),poster_age int(11),poster_gender varchar(45),poster_location varchar(45)

最佳答案

最有可能的是,drawCharts 在数据准备好之前被调用...

也许试试这样的……

google.load("visualization", "1.0", {
callback: function () {
var queryObject="";
var queryObjectLen="";
$.ajax({
url: 'getdata.jsp',
dataType: 'json',
success: function (data) {
queryObject = eval('(' + JSON.stringify(data) + ')');
queryObjectLen = queryObject.opdetails.length;

var data = new google.visualization.DataTable();
data.addColumn('string', 'gender');
data.addColumn('number', 'gender_count');
for(var i=0;i<queryObjectLen;i++) {
var gender = queryObject.opdetails[i].gender;
var count = queryObject.opdetails[i].count;
data.addRows([
[gender,parseInt(count)]
]);
}

var data2 = new google.visualization.DataTable();
data2.addColumn('string', 'location');
data2.addColumn('number', 'location_count');
for(var i=0;i<queryObjectLen;i++) {
var location = queryObject.opdetails[i].location;
var count2 = queryObject.opdetails[i].count2;
data2.addRows([
[location,parseInt(count2)]
]);
}

var options = {
title: 'Posts By Gender',
};

var options2 = {
title: 'Posts by Location',
colors: ['green','yellow'],
hAxis: {
title: 'Location'
},
vAxis: {
title: 'No. of Posts'
}
};

var chart1 = new google.visualization.PieChart(document.getElementById('chart1'));
chart1.draw(data,options);

var chart2 = new google.visualization.ColumnChart(document.getElementById('chart2'));
chart2.draw(data2,options2);
},
error: function (xhr, type) {
alert('server error occoured')
}
});
},
packages:["corechart"]
});

关于javascript - JSP x 谷歌图表 X MySQL : Multiple Charts Not Displaying Entire Information,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37855005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com