解決百度Echarts圖表坐標(biāo)軸越界的方法
Echarts是由百度提供的數(shù)據(jù)可視化解決方案, 可以讓我們快速實(shí)現(xiàn)功能豐富的圖表,官網(wǎng)鏈接
使用方法
1.引入echarts.min.js文件
下載echarts.min.js文件,下載地址
創(chuàng)建一個(gè)掛載節(jié)點(diǎn)
將數(shù)據(jù)展示到掛載節(jié)點(diǎn)
生成數(shù)據(jù), 并將數(shù)據(jù)導(dǎo)入到option配置對(duì)象中
// 創(chuàng)建數(shù)據(jù) var base = +new Date(1968, 9, 3); var oneDay = 24 * 3600 * 1000; var date = []; var data = [Math.random() * 300]; for (var i = 1; i < 20000; i++) { var now = new Date(base += oneDay); date.push([now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/')); data.push(Math.round((Math.random() - 0.5) * 20 + data[i - 1])); } // 創(chuàng)建對(duì)象option option = { tooltip: { trigger: 'axis', position: function (pt) { return [pt[0], '10%']; } }, title: { left: 'center', text: '大數(shù)據(jù)量面積圖', }, toolbox: { feature: { dataZoom: { yAxisIndex: 'none' }, restore: {}, saveAsImage: {} } }, xAxis: { type: 'category', boundaryGap: false, data: date }, yAxis: { type: 'value', boundaryGap: [0, '100%'] }, dataZoom: [{ type: 'inside', start: 0, end: 10 }, { start: 0, end: 10, handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z', handleSize: '80%', handleStyle: { color: '#fff', shadowBlur: 3, shadowColor: 'rgba(0, 0, 0, 0.6)', shadowOffsetX: 2, shadowOffsetY: 2 } }], series: [ { name:'模擬數(shù)據(jù)', type:'line', smooth:true, symbol: 'none', sampling: 'average', itemStyle: { color: 'rgb(255, 70, 131)' }, areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgb(255, 158, 68)' }, { offset: 1, color: 'rgb(255, 70, 131)' }]) }, data: data } ] };
將option添加到掛載節(jié)點(diǎn)
// 將option數(shù)據(jù)掛載到main節(jié)點(diǎn) echarts.init(document.getElementById('main')).setOption(option);
如何防止坐標(biāo)軸越界
上方圖表如果展示到移動(dòng)版, 坐標(biāo)軸可能部分無法顯示
解決方法很簡單,只需在option中加一個(gè)配置項(xiàng)即可
grid:{ containLabel: true },
源碼:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Echarts-Demo</title> <script src="./echarts.min.js"></script> </head> <body> <div id="main" style="width:100%;height:400px;"></div> <script> // 創(chuàng)建數(shù)據(jù) var base = +new Date(1968, 9, 3); var oneDay = 24 * 3600 * 1000; var date = []; var data = [Math.random() * 300]; for (var i = 1; i < 20000; i++) { var now = new Date(base += oneDay); date.push([now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/')); data.push(Math.round((Math.random() - 0.5) * 20 + data[i - 1])); } // 創(chuàng)建對(duì)象option option = { tooltip: { trigger: 'axis', position: function (pt) { return [pt[0], '10%']; } }, title: { left: 'center', text: '大數(shù)據(jù)量面積圖', }, toolbox: { feature: { dataZoom: { yAxisIndex: 'none' }, restore: {}, saveAsImage: {} } }, xAxis: { type: 'category', boundaryGap: false, data: date }, yAxis: { type: 'value', boundaryGap: [0, '100%'] }, dataZoom: [{ type: 'inside', start: 0, end: 10 }, { start: 0, end: 10, handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z', handleSize: '80%', handleStyle: { color: '#fff', shadowBlur: 3, shadowColor: 'rgba(0, 0, 0, 0.6)', shadowOffsetX: 2, shadowOffsetY: 2 } }], series: [ { name:'模擬數(shù)據(jù)', type:'line', smooth:true, symbol: 'none', sampling: 'average', itemStyle: { color: 'rgb(255, 70, 131)' }, areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgb(255, 158, 68)' }, { offset: 1, color: 'rgb(255, 70, 131)' }]) }, data: data } ], grid:{ containLabel: true } }; // 將option數(shù)據(jù)掛載到main節(jié)點(diǎn) echarts.init(document.getElementById('main')).setOption(option); </script> </body> </html>
小結(jié):
上面實(shí)例有一個(gè)在線的版本,http://echarts.baidu.com/examples/editor.html?c=area-simple, 感興趣可以打開網(wǎng)頁, 體會(huì)一下各種配置項(xiàng)的用途
博主開始沒有找到正確的解決方法, 導(dǎo)致走了彎路, 最后發(fā)現(xiàn)添加一個(gè)參數(shù)就能順利解決, 這里分享給大家,希望沉迷開發(fā)的小伙伴, 能少走彎路
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)經(jīng)典排序算法之選擇排序
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)經(jīng)典排序算法之選擇排序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12JavaScript表單驗(yàn)證實(shí)例之驗(yàn)證表單項(xiàng)是否為空
表單驗(yàn)證幾乎在每個(gè)需要注冊(cè)或者是登錄的網(wǎng)站都是必不可少,下面通過本篇文章給大家介紹JavaScript表單驗(yàn)證實(shí)例之驗(yàn)證表單項(xiàng)是否為空,涉及到j(luò)s表單驗(yàn)證實(shí)例相關(guān)知識(shí),對(duì)js表單驗(yàn)證實(shí)例代碼需要的朋友一起學(xué)習(xí)吧2016-01-01Javascript單元測試框架QUnitjs詳細(xì)介紹
這篇文章主要介紹了Javascript單元測試框架QUnitjs詳細(xì)介紹,需要的朋友可以參考下2014-05-05JavaScript數(shù)組push方法使用注意事項(xiàng)
push() 方法可向數(shù)組的末尾添加一個(gè)或多個(gè)元素,并返回新的長度。這篇文章主要介紹了JavaScript數(shù)組push方法使用注意,需要的朋友可以參考下2017-10-10TypeScript模塊與命名空間的關(guān)系和使用方法
在TypeScript中就像在EC5中一樣,任何包含頂級(jí)import或export的文件都被認(rèn)為是一個(gè)模塊,下面這篇文章主要給大家介紹了關(guān)于如何在TypeScript使用模塊與命名空間以及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2023-03-03微信小程序用戶授權(quán)、位置授權(quán)及獲取微信綁定手機(jī)號(hào)
這篇文章主要介紹了信小程序用戶授權(quán)、位置授權(quán)及獲取微信綁定手機(jī)號(hào),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07