morris.js繪製圖表
阿新 • • 發佈:2019-02-08
demo的百度雲連結: https://pan.baidu.com/s/1abCRoMXboq0EDe5697K6LQ 密碼: k25i
效果圖:
當然還有很多,不展示了.
用法說明:匯入核心的幾個包:
1.jquery.js
2.morris.js
3.raphael-2.1.0.min.js
注意:這裡要強調的是:html標籤中定義div,將id與js中繪製的圖表的element屬性的值保持一致即可,並且定義了幾個圖表,html中一定要對應幾個id與之匹配的div.否則就會報錯,影響到其他js程式碼的執行.錯誤為:
我的圖表繪製在了custom-scripts.js中.
怎麼繪製,一看程式碼便知道了.具體程式碼如下:
(function ($) { "use strict"; var mainApp = { initFunction: function () { /*MENU ------------------------------------*/ $('#main-menu').metisMenu(); $(window).bind("load resize", function () { if ($(this).width() < 768) { $('div.sidebar-collapse').addClass('collapse') } else { $('div.sidebar-collapse').removeClass('collapse') } }); /* MORRIS BAR CHART -----------------------------------------*/ Morris.Bar({ element: 'morris-bar-chart', data: [{ y: '2006', a: 100, b: 90 }, { y: '2007', a: 75, b: 65 }, { y: '2008', a: 50, b: 40 }, { y: '2009', a: 75, b: 65 }, { y: '2010', a: 50, b: 40 }, { y: '2011', a: 75, b: 65 }, { y: '2012', a: 100, b: 90 }], xkey: 'y', ykeys: ['a', 'b'], labels: ['Series A', 'Series B'], hideHover: 'auto', resize: true }); /* MORRIS DONUT CHART ----------------------------------------*/ Morris.Donut({ element: 'morris-donut-chart', data: [{ label: "西安", value: 100 }, { label: "新豐鎮", value: 200 }, { label: "安康", value: 300 },{ label: "延安", value: 400 }], resize: true }); /* MORRIS AREA CHART ----------------------------------------*/ Morris.Area({ element: 'morris-area-chart', data: [{ period: '2010 Q1', iphone: 2666, ipad: null, itouch: 2647 }, { period: '2010 Q2', iphone: 2778, ipad: 2294, itouch: 2441 }, { period: '2010 Q3', iphone: 4912, ipad: 1969, itouch: 2501 }, { period: '2010 Q4', iphone: 3767, ipad: 3597, itouch: 5689 }, { period: '2011 Q1', iphone: 6810, ipad: 1914, itouch: 2293 }, { period: '2011 Q2', iphone: 5670, ipad: 4293, itouch: 1881 }, { period: '2011 Q3', iphone: 4820, ipad: 3795, itouch: 1588 }, { period: '2011 Q4', iphone: 15073, ipad: 5967, itouch: 5175 }, { period: '2012 Q1', iphone: 10687, ipad: 4460, itouch: 2028 }, { period: '2012 Q2', iphone: 8432, ipad: 5713, itouch: 1791 }], xkey: 'period', ykeys: ['iphone', 'ipad', 'itouch'], labels: ['iPhone', 'iPad', 'iPod Touch'], pointSize: 2, hideHover: 'auto', resize: true }); /* MORRIS LINE CHART ----------------------------------------*/ Morris.Line({ element: 'morris-line-chart', data: [{ y: '2006', a: 100, b: 90 }, { y: '2007', a: 75, b: 65 }, { y: '2008', a: 50, b: 40 }, { y: '2009', a: 75, b: 65 }, { y: '2010', a: 50, b: 40 }, { y: '2011', a: 75, b: 65 }, { y: '2012', a: 100, b: 90 }], xkey: 'y', ykeys: ['a', 'b'], labels: ['Series A', 'Series B'], hideHover: 'auto', resize: true }); }, initialization: function () { mainApp.initFunction(); } } // Initializing /// $(document).ready(function () { mainApp.initFunction(); }); }(jQuery));