// 立即执行函数,防止变量污染 (function() {})(); // 柱状图模块1 (function() { // 1.实例化对象 var myChart = echarts.init(document.querySelector(".bar .chart")); function randomData() { now = new Date(+now + oneDay); value = value + Math.random() * 21 - 10; return { name: now.toString(), value: [ [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'), Math.round(value) ] }; } var data = []; var now = +new Date(2020, 9, 3); var oneDay = 24 * 3600 * 1000; var value = Math.random() * 1000; for (var i = 0; i < 1000; i++) { data.push(randomData()); } option = { color: ['green'], grid: { top: 6, // left: 0, bottom: 4, }, tooltip: { trigger: 'axis', formatter: function(params) { params = params[0]; var date = new Date(params.name); return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1]; }, axisPointer: { animation: false } }, xAxis: { type: 'time', splitLine: { show: false }, axisLine: { lineStyle: { color: "rgba(255,255,255,.0)" } }, axisLabel: { textStyle: { color: "rgba(255,255,255,.6)", fontSize: 12 }, formatter: '{value}' }, }, yAxis: { type: 'value', boundaryGap: [0, '100%'], splitLine: { show: false }, axisLine: { lineStyle: { color: "rgba(255,255,255,.2)" } }, axisLabel: { textStyle: { color: "rgba(255,255,255,.6)", fontSize: 12 }, formatter: '{value}' }, }, series: [{ name: '模拟数据', type: 'line', showSymbol: false, hoverAnimation: false, data: data }] }; setInterval(function() { for (var i = 0; i < 5; i++) { data.shift(); data.push(randomData()); } myChart.setOption({ series: [{ data: data }] }); }, 1000); // 3.把配置项给实例对象 myChart.setOption(option); // 4.让图表随屏幕自适应 window.addEventListener('resize', function() { myChart.resize(); }) })(); // 柱状图模块2 (function() { // 1.实例化对象 var myChart = echarts.init(document.querySelector(".bar2 .chart")); function randomData() { now = new Date(+now + oneDay); value = value + Math.random() * 21 - 10; return { name: now.toString(), value: [ [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'), Math.round(value) ] }; } var data = []; var now = +new Date(2020, 9, 3); var oneDay = 24 * 3600 * 1000; var value = Math.random() * 1000; for (var i = 0; i < 1000; i++) { data.push(randomData()); } option = { color: ['green'], grid: { top: 6, bottom: 4, }, tooltip: { trigger: 'axis', formatter: function(params) { params = params[0]; var date = new Date(params.name); return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1]; }, axisPointer: { animation: false } }, xAxis: { type: 'time', splitLine: { show: false }, axisLine: { lineStyle: { color: "rgba(255,255,255,.0)" } }, axisLabel: { textStyle: { color: "rgba(255,255,255,.6)", fontSize: 12 }, formatter: '{value}' } }, yAxis: { type: 'value', boundaryGap: [0, '100%'], splitLine: { show: false }, axisLine: { lineStyle: { color: "rgba(255,255,255,.2)" } }, axisLabel: { textStyle: { color: "rgba(255,255,255,.6)", fontSize: 12 }, formatter: '{value}' } }, series: [{ name: '模拟数据', type: 'line', showSymbol: false, hoverAnimation: false, data: data }] }; setInterval(function() { for (var i = 0; i < 5; i++) { data.shift(); data.push(randomData()); } myChart.setOption({ series: [{ data: data }] }); }, 1000); // 3.把配置项给实例对象 myChart.setOption(option); // 4.让图表随屏幕自适应 window.addEventListener('resize', function() { myChart.resize(); }) })(); // 折线图模块1 (function() { var myChart = echarts.init(document.querySelector(".line .chart")); function randomData() { now = new Date(+now + oneDay); value = value + Math.random() * 21 - 10; return { name: now.toString(), value: [ [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'), Math.round(value) ] }; } var data = []; var now = +new Date(2020, 9, 3); var oneDay = 24 * 3600 * 1000; var value = Math.random() * 1000; for (var i = 0; i < 1000; i++) { data.push(randomData()); } option = { color: ['green'], grid: { top: 6, bottom: 4, }, tooltip: { trigger: 'axis', formatter: function(params) { params = params[0]; var date = new Date(params.name); return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1]; }, axisPointer: { animation: false } }, xAxis: { type: 'time', splitLine: { show: false }, axisLine: { lineStyle: { color: "rgba(255,255,255,.0)" } }, axisLabel: { textStyle: { color: "rgba(255,255,255,.6)", fontSize: 12 }, formatter: '{value}' } }, yAxis: { type: 'value', boundaryGap: [0, '100%'], splitLine: { show: false }, axisLine: { lineStyle: { color: "rgba(255,255,255,.2)" } }, axisLabel: { textStyle: { color: "rgba(255,255,255,.6)", fontSize: 12 }, formatter: '{value}' } }, series: [{ name: '模拟数据', type: 'line', showSymbol: false, hoverAnimation: false, data: data }] }; setInterval(function() { for (var i = 0; i < 5; i++) { data.shift(); data.push(randomData()); } myChart.setOption({ series: [{ data: data }] }); }, 1000); myChart.setOption(option); // 4.让图表随屏幕自适应 window.addEventListener('resize', function() { myChart.resize(); }) // 5.点击切换2020 和 2021 的数据 $('.line h2 a').on('click', function() { // console.log($(this).index()); // 点击a 之后 根据当前a的索引号 找到对应的 yearData 相关对象 // console.log(yearData[$(this).index()]); var obj = yearData[$(this).index()]; option.series[0].data = obj.data[0]; option.series[1].data = obj.data[1]; // 选中年份高亮 $('.line h2 a').removeClass('a-active'); $(this).addClass('a-active'); // 需要重新渲染 myChart.setOption(option); }) })(); // 折线图模块2 (function() { var myChart = echarts.init(document.querySelector('.line2 .chart')); function randomData() { now = new Date(+now + oneDay); value = value + Math.random() * 21 - 10; return { name: now.toString(), value: [ [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'), Math.round(value) ] }; } var data = []; var now = +new Date(2020, 9, 3); var oneDay = 24 * 3600 * 1000; var value = Math.random() * 1000; for (var i = 0; i < 1000; i++) { data.push(randomData()); } option = { color: ['green'], grid: { top: 6, bottom: 4, }, tooltip: { trigger: 'axis', formatter: function(params) { params = params[0]; var date = new Date(params.name); return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1]; }, axisPointer: { animation: false } }, xAxis: { show: false, type: 'time', splitLine: { show: false }, axisLine: { lineStyle: { color: "rgba(255,255,255,.0)" } }, axisLabel: { textStyle: { color: "rgba(255,255,255,.6)", fontSize: 12 }, formatter: '{value}' }, }, yAxis: { type: 'value', boundaryGap: [0, '100%'], splitLine: { show: false }, axisLine: { lineStyle: { color: "rgba(255,255,255,.2)" } }, axisLabel: { textStyle: { color: "rgba(255,255,255,.6)", fontSize: 12 }, formatter: '{value}' } }, series: [{ name: '模拟数据', type: 'line', showSymbol: false, hoverAnimation: false, data: data }] }; setInterval(function() { for (var i = 0; i < 5; i++) { data.shift(); data.push(randomData()); } myChart.setOption({ series: [{ data: data }] }); }, 1000); myChart.setOption(option); window.addEventListener('resize', function() { myChart.resize(); }) })();