Google图表行为异常

时间:2019-03-14 13:59:43

标签: php jquery google-visualization

所以我有这张Google图表,它从数据库中获取数据,看起来像这样:

enter image description here

如您所见,在所有数据上都拖动了一条线(越走越糟)

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="dashboard">
         <div id="chart_div"></div>
         <div id="control_div"></div>
         <div id='dbgchart'></div>
</body>
</html>

<?php 
include "../includes/db_conn.php";

$sql = "SELECT FD_DATUM_INGEVOERD || ' ' || FT_TIJD_INGEVOERD FROM BANDZENDINGEN WHERE FD_DATUM_INGEVOERD BETWEEN '2012-06-04' AND '2012-06-05' AND FI_GEBRUIKER1='205'";
$sfm = $dbh->prepare($sql);
$sfm->execute();

$sqla = "SELECT FI_AFVOERKANAAL FROM BANDZENDINGEN WHERE FD_DATUM_INGEVOERD BETWEEN '2012-06-04' AND '2012-06-05' AND FI_GEBRUIKER1='205'";
$sfma = $dbh->prepare($sqla);
$sfma->execute();

$dateArr = $sfm->fetchAll(PDO::FETCH_COLUMN, 0);

$numbers = $sfma->fetchAll(PDO::FETCH_COLUMN, 0);
?>
<script type="text/javascript">
google.charts.load('44', {
  callback: drawAxisTickColors,
  packages: ['controls', 'corechart']
});


function drawAxisTickColors() {
  var data = new google.visualization.DataTable();
  data.addColumn('datetime', 'Date');
  data.addColumn('number', 'value');

  var dateArr2 = (<?php echo json_encode($dateArr); ?>);
  console.log(dateArr2);

  var bitcoinArr = (<?php echo json_encode($numbers); ?>);
  console.log(bitcoinArr);

  bitcoinArr = bitcoinArr.map(Number);

  var array = [];

  var length = Math.min(dateArr2.length, bitcoinArr.length);
  for (var i = 0; i < length; ++i) {
  array.push([ new Date(dateArr2[i]), bitcoinArr[i] ]);
  }

  data.addRows(array);

  var control = new google.visualization.ControlWrapper({
  controlType: 'ChartRangeFilter',
  containerId: 'control_div',
  options: {
      filterColumnIndex: 0,
      ui: {
          chartOptions: {
              height: 50,
              width: 600,
              chartArea: {
                  width: '80%'
              }
          }
      }
  }
  });

  var chart = new google.visualization.ChartWrapper({
  chartType: 'LineChart',
  containerId: 'chart_div',
  options: {
    width: 620,
    chartArea: {
      width: '80%'
    },
    hAxis: {
      format: 'hh:ss',
      slantedText: false,
      maxAlternation: 1
    }
  }
  });

 function setOptions() {
  var firstDate;
  var lastDate;
  var v = control.getState();

  if (v.range) {
    document.getElementById('dbgchart').innerHTML = v.range.start + ' to ' + v.range.end;
    firstDate = new Date(v.range.start.getTime() + 1);
    lastDate = new Date(v.range.end.getTime() - 1);
    data.setValue(v.range.start.getMonth(), 0, firstDate);
    data.setValue(v.range.end.getMonth(), 0, lastDate);
  } else {
    firstDate = data.getValue(0, 0);
    lastDate = data.getValue(data.getNumberOfRows() - 1, 0);
  }

  var ticks = [];
  for (var i = firstDate.getMonth(); i <= lastDate.getMonth(); i++) {
    ticks.push(data.getValue(i, 0));
  }

  chart.setOption('hAxis.ticks', ticks);
  chart.setOption('hAxis.viewWindow.min', firstDate);
  chart.setOption('hAxis.viewWindow.max', lastDate);
  if (dash) {
    chart.draw();
  }
 }

 setOptions();

 google.visualization.events.addListener(control, 'statechange', setOptions);

 var dash = new google.visualization.Dashboard(document.getElementById('dashboard'));
 dash.bind([control], [chart]);
 dash.draw(data);
 }
 </script>

我已经尝试删除毫秒了,没关系。

那我做错了什么? 为什么要打apping? 请帮忙。

编辑

只是发现了一些东西,它跳过了像这样的数字:2012-06-04 07:03:06 14 数据存在,但从未显示

编辑

我发现了问题

  if (v.range) {
    document.getElementById('dbgchart').innerHTML = v.range.start + ' to ' + v.range.end;
    firstDate = new Date(v.range.start.getTime() + 1);
    lastDate = new Date(v.range.end.getTime() - 1);
    data.setValue(v.range.start.getMonth(), 0, firstDate);
    data.setValue(v.range.end.getMonth(), 0, lastDate);
  } else {
    firstDate = data.getValue(0, 0);
    lastDate = data.getValue(data.getNumberOfRows() - 1, 0);
  }

data.setValue是问题,但是如何解决?

2 个答案:

答案 0 :(得分:1)

通过在所有数据上拖动行来纠正问题
加载行后对数据表进行排序...

data.addRows(array);
data.sort([{column: 0}]);

答案 1 :(得分:0)

这样做对我有用:

   data.setValue(v.range.start.getMonth(), 0);
   data.setValue(v.range.end.getMonth(), 0);