2017-09-29 15 views
0

JSONオブジェクトからGoogleのカレンダーのグラフAPIの行を作成する方法: enter image description here次のように私は私のコンソールでJSONオブジェクトを受け取っ

をし、私はGoogleのカレンダーにこれを渡すように、このJSONオブジェクトを変換したいですグラフAPIのaddRows()関数。

次のように私のスクリプトは次のとおりです。

function drawChart() { 
    var dataTable = new google.visualization.DataTable(); 
    dataTable.addColumn({ type: 'date', id: 'Date' }); 
    dataTable.addColumn({ type: 'number', id: 'Won/Loss' }); 
    dataTable.addRows([ 
     //HERE TO ADD DATA LIKE [new Date(2017, 09, 19),9],[new Date(2017, 09, 20),9] 
    ]); 

    var chart = new google.visualization.Calendar(document.getElementById('calendar_basic')); 

    var options = { 
    title: "Daily hours utilization", 
    height: 350, 
    }; 

    chart.draw(dataTable, options); 

だから、基本的に、私は

{today_date: "2017年9月19日"、時間: "9"}変換したい

から

[新しい日付(2017、9、1 9)、9]、

と出力の代わりに警告の

答えて

1

本来、日付文字列を日付オブジェクトに変換しようとしています。

これを達成する1つの方法は、jsonデータ構造をループしてそれから行配列を作成することです。このループが終了したら、行配列をaddRowsメソッドに渡すことができます。 https://jsfiddle.net/JParkinson1991/aukwxo6L/

あなたのJSONデータ構造を前提としますが、あなたが私のテストデータから行のアレイの出力を見ることができるように、私は少しjsfiddleを作成している

//Store processed rows in this array 
$rows = []; 

//Sample json data structure (pull yours from the source) 
$jsonData = [ 
    { 
    today_date: "2017-09-19", 
    hours: "9" 
    }, 
    { 
    today_date: "2017-09-20", 
    hours: "9" 
    }, 
]; 

//Loop over every object within the json data, parsing the date string and 
//creating a date object from it. 
//Add the date object and the hours field to the rows array 
$jsonData.forEach(function($data){ 
    $dateElements = $data.today_date.split("-"); 
    $rows.push([ 
     new Date($dateElements[0], $dateElements[1], $dateElements[2]), 
     $data.hours 
    ]); 
}); 

//Using your code above, add the rows 
var dataTable = new google.visualization.DataTable(); 
dataTable.addColumn({ type: 'date', id: 'Date' }); 
dataTable.addColumn({ type: 'number', id: 'Won/Loss' }); 
dataTable.addRows($rows); 

$ jsonDataという変数に格納されます

希望します。


EDIT:Abhijit Kumbhar

から、あなたの指針の助けを借りて、私は私のコードを編集し、それが今で正常に動作し、以下のように、私はいくつかの変更は、彼らは以下のとおりです。

最終的な解決策:

function drawChart() { 
    var rows =[]; 

    jsDailyCount.forEach(function(data){ 
     var dateElements = data.today_date.split("-"); 
     rows.push([ 
      new Date(dateElements[0], dateElements[1], dateElements[2]), 
      parseFloat(data.hours) 
     ]); 
    }); 

    var dataTable = new google.visualization.DataTable(); 
    dataTable.addColumn({ type: 'date', id: 'Date' }); 
    dataTable.addColumn({ type: 'number', id: 'hours' }); 
    dataTable.addRows(rows); 

    var chart = new google.visualization.Calendar(document.getElementById('calendar_basic')); 
    var options = { 
     title: "Daily hours utilization", 
     height: 350, 
    }; 
    chart.draw(dataTable, options); 
+0

@Abhijit Kumbhar私はあなたの編集を微調整して、最終的な解決策から不要なコードを削除しました。それを見て価値がある。 – JParkinson1991

0

var s = {today_date: "2017-09-19", hours: "9"}; 
 
var m = s.hours; 
 
var d = s.today_date.split("-"); 
 
alert(d[0] + " " + d[1] + " " + d[2] + " " + m);
enter image description hereのような、あなたのスクリプトでこれらの値を使用します。

関連する問題