使用echarts对数据库数据可视化

1、echarts定制

在echart官网下载js文件
链接: echarts官网.
在这里插入图片描述
这里只用到了折线图,所以直接默认定制就可以下载了
懒得下载的可以直接用
链接: https://pan.baidu.com/s/1-5IWuIfW_4skuN8Op7O7OQ.
提取码:p39q

2、数据库

我只是用来做温湿度的可视化,所以我的数据只有时间、温度、湿度
在这里插入图片描述

3、网页实现

(1)一对一

下面展示一些 内联代码片

main.php
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8" http-equiv="refresh" content="60">
	<title>Intelligent Monitoring</title>
    <script src="echarts.min.js"></script>
</head>
<body bgcolor="black">
</script>
	<div class="wrapper">
	</div>
    <div id="main" style="width: 900px;height:500px;"></div>
    <?php
	$con = @mysqli_connect("localhost","用户名","密码") or die(				"failed to connect database¡");
            mysqli_select_db($con,"数据库名");
			$sql = 'SELECT * FROM (select * from 表名 order by id desc limit 10) aa ORDER BY id';// 获取最新十条数据
            $result = mysqli_query($con,$sql);
			$Num=mysqli_num_rows($result);  
			$time=array();// 定义时间数组,保存时间
			$temperature=array();// 定义温度数组
			for($i=0; $i<$Num; $i++){
				$row = mysqli_fetch_assoc($result);
				$time[$i] = $row['time'];// 保存从数据库中获取的数据
				$temperature[$i] = $row['temperature'];// 保存从数据库中获取的数据
			}
			mysqli_free_result($result);
			mysqli_close($con);
	echo "
    <script>
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        // 指定图表的配置项和数据
		var option = {
			xAxis: {
				type: 'category',
				data: ['{$time[0]}', '{$time[1]}', '{$time[2]}', 		 '{$time[3]}', '{$time[4]}', '{$time[5]}', '{$time[6]}', '{$time[7]}', '{$time[8]}', '{$time[9]}']// x轴显示数据
			},
			yAxis: {
				type: 'value'
			},
			textStyle: {
	        	fontWeight: 'normal',              //标题颜色
	        	color: '#FFFFFF'
	      	},
			series: [{
				name:'温度',
				data: [$temperature[0], $temperature[1], $temperature[2], $temperature[3], $temperature[4], $temperature[5], $temperature[6], $temperature[7], $temperature[8], $temperature[9]],// y轴显示数据
				type: 'line', // line为折线图,bar为条形图
				itemStyle:{
					normal:{
						label:{
							show:true, // 选择是否显示每个点的数据
						},
					}
				}
			}]
		};
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>";
	?>
</body>
</html>

效果图:
在这里插入图片描述

(2)一对多

折线图同时显示温湿度

main.php
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8" http-equiv="refresh" content="60">
	<title>Intelligent Monitoring</title>
    <script src="echarts.min.js"></script>
