我們目前正在使用客戶溫度計在完成支持票后調查我們的客戶。不幸的是,customerthermometers 小部件看起來很惡心。所以我目前正在嘗試使用 Chart.js 創建一個圖表,并使用 PHP 從 customerthermometers 的 API 動態獲取數據。PHP-array ($dataPoints) 中的所有部分都在圖表中考慮在內,除了“數據”,我真的不知道如何進一步進行。<?php$gold = file_get_contents( 'https://app.customerthermometer.com/api.php?apiKey=<api_key>&getMethod=getNumResponsesValue&temperatureID=1' );$green = file_get_contents( 'https://app.customerthermometer.com/api.php?apiKey=<api_key>&getMethod=getNumResponsesValue&temperatureID=2' );$yellow = file_get_contents( 'https://app.customerthermometer.com/api.php?apiKey=<api_key>&getMethod=getNumResponsesValue&temperatureID=3' );$red = file_get_contents( 'https://app.customerthermometer.com/api.php?apiKey=<api_key>&getMethod=getNumResponsesValue&temperatureID=4' );$dataPoints = array( array( "label" => "Gold Star" , "data" => $gold , "backgroundColor" => "rgba(255,215,0,1)"), array( "label" => "Green Light" , "data" => $green , "backgroundColor" => "rgba(0,128,0,1)"), array( "label" => "Yellow Light" , "data" => $yellow , "backgroundColor" => "rgba(255,255,0,1)"), array( "label" => "Red Light" , "data" => $red , "backgroundColor" => "rgba(255,0,0,1)" ));?><html><head><meta charset="utf-8"><title>Chart Test</title></head><body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script src="../assets/chart28/dist/Chart.js"></script> <canvas id="myChart" width="100" height="20"></canvas><script>var ctx = document.getElementById('myChart');var myChart = new Chart(ctx, { type: 'bar', data: { labels: ['Gold Star', 'Green Light', 'Yellow Light', 'Red Light'], datasets: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?> }, options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } }});</script> <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?></body></html>
1 回答
慕森王
TA貢獻1777條經驗 獲得超3個贊
的數據屬性$dataPoints應該是一個數組。如果您獲得單個值,則必須對其進行轉換
...
$dataPoints = array(
array( "label" => "Gold Star" , "data" => array($gold) , "backgroundColor" => "rgba(255,215,0,1)"),
array( "label" => "Green Light" , "data" => array($green) , "backgroundColor" => "rgba(0,128,0,1)"),
array( "label" => "Yellow Light" , "data" => array($yellow) , "backgroundColor" => "rgba(255,255,0,1)"),
array( "label" => "Red Light" , "data" => array($red) , "backgroundColor" => "rgba(255,0,0,1)" )
);
...
- 1 回答
- 0 關注
- 198 瀏覽
添加回答
舉報
0/150
提交
取消
