echarts简单的实时图表更新

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>echarts实时图表</title>
		<script src="./echarts.js"></script>
		<style>
			*{margin: 0;padding: 0;}
			.main{width: 500px;height: 300px;border: 1px solid #333;margin: 100px;}
		</style>
		
	</head>
	<body>
		
		<div class="main" id="echarts">
			
		</div>
		<script>
			window.onload = function(){
				var arr = [
					[820, 932, 901, 934, 1290, 1330, 1320],
					[320, 232, 401, 834, 1290, 1030, 620],
					[620, 432, 701, 634, 1290, 1330, 920],
					[520, 632, 901, 834, 1290, 430, 720]
				]
				var mychart = echarts.init(document.getElementById("echarts"));
				var option = {
					title: {
						text: '图表标题信息', //主标题
						subtext: '副标题显示', //副标题
						x: 'center', //标题文字居中
						textStyle: {  //主标题样式
							color: '#f60',
							lineHeight: 40
						},
						subtextStyle: { //副标题样式
							color: '#f90',
							lineHeight: 20
						},
						top: '-2%'
					},
					backgroundColor: '#f9f9f9', //整个图表的背景
					color: ['#f60'], //系列柱的颜色
					xAxis: {
						type: 'category',
						data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
					},
					yAxis: {
						type: 'value',
						axisLine: {
							symbol: 'arrow',
							lineStyle: {
								color: '#ff0'
							}
						},
						axisLabel: {
							formatter: '{value}万', //刻度标签的内容格式器,支持字符串模板和回调函数两种形式
// 							formatter: function(value,index){
// 								if(value > 0) {
// 									return value + '万'
// 								}
// 								else{
// 									return value
// 								}
// 							}
						}
					},
					grid: { //距离左右上下的边距
						left: '0',
						right: '0',
						bottom: '0',
						containLabel: true //grid 区域是否包含坐标轴的刻度标签
					},
					series: [{
						data: [820, 932, 901, 934, 1290, 1330, 1320],
						type: 'bar'
					}]
				};
				mychart.setOption(option);
				//设置定时器,改变数据
				setInterval(getData,2000);
				
				//获取数据
				function getData(){
					var num = Math.floor(Math.random()*arr.length);
					var data = arr[num];
					mychart.setOption({
						series: [{
							data: data
						}]
					})
				}
				getData();
			}
			
		</script>
	</body>
</html>

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LLL_LH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值