flot を使用して、MySQL データベースから取得したデータをプロットしようとしています。私はユーザーのログイン訪問をログに記録しており、特定の月の 1 日あたりの訪問数を取得する SQL 関数 getStats($day) を持っています。これを適切に行う方法の例をオンラインでいくつか読んだことがありますが、何らかの理由で、JavaScriptファイルの配列データをグラフ化しようとすると、「空になります-> 'data:」. 以下は私のコードです。私は CI フレームワークを使用しているため、私のコードについて質問がある場合は、それが原因である可能性が最も高いです。ハードコーディングされたデータがあり、正常に動作します。現在、データベースでflotを使用することについてはあまりありません。
モデル---> metrics.php
function showUsers() {
//get the number of days in the current month and the first day
$num_days = cal_days_in_month(CAL_GREGORIAN, date(m), date(Y));
$first_day = strtotime(date('Y').'-'.date('m').'-01');
//iterate through all of the days
while( $i < $num_days ) {
//get the user visits for each day of the month
$log = $this->db->getStats($first_day);
//add +1 day to the current day
$first_day += 86400;
$flot_visits[] = '['.($first_day*1000).','.$log->fields['total'].']';
$i++;
}
//format in acceptable format for flot
$content['visits'] = '['.implode(',',$flot_visits).']';
//load the view and pass the array
$this->load->vars($content);
$this->load->view('metrics/user_metrics', $content);
}
表示 ---> user_metrics.php
<div id ="visits_graph" style="width:600px; height:300px"></div>
Javascript ---> user_metrics.js
function metrics() {
$.ajax({
type: "POST",
url: pathurl + "metrics/showUsers",
data: "",
success: function(data) {
graph_visits();
}
});
}
function graph_visits() {
$.plot($('#visits_graph'), [
{ label: 'Visits', data: <?php echo $visits ?> }
], {
xaxis: {
mode: "time",
timeformat: "%m/%d/%y"
},
lines: { show: true },
points: { show: true },
grid: { backgroundColor: #fffaff' }
});
}