javascript - Google图表将标题显示为标签

标签 javascript php sql google-visualization axis-labels

在Google图表中处理折线图。我无法显示测量标题作为标签的问题。我将进一步获得不同的测量值,现在我一次只能显示一个测量值。

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



这是可视化效果,如您所见,我希望例程。值显示标题:T_Badende_per_Time:



码:

<?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 - Google图表将标题显示为标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757653/

相关文章:

php - api调用后Wordpress使cf7无效

php - Excel 导出 CSV 导入 PHPmyAdmin

mysql - 日期之间的MySQL子查询

mysql - SQL中表格的第一个和第二个日期

javascript - 未捕获的类型错误 : undefined is not a function when readind map in javascript

javascript - <a>中的Href无法正常工作

javascript - 如何在由 8 个不同的 "prism"图像组成的全宽背景上保持 100% 的高度 + 纵横比

javascript - 如何在 IE HTML 条件中生成 "else"?

php - curl错误35,可能的原因?

sql - 在数据库中使用 View 有什么好处?