douyong8801 2009-07-15 20:51
浏览 7
已采纳

任何人都可以帮我PHP图表?

I am trying to display Total Order Amount Per Day in the Present Month.

$sql = "SELECT date(order_placed_date), 
               COUNT(order_id), 
               SUM(order_total) AS order_total 
        FROM orders 
        WHERE order_placed_date>=date_sub(current_date, INTERVAL 31 DAY) 
        GROUP BY date(order_placed_date)";

if (!$sql) die('Invalid query: ' . mysql_error());

$result = mysql_query($sql);
$data = array();
while($row = mysql_fetch_array($result))
{
  $data[] = intval($row['order_total']);
}

/*
 *  Create a title object and set the text to present month.
 */
$title = new title('Monthly Sales Statistics for '.date("F Y").' (US Dollar)');
$title->set_style( "{font-size: 18px; 
                     font-family: Calibri; 
                     color: #808000; 
                     text-align: center;}" );

//Make our Bar Chart
$bar = new bar_filled('#f99bd6', '#ee0099');
$bar->set_values($data);

//Create a new Chart object
$chart = new open_flash_chart();

// add the title to the chart:
$chart->set_title( $title );
$chart->set_bg_colour("#FFFFFF");

// Display the bar object on the chart
$chart->add_element($bar);

// create an X Axis object
$x = new x_axis();

// grid line and tick every 10
$x->set_range(
    mktime(0, 0, 0, 7, 1, date('Y')),    // <-- min == 1st Jan, this year
    mktime(0, 0, 0, 7, 31, date('Y'))    // <-- max == 31st Jan, this year
    );

// show ticks and grid lines for every day:
$x->set_steps(86400);

$labels = new x_axis_labels();

// tell the labels to render the number as a date:
$labels->text('#date:j #');

// generate labels for every day
$labels->set_steps(86400);

// only display every other label (every other day)
$labels->visible_steps(1);

// finally attach the label definition to the x axis
$x->set_labels($labels);


//Create a Y Axis object
$y_axis = new y_axis();
$y_axis->set_range(1, 15000, 1000);

//Add the y-axis object to the chart
$chart->add_y_axis($y_axis);
$chart->set_x_axis( $x );
echo $chart->toPrettyString();
?>

This is what i am getting:

Can anyone tell me where is the problem and how to correct it ?

Here is the print_r($data) Output:

Here is the output from print_r($data)

Array
(
    [0] => 7721
    [1] => 2169
    [2] => 2249
    [3] => 5509
    [4] => 8729
    [5] => 5899
    [6] => 1793
    [7] => 11307
    [8] => 0
    [9] => 0
    [10] => 0
    [11] => 0
    [12] => 0
    [13] => 0
    [14] => 0
    [15] => 0
    [16] => 0
    [17] => 0
    [18] => 0
    [19] => 0
    [20] => 0
    [21] => 0
    [22] => 0
    [23] => 0
    [24] => 0
    [25] => 0
    [26] => 0
    [27] => 0
    [28] => 0
    [29] => 0
    [30] => 0
)

I changed the while loop :

for( $i=1; $i<32; $i++ )
{
    $row = mysql_fetch_array($result);    
    $data[] = intval($row['order_total']);
}

Here is the updated Chart when using the above for loop: alt text http://static.zooomr.com/images/7807811_1b3c7c7274_o.png

  • 写回答

1条回答 默认 最新

  • dongming4994 2009-07-15 21:52
    关注

    The problem is, like I said in the other thread, that you've got 31 elements of data (one per day), but this code is creating one x-axis element for every 2,678,400 seconds of those 31 days:

    // grid line and tick every 10
    $x->set_range(
        mktime(0, 0, 0, 7, 1, date('Y')),    // <-- min == 1st
    Jan, this year
        mktime(0, 0, 0, 7, 31, date('Y'))    // <-- max == 31st
    Jan, this year
        );
    
    // show ticks and grid lines for every day: 
    $x->set_steps(86400);
    

    If you get rid of those lines you'll be it'll work a lot better.

    You then need to provide an array of labels for the x-axis elements, and set it using $x->set_labels_from_array($array_of_labels);

    There's an example of how to do that here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?