从不同的日子获取数据

时间:2011-08-14 10:57:18

标签: php mysql charts

我的表“用户”中有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:[]

如何获得?

2 个答案:

答案 0 :(得分:0)

如果需要使用图形生成img,可以使用pChart2,一个生成图形的php库: 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" />';

答案 1 :(得分: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)?>]
}]