echarts测试
页面中快速使用echarts
{echarts=4 width=[100%,50,50,46] height=80%}
myChart.showLoading(); $.get(ROOT_PATH + 'data/asset/data/life-expectancy.json', function (data) { myChart.hideLoading(); var itemStyle = { opacity: 0.8, shadowBlur: 10, shadowOffsetX: 0, shadowOffsetY: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' }; var sizeFunction = function (x) { var y = Math.sqrt(x / 5e8) + 0.1; return y * 80; }; // Schema: var schema = [ {name: 'Income', index: 0, text: '人均收入', unit: '美元'}, {name: 'LifeExpectancy', index: 1, text: '人均寿命', unit: '岁'}, {name: 'Population', index: 2, text: '总人口', unit: ''}, {name: 'Country', index: 3, text: '国家', unit: ''} ]; option = { baseOption: { timeline: { axisType: 'category', orient: 'vertical', autoPlay: true, inverse: true, playInterval: 1000, left: null, right: 0, top: 20, bottom: 20, width: 55, height: null, label: { color: '#999' }, symbol: 'none', lineStyle: { color: '#555' }, checkpointStyle: { color: '#bbb', borderColor: '#777', borderWidth: 2 }, controlStyle: { showNextBtn: false, showPrevBtn: false, color: '#666', borderColor: '#666' }, emphasis: { label: { color: '#fff' }, controlStyle: { color: '#aaa', borderColor: '#aaa' } }, data: [] }, backgroundColor: '#404a59', title: [{ text: data.timeline[0], textAlign: 'center', left: '63%', top: '55%', textStyle: { fontSize: 100, color: 'rgba(255, 255, 255, 0.7)' } }, { text: '各国人均寿命与GDP关系演变', left: 'center', top: 10, textStyle: { color: '#aaa', fontWeight: 'normal', fontSize: 20 } }], tooltip: { padding: 5, backgroundColor: '#222', borderColor: '#777', borderWidth: 1, formatter: function (obj) { var value = obj.value; return schema[3].text + ':' + value[3] + '
' + schema[1].text + ':' + value[1] + schema[1].unit + '
' + schema[0].text + ':' + value[0] + schema[0].unit + '
' + schema[2].text + ':' + value[2] + '
'; } }, grid: { top: 100, containLabel: true, left: 30, right: '110' }, xAxis: { type: 'log', name: '人均收入', max: 100000, min: 300, nameGap: 25, nameLocation: 'middle', nameTextStyle: { fontSize: 18 }, splitLine: { show: false }, axisLine: { lineStyle: { color: '#ccc' } }, axisLabel: { formatter: '{value} $' } }, yAxis: { type: 'value', name: '平均寿命', max: 100, nameTextStyle: { color: '#ccc', fontSize: 18 }, axisLine: { lineStyle: { color: '#ccc' } }, splitLine: { show: false }, axisLabel: { formatter: '{value} 岁' } }, visualMap: [ { show: false, dimension: 3, categories: data.counties, calculable: true, precision: 0.1, textGap: 30, textStyle: { color: '#ccc' }, inRange: { color: (function () { var colors = ['#bcd3bb', '#e88f70', '#edc1a5', '#9dc5c8', '#e1e8c8', '#7b7c68', '#e5b5b5', '#f0b489', '#928ea8', '#bda29a']; return colors.concat(colors); })() } } ], series: [ { type: 'scatter', itemStyle: itemStyle, data: data.series[0], symbolSize: function(val) { return sizeFunction(val[2]); } } ], animationDurationUpdate: 1000, animationEasingUpdate: 'quinticInOut' }, options: [] }; for (var n = 0; n < data.timeline.length; n++) { option.baseOption.timeline.data.push(data.timeline[n]); option.options.push({ title: { show: true, 'text': data.timeline[n] + '' }, series: { name: data.timeline[n], type: 'scatter', itemStyle: itemStyle, data: data.series[n], symbolSize: function(val) { return sizeFunction(val[2]); } } }); } myChart.setOption(option); });
$.get(ROOT_PATH + 'data/asset/data/hangzhou-tracks.json', function(data) { var lines = data.map(function (track) { return { coords: track.map(function (seg, idx) { return seg.coord; }) }; }); myChart.setOption(option = { bmap: { center: [120.13066322374, 30.240018034923], zoom: 14, roam: true, mapStyle: { styleJson: [{ 'featureType': 'water', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'land', 'elementType': 'all', 'stylers': { 'color': '#f3f3f3' } }, { 'featureType': 'railway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'highway', 'elementType': 'all', 'stylers': { 'color': '#fdfdfd' } }, { 'featureType': 'highway', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'arterial', 'elementType': 'geometry', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'arterial', 'elementType': 'geometry.fill', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'poi', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'green', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'subway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'manmade', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'local', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'arterial', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'boundary', 'elementType': 'all', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'building', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'label', 'elementType': 'labels.text.fill', 'stylers': { 'color': '#999999' } }] } }, series: [{ type: 'lines', coordinateSystem: 'bmap', data: lines, polyline: true, lineStyle: { color: 'purple', opacity: 0.6, width: 1 } }] }); });
3d测试
{echarts=2 width=100% height=100% }
var config = { color: '#c0101a', levels: 1, intensity: 4, threshold: 0.01 } var canvas = document.createElement('canvas'); canvas.width = 4096; canvas.height = 2048; context = canvas.getContext("2d"); context.lineWidth = 0.5; context.strokeStyle = config.color; context.fillStyle = config.color; context.shadowColor = config.color; $.when( $.getScript(ROOT_PATH + 'vendors/d3/d3-contour.js'), $.getScript(ROOT_PATH + 'vendors/d3/d3-geo.js'), $.getScript(ROOT_PATH + 'vendors/d3/d3-timer.js') ).done(function () { image(ROOT_PATH + 'data-gl/asset/bathymetry_bw_composite_4k.jpg').then(function(image) { var m = image.height, n = image.width, values = new Array(n * m), contours = d3.contours().size([n, m]).smooth(true), projection = d3.geoIdentity().scale(canvas.width / n), path = d3.geoPath(projection, context); // StackBlur.R(image, 5); for (var j = 0, k = 0; j < m; ++j) { for (var i = 0; i < n; ++i, ++k) { values[k] = image.data[(k << 2)] / 255; } } var opt = { image: canvas } var results = []; function update(threshold, levels) { context.clearRect(0, 0, canvas.width, canvas.height); var thresholds = []; for (var i = 0; i < levels; i++) { thresholds.push((threshold + 1 / levels * i) % 1); } results = contours.thresholds(thresholds)(values); redraw(); } function redraw() { results.forEach(function (d, idx) { context.beginPath(); path(d); context.globalAlpha = 1; context.stroke(); if (idx > config.levels / 5 * 3) { context.globalAlpha = 0.01; context.fill(); } }); opt.onupdate(); } d3.timer(function(t) { var threshold = (t % 10000) / 10000; update(threshold, 1); }); initCharts(opt); update(config.threshold, config.levels); }); function image(url) { return new Promise(function(resolve) { var image = new Image(); image.src = url; image.onload = function() { var canvas = document.createElement("canvas"); canvas.width = image.width / 8; canvas.height = image.height / 8; var context = canvas.getContext("2d"); context.drawImage(image, 0, 0, canvas.width, canvas.height); resolve(context.getImageData(0, 0, canvas.width, canvas.height)); }; }); } function initCharts(opt) { var contourChart = echarts.init(document.createElement('canvas'), null, { width: 4096, height: 2048 }); var img = new echarts.graphic.Image({ style: { image: opt.image, x: -1, y: -1, width: opt.image.width + 2, height: opt.image.height + 2 } }); contourChart.getZr().add(img); opt.onupdate = function () { img.dirty(); }; myChart.setOption({ backgroundColor: '#000', globe: { environment: ROOT_PATH + 'data-gl/asset/starfield.jpg', heightTexture: ROOT_PATH + 'data-gl/asset/bathymetry_bw_composite_4k.jpg', displacementScale: 0.05, displacementQuality: 'high', baseColor: '#000', shading: 'realistic', realisticMaterial: { roughness: 0.2, metalness: 0 }, postEffect: { enable: true, depthOfField: { // enable: true } }, light: { ambient: { intensity: 0 }, main: { intensity: 0.1, shadow: false }, ambientCubemap: { texture: ROOT_PATH + 'data-gl/asset/lake.hdr', exposure: 1, diffuseIntensity: 0.5, specularIntensity: 2 } }, viewControl: { autoRotate: false }, layers: [{ type: 'blend', blendTo: 'emission', texture: contourChart, intensity: config.intensity }] } }); } });
再测试一组
{echarts=5 width=[50%,50%,100%] height=25%}
option = { title: { text: '堆叠区域图' }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross', label: { backgroundColor: '#6a7985' } } }, legend: { data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎'] }, toolbox: { feature: { saveAsImage: {} } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'category', boundaryGap: false, data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] } ], yAxis: [ { type: 'value' } ], series: [ { name: '邮件营销', type: 'line', stack: '总量', areaStyle: {}, data: [120, 132, 101, 134, 90, 230, 210] }, { name: '联盟广告', type: 'line', stack: '总量', areaStyle: {}, data: [220, 182, 191, 234, 290, 330, 310] }, { name: '视频广告', type: 'line', stack: '总量', areaStyle: {}, data: [150, 232, 201, 154, 190, 330, 410] }, { name: '直接访问', type: 'line', stack: '总量', areaStyle: {}, data: [320, 332, 301, 334, 390, 330, 320] }, { name: '搜索引擎', type: 'line', stack: '总量', label: { normal: { show: true, position: 'top' } }, areaStyle: {}, data: [820, 932, 901, 934, 1290, 1330, 1320] } ] };
var data = []; for (var i = 0; i <= 360; i++) { var t = i / 180 * Math.PI; var r = Math.sin(2 * t) * Math.cos(2 * t); data.push([r, i]); } option = { title: { text: '极坐标双数值轴' }, legend: { data: ['line'] }, polar: { center: ['50%', '54%'] }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } }, angleAxis: { type: 'value', startAngle: 0 }, radiusAxis: { min: 0 }, series: [{ coordinateSystem: 'polar', name: 'line', type: 'line', showSymbol: false, data: data }], animationDuration: 2000 };
option = { tooltip: { trigger: 'axis', axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: 'shadow' // 默认为直线,可选为:'line' | 'shadow' } }, legend: { data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎', '百度', '谷歌', '必应', '其他'] }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'category', data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] } ], yAxis: [ { type: 'value' } ], series: [ { name: '直接访问', type: 'bar', data: [320, 332, 301, 334, 390, 330, 320] }, { name: '邮件营销', type: 'bar', stack: '广告', data: [120, 132, 101, 134, 90, 230, 210] }, { name: '联盟广告', type: 'bar', stack: '广告', data: [220, 182, 191, 234, 290, 330, 310] }, { name: '视频广告', type: 'bar', stack: '广告', data: [150, 232, 201, 154, 190, 330, 410] }, { name: '搜索引擎', type: 'bar', data: [862, 1018, 964, 1026, 1679, 1600, 1570], markLine: { lineStyle: { type: 'dashed' }, data: [ [{type: 'min'}, {type: 'max'}] ] } }, { name: '百度', type: 'bar', barWidth: 5, stack: '搜索引擎', data: [620, 732, 701, 734, 1090, 1130, 1120] }, { name: '谷歌', type: 'bar', stack: '搜索引擎', data: [120, 132, 101, 134, 290, 230, 220] }, { name: '必应', type: 'bar', stack: '搜索引擎', data: [60, 72, 71, 74, 190, 130, 110] }, { name: '其他', type: 'bar', stack: '搜索引擎', data: [62, 82, 91, 84, 109, 110, 120] } ] };
var hours = ['12a', '1a', '2a', '3a', '4a', '5a', '6a', '7a', '8a', '9a','10a','11a', '12p', '1p', '2p', '3p', '4p', '5p', '6p', '7p', '8p', '9p', '10p', '11p']; var days = ['Saturday', 'Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday', 'Sunday']; var data = [[0,0,5],[0,1,1],[0,2,0],[0,3,0],[0,4,0],[0,5,0],[0,6,0],[0,7,0],[0,8,0],[0,9,0],[0,10,0],[0,11,2],[0,12,4],[0,13,1],[0,14,1],[0,15,3],[0,16,4],[0,17,6],[0,18,4],[0,19,4],[0,20,3],[0,21,3],[0,22,2],[0,23,5],[1,0,7],[1,1,0],[1,2,0],[1,3,0],[1,4,0],[1,5,0],[1,6,0],[1,7,0],[1,8,0],[1,9,0],[1,10,5],[1,11,2],[1,12,2],[1,13,6],[1,14,9],[1,15,11],[1,16,6],[1,17,7],[1,18,8],[1,19,12],[1,20,5],[1,21,5],[1,22,7],[1,23,2],[2,0,1],[2,1,1],[2,2,0],[2,3,0],[2,4,0],[2,5,0],[2,6,0],[2,7,0],[2,8,0],[2,9,0],[2,10,3],[2,11,2],[2,12,1],[2,13,9],[2,14,8],[2,15,10],[2,16,6],[2,17,5],[2,18,5],[2,19,5],[2,20,7],[2,21,4],[2,22,2],[2,23,4],[3,0,7],[3,1,3],[3,2,0],[3,3,0],[3,4,0],[3,5,0],[3,6,0],[3,7,0],[3,8,1],[3,9,0],[3,10,5],[3,11,4],[3,12,7],[3,13,14],[3,14,13],[3,15,12],[3,16,9],[3,17,5],[3,18,5],[3,19,10],[3,20,6],[3,21,4],[3,22,4],[3,23,1],[4,0,1],[4,1,3],[4,2,0],[4,3,0],[4,4,0],[4,5,1],[4,6,0],[4,7,0],[4,8,0],[4,9,2],[4,10,4],[4,11,4],[4,12,2],[4,13,4],[4,14,4],[4,15,14],[4,16,12],[4,17,1],[4,18,8],[4,19,5],[4,20,3],[4,21,7],[4,22,3],[4,23,0],[5,0,2],[5,1,1],[5,2,0],[5,3,3],[5,4,0],[5,5,0],[5,6,0],[5,7,0],[5,8,2],[5,9,0],[5,10,4],[5,11,1],[5,12,5],[5,13,10],[5,14,5],[5,15,7],[5,16,11],[5,17,6],[5,18,0],[5,19,5],[5,20,3],[5,21,4],[5,22,2],[5,23,0],[6,0,1],[6,1,0],[6,2,0],[6,3,0],[6,4,0],[6,5,0],[6,6,0],[6,7,0],[6,8,0],[6,9,0],[6,10,1],[6,11,0],[6,12,2],[6,13,1],[6,14,3],[6,15,4],[6,16,0],[6,17,0],[6,18,0],[6,19,0],[6,20,1],[6,21,2],[6,22,2],[6,23,6]]; option = { title: { text: 'Punch Card of Github', link: 'https://github.com/pissang/echarts-next/graphs/punch-card' }, legend: { data: ['Punch Card'], left: 'right' }, polar: {}, tooltip: { formatter: function (params) { return params.value[2] + ' commits in ' + hours[params.value[1]] + ' of ' + days[params.value[0]]; } }, angleAxis: { type: 'category', data: hours, boundaryGap: false, splitLine: { show: true, lineStyle: { color: '#999', type: 'dashed' } }, axisLine: { show: false } }, radiusAxis: { type: 'category', data: days, axisLine: { show: false }, axisLabel: { rotate: 45 } }, series: [{ name: 'Punch Card', type: 'scatter', coordinateSystem: 'polar', symbolSize: function (val) { return val[2] * 2; }, data: data, animationDelay: function (idx) { return idx * 5; } }] };
如果遇到无法显示的图,很大可能就是引入的json文件,蛋哥没有整理下来,放到蛋哥的服务器上。