gpt4 book ai didi

javascript - 使用mysql数据库在d3中绘制条形图

转载 作者:行者123 更新时间:2023-11-29 12:14:14 27 4
gpt4 key购买 nike

我通过从 mysql 数据库中获取两列并将其转换为 json 对象来创建一个表,它工作正常,但我没有了解如何使用这两列绘制条形图。如果有人可以指导我,这将非常有帮助..这是我的代码:

<%@ page language="java" import="java.sql.*"%>
<%@ page language="java" import="org.json.JSONObject"%>
<html>
<head><title>Read from mySQL Database</title>
<!DOCTYPE html>
<script src="http://d3js.org/d3.v3.min.js"></script>
<meta charset="utf-8">

</head>

<body>

<div align="center" width="85%">
<center>
<table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="658" height="63">
<tbody>
<%
String DRIVER = "org.gjt.mm.mysql.Driver";
Class.forName(DRIVER).newInstance();


Connection con=null;
ResultSet rst=null;
Statement stmt=null;

try{
String url="jdbc:mysql://localhost/sales?user=root&password=root";

int i=1;
con=DriverManager.getConnection(url);
stmt=con.createStatement();
rst=stmt.executeQuery("select * from product limit 10");
JSONObject jsonObject = new JSONObject();

while(rst.next()){
jsonObject.put("sku"+i,rst.getString(2));

%>
<tr>

<td bgColor="#ffff98" vAlign="top" width="107" height="19"><%=rst.getString(2)%></td>
<td bgColor="#ffff98" vAlign="top" width="270" height="19"><%=rst.getString(4)%></td>

</tr>
<%


i++;
}
String outputString = jsonObject.toString();
rst.close();
stmt.close();
con.close();
}catch(Exception e){
System.out.println(e.getMessage());
}

%>

</tbody>
</table>
</center>
</div>
</body>
</html>

最佳答案

从代码的外观来看,您正在创建一个 JavaScript 对象,例如:

jsonObject = {"sku1": "a string", "sku2": "another string"...

d3.js 虽然确实喜欢使用 JavaScript 对象数组:

jsonObject = [
{
sku: "a string",
otherColumn: 3
},
{
sku: "another string",
otherColumn: 4
}
...

其中“sku”是列名称,“otherColumn”是值。

因此,以这种形式获取数据应该很简单:

JSONArray jsonArray = new JSONArray();

while(rst.next()){
JSONObject jsonObject = new JSONObject();
jsonObject.put("sku", rst.getString(2));
jsonObject.put("otherColumn", rst.getInt(4)); // assuming these are numbers NOT STRINGS or the bar chart would have nothing to draw

jsonArray.put(jsonObject);
}

// rest of java code here, close out connections, etc...

%>

<script>
// switch over to JavaScript and d3
// get java variable into JavaScript
var javaScriptData = "<%out.print(jsonArray.toString());%>";

// bar chart code goes here
....

要创建条形图,请查看 this sample code :

var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10, "%");

var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

x.domain(javaScriptData.map(function(d) { return d.sku; }));
y.domain([0, d3.max(javaScriptData, function(d) { return d.otherColumn; })]);

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);

svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("otherColumn");

svg.selectAll(".bar")
.data(javaScriptData)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.sku); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.otherColumn); })
.attr("height", function(d) { return height - y(d.otherColumn); });

关于javascript - 使用mysql数据库在d3中绘制条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118779/

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