版权声明:请点击关注 https://blog.csdn.net/weixin_42914677/article/details/89158319
首先,编写代码之前,先下载好jquery和echarts包,及你要显示的json数据
jquery下载地址:https://jquery.com/
echarts下载地址:https://echarts.baidu.com/dist/echarts.min.js
这是我的json数据(给大家参考)
data03.json
{"data":[
{"num":385,"bookType":"马列主义"},{"num":4486,"bookType":"哲学、宗教"},{"num":1355,"bookType":"社会科学"},{"num":1760,"bookType":"政治、法律"},{"num":93,"bookType":"军事"},{"num":2253,"bookType":"经济"},
{"num":6557,"bookType":"文化"},{"num":6286,"bookType":"语言"},
{"num":31826,"bookType":"文学"},{"num":5261,"bookType":"艺术"},
{"num":5780,"bookType":"历史、地理"},{"num":88,"bookType":"自然科学"},{"num":1556,"bookType":"其他"},{"num":258,"bookType":"天文学"},
{"num":156,"bookType":"生物科学"},{"num":524,"bookType":"医药、卫生"},{"num":77,"bookType":"农业科学"},{"num":2661,"bookType":"工业技术"},
{"num":11,"bookType":"交通运输"},{"num":9,"bookType":"航天"},
{"num":89,"bookType":"环境科学"},{"num":199,"bookType":"综合性图书"}]}
第一步:新建一个文件夹(为了管理好自己编写的代码)echarts-03
第二步:将需要的文件复制到文件夹中
第三步:新建一个html页面(echarts03.html),页面内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>echarts-03</title>
</head>
<body>
<body>
<div id="main" style="height:600px;"></div>
<!-- 引入 echarts.js -->
<script src="jquery-3.3.1.min.js"></script>
<script src="echarts.min.js"></script>
<script src="echarts03.js"></script>
</body>
</html>
第四步:新建js文件echarts03.js,为了把HTML页面的js分离出来,内容如下
jQuery(document).ready(function($) {
var myChart = echarts.init(document.getElementById('main'));
var option = {
title : {//标题
text: '凯里学院成绩排名前20%学生借阅图书类型信息统计',
subtext: '2019年'
},
tooltip : {
trigger: 'axis'
},
toolbox: {//工具框
show : true,
feature : {
mark : {show: true},
dataView : {show: true, readOnly: false},
magicType : {show: true, type: ['line', 'bar']},
restore : {show: true},
saveAsImage : {show: true}
}
},
calculable : true,
xAxis : [//x轴
{
type : 'category',
data : []
}
],
yAxis : [//y轴
{
type : 'value'
}
],
series : [
{
name:'数量',
type:'bar',
data:[],//对应下面男数据
itemStyle: {
normal: {
label : {
show: true, position: 'top'
}
}
}
}
]
};
//设置图表
myChart.setOption(option);
//获取和处理数据
$.getJSON("data03.json",function(json){
var d=json.data;
var numlist=[];
var bookTypelist=[];//名称
// 循环获取名称
for(var i=0;i<d.length;i++){
numlist.push(d[i].num);
bookTypelist.push(d[i].bookType);
}
// 将数据添加到数据图表中
myChart.setOption({
xAxis: {
//显示名称
data: bookTypelist
},
series: [{
// 根据名字对应到相应的系列
name: '数量',
data: numlist
}
]
});
});
//myChart.hideLoading();
});
第五步:设置浏览器(参照https://blog.csdn.net/weixin_42914677/article/details/89077653),并打开,然后将echarts03.html拖到浏览器来打开。显示效果如下: