1.echarts学习前言

最近接触到echarts,发现数据可视化真的是魅力无穷啊,各种变幻的曲线交错,以及‘曼妙’的动画效果真是让人如痴如醉!

下面就来一起欣赏她的美...

“ ECharts是中国的,也是世界的。”

      —— 浙江大学教授 · 陈为

“ ECharts,发现数据可视化之美!”

     ——中国传媒大学教授 · 沈浩

大数据时代
              重新定义数据图表的时候到了......

2.echarts的demo代码

需求:ajax请求到json格式数据,动态添加到曲线和表格中

jquery:1.8.3版本

bootstrap:2.3.2版本

用代码说话是我们的游戏规则(吼吼):

echarts/qiyue.html 中 qiyue.html文件:

  1. <html>
  2. <head>
  3. <title>*echarts*</title>
  4. <meta charset="utf-8"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
  6. <script src="js/jquery-1.8.3.min.js"></script>
  7. <script src="js/bootstrap.min.js"></script>
  8. <script src="js/echarts.js"></script>
  9. <style type="text/css">
  10. *{
  11. font-family: "微软雅黑";
  12. }
  13. .count-title{
  14. padding-bottom: 15px;
  15. margin-bottom: 20px;
  16. border-bottom: 1px solid #ccc;
  17. }
  18. #main{
  19. width: 100%;
  20. height: 500px;
  21. margin: 0 auto;
  22. }
  23. .total-incre{
  24. padding-left: 147px;
  25. margin-top: 5px;
  26. position: absolute;
  27.  
  28. }
  29. .total-incre strong{
  30. color: #e5903a;
  31. }
  32. .chooseTime{
  33. position: absolute;
  34. right: 100px;
  35. top:18px;
  36. }
  37. .table td,.table th{
  38. text-align: center;
  39. }
  40. .agencyNew{
  41. width: 90%;
  42. }
  43. </style>
  44.  
  45. </head>
  46. <body>
  47. <div class="container agencyNew">
  48. <div class="row">
  49. <!-- Split button -->
  50.  
  51. <h1 class="count-title">XX销售情况</h1>
  52.  
  53. <p class="total-incre">
  54. 合计:<strong class="total">888</strong> | 新增 :<strong class="increase">88</strong>
  55. </p>
  56. <!--下拉框-->
  57. <div class="dropdown chooseTime">
  58. <a class="btn btn-default dropdown-toggle" role="button" data-toggle="dropdown" data-target="#" >
  59. 最近一个月
  60. <b class="caret"></b>
  61. </a>
  62. <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
  63. <li><a href="#">最近一个月</a></li>
  64. <li role="separator" class="divider"></li>
  65. <li><a href="#">2017年2月</a></li>
  66. <li><a href="#">2017年1月</a></li>
  67. </ul>
  68. </div>
  69. <!--echarts曲线容器-->
  70. <div id="main">
  71.  
  72. </div>
  73. <!--表格-->
  74. <table class="table table-bordered table-striped table-hover">
  75. <thead>
  76. <!--动态获取表头-->
  77.  
  78. </thead>
  79. <tbody>
  80. <!--动态获取表格-->
  81. </tbody>
  82. </table>
  83. </div>
  84. </div>
  85. </body>
  86. <script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
  87. <script src="js/qiyue.js"></script>
  88.  
  89. </html>

echarts/js/qiyue.js 中 qiyue.js文件:

  1. var myChart,option;
  2. $(function(){
  3. require.config({
  4. paths: {
  5. 'echarts': 'http://echarts.baidu.com/build/dist'
  6. }
  7. });
  8.  
  9. require(
  10. [
  11. 'echarts',
  12. 'echarts/chart/line', // 按需加载所需图表,如需动态类型切换功能,别忘了同时加载相应图表
  13. 'echarts/chart/bar'
  14. ],
  15. function (ec) {
  16. myChart = ec.init(document.getElementById('main'));
  17. option = {
  18. title: {
  19. text: 'XX销售情况',
  20. subtext: ''
  21. },
  22. tooltip: {
  23. trigger: 'axis'
  24. },
  25. legend: {
  26. data: []
  27. },
  28. toolbox: {
  29. show : true,
  30. feature : {
  31. mark : {show: true},
  32. dataView : {show: true, readOnly: false},
  33. magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
  34. restore : {show: true},
  35. saveAsImage : {show: true}
  36. }
  37. },
  38.  
  39. xAxis: [
  40. {
  41. type : 'category',
  42. boundaryGap : false,
  43. data : [ ]
  44. ,
  45. axisLabel: {
  46. interval:0,//横轴信息全部显示
  47. rotate: 30,//60度角倾斜显示
  48. formatter:function(val){
  49. // return val.split("").join("\n"); //横轴信息文字竖直显示
  50. return val;
  51. },
  52. textStyle: {
  53. color: '#000',
  54. align: 'center',
  55. fontWeight: 'bold'
  56. }
  57.  
  58. }
  59. }
  60. ],
  61. yAxis: [],
  62. series: []
  63. };
  64. // myChart = require('echarts').init(document.getElementById('main'));
  65. myChart.showLoading({
  66. // text : '数据获取中',
  67. effect: 'whirling'
  68. });
  69. getData();
  70.  
  71. });
  72. });
  73.  
  74. //请求json
  75. var fields,
  76. itemsMap,
  77. seriesItem,
  78. yAxis_arr = [],
  79. thead = '',
  80. tbody = '',
  81. tbody_tr = '';
  82. function getData(){
  83. $.ajax({
  84. url : 'data0.json',
  85. dataType : 'json',
  86. async : false,
  87. type : 'get',
  88. success : function(json){
  89. // console.log(json.data);
  90. console.log(option);
  91. fields = json.data.fields;
  92. itemsMap = json.data.itemsMap;
  93.  
  94. createEcharts();//动态创建曲线图
  95. createTab();//动态创建表格
  96. myChart.hideLoading();
  97. myChart.setOption(option);
  98.  
  99. },
  100.  
  101. error : function(XMLHttpRequest, textStatus, errorThrown){
  102.  
  103. if(textStatus == 'parsererror'){
  104.  
  105. alert('数据为空或者SQL语句错误!');
  106. }
  107.  
  108. console.log(errorThrown);
  109. }
  110. });
  111. }
  112.  
  113. /*
  114. * 动态创建曲线图
  115. */
  116. function createEcharts(){
  117.  
  118. // series
  119. for(var i=1; i<fields.length; i++){
  120. if(i==1){
  121. itemStyle = {
  122. normal: {
  123. areaStyle: {
  124. type: 'default'
  125. }
  126. }
  127. };
  128. }else{
  129. itemStyle = {
  130. normal: {
  131. color: '#70bf41'
  132.  
  133. }
  134. };
  135. }
  136. option.legend.data.push(fields[i]); // legend
  137. seriesItem = {};
  138. seriesItem.name = fields[i];
  139. seriesItem.type = 'line';
  140. seriesItem.smooth = false;
  141. seriesItem.yAxisIndex= i-1;
  142. seriesItem.itemStyle = itemStyle;
  143.  
  144. seriesItem.data = [];
  145.  
  146. for(var key in itemsMap){
  147. seriesItem.data.push(itemsMap[key][i]);
  148. }
  149.  
  150. // 填充默认显示曲线的数据
  151. option.series.push(seriesItem);
  152. // option.series[0].type = 'line';
  153. // option.series[1].type = 'bar';
  154. // yAxis
  155. var yAxis_obj = {};
  156. yAxis_obj.type = 'value';
  157. yAxis_obj.name = fields[i];
  158. yAxis_obj.show = true;
  159. yAxis_arr.push(yAxis_obj);
  160.  
  161. }
  162. option.yAxis = yAxis_arr;
  163. console.log(yAxis_arr);
  164.  
  165. }
  166. /*
  167. * 动态创建表格
  168. */
  169. function createTab(){
  170. //动态创建表头
  171. for(var i=0; i<fields.length; i++){
  172.  
  173. thead += '<th>'+fields[i]+'</th>';
  174. $('.table thead').html('<tr>'+thead+'</tr>');
  175.  
  176. }
  177.  
  178. for(var j in itemsMap){
  179. var tbody_td = '';
  180. option.xAxis[0].data.push(itemsMap[j][0]); // XAxis
  181. for(var k=0; k<itemsMap[j].length; k++){
  182.  
  183. tbody_td += '<td>'+itemsMap[j][k]+'</td>';
  184.  
  185. }
  186. // console.log(tbody_td);
  187. tbody_tr += '<tr>'+tbody_td+'</tr>';
  188. }
  189. $('.table tbody').html(tbody_tr);
  190.  
  191. }

echarts/data0.json 中data0.json文件:

  1. {
  2. "data": {
  3. "itemsMap": {
  4. "1": ["2017-03-1", "3", "8"],
  5. "2": ["2017-03-2", "18", "20"],
  6. "3": ["2017-03-3", "43", "54"],
  7. "4": ["2017-03-4", "50", "74"],
  8. "5": ["2017-03-5", "39", "41"],
  9. "6": ["2017-03-6", "20", "52"],
  10. "7": ["2017-03-7", "21", "25"],
  11. "8": ["2017-03-8", "16", "26"],
  12. "9": ["2017-03-9", "10", "11"],
  13. "10": ["2017-03-10", "8", "10"]
  14.  
  15. },
  16. "fields": ["日期", "预购", "成交"],
  17. "status": 1
  18. }
  19. }

