echart实时显示投票结果

数据库表:echart

--
-- 表的结构 `echart`
--

CREATE TABLE IF NOT EXISTS `echart` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) COLLATE utf8_bin NOT NULL, `sex` tinyint(4) NOT NULL, `votes` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=7 ;

--
-- 转存表中的数据 `echart`
--

INSERT INTO `echart` (`id`, `name`, `sex`, `votes`) VALUES (1, '黄晓明', 1, 100), (2, '白百合', 2, 200), (3, '李易峰', 1, 150), (4, '林心如', 2, 100), (5, '赵薇', 2, 30), (6, '范冰冰', 2, 20);

提供数据接口的function.php

<?php $mysqli = new mysqli('localhost','root','','test'); $mysqli->set_charset('utf8'); $type = addslashes($_GET['type']); if($type=='bar'){ $sql = 'SELECT * FROM echart ORDER BY id'; $result = $mysqli->query($sql); $res = $result->fetch_all(MYSQLI_ASSOC); $mysqli->close(); foreach ($res as $v) { $namelist[] = $v['name']; $voteslist[] = intval($v['votes']); } $name = json_encode($namelist); $votes = json_encode($voteslist); $json = '{"success":true,"name":'.$name.',"votes":'.$votes.'}'; echo $json; } if($type=='pie'){ $sql = 'SELECT sex,count(*) as num FROM echart GROUP BY sex'; $result = $mysqli->query($sql); $res = $result->fetch_all(MYSQLI_ASSOC); $mysqli->close(); foreach ($res as $v) { if($v['sex']==1){ $man = intval($v['num']); } if($v['sex']==2){ $woman = intval($v['num']); } } $json = '{"success":true,"man":'.$man.',"woman":'.$woman.'}'; echo $json; }

竖版柱状图显示投票结果

echart实时显示投票结果_第1张图片

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- 引入 echarts.js -->
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="./echarts.common.min.js"></script>
</head>
<body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 1000px;height:600px;margin:30px auto;"></div>
    <script type="text/javascript"> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); // 指定图表的配置项和数据 myChart.setOption({ title: { text: 'XXX大赛投票结果展示(竖版)' }, tooltip: {}, legend: { data:['票数'] }, xAxis: { data: [] }, yAxis: {}, series: [{ name: '票数', type: 'bar', data: [] }] }); function check(){ $.ajax({ type:"GET", url:"./function.php?type=bar", dataType:"json", success:function(data){ // 填入数据 //alert(data.name); myChart.setOption({ xAxis: { data: data.name }, series: [{ // 根据名字对应到相应的系列 name: '票数', data: data.votes, markPoint : { data : [ {type : 'max', name: '最多票数'}, {type : 'min', name: '最少票数'} ] }, markLine : { data : [ {type : 'average', name: '平均票数'} ] } }] }); } }); } window.setInterval(check, 100); //每秒执行一次 </script>
</body>
</html>

横版柱状图显示投票结果

echart实时显示投票结果_第2张图片

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- 引入 echarts.js -->
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="./echarts.common.min.js"></script>
</head>
<body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 800px;height:600px;margin:30px auto;"></div>
    <script type="text/javascript"> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); // 指定图表的配置项和数据 myChart.setOption({ title: { text: 'XXX大赛投票结果展示(横版)' }, tooltip: {}, legend: { data:['票数'] }, xAxis: { }, yAxis: { data: [] }, series: [{ name: '票数', type: 'bar', data: [] }] }); function check(){ $.ajax({ type:"GET", url:"./function.php?type=bar", dataType:"json", success:function(data){ // 填入数据 //alert(data.name); myChart.setOption({ yAxis: { data: data.name }, series: [{ // 根据名字对应到相应的系列 name: '票数', data: data.votes, markPoint : { data : [ {type : 'max', name: '最多票数'}, {type : 'min', name: '最少票数'} ] }, markLine : { data : [ {type : 'average', name: '平均票数'} ] } }] }); } }); } window.setInterval(check, 100); //每0.1秒执行一次 </script>
</body>
</html>

比例饼图显示性别

echart实时显示投票结果_第3张图片

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <!-- 引入 echarts.js -->
    <script src="echarts.common.min.js"></script>
</head>
<body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 1000px;height:600px;margin:30px auto;"></div>
    <script type="text/javascript"> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); // 指定图表的配置项和数据 myChart.setOption({ title: { text: '性别比例' }, series: [{ name: '性别比例', type: 'pie', radius:'80%', //roseType: 'angle', data: [] }], }); function check(){ $.ajax({ type:"GET", url:"./function.php?type=pie", dataType:"json", success:function(data){ // 填入数据 myChart.setOption({ series: [{ data: [ { value:data.man, name:"男性", itemStyle: { normal: { color: '#1ABC9C' } } }, { value:data.woman, name:"女性", itemStyle: { normal: { color: '#E74C3C' } } } ] }] }); }, error:function(res){ alert('error:'+res.status); } }); } window.setInterval(check, 100); //每0.1秒执行一次 </script>
</body>
</html>

你可能感兴趣的:(echart实时显示投票结果)