2011-08-14 43 views
1

我在我的表「用戶」中有7列。列是這樣的:從不同的日子獲取數據

ac0 // Represents clicks made today 
ac1 // Represents clicks made from ac0 
ac2 // Represents clicks made from ac1 
ac3 // Represents clicks made from ac2 
ac4 // Represents clicks made from ac3 
ac5 // Represents clicks made from ac4 
ac6 // Represents clicks made from ac5 

我有一個圖表中顯示這一點,那就是建立這樣的:

xAxis: { 
categories: ['Today', 'Yesterday', '12-08', '11-08','10-08','09-08','08-08'] 
}, 
series: [{ 
name: 'Clicks', 
data: [1,4,5,2,6,4,6] 
}] 

正如你所看到的,我必須將ac#插入data:[]字段,然後爲每個添加一個日期,以插入categories:[]

如何獲得?

+0

你試過了什麼代碼? –

+0

目前還沒有,因爲我有疑問。 –

+0

如果ac1數據來自昨天或明天,ac2是來自兩天前或兩天後,我不會不知道如何,等等。 –

回答

0

如果你只需要填寫「類別」和「數據」它更容易:

<?php 
    mysql_connect('localhost', 'root', ''); 
    $link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error()); 
    mysql_select_db('test') or die('Could not select database'); 

    // Performing SQL query 
    $query = 'SELECT ac0, ac1, ac2, ac3, ac4, ac5, ac6 FROM users WHERE id = 1'; 
    $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
    { 
     $data = $row; 
    } 

    $days = array('Today', 'Yesterday'); 
    for ($i=2; $i<7;$i++) 
    { 
     $days[$i] = date('d-m', strtotime('-'.$i.' day')); 
    } 
?> 
xAxis: { 
categories: ['<?php echo implode("', '", $days); ?>'] 
}, 
series: [{ 
name: 'Clicks', 
data: [<?php echo implode(',', $data)?>] 
}] 
+0

謝謝!!這工作。雖然,我將$ i + 1更改爲$ i + 0,因爲ac0代表TODAYS點擊次數。再次 謝謝! –

+0

是的,你是對的,你可以刪除「+0」的東西,並留下「$我」。我正在更新答案 –

0

如果需要,以產生具有可用於pChart2,其生成圖表的PHP庫中的圖形一個img: graph generated by pchart

下面是產生上述曲線圖中的代碼:

<?php 
    mysql_connect('localhost', 'root', ''); 

    $link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error()); 

    mysql_select_db('test') or die('Could not select database'); 

    // Performing SQL query 
    $query = 'SELECT ac0, ac1, ac2, ac3, ac4, ac5, ac6 FROM users WHERE id = 1'; 
    $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 


    while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
    { 
     $data = $row; 
    } 

    // Free resultset 
    mysql_free_result($result); 

     // Closing connection 
    mysql_close($link); 

    // include pChart library 
    include("pchart/class/pData.class.php"); 
    include("pchart/class/pDraw.class.php"); 
    include("pchart/class/pImage.class.php"); 

    $MyData = new pData(); 
    // Add point to the graph 
    $MyData->addPoints($data, "clicks"); 

    // Configure Axis labels 
    $MyData->setAxisName(0, "Clicks"); 
    $MyData->addPoints(array('Today', 'Yesterday', '12-08', '11-08', '10-08', '09-08', '08-08'), "Labels"); 
    $MyData->setSerieDescription("Labels", "Days"); 
    $MyData->setAbscissa("Labels"); 

    // Generate bar chart 
    $myPicture = new pImage(700, 230, $MyData); 
    $myPicture->setGraphArea(60, 60, 600, 200); 
    $myPicture->drawScale(array("DrawSubTicks" => TRUE)); 
    $myPicture->setShadow(TRUE, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10)); 
    $myPicture->setFontProperties(array("FontName" => "pchart/fonts/verdana.ttf", "FontSize" => 6)); 
    $myPicture->setShadow(FALSE); 
    $myPicture->drawBarChart(array("DisplayValues" => TRUE, "DisplayColor" => DISPLAY_MANUAL, "DisplayR"=>0, "DisplayG"=>0,"DisplayB"=>0, "Rounded" => TRUE, "Surrounding" => 60)); 

    /* Render the picture (choose the best way) */ 
    $myPicture->render("img/mygraph.png"); 

    echo '<img src="img/mygraph.png" />';