五分钟上手:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>五分钟上手之折线图</title> <!-- 引入 echarts.js --> <script src="js/echarts.min.js"></script> <script src="js/jquery-1.11.3.js"></script> </head> <body> <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="width: 600px;height:400px;"></div> <script type="text/javascript"> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); // 指定图表的配置项和数据 myChart.setOption({ title: { text: '近七日收益' }, tooltip: { trigger: 'axis' }, legend: { data:['近七日收益'] }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, toolbox: { feature: { saveAsImage: {} } }, xAxis: { type: 'category', boundaryGap: false, data: ["1","2","3","4","5"] }, yAxis: { type: 'value' }, series: [ { name:'近七日收益', type:'line', stack: '总量', data:["1","2","3","4","5"] } ] }); // 异步加载数据 /* $.get('data.json').done(function (data) { // 填入数据 myChart.setOption({ xAxis: { data: data.categories }, series: [{ // 根据名字对应到相应的系列 name: '销量', data: data.data }] });});*/ </script> </body> </html>
和json数据结合:
如图所示:
实现代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/jquery-1.11.3.js"></script> <!-- 引入 echarts.js --> <script src="js/echarts.min.js"></script> </head> <body> <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="width: 900px;height:400px;"></div> </body> <!--月收益--> <script type="text/javascript"> function bb() { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); $.get('data1.json').done(function(data) { myChart.setOption({ title: { text: '月收益' }, tooltip: {}, // legend: { // data:['收益'] // }, xAxis: { data: data.categories }, yAxis: {}, series: [{ name: '收益', type: 'line', data: data.data }] }); }); } bb(); </script> </html>
数据:data1.json文件
{ "categories": [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" ], "data": [ 820, 932, 901, 9134, 1290, 330, 120 ] }
ajax数据交互:
// 折线图 var xChart = echarts.init(document.getElementById("main")); function xFun(x_data, y_data) { xChart.setOption({ color : [ '#3398DB' ], tooltip : { trigger : 'axis', axisPointer : { // 坐标轴指示器,坐标轴触发有效 type : 'shadow' // 默认为直线,可选为:'line' | 'shadow' } }, legend : { data : [ '心率值' ] }, grid : { left : '3%', right : '20%', bottom : '20%', containLabel : true }, xAxis : [ { type : 'category', data : x_data, } ], yAxis: [ { //纵轴标尺固定 type: 'value', scale: true, name: '心率值', max: 140, min: 0, splitNumber:20, boundaryGap: [0.2, 0.2] } ], series : [ { name : '心率', type : 'line', data : y_data } ] }, true); }