123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- // 立即执行函数,防止变量污染 (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();
- })
- })();
|