前台可视化依据新表结构的修改

This commit is contained in:
wlx 2015-01-21 07:22:29 +00:00
parent 0d251e4206
commit 1d4629d22a
3 changed files with 47 additions and 159 deletions

View File

@ -50,7 +50,6 @@ class VisualController extends Zend_Controller_Action
$this->_helper->layout->disableLayout();
$uuid = $this->_getParam("uuid");
//$record_type = $this->_getParam("dataset");
$record_subset = $this->_getParam("subdataset");
$record = new Visual\Record($uuid,$record_subset);

View File

@ -30,11 +30,11 @@ $this->nav[] = array('link'=>"/data/visual",'title'=>'可视化数据列表');
</div>
<hr />
<?php $vars = (new \Westdc\Visual\VariableEncoder)->normaldecode($this->data['vars']);?>
<?php $vars = (new \Westdc\Visual\VariableEncoder)->normaldecode($this->data['variable']);?>
<?php foreach($vars as $k=>$v) { ?>
<a class="btn btn-default control-btn" href="javascript:void(0);" data-dataset="" data-subdataset="<?= $v['field'] ?>" data-seriename="<?= $v['name'] ?>">
<?= $v['name'] ?>
<a class="btn btn-default control-btn" href="javascript:void(0);" data-subdataset="<?= $v['name'] ?>" data-seriename="<?= $v['title'] ?>">
<?= $v['title'] ?>
</a>
<?php } ?>
@ -67,7 +67,6 @@ $(function() {
Highcharts.setOptions({
lang: {
months : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
//months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
weekdays: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
},
global: {
@ -87,17 +86,7 @@ $(function() {
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
second: '%Y-%m-%d<br/>%H:%M:%S',
minute: '%Y-%m-%d<br/>%H:%M',
hour: '%Y-%m-%d<br/>%H:%M',
day: '%Y<br/>%m-%d',
week: '%Y<br/>%m-%d',
month: '%Y-%m',
year: '%Y'
}
type: 'datetime'
},
tooltip : {
@ -138,7 +127,7 @@ $(function() {
$.ajax({
'url': '/visual/data',
'data': 'uuid=' + uuid + '&dataset=' + dataset + '&subdataset=' + subdataset,
'data': 'uuid=' + uuid + '&subdataset=' + subdataset,
'method': 'GET',
'dataType': 'json',
'success': function(data){

View File

@ -4,25 +4,16 @@ namespace Westdc\Visual;
class Record extends Database
{
public $subdataset,$valueFilter,$vars;
public $timeFiled = "utctime";
public $subdataset,$valueFilter,$vars,$uuid,$xaxis;
private $sql;
function __construct($uuid = NULL,$identifier = "")
function __construct($uuid,$identifier )
{
parent::__construct();
if(!empty($identifier))
{
$this->subdataset = $identifier;
}
if(!empty($uuid) && !empty($identifier))
{
$this->initVisual($uuid);
}
$this->uuid=$uuid;
$this->subdataset = $identifier;
$this->init();
}
public function __invoke()
@ -30,72 +21,24 @@ class Record extends Database
return $this->getData();
}
public function initVisual($uuid)
public function init()
{
$visual = new Visual;
$var_data = $visual->getVisualVars($uuid);
$this->vars = $var_data['vars'];
if(!empty($var_data['visual_valuefilter']))
$this->valueFilter = (float)$var_data['visual_valuefilter'];
if($var_data['visual_timefield_model'] == 'single') {
$this->timeFiled = '"' . $var_data['visual_timefield'] . '"';
}//single
elseif($var_data['visual_timefield_model'] == 'multiple'){
$timefield_temp = "(";
if(!empty($var_data['visual_timefield_year']))
{
$timefield_temp .= $var_data['visual_timefield_year'] . "::character varying||";
}
$timefield_temp .= "'-'";
if(!empty($var_data['visual_timefield_month']))
{
$timefield_temp .= "||".$var_data['visual_timefield_month']."::character varying||";
}else{
$timefield_temp .= '1';
}
$timefield_temp .= "'-";
if(!empty($var_data['visual_timefield_day']))
{
$timefield_temp .= "'||".$var_data['visual_timefield_day'] . '::character varying';
}else{
$timefield_temp .= "1'";
}
$timefield_temp .= ")";
$timefield_temp .= "::timestamp without time zone";
$this->timeFiled = $timefield_temp;
}//multiple
elseif($var_data['visual_timefield_model'] == 'custom'){
$this->timeFiled = $var_data['visual_timefield_custom'];
}//custom
//时间字段为空的bug
if(empty($var_data['visual_timefield_model'])){
$this->timeFiled = "utctime";
}else{
if($var_data['visual_timefield_model'] == 'single' && empty($var_data['visual_timefield'])){
$this->timeFiled = "utctime";
}elseif($var_data['visual_timefield_model'] == 'multiple' && empty($var_data['visual_timefield_year'])){
$this->timeFiled = "utctime";
}elseif($var_data['visual_timefield_model'] == 'custom' && empty($var_data['visual_timefield_custom'])) {
$this->timeFiled = "utctime";
}
}
if(isset($var_data['data']) && !empty($var_data['data']))
$this->sql = $this->makeSql($var_data['data']);
else
$this->sql = $this->makeSql($var_data['visual_datatable']);
$var_data = $visual->getVisualVars($this->uuid);
$this->vars = $var_data['variable'];
$this->xaxis=$var_data['xaxis'];
$json=json_decode($this->vars,true);
$subvar=array_filter($json,function($sub) { return $sub['name']==$this->subdataset; });
if (isset($subvar['sql']) && !empty($subvar['sql']))
$this->sql=$this->makeSql($subvar['sql']);
elseif(isset($var_data['data']) && !empty($var_data['data']))
{
$condition=null;
if (isset($subvar['condition']) && !empty($subvar['condition']))
$condition=$subvar['condition'];
$this->sql = $this->makeSql($var_data['data'],$condition);
}
//echo $this->sql;exit();
}
@ -107,64 +50,43 @@ class Record extends Database
public function getData()
{
$rs = $this->db->query($this->sql);
$data = [];
while($row = $rs->fetch(\PDO::FETCH_ASSOC))
{
$row['utctime'] = $this->utcMsTime(strtotime($row['utctime']));
//var_dump($row);
$data[] = array(
$row['utctime'],
$row['value']
);
$t=[];
foreach($row as $k=>$v)
$t[]=$v;
$data[] = $t;
}
return $data;
}
public function makeSql($table)
public function makeSql($table,$condition=NULL)
{
$wheresql = "";
if ($condition)
{
$filter=explode(';',$condition);
if(!empty($filter))
{
if(is_string($filter))
$wheresql = " WHERE $filter ";
elseif(is_array($filter))
{
$wheresql = ' WHERE '.join(" AND ",$filter);
}
}
}
if(!empty($this->valueFilter))
{
$wheresql = " WHERE \"$this->subdataset\"<>".$this->valueFilter .' ';
}
$filter = $this->getFilter($this->subdataset);
if(!empty($filter))
{
if(is_string($filter))
$wheresql = ' WHERE "'.$this->subdataset.'"'. $filter .' ';
elseif(is_array($filter))
{
$wheresql = [];
foreach($filter as $v)
{
if(preg_match("/^[<|>|=|!](.*)/",$v))
$wheresql[] = '"'.$this->subdataset.'"'.$v;
else
$wheresql[] = $v;
}
$wheresql = ' WHERE '.join(" AND ",$wheresql);
}
}
$sql = "SELECT
{$this->timeFiled} as utctime , \"{$this->subdataset}\" as value
FROM $table as tbl
$sql = "SELECT {$this->xaxis},{$this->subdataset}
FROM ($table) as tbl
$wheresql
ORDER BY
extract(YEAR from {$this->timeFiled}) ASC,
extract(MONTH from {$this->timeFiled}) ASC,
extract(DAY from {$this->timeFiled}) ASC,
extract(HOUR from {$this->timeFiled}) ASC,
extract(MINUTE from {$this->timeFiled}) ASC,
extract(SECOND from {$this->timeFiled}) ASC
";
ORDER BY {$this->xaxis}";
return $sql;
}
@ -174,26 +96,4 @@ class Record extends Database
else
return ($time)*1000;
}
public function getFilter($subdataset)
{
$vars = json_decode($this->vars,TRUE);
if(!is_array($vars) || count($vars)<1)
return null;
foreach($vars as $v)
{
if($v['field'] == $subdataset)
{
if(isset($v['filter']) && !empty($v['filter']))
{
return $v['filter'];
}else{
return null;
}
}
}
return null;
}
}