如何在图表中显示每天的总订单金额 [英] How to Display the Total Order Amount per Day in a Chart

查看:187
本文介绍了如何在图表中显示每天的总订单金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法得到这个工作,我需要在图表上显示每天的总订单金额。



我有Open Flash图表以下代码:






 <?php 

//数据库连接的设置
include_once($ root_folder_path。includes / common.php);
include_once(./ admin_common.php);
include_once(./ ofc_chart_library / open-flash-chart.php);

$ sql =SELECT * from orders ORDER BY 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']);
}

/ *
*创建一个标题对象并将文本设置为当前月份。
* /
$ title = new title(''每月销售统计'。date(F Y)。'(美元)');
$ title-> set_style({font-size:18px; font-family:Calibri; color:#808000; text-align:center;});

//创建我们的条形图
$ bar = new bar_filled('#f99bd6','#ee0099');
$ bar-> set_values($ data);

//创建一个新的图表对象
$ chart = new open_flash_chart();

//将标题添加到图表:
$ chart-> set_title($ title);
$ chart-> set_bg_colour(#FFFFFF);

//在图表上显示bar对象
$ chart-> add_element($ bar);

/ *
*创建Y轴对象
* /
$ y_axis = new y_axis();
$ y_axis-> set_range(0,12000,1000);

//将y轴对象添加到图表
$ chart-> add_y_axis($ y_axis);

echo $ chart-> toPrettyString();
?>






现在上面的代码显示正常,这里是快照:



我想要什么



这些日期是如何存储在我的数据库中,这是一个日期时间类型:


2008-12-30 00:06:24

2009-02-03 01:57: 17

2009-02-03 01:58:27

2009-05-03 01:58:48

2009-06-03 02:00 :31

2009-07-03 02:01:47

2009-07-03 02:02:31

2009-07-04 14: 21:18

2009-07-04 14:21:36

2009-07-04 14:22:18

2009-07-04 14 :23:29

2009-07-04 14:24:24


我试过下面的代码在X轴中显示日期:






 <?php 

include_once'php-ofc-library / open-flash-chart.php'

//连接到MySQL数据库
$ con = mysql_connect(localhost,root,password);
if(!$ con)die('Could not connect:'。mysql_error());
$ db_selected = mysql_select_db(cart,$ con);
if(!db_selected)die(Could not find the database:。mysql_error());

$ sql =SELECT * from orders ORDER BY order_placed_date;
if(!$ sql)die('Invalid query:'。mysql_error());

$ result = mysql_query($ sql);

$ amountData = array();


while($ row = mysql_fetch_array($ result))
{
$ amountData [] = intval($ row ['order_total']);
}

$ chart = new open_flash_chart();
$ chart-> set_title(new title('Open Flash Chart Downloads'));

//创建条形图
$ bar = new bar_filled('#df95c3','#f34db7');
$ bar-> set_values($ amountData);

//创建一个新的图表对象
$ chart = new open_flash_chart();

/ *
*创建一个标题对象并将文本设置为当前月份。
* /
$ title = new title(''每月销售统计'。date(F Y)。'(美元)');
$ title-> set_style({font-size:18px; font-family:Calibri; color:#808000; text-align:center;});

//将标题添加到图表:
$ chart-> set_title($ title);
$ chart-> set_bg_colour(#FFFFFF);


//将bar对象添加到图表
$ chart-> add_element($ bar);


//
//创建X Axis对象
//
$ x_axis = new x_axis();
//网格线并且每10分钟一次
$ x_axis-> set_range(
mktime(0,0,0,1,1,date('Y')),//< ; - min ==今年1月1日
mktime(0,0,0,1,31,date('Y'))//< - max == 31st Jan, $ b);
//显示每天的ticks和网格线:
$ x_axis-> set_steps(86400);

$ labels = new x_axis_labels();
//告诉标签将数字渲染为日期:
$ labels-> text('#date:d#');
//每天生成标签
$ labels-> set_steps(86400);
//只显示每隔一个标签(每隔一天)
$ labels-> visible_steps(1);


//最后将标签定义附加到x轴
$ x_axis-> set_labels($ labels);


$ y_axis = new y_axis();
$ y_axis-> set_range(0,3000,200);

//在图表上显示轴
$ chart-> set_x_axis($ x_axis);
$ chart-> add_y_axis($ y_axis);

echo $ chart-> toPrettyString();


?>






这是我得到的:



金额正在显示,但有



我如何解决这个问题,所以从数据库获取订单日期,然后根据



=============================================================================================== ===================================

下面是






 < ?php 

include_once'php-ofc-library / open-flash-chart.php';

//连接到MySQL数据库
$ con = mysql_connect(localhost,root,password);
if(!$ con)die('Could not connect:'。mysql_error());
$ db_selected = mysql_select_db(cart,$ con);
if(!db_selected)die(Could not find the database:。mysql_error());

$ sql =SELECT * from orders ORDER BY order_placed_date;
if(!$ sql)die('Invalid query:'。mysql_error());

$ result = mysql_query($ sql);

$ dateData = array();
$ TotalAmountData = array();

while($ row = mysql_fetch_array($ result))
{
$ TotalAmountData [] = intval($ row ['order_total']);
$ dateData [] = $ row ['order_placed_date'];

}

print'< pre>';
Print_r($ TotalAmountData);
print_r($ dateData);
print'< / pre>'; [/ code]

?>






显示以下JSON数据:

 数组//这是每天总订单金额

[0] => 2499
[1] => 199
[2] => 449
[3] => 299
[4] => 359
[5] > 279
[6] => 1359
[7] => 5099
[8] => 2621
[9] => 2169
[10] => 2249
[11] => 5509

数组//这是放置订单的日期时间

[0] => 2008-12-30 00:06:24
[1] => 2009-02-03 01:57:17
[2] => 2009-02- 03 01:58:27
[3] => 2009-05-03 01:58:48
[4] => 2009-06-03 02:00:31
[5] => 2009-07-03 02:01:47
[6] => 2009-07-03 02:02:31
[7] => 2009-07- 04 14:21:18
[8] => 2009-07-04 14:21:36
[9] => 2009-07-04 14:22:18
[10] => 2009-07-04 14:23:29
[11] => 2009-07-04 14:24:24

我试图通过从日期中删除时间来选择日期:

  SELECT DATE_FORMAT(order_placed_date,'%m-%d-%Y'),order_total FROM orders 

但上面的代码显示了JSON数据的空值



更新:
任何人都可以帮助?

解决方案

您的值是真的每天总计吗?



您的第一张图表的问题是,您没有将您的值每天计入总计 - 打开Flash图表不会为你做这个。您可以将其作为SQL查询的一部分(例如,使用 DATE() 将数据时间转换为日期),或者通过解析和重新格式化日期,并将同一天的值相加在一起,使用PHP。请注意,根据您处理x轴的方式,您可能还需要为没有任何订单的天添加0个条目。



您的第二个图表的问题是,重新设置每秒一个元素,但您的数据应该是每天。这里的重要事情是了解折线图和散点图之间的差异图表。折线图使用一维数据点数组绘制,而散点图使用坐标绘图。


I cant seem to get this working, i need to display the Total Order Amount per day on the Chart.

I got the Open Flash chart to work by the following code:


<?php

// Settings for Database Connection
include_once($root_folder_path . "includes/common.php");
include_once("./admin_common.php");
include_once("./ofc_chart_library/open-flash-chart.php");

$sql = "SELECT * from orders ORDER BY 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 a Y Axis object
*/
$y_axis = new y_axis();
$y_axis->set_range(0, 12000, 1000);

// Add the y-axis object to the chart
$chart->add_y_axis($y_axis);

echo $chart->toPrettyString();
?>


Now the above code displayed fine, here is the snapshot:

What i want is to display the Date on the X-Axis, taking the date values from my MySQL Database.

Here are how the dates are stored in my database, which is a DateTime Type:

2008-12-30 00:06:24
2009-02-03 01:57:17
2009-02-03 01:58:27
2009-05-03 01:58:48
2009-06-03 02:00:31
2009-07-03 02:01:47
2009-07-03 02:02:31
2009-07-04 14:21:18
2009-07-04 14:21:36
2009-07-04 14:22:18
2009-07-04 14:23:29
2009-07-04 14:24:24

I tried the following code to display the Date in the X-Axis:


<?php

include_once 'php-ofc-library/open-flash-chart.php';

//Connect to MySQL Database
$con = mysql_connect("localhost", "root", "password");
    if (!$con) die('Could not connect : ' . mysql_error());
$db_selected = mysql_select_db("cart",$con);
    if (!db_selected) die ("Could not find the database: " . mysql_error());

$sql = "SELECT * from orders ORDER BY order_placed_date";
    if (!$sql) die('Invalid query: ' . mysql_error());

$result = mysql_query($sql);

$amountData = array();


while($row = mysql_fetch_array($result))
{
  $amountData[] = intval($row['order_total']); 
}

$chart = new open_flash_chart();
$chart->set_title(new title('Open Flash Chart Downloads'));

//Make Bar Chart
$bar = new bar_filled('#df95c3', '#f34db7');
$bar->set_values($amountData);

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

/*
 *  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;}" );

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


// Add the bar object to the chart
$chart->add_element($bar);


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

$labels = new x_axis_labels();
// tell the labels to render the number as a date:
$labels->text('#date:d#');
// 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_axis->set_labels($labels);


$y_axis = new y_axis();
$y_axis->set_range(0, 3000, 200);

// Display the Axis on the Chart
$chart->set_x_axis($x_axis);
$chart->add_y_axis($y_axis);

echo $chart->toPrettyString();


?>


Here is what i got:

the Amount is being shown, but there are all in one line at the 0 x-axis.

How can I solve this problem, so get the Order Dates from the Database and then display them on the Chart according to the date the order was placed.

============================================================

Below is the JSON data for both Order Totals and the Date they were placed.


<?php

include_once 'php-ofc-library/open-flash-chart.php';

//Connect to MySQL Database
$con = mysql_connect("localhost", "root", "password");
    if (!$con) die('Could not connect : ' . mysql_error());
$db_selected = mysql_select_db("cart",$con);
    if (!db_selected) die ("Could not find the database: " . mysql_error());

$sql = "SELECT * from orders ORDER BY order_placed_date";
    if (!$sql) die('Invalid query: ' . mysql_error());

$result = mysql_query($sql);

$dateData = array();
$TotalAmountData = array();

while($row = mysql_fetch_array($result))
{
  $TotalAmountData [] = intval($row['order_total']);
  $dateData[] = $row['order_placed_date'];

}

print '<pre>';
print_r( $TotalAmountData );
print_r( $dateData );
print '</pre>';[/code]

?>


This shows the following JSON data:

Array  // This is the Total Order Amount Per Day
(
    [0] => 2499
    [1] => 199
    [2] => 449
    [3] => 299
    [4] => 359
    [5] => 279
    [6] => 1359
    [7] => 5099
    [8] => 2621
    [9] => 2169
    [10] => 2249
    [11] => 5509
)
Array // This is the DateTime on which the orders where placed
(
    [0] => 2008-12-30 00:06:24
    [1] => 2009-02-03 01:57:17
    [2] => 2009-02-03 01:58:27
    [3] => 2009-05-03 01:58:48
    [4] => 2009-06-03 02:00:31
    [5] => 2009-07-03 02:01:47
    [6] => 2009-07-03 02:02:31
    [7] => 2009-07-04 14:21:18
    [8] => 2009-07-04 14:21:36
    [9] => 2009-07-04 14:22:18
    [10] => 2009-07-04 14:23:29
    [11] => 2009-07-04 14:24:24
)

I tried to select the Date by removing the Time from the Date:

SELECT DATE_FORMAT(order_placed_date, '%m-%d-%Y'), order_total FROM orders

But the above code showed Empty values for JSON Data

Update: Can anyone help ?

解决方案

Are your values really totals per day? It looks like it's the total per order, with the date of the order.

The problem with your first graph is that you're not bucketing your values to totals per day - Open Flash Chart won't do this for you. You could do this as part of your SQL query (eg use DATE() to convert the datetimes to dates), or with PHP by parsing and reformatting the dates, and adding together values for the same day. Note that depending on how you handle the x-axis you might also need to add 0 entries for days without any orders.

The problem with your second graph is that you're setting one element per second, but your data should be per day. The important thing here is to understand the difference between line charts and scatter charts in Open Flash Chart. Line charts are plotted using a one-dimensional array of data points, whereas scatter charts are plotted with co-ordinates.

这篇关于如何在图表中显示每天的总订单金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