javascript - 谷歌图表将标题显示为标签

标签 javascript php sql google-visualization axis-labels

在 Google Chart 中处理折线图。存在问题,我无法将测量的标题显示为标签。我还将获得不同的测量结果,现在我一次只能显示一个测量结果。

这是数据库中的sql查询和输出:

enter image description here

这是可视化效果,如您所见,我希望routine.value 显示标题:T_Badende_per_Time:

enter image description here

代码:

<?php

    $con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
    mysql_select_db("nih_bw", $con);

    $sth = mysql_query("Select measurements.title, routines.value, routines.date, routines.time from routines, measure_routine, measurements Where routines.id=measure_routine.routine_id AND measure_routine.measure_id=measurements.id AND measurements.title='T_Badende_per_Time' order by routines.date, routines.time;");

    $rows = array();
    //flag is not needed
    $flag = true;
    $table = array();

    $table['cols'] = array(

    array('label' => 'routines.date' & 'routines.time', 'type' => 'datetime'),
    array('label' => 'routines.value', 'type' => 'number'),

    );

    $rows = array();

    while($r = mysql_fetch_assoc($sth)) {
        $temp = array();
    // assumes dates are in the format "yyyy-MM-dd"
    $dateString = $r['date'];
    $dateArray = explode('-', $dateString);
    $year = $dateArray[0];
    $month = $dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
    $day = $dateArray[2];

    // assumes time is in the format "hh:mm:ss"
    $timeString = $r['time'];
    $timeArray = explode(':', $timeString);
    $hours = $timeArray[0];
    $minutes = $timeArray[1];
    $seconds = $timeArray[2];

    $temp = array();
    $temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)"); 
    $temp[] = array('v' => (string) $r['value']);


    $rows[] = array('c' => $temp);

    }

    $table['rows'] = $rows;
    $jsonTable = json_encode($table);
     echo $jsonTable;   

?>


<html>
  <head>

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = new google.visualization.DataTable(<?=$jsonTable?>);



        var options = {
        /*width: 900, height: 900, */
          title: 'Visualization',
          curveType: 'function', 
           legend: { position: 'bottom' },
           pointSize: 5,
        vAxis: {title: "Values", titleTextStyle: {italic: false}},
        hAxis: {title: "Time", titleTextStyle: {italic: false}},
        explorer: { 
                actions: ['dragToZoom', 'rightClickToReset'], 
                axis: 'vertical'}


        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);


      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

最佳答案

如果您希望每种测量类型占一行,则需要通过 measurements.title 转换数据。 MySQL不支持PIVOT语句,所以你必须像这样伪造它:

SELECT
    routines.data,
    routines.time,
    SUM(IF(measurements.title = 'T_Badende_per_Time', , 0)) CAST(REPLACE(measurements.value, ',', '.') AS DECIMAL(18, 2)) as T_Badende_per_Time,
    SUM(IF(measurements.title = 'measure_2', CAST(REPLACE(measurements.value, ',', '.') AS DECIMAL(18, 2)), 0)) as measure_2,
    SUM(IF(measurements.title = 'measure_3', CAST(REPLACE(measurements.value, ',', '.') AS DECIMAL(18, 2)), 0)) as measure_3
    etc...
FROM
    routines
    INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
    INNER JOIN measurements ON measure_routine.measure_id = measurements.id
WHERE
    <conditions>
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time

然后在 PHP 中,为每种测量类型创建一列:

$table['cols'] = array(
    array('label' => 'routines.date' & 'routines.time', 'type' => 'datetime'),
    array('label' => 'T_Badende_per_Time', 'type' => 'number'),
    array('label' => 'measure_2', 'type' => 'number'),
    array('label' => 'measure_3', 'type' => 'number')
    // etc
);

$rows = array();

while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    // assumes dates are in the format "yyyy-MM-dd"
    $dateString = $r['date'];
    $dateArray = explode('-', $dateString);
    $year = $dateArray[0];
    $month = $dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
    $day = $dateArray[2];

    // assumes time is in the format "hh:mm:ss"
    $timeString = $r['time'];
    $timeArray = explode(':', $timeString);
    $hours = $timeArray[0];
    $minutes = $timeArray[1];
    $seconds = $timeArray[2];

    $temp = array();
    $temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)");
    $temp[] = array('v' => (string) $r['T_Badende_per_Time']);
    $temp[] = array('v' => (string) $r['measure_2']);
    $temp[] = array('v' => (string) $r['measure_3']);
    // etc..

    $rows[] = array('c' => $temp);
}

关于javascript - 谷歌图表将标题显示为标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757653/

相关文章:

javascript - 如果端口范围太宽,Node 中的端口扫描将不起作用

javascript - 使用 'this' 变量的类和子类

javascript - 在按钮焦点 tabindex jquery

php - 将哪些文件放在 Bitbucket 上以便我的团队可以使用?

php - 如何从多维 PHP 数组中获取单个值

sql - 从列中提取数字

javascript - 模态显示但 show.bs.modal 未触发

php - 如何减少多个依赖查询

mysql - SQL 查询查找具有相同名称字段的两行之间的负变化

sql - 创建用户动态名称并分配角色