2014-03-31 2 views
0

Google 차트에서 꺾은 선형 차트 작업. 문제점이 있으면 측정 제목을 레이블로 표시 할 수 있습니다. 나는 다른 측정을 할 것이고, 지금은 한 번에 하나의 측정만을 보여줄 수 있습니다. T_Badende_per_Time :구글 차트 쇼 제목 레이블로

enter image description here

enter image description here

이것은 내가 제목을 표시하기 위해 routine.value를 원하는 볼 수 있듯이, 시각화입니다 : 여기

는 데이터베이스의 SQL 쿼리 및 출력

코드 :

<?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> 

답변

3

당신이 측정 유형 당 하나 개의 라인을 원하는 경우에,

[...] 
$vAxis = false; 
while($r = mysql_fetch_assoc($sth)) { 
    // this will overwrite itself, it looks to me that in your case it doesn't matter 
    $vAxis = $r["title"]; 
    [...] 

과 : 당신은 이미 라인 73 (vAxis: {title: "Values"})에서 설정하지만, PHP에서 변수로 설정해야합니다 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); 
} 
+0

감사합니다, SQL 쿼리 큰했다. 그러나 문제가 여기에 보이는 데 : https://www.dropbox.com/s/1znyjx5zs0qtr2d/saved.png SQL squery는 10 진수 값을 제거하고 정수로 만듭니다. 또 다른 질문은 Google 차트가 십진수를 지원하는지 아니면 문자열을 점으로 대체해야합니까? 그렇다면 어떻게 수행 될까요? 도움 주셔서 감사합니다, 정말로 그것을 appriciete. – user3270211

+0

이상한 행동입니다. 'SUM'이 '0'때문에 데이터 필드를 정수로 타입 변환하는지 궁금합니다. 0을 데이터 열과 동일한 데이터 유형으로 형변환하십시오 (예 :'CAST (0 AS DECIMAL (18, 2))'). – asgallant

+0

고마워, 나는 이것을 시도하고 완벽하게 작동합니다. 나에게 보여준 SQL 쿼리에서 구현하는 방법을 보여 주시겠습니까? https://www.dropbox.com/s/bg5jq7hmz6wehm2/replace.png 나는 당신의 모든 시간과 에너지에 정말로 감사하고 있습니다. – user3270211

0

글쎄, vAxis.title을 설정해야합니다. 다음 출력이

vAxis: {title: "<?=$vAxis ?: "default title"; ?>"}