</head>
<body bgcolor="black">
</script>

	<div class="wrapper">

	</div>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 900px;height:500px;"></div>
    <?php
	//require "db2.php";
	$con = @mysqli_connect("localhost","用户名","密码") or die(				"failed to connect database¡");

            mysqli_select_db($con,"数据库名");

			$sql = 'SELECT * FROM (select * from 表名 order by id desc limit 10) aa ORDER BY id';

            $result = mysqli_query($con,$sql);

			$Num=mysqli_num_rows($result);  
			
			$time=array();
			$temperature=array();
			$humidity=array();
			for($i=0; $i<$Num; $i++){
				$row = mysqli_fetch_assoc($result);
				$time[$i] = $row['time'];
				$temperature[$i] = $row['temperature'];
				$humidity[$i] = $row['humidity'];					
			}
			mysqli_free_result($result);
			mysqli_close($con);
	echo "
    <script>
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        // 指定图表的配置项和数据
		option = {
    title: {
        text: '折线图',
		textStyle: {
	        	fontWeight: 'normal',              //标题颜色
	        	color: '#FFFFFF'
	      	},
    },
	textStyle: {
	        	fontWeight: 'normal',              //标题颜色
	        	color: '#FFFFFF'
	      	},
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['温度', '湿度'],
		textStyle: {
	        	fontWeight: 'normal',              //标题颜色
	        	color: '#FFFFFF'
	      	},
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis:{
        type: 'category',
        boundaryGap: false,
        data: ['{$time[0]}', '{$time[1]}', '{$time[2]}', '{$time[3]}', '{$time[4]}', '{$time[5]}', '{$time[6]}', '{$time[7]}', '{$time[8]}', '{$time[9]}']},
    yAxis: [
		{
			type: 'value',
			position: 'left',
			splitLine: {
				show: false// 是否有线
			},
			scale:false,// 刻度是否自动修改
			axisLabel: {
 				formatter:'{value} ℃'//单位
			},	
   		}, 
		{
			type: 'value',
			position: 'right',
			splitLine: {
				show: false
			},
			scale:false,
			axisLabel: {
 				formatter:'{value} %rh'//单位
			},
    	}
	],
    series: [
        {
            name: '温度',
            type: 'line',
	        yAxisIndex: 0,  // 通过这个判断左右
            data: [$temperature[0], $temperature[1], $temperature[2], $temperature[3], $temperature[4], $temperature[5], $temperature[6], $temperature[7], $temperature[8], $temperature[9]]
        },		
        {
            name: '湿度',
            type: 'line',
	        yAxisIndex: 1,
            data: [$humidity[0], $humidity[1], $humidity[2], $humidity[3], $humidity[4], $humidity[5], $humidity[6], $humidity[7], $humidity[8], $humidity[9]]
        }
		
    ]
};
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>";
	?>
</body>
</html>

效果:
在这里插入图片描述
由于温湿度是不同类型的数据,所以这里用了双纵轴来表示
如果只需要一条纵轴

main.php
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8" http-equiv="refresh" content="60">
	<title>Intelligent Monitoring</title>
    <script src="echarts.min.js"></script>
</head>
<body bgcolor="black">
</script>

	<div class="wrapper">

	</div>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 900px;height:500px;"></div>
    <?php
	//require "db2.php";
	$con = @mysqli_connect("localhost","用户名","密码") or die(				"failed to connect database¡");
            mysqli_select_db($con,"数据库名");
			$sql = 'SELECT * FROM (select * from 表名 order by id desc limit 10) aa ORDER BY id';
            $result = mysqli_query($con,$sql);
			$Num=mysqli_num_rows($result);  
			$time=array();
			$temperature=array();
			$humidity=array();
			for($i=0; $i<$Num; $i++){
				$row = mysqli_fetch_assoc($result);
				$time[$i] = $row['time'];
				$temperature[$i] = $row['temperature'];
				$humidity[$i] = $row['humidity'];					
			}
			mysqli_free_result($result);
			mysqli_close($con);
	echo "
    <script>
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        // 指定图表的配置项和数据
		option = {
    title: {
        text: '折线图',
		textStyle: {
	        	fontWeight: 'normal',              //标题颜色
	        	color: '#FFFFFF'
	      	},
    },

	textStyle: {
	        	fontWeight: 'normal',              //标题颜色
	        	color: '#FFFFFF'
	      	},
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['温度', '湿度'],
		textStyle: {
	        	fontWeight: 'normal',              //标题颜色
	        	color: '#FFFFFF'
	      	},
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis:{
        type: 'category',
        boundaryGap: false,
        data: ['{$time[0]}', '{$time[1]}', '{$time[2]}', '{$time[3]}', '{$time[4]}', '{$time[5]}', '{$time[6]}', '{$time[7]}', '{$time[8]}', '{$time[9]}']},
		
    yAxis: [
		{
			type: 'value',	
   		}
	],
    series: [
        {
            name: '温度',
            type: 'line',
            data: [$temperature[0], $temperature[1], $temperature[2], $temperature[3], $temperature[4], $temperature[5], $temperature[6], $temperature[7], $temperature[8], $temperature[9]]
        },
			
        {
            name: '湿度',
            type: 'line',
            data: [$humidity[0], $humidity[1], $humidity[2], $humidity[3], $humidity[4], $humidity[5], $humidity[6], $humidity[7], $humidity[8], $humidity[9]]
        }
		
    ]
};
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>";
	
	
	?>
</body>
</html>

效果:
在这里插入图片描述

第一次用echarts,有哪里不对多多交流哈哈哈

如需转载,请注明出处。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值