tp5结合echart生成线型图和柱型图

1.静态页面处理

 <div id="statistics" style="height: 400px;"></div>
<div class="hDiv">
                <div class="hDivBox">
                    <table cellspacing="0" cellpadding="0">
                        <thead>
                        <tr>
                            <th class="sign" axis="col0">
                                <div style="width: 24px;"><i class="ico-check"></i></div>
                            </th>
                            <th align="center" abbr="article_title" axis="col3" class="">
                                <div style="text-align: center; width: 120px;" class="">时间</div>
                            </th>
                            <th align="center" abbr="ac_id" axis="col4" class="">
                                <div style="text-align: center; width: 100px;" class="">订单数</div>
                            </th>
                            <th align="center" abbr="article_show" axis="col5" class="">
                                <div style="text-align: center; width: 100px;" class="">销售总额</div>
                            </th>
                            <th align="center" abbr="article_time" axis="col6" class="">
                                <div style="text-align: center; width: 100px;" class="">客单价</div>
                            </th>
                            <th align="center" axis="col1" class="handle">
                                <div style="text-align: center; width: 150px;">操作</div>
                            </th>
                        </tr>
                        </thead>
                    </table>
                </div>
            </div>
<script src="__PUBLIC__/echart/echarts.min.js" type="text/javascript"></script>
<script src="__PUBLIC__/echart/macarons.js"></script>
<script src="__PUBLIC__/echart/china.js"></script>

2.js内容

<script type="text/javascript">
    var res = {$result};
    var myChart = echarts.init(document.getElementById('statistics'),'macarons');
    option = {
        tooltip : {
            trigger: 'axis'
        },
        toolbox: {
            show : true,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType: {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : true,
        legend: {
            data:['交易金额','订单数','客单价']
        },
        xAxis : [
            {
                type : 'category',
                data : res.time
            }
        ],
        yAxis : [
            {
                type : 'value',
                name : '金额',
                axisLabel : {
                    formatter: '{value} ¥'
                }
            },
            {
                type : 'value',
                name : '客单价',
                axisLabel : {
                    formatter: '{value} ¥'
                }
            }
        ],
        series : [
            {
                name:'交易金额',
                type:'bar',
                data:res.amount
            },
            {
                name:'订单数',
                type:'bar',
                data:res.order
            },
            {
                name:'客单价',
                type:'line',
                yAxisIndex: 1,
                data:res.sign
            }
        ]
    };
    myChart.setOption(option);
    $(document).ready(function(){
        // 表格行点击选中切换
        $('#flexigrid > table>tbody >tr').click(function(){
            $(this).toggleClass('trSelected');
        });
        // 点击刷新数据
        $('.fa-refresh').click(function(){
            location.href = location.href;
        });

</script>

3.php从数据库获取数据,并进行处理

  $sql .= " where create_time>$this->begin and create_time<$this->end AND status>0 group by gap ";
        $res = Db::query($sql);//订单数,交易额
        foreach ($res as $val){
            $arr[$val['gap']] = $val['tnum'];
            $brr[$val['gap']] = $val['amount'];
        }

        for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
            $tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
            $tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
            $tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);
            $order_arr[] = $tmp_num;
            $amount_arr[] = $tmp_amount;
            $sign_arr[] = $tmp_sign;
            $date = date('Y-m-d',$i);
            $list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60));
            $day[] = $date;
        }
        rsort($list);
        $this->assign('list',$list);
        $result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day);
        $this->assign('result',json_encode($result));
        return $this->fetch();

4.最终生成的图形
这里写图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值