<%@page pageEncoding="UTF-8" import="java.sql.*"%> <!DOCTYPE html> <html style="height: 100%"> <head> <meta charset="utf-8"> <title>柱状图显示数值</title> </head> <body style="height:600px; margin: 0"> <div id="main" style="width: 2000px;height:80%;"></div> <script type="text/javascript" src="js/echarts.min.js"></script> <script> function show(title,value){ var myChart = echarts.init(document.getElementById('main')); // 指定图表的配置项和数据 var option = { // 标题 title: { text: 'ECharts 入门示例' }, // 工具箱 toolbox: { show: true, feature: { dataZoom: { yAxisIndex: 'none' }, dataView: {readOnly: false}, magicType: {type: ['line', 'bar']}, restore: {}, saveAsImage: {} } }, // 图例 legend: { data: ['销量'] }, // x轴 xAxis: { data: title }, yAxis: { type: 'value' }, // 数据 series: [{ name: '销量', type: 'bar', data: value, itemStyle: { normal: { label: { show: true, //开启显示 position: 'top', //在上方显示 textStyle: { //数值样式 color: 'black', fontSize: 16 } } } }, }] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } </script> <% Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://101.43.149.243:3306/test"; Connection con=DriverManager.getConnection(url,"guest","guest"); String sql="select * from logpvbyprovince order by num desc limit 10"; PreparedStatement pst=con.prepareCall(sql); ResultSet rs=pst.executeQuery(); %> <script type="text/javascript"> title=new Array(); value=new Array(); <% while(rs.next()){ %> title.push("<%=rs.getString(1)%>");value.push(<%=rs.getInt(2)%>); <% } rs.close(); pst.close(); con.close(); %> show(title,value); </script> </body> </html>改为用饼图展示
时间: 2024-04-29 13:19:23 浏览: 163
利用JDBC连接MS-SQL-SERVER-2008.doc
好的,我来为您改写代码,展示用饼图展示前10名省份的数据:
<%@page pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
<title>饼图显示数值</title>
</head>
<body style="height:600px; margin: 0">
<div id="main" style="width: 800px;height:80%;"></div>
<script type="text/javascript" src="js/echarts.min.js"></script>
<script>
function show(title,value){
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '前10名省份饼图',
left: 'center'
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 10,
data: title
},
series: [
{
name: '访问来源',
type: 'pie',
radius: ['50%', '70%'],
avoidLabelOverlap: false,
label: {
show: false,
position: 'center'
},
emphasis: {
label: {
show: true,
fontSize: '30',
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
data: value
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
}
</script>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://101.43.149.243:3306/test";
Connection con=DriverManager.getConnection(url,"guest","guest");
String sql="select * from logpvbyprovince order by num desc limit 10";
PreparedStatement pst=con.prepareCall(sql);
ResultSet rs=pst.executeQuery();
%>
<script type="text/javascript">
var title = new Array();
var value = new Array();
<% while(rs.next()){ %>
title.push("<%=rs.getString(1)%>");
value.push({value:<%=rs.getInt(2)%>, name:"<%=rs.getString(1)%>"});
<% } %>
rs.close();
pst.close();
con.close();
show(title, value);
</script>
</body>
</html>
希望这可以帮助您展示饼图。
阅读全文