echarts数据自我定制(一)

有没有发现echarts官网上面的数据都是直接显示定义在js里面的呢?如果要自己定制,该怎么办?下面就让伦家为你揭晓:

php+myssql+echarts展示静态动态图

提前准备好数据库,数据表以及存储的数据,我用的是phpmyadmin,非常easy,秒建成功哦~~

文件包括:

invoke.php,TestDB.php,AjaxService.php,xxx.html

使用方法:将这几个文件放在你apache服务器的www目录下,然后浏览器输入127.0.0.1/xxx.html,你懂得i_f01.gif

以下是详细介绍:

1.invoke.php 数据库连接等相关事宜


<?php
ob_start();
//========================================
function createDB(){
$conn=mysql_connect("localhost","root","123qwe");//亲设置的数据库账号密码
mysql_query("set names 'utf8'");
mysql_select_db("plusoft_test",$conn);//亲的数据库
return $conn;
}
//========================================
$methodName = $_GET["method"];
if($methodName != null){
eval("\$method = ".$methodName.";");
if($method != null) $method();
}
function request($name){
$value = $_GET[$name];
if($value == null){
$value = $_POST[$name];
}
return $value;
}
function writeJSON($obj){
if(is_string($obj)) {
ob_end_clean();
print_r($obj);
}else {
$json = json_encode($obj);
ob_end_clean();
print_r($json);
}
}
function gbk2utf8($data){
if(is_array($data)){
return array_map('gbk2utf8', $data);
}
return iconv('gbk','utf-8',$data);
}
function php_json_decode($str){
$stripStr = stripslashes($str);
$json = json_decode($stripStr,true);
return $json;
}
?>


2.TestDB.php 直接基于数据库的服务,与具体的数据表打交道,取出数据

<?php
require_once('invoke.php');
class TestDB{
 //获取x轴的数据,根据实际需求取类型。有字符串,数值以及日期三种
public function GetXaxisname()
{
$myconn = createDB();
$sql = "select a.name from xaxis a";
$result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return $data;
}
public function GetXaxisnum()
{
$myconn = createDB();
$sql = "select a.num from xaxis a";
$result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return $data;
}
public function GetXaxisdate()
{
$myconn = createDB();
$sql = "select a.mydate from xaxis a";
$result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return $data;
}
//获取y轴的数据,根据实际需求取类型。有字符串,数值以及日期三种
public function GetYaxisname()
{
$myconn = createDB();
$sql = "select b.name from yaxis b";
$result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return $data;
}
public function GetYaxisnum()
{
$myconn = createDB();
$sql = "select b.num from yaxis b";
$result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return $data;
}
public function GetYaxisdate()
{
$myconn = createDB();
$sql = "select b.mydate from yaxis b";
$result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return $data;
}
//获取坐标的具体数据
public function GetData($id)
{
$myconn = createDB();
$sql = "select data from chartdata where id = '".$id."'";
    $result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return count($data) > 0 ? $data[0] : null;
}
/*
public function GetDev($id){
$myconn = createDB();
$sql = "select * from t_dev where id = '".$id."'";
    $result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return count($data) > 0 ? $data[0] : null;
}
*/
//取map的值
public function GetMapData()
{
$myconn = createDB();
$sql = "select city,value from mapdata";
$result=mysql_query($sql,$myconn);
$data = array();
while($row=mysql_fetch_array($result))
{
array_push($data,$row);
}
return $data;
}
}
?>

3.AjaxService.php 定制的服务,输出格式化成json数据

<?php
require_once('invoke.php');
function GetXaxisname(){
require_once('TestDB.php');
$testDB = new TestDB();
$data = $testDB->GetXaxisname();
$json = json_encode($data);
print_r($json);
}
function GetXaxisnum(){
require_once('TestDB.php');
$testDB = new TestDB();
$data = $testDB->GetXaxisnum();
$json = json_encode($data);
print_r($json);
}
function GetXaxisdate(){
require_once('TestDB.php');
$testDB = new TestDB();
$data = $testDB->GetXaxisdate();
$json = json_encode($data);
print_r($json);
}
function GetYaxisname(){
require_once('TestDB.php');
$testDB = new TestDB();
$data = $testDB->GetYaxisname();
$json = json_encode($data);
print_r($json);
}
function GetYaxisnum(){
require_once('TestDB.php');
$testDB = new TestDB();
$data = $testDB->GetYaxisnum();
$json = json_encode($data);
print_r($json);
}
function GetYaxisdate(){
require_once('TestDB.php');
$testDB = new TestDB();
$data = $testDB->GetYaxisdate();
$json = json_encode($data);
print_r($json);
}
function GetData(){
$id = $_GET["id"];
require_once('TestDB.php');
$testDB = new TestDB();
$data = $testDB->GetData($id);
$json = json_encode($data);
print_r($json);
};
function GetMapData(){
require_once('TestDB.php');
$testDB = new TestDB();
$data = $testDB->GetMapData();
$json = json_encode($data);
print_r($json);
}
//生成一定数量的不重复的随机数,所以在php方法中就转换成json格式,这样方便处理。js不认识这种类型的数据
function RandTest(){
//初始化数据范围【最小值,最大值】和生成数据的个数
$min=0;
$max=100;
$num=100;
$count=0;
$return=array();
while($count < $num)
{
$return[] = mt_rand($min , $max);
$return =array_flip(array_flip($return));
$count = count($return);
}
shuffle($return);////data=Array ( [0] => 4 [1] => 2 [2] => 10 [3] => 14 [4] => 20 [5] => 16 [6] => 1 )
$json = json_encode($return);
 print_r($json);//[4,10,12,1,11,17,8]
}
//生产0到1之间的小数一位
function RandFloat(){
 $min=0;
 $max=1;
 // $return=rand($min , $max)/10;
 $return=$min + mt_rand() / mt_getrandmax() * ($max - $min);
 $json = json_encode($return);
 print_r($json);
}
//生产0到20之间的整数一位
function RandANum(){
 $min=0;
 $max=10;
 // $return=rand($min , $max)/10;
 $return=mt_rand($min , $max);
 $json = json_encode($return);
 print_r($json);
}
//生产20到40之间的整数一位
function RandANum2(){
 $min=20;
 $max=40;
 // $return=rand($min , $max)/10;
 $return=mt_rand($min , $max);
 $json = json_encode($return);
 print_r($json);
}
//生产500到1000之间的整数一位
function RandBigNum(){
 $min=500;
 $max=1000;
 // $return=rand($min , $max)/10;
 $return=mt_rand($min , $max);
 $json = json_encode($return);
 print_r($json);
}
?>


哇,字数要爆棚了,下一个讲具体的图形定制吧。


你可能感兴趣的:(自定义,ECharts,数据设置)