3.运行结果展示

由于鹅的博客还没有直接运行功能,为了浏览方便我就把效果图贴上吧(= =)

tips:所有数据纯属虚构哦。

源代码在github上提供https://github.com/yingliyu/commonDemo.git

echarts系列之动态加载数据的更多相关文章

  1. echarts官网上的动态加载数据bug被我解决。咳咳/。

    又是昨天,为什么昨天发生了这么多事.没办法,谁让我今天没事可做呢. 昨天需求是动态加载数据,画一个实时监控的折线图.大概长这样. 我屁颠屁颠的把代码copy过来,一运行,caocaocao~bug出现 ...

  2. Echarts使用及动态加载图表数据

    Echarts使用及动态加载图表数据 官网:http://echarts.baidu.com/ 1.文档 2.实例 名词: 1.统计维度(说明数据) 维度就是统计致力于建立一个基于多方位统计(时间.地 ...

  3. AppCan学习笔记----关闭页面listview动态加载数据

    AppCan页面关闭 AppCan 的页面是由两个HTML组成,如果要完全关闭的话需要在主HTML eg.index.html中关闭,关闭方法:appcan.window.close(-1); 管道 ...

  4. [JS前端开发] js/jquery控制页面动态加载数据 滑动滚动条自动加载事件

    页面滚动动态加载数据,页面下拉自动加载内容 相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多 有手机的相信都见过这样的效果:进入qq空间,向下拉动空 ...

  5. 微信小程序(五) 利用模板动态加载数据

    利用模板动态加载数据,其实是对上一节静态数据替换成动态数据:

  6. mui 动态加载数据出现的问题处理 (silder轮播组件 indexedList索引列表 下拉刷新不能继续加载数据)

    mui-slider 问题:动态给mui的图片轮播添加图片,轮播不滚动. 解决:最后把滚动轮播图片的mui(".mui-slider").slider({interval: 300 ...

  7. ASP.NET MVC动态加载数据

    ASP.NET MVC动态加载数据,一般的做法是使用$.each方法来循环产生tabel: 你可以在html时先写下非动态的部分:  Source Code 上图中,有一行代码: <tbody ...

  8. js实现滚动条来动态加载数据

    主要angular2+es6 data:Array<any> //展示的数据 allData:Array<any> //全部的数据 size:number = 10 //每次动 ...

  9. js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的

    js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的 相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多 有手机的相信都见过这样 ...

随机推荐

  1. 简易推荐引擎的python实现

    代码地址如下:http://www.demodashi.com/demo/12913.html 主要思路 使用协同过滤的思路,从当前指定的用户过去的行为和其他用户的过去行为的相似度进行相似度评分,然后 ...

  2. 解决window10系统电脑插入耳机之后没有声音的问题

    其实办法也是从百度百科上查到的 ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄     可能是因为自己某个不小心的操作更改了设置 1. 首先要点开设置按钮,在搜索栏输入控制面板 (当然知道控制面板在哪里的小伙伴就不用 ...

  3. android应用多线程守护让你非常难杀死它

    1.android 应用开启后启动一个服务 public class TestserviceActivity extends Activity { /** Called when the activi ...

  4. 【LeetCode OJ 016】3Sum Closest

    题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...

  5. oracle的一些问题

    好久时间没有用oracle,这次因为有个项目的需要,又重新温习了一下oracle. 我使用的oracle的版本是windows oracle 11g_R2. 首先先说一下安装.这个没有太大的问题,主要 ...

  6. 李洪强漫谈iOS开发[C语言-001]-开发概述

  7. iOS10 获取系统通讯录新方法

    #import <ContactsUI/ContactsUI.h> 遵循代理 CNContactPickerDelegate 调用通讯录 如果在iOS10的机器上调用以前的ABPeople ...

  8. easyUI combox静态动态联动

    easyUI重写了select,取而代之的是combobox,有如下几种方式可以创建一个combobox 1.使用select标签,并加上class="easyui-combobox&quo ...

  9. php 遍历静态html成文章列表

    准备 代码 <?php $root=__DIR__; //全站目录 function my($dir){ static $item_arr=array(); $a=scandir($dir); ...

  10. 推荐个WIN7下小巧的可转录声音的软件-Audio Record Wizard V6.99

    之前是XP上用的是 WaveCN 2.0.0.5,但这个软件好久没更新了,不支持WIN7 最终找到了Audio Record Wizard V6.99,尽管没 WaveCN 2.0.0.5好用,但也全 ...