我在条形图(Morris.bar)中无法获取数据(来自mysql)。我在数组中得到了我想要的结果,如下所示:
Array
(
[0] => Array
(
[total] => 200.00
[year] => 2015
[month] => 2
)
[1] => Array
(
[total] => 500.00
[year] => 2015
[month] => 3
)
[2] => Array
(
[total] => 500.00
[year] => 2016
[month] => 2
)
[3] => Array
(
[total] => 600.00
[year] => 2016
[month] => 3
)
[4] => Array
(
[total] => 600.00
[year] => 2016
[month] => 4
)
)
我想在这样的条形图中获取数据(始终显示所有月份,如果没有值或空值,则回显0(零))
{month: "<?php echo $months[0]; ?>", <?php echo $last_year; ?>:VALUE_THAT_BELONGS_TO_JAN_2015(LAST_YEAR), <?php echo $this_year; ?>: VALUE_THAT_BELONGS_TO_JAN_2016 (CURRENT YEAR)},
现在完成脚本:
<?php $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"); ?>
Morris.Bar({
element: 'graph_invoices_month',
data : [
{month: "<?php echo $months[0]; ?>", <?php echo $last_year; ?>: 1000, <?php echo $this_year; ?>: 1100},
{month: "<?php echo $months[1]; ?>", <?php echo $last_year; ?>: 3351, <?php echo $this_year; ?>: 629},
{month: "<?php echo $months[2]; ?>", <?php echo $last_year; ?>: 3269, <?php echo $this_year; ?>: 618},
{month: "<?php echo $months[3]; ?>", <?php echo $last_year; ?>: 3246, <?php echo $this_year; ?>: 661},
{month: "<?php echo $months[4]; ?>", <?php echo $last_year; ?>: 3257, <?php echo $this_year; ?>: 667},
{month: "<?php echo $months[5]; ?>", <?php echo $last_year; ?>: 3248, <?php echo $this_year; ?>: 627},
{month: "<?php echo $months[6]; ?>", <?php echo $last_year; ?>: 3171, <?php echo $this_year; ?>: 660},
{month: "<?php echo $months[7]; ?>", <?php echo $last_year; ?>: 3171, <?php echo $this_year; ?>: 676},
{month: "<?php echo $months[8]; ?>", <?php echo $last_year; ?>: 3201, <?php echo $this_year; ?>: 656},
{month: "<?php echo $months[9]; ?>", <?php echo $last_year; ?>: 3215, <?php echo $this_year; ?>: 622},
{month: "<?php echo $months[10]; ?>", <?php echo $last_year; ?>: 3215, <?php echo $this_year; ?>: 622},
{month: "<?php echo $months[11]; ?>", <?php echo $last_year; ?>: 3215, <?php echo $this_year; ?>: 622}
],
xkey: 'month',
ykeys: ['<?php echo $last_year; ?>', '<?php echo $this_year; ?>'],
labels: ['<?php echo $last_year; ?>', '<?php echo $this_year; ?>'],
resize: true,
barColors: ['<?php echo $this_year_color; ?>', '<?php echo $last_year_color; ?>'],
xLabelAngle: 60
});
进一步澄清:这就是我之后:
month: Jan 2015 = 0 : Jan 2016 = 0
month: Feb 2015 = 200 : Feb 2016 = 500
month: Mar 2015 = 500 : Mar 2016 = 600
month: Apr 2015 = 0 : Apr 2016 = 600
month: May 2015 = 0 : May 2016 = 0
month: Jun 2015 = 0 : Jun 2016 = 0
month: Jul 2015 = 0 : Jul 2016 = 0
month: Aug 2015 = 0 : Aug 2016 = 0
month: Sep 2015 = 0 : Sep 2016 = 0
month: Oct 2015 = 0 : Oct 2016 = 0
month: Nov 2015 = 0 : Nov 2016 = 0
month: Dec 2015 = 0 : Dec 2016 = 0
希望有人可以对此有所了解,因为它让我疯了!在此先感谢您的帮助!
答案 0 :(得分:1)
Phewww,我设法回答了我自己的问题。如果其他人需要类似的解决方案,那么这就是我的代码:
<?php $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"); ?>
Morris.Bar({
element: 'graph_invoices_month',
data : [
<?php foreach($months as $month => $value){ ?>
{month: "<?php echo $value; ?>",
<?php echo $last_year; ?>:
<?php $total_revenue_last_year = 0; ?>
<?php foreach($total_revenue_by_months as $total_revenue_by_month){ ?>
<?php if (($month + 1) == $total_revenue_by_month['month'] && $last_year == $total_revenue_by_month['year']) { ?>
<?php $total_revenue_last_year = $total_revenue_by_month['total_month_revenue']; ?>
<?php } ?>
<?php } ?>
<?php if (isset($total_revenue_last_year)){
echo $total_revenue_last_year . ",";
} else { echo "0,"; } ?>
<?php echo $this_year; ?>:
<?php $total_revenue_this_year = 0; ?>
<?php foreach($total_revenue_by_months as $total_revenue_by_month){ ?>
<?php if (($month + 1) == $total_revenue_by_month['month'] && $this_year == $total_revenue_by_month['year']) { ?>
<?php $total_revenue_this_year = $total_revenue_by_month['total_month_revenue']; ?>
<?php } ?>
<?php } ?>
<?php if (isset($total_revenue_this_year)){
echo $total_revenue_this_year . "},";
} else { echo "0},"; } ?>
<?php } ?>
],
xkey: 'month',
ykeys: ['<?php echo $last_year; ?>', '<?php echo $this_year; ?>'],
labels: ['<?php echo $last_year; ?>', '<?php echo $this_year; ?>'],
resize: true,
barColors: ['<?php echo $this_year_color; ?>', '<?php echo $last_year_color; ?>'],
xLabelAngle: 60
});