mysql+thinkphp +amcharts 完成图表统计功能

思路:从mysql数据库查询数据,经过thinkphp 后端控制器做逻辑处理,返回给前端,前端调用阿妈charts 插件

1.数据查询:

public function order($time=‘‘,$radio=1){
        if($time== ‘‘){
            $time = Date("Y");
        }
        $bt = $time."-01-01 00:00:00";
        $et = (string)((int)$time+1)."-01-01 00:00:00";
        $sql=‘‘;
        if($radio==1){
            $sql="SELECT right(tt.month,2) as month, IFNULL( we.web, 0 ) as web, IFNULL( wx.weixin, 0 ) as weixin, IFNULL( ad.andriod, 0 ) as andriod , IFNULL( ip.iphone, 0 ) as iphone
            FROM (
            (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH FROM  `order`
            WHERE State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS tt
            LEFT JOIN (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , SUM(  `PayMoney` ) AS web
            FROM  `order`
            WHERE  `OrderSource` =1
            AND State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS we ON tt.month = we.month
            LEFT JOIN (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , SUM(  `PayMoney` ) AS weixin
            FROM  `order`
            WHERE  `OrderSource` =2
            AND State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS wx ON tt.month = wx.month
            LEFT JOIN (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , SUM(  `PayMoney` ) AS andriod
            FROM  `order`
            WHERE  `OrderSource` =3
            AND State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS ad ON tt.month = ad.month
            LEFT JOIN (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , SUM(  `PayMoney` ) AS iphone
            FROM  `order`
            WHERE  `OrderSource` =4
            AND State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS ip ON tt.month = ip.month
            )";
        }else{
            $sql="SELECT right(tt.month,2) as month, IFNULL( we.web, 0 ) as web, IFNULL( wx.weixin, 0 ) as weixin, IFNULL( ad.andriod, 0 ) as andriod, IFNULL( ip.iphone, 0 ) as iphone
            FROM (
            (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH FROM  `order`
            WHERE State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS tt
            LEFT JOIN (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , count(  `id` ) AS web
            FROM  `order`
            WHERE  `OrderSource` =1
            AND State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS we ON tt.month = we.month
            LEFT JOIN (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , count(  `id` ) AS weixin
            FROM  `order`
            WHERE  `OrderSource` =2
            AND State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS wx ON tt.month = wx.month
            LEFT JOIN (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , count(  `id` ) AS andriod
            FROM  `order`
            WHERE  `OrderSource` =3
            AND State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS ad ON tt.month = ad.month
            LEFT JOIN (

            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , count(  `id` ) AS iphone
            FROM  `order`
            WHERE  `OrderSource` =4
            AND State = 2
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS ip ON tt.month = ip.month
            )";
        }
        $m = new Model();//实例化对象模型
        //echo $sql;
        $o = $m->query($sql);
        //dump(json_encode($o));
        $this-> o = json_encode($o);
        $this-> ol = $o;
        $this-> time = $time;
        $this-> radio = $radio;
        $this-> timearr = $temp;
        $this ->display(‘order‘);
        }

    public function recharge($time=‘‘,$radio=1,$year=NULL,$month=NULL){
        $this->meta_title = ‘充值信息统计‘;
        $sql= ‘‘;
        if($radio==1){//按月统计
            if($time== ‘‘){
            $time = Date("Y");
            }
            $bt = $time."-01-01 00:00:00";
            $et = (string)((int)$time+1)."-01-01 00:00:00";
            $sql="SELECT right(tt.month,2) as month, IFNULL( we.usum, 0 ) as usum
            FROM (
            (
            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH FROM  `recharge`
            WHERE PayType in(0,1)
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS tt
            LEFT JOIN (
            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH , sum(  `Money` ) AS usum
            FROM  `recharge`
            WHERE PayType in(0,1)
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS we ON tt.month = we.month
            )";
            $this-> showname = ‘月‘;
        }else{//按天统计
            if(!$year){
                $year = Date("Y");
            }
            $time = Date("Y");
            if(!$month){
                $month = Date("m");
            }
            $bt = $year.‘-‘.$month."-01 00:00:00";
            $et = date(‘Y-m-d H:i:s‘,strtotime(‘+1 month‘,strtotime($bt)));
             $sql="SELECT right(tt.month,2) as month, IFNULL( we.usum, 0 ) as usum
            FROM (
            (
            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m-%d‘ ) AS
            MONTH FROM  `recharge`
            WHERE PayType in(0,1)
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS tt
            LEFT JOIN (
            SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m-%d‘ ) AS
            MONTH , sum(  `Money` ) AS usum
            FROM  `recharge`
            WHERE PayType in(0,1)
            AND AddTime >= ‘$bt‘
            AND AddTime < ‘$et‘
            GROUP BY MONTH
            ) AS we ON tt.month = we.month
            )";
            $this-> showname = ‘天‘;
        }

        $m = new Model();//实例化对象模型
        //echo $sql;
        $o = $m->query($sql);
        //dump($o);
        $this-> o = json_encode($o);
        $this-> ol = $o;
        $this-> time = $time;
        $this-> radio = $radio;
        $this-> year = $year;
        $this-> month = $month;
        $this ->display(‘recharge‘);
    }

用到分组查询,sql 函数 有

A.  right(tt.month,2) as month

B.  IFNULL( we.usum, 0 ) as usum

C.  count(  `id` ) AS unum

D. sum(  `Money` ) AS usum

E.  SELECT DATE_FORMAT(  `AddTime` ,  ‘%Y-%m‘ ) AS
            MONTH FROM  `table`
            WHERE XXXXX
            GROUP BY MONTH(分组group by  直接用select 后面的month即可)
‘%Y-%m‘   是时间格式,DATE_FORMAT  用于时间格式为“2012-01-01 00:01:22”的这种形式。

F.  SELECT FROM_UNIXTIME(  `AddTime` ,  ‘%Y-%m-%d‘) AS
        MONTH , count(  `id` ) AS unum
        FROM  `table`
        WHERE  XXXXX
        GROUP BY MONTH

FROM_UNIXTIME 用于时间戳格式。

分组的思想是先查询出有哪些组,然后在查询出符合的数据,然后用left join 来组合。

2. 前端数据整合

A.  数据格式

给前端的数据格式为JSON:

var chartData = [
                {
                    "month": "01",
                    "web": 2.5,
                    "weixin": 2.5,
                    "andriod": 2.1,
                    "iphone": 1.3
                },
                {
                    "month": "02",
                    "web": 2.6,
                   "weixin": 2.7,
                     "andriod": 2.2,
                    "iphone": 1.3
                                  },
                {
                    "month": "03",
                    "web": 2.5,
                    "weixin": 2.5,
                    "andriod": 2.1,
                    "iphone": 1.3
                },
                {
                    "month": "04",
                    "web": 2.6,
                   "weixin": 2.7,
                     "andriod": 2.2,
                    "iphone": 1.3
                                  },
                {
                    "month": "05",
                    "web": 2.8,
                  "weixin": 2.9,
                      "andriod": 2.4,
                    "iphone": 1.3
                    },
                {
                    "month": "06",
                    "web": 2.8,
                  "weixin": 2.9,
                      "andriod": 2.4,
                    "iphone": 1.3
                   
                },
                {
                    "month": "07",
                    "web": 2.5,
                    "weixin": 2.5,
                    "andriod": 2.1,
                    "iphone": 1.3
                },
                {
                    "month": "08",
                    "web": 2.6,
                   "weixin": 2.7,
                     "andriod": 2.2,
                    "iphone": 1.3
                                  },
                {
                    "month": "09",
                    "web": 2.8,
                  "weixin": 2.9,
                      "andriod": 2.4,
                    "iphone": 1.3
                    },
                {
                    "month": "10",
                    "web": 2.8,
                  "weixin": 2.9,
                      "andriod": 2.4,
                    "iphone": 1.3
                   
                },
                {
                    "month": "11",
                    "web": 2.8,
                  "weixin": 2.9,
                      "andriod": 2.4,
                    "iphone": 1.3
                    },
                {
                    "month": "12",
                    "web": 2.8,
                  "weixin": 2.9,
                      "andriod": 2.4,
                    "iphone": 1.3
                   
                }
            ];

thinkphp  查询出来的数据时数组,用json_encode  转换成JSON 数组

$o = $m->query($sql);

$this-> o = json_encode($o);

$this-> ol = $o;

B.  html  代码(要画图的层)

<div id="chartdiv" style="width: 700px; height: 500px;float:left"></div>

C. 画图

柱状图:

var chart;
            var chartData = <?=$o?>;
                        AmCharts.ready(function () {
                // SERIAL CHART
                chart = new AmCharts.AmSerialChart();
                chart.dataProvider = chartData;
                chart.categoryField = "month";
                chart.plotAreaBorderAlpha = 0.2;

                // AXES
                // category
                var categoryAxis = chart.categoryAxis;
                categoryAxis.gridAlpha = 0.1;
                categoryAxis.axisAlpha = 0;
                categoryAxis.gridPosition = "start";

                // value
                var valueAxis = new AmCharts.ValueAxis();
                valueAxis.stackType = "regular";
                valueAxis.gridAlpha = 0.1;
                valueAxis.axisAlpha = 0;
                chart.addValueAxis(valueAxis);

                // GRAPHS
                // first graph
                var graph = new AmCharts.AmGraph();
                graph.title = "官网";
                graph.labelText = "[[value]]";
                graph.valueField = "web";
                graph.type = "column";
                graph.lineAlpha = 0;
                graph.fillAlphas = 1;
                graph.lineColor = "#ef3051";
                graph.balloonText = "<span style=‘color:#555555;‘>[[category]]</span><br><span style=‘font-size:14px‘>[[title]]:<b>[[value]]</b></span>";
                chart.addGraph(graph);

                // second graph
                graph = new AmCharts.AmGraph();
                graph.title = "微信";
                graph.labelText = "[[value]]";
                graph.valueField = "weixin";
                graph.type = "column";
                graph.lineAlpha = 0;
                graph.fillAlphas = 1;
                graph.lineColor = "#d46724";
                graph.balloonText = "<span style=‘color:#555555;‘>[[category]]</span><br><span style=‘font-size:14px‘>[[title]]:<b>[[value]]</b></span>";
                chart.addGraph(graph);

                // third graph
                graph = new AmCharts.AmGraph();
                graph.title = "安卓";
                graph.labelText = "[[value]]";
                graph.valueField = "andriod";
                graph.type = "column";
                //graph.newStack = true; // this line starts new stack
                graph.lineAlpha = 0;
                graph.fillAlphas = 1;
                graph.lineColor = "#21d277";
                graph.balloonText = "<span style=‘color:#555555;‘>[[category]]</span><br><span style=‘font-size:14px‘>[[title]]:<b>[[value]]</b></span>";
                chart.addGraph(graph);

                // fourth graph
                graph = new AmCharts.AmGraph();
                graph.title = "苹果";
                graph.labelText = "[[value]]";
                graph.valueField = "iphone";
                graph.type = "column";
                graph.lineAlpha = 0;
                graph.fillAlphas = 1;
                graph.lineColor = "#7e90f2";
                graph.balloonText = "<span style=‘color:#555555;‘>[[category]]</span><br><span style=‘font-size:14px‘>[[title]]:<b>[[value]]</b></span>";
                chart.addGraph(graph);

                // LEGEND
                var legend = new AmCharts.AmLegend();
                legend.borderAlpha = 0.2;
                legend.horizontalGap = 10;
                chart.addLegend(legend);
                chart.depth3D = 25;
                chart.angle = 30;

                // WRITE
                chart.write("chartdiv");
            });

            // this method sets chart 2D/3D
            function setDepth() {
                if (document.getElementById("rb1").checked) {
                    chart.depth3D = 0;
                    chart.angle = 0;
                } else {
                    chart.depth3D = 25;
                    chart.angle = 30;
                }
                chart.validateNow();
            }

折线图:

    var chart;
    var chartData = <?=$o?>;
AmCharts.ready(function () {
           // SERIAL CHART
    chart = new AmCharts.AmSerialChart();
    chart.pathToImages = "__IMG__/amcharts/images/";
    chart.marginLeft = 20;
    chart.marginRight = 20;
    chart.marginTop = 20;
    chart.dataProvider = chartData;
    chart.categoryField = "month";

    // AXES
    // category
    var categoryAxis = chart.categoryAxis;
    categoryAxis.parseDates = false; // as our data is date-based, we set parseDates to true
    categoryAxis.minPeriod = "MM"; // our data is daily, so we set minPeriod to DD
    // value axis
    var valueAxis = new AmCharts.ValueAxis();
    valueAxis.inside = true;
    valueAxis.tickLength = 0;
    valueAxis.axisAlpha = 0;
    //valueAxis.minimum = 0;
    //valueAxis.maximum = 100000000;
    chart.addValueAxis(valueAxis);

    // GRAPH
    var graph = new AmCharts.AmGraph();
    graph.dashLength = 3;
    graph.lineColor = "#7717D7";
    graph.valueField = "usum";
    graph.dashLength = 3;
    graph.bullet = "round";
    chart.addGraph(graph);

    // CURSOR
    var chartCursor = new AmCharts.ChartCursor();
    chartCursor.cursorAlpha = 0;
    chart.addChartCursor(chartCursor);

    // GUIDES are used to create horizontal range fills
    var guide = new AmCharts.Guide();
    guide.value = 0;
    guide.toValue = 1000000000000;
    guide.fillColor = "#CC0000";
    guide.fillAlpha = 0.2;
    guide.lineAlpha = 0;
    valueAxis.addGuide(guide);
    // WRITE
    chart.write("chartdiv");
});
    // this method sets chart 2D/3D
    function setDepth() {
        if (document.getElementById("rb1").checked) {
            chart.depth3D = 0;
            chart.angle = 0;
        } else {
            chart.depth3D = 25;
            chart.angle = 30;
        }
        chart.validateNow();
    }

amcharts  插件要用到的文件:
amcharts.js    amcharts图片文件夹(如下)

mysql+thinkphp +amcharts 完成图表统计功能

时间: 2024-11-05 23:21:33

mysql+thinkphp +amcharts 完成图表统计功能的相关文章

amCharts 金融图表股票走势K线图控件介绍

amCharts控件股票走势图的主要目的是为了显示金融图表,但它可以用于任何日期(时间)为基础的数据可视化. 股票走势图,是一个功能强大的应用程序,让你忘了最枯燥的部分构建复杂的统计系统.它具有内置的期间和数据集选择工具,并能较长组数据.这使您可以向下钻取图表不用任何额外的编码,只使用一个数据文件.单击"最大"按钮下面的图表,你会发现该图表显示月度数据.点击"1月"的按钮,你会看到,现在的图表显示每天的数据. 主要特点: 支持线,柱,烛台,OHLC,步线的图表类型.

通过百度echarts实现数据图表展示功能

现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解echarts是个怎样技术的开发者来说,可以到echarts官网进行学习了解,官网有详细的API文档和实例供大家参考学习. 2.以下是我在工作中实现整理出来的实例源码: 公用的支持js文件 echarts.js.echarts.min.js,还有其他的图表需要支持的js文件也可以到官网下载 echa

【Jsp】利用Application对象实现访问人数统计功能

利用Application对象实现访问人数的统计很常见,但是一些书籍讲解这一技术的时候,往往没有做完整.一旦服务器重启,由于Application会被销毁,重新建立,人数统计则会再一次从零开始.解决方法,个人认为,应该同时利用到数据库,这样无论服务器要重启或者遭遇到停机什么,被统计的人数还是会一直累计上去,不会丢失.下面就介绍一个例子,解决利用Application对象实现访问人数统计功能由于服务器重启或者停机而导致从零开始的问题,同时介绍如果利用Application对象把保存到web.xml

Springboot项目配置druid数据库连接池,并监控统计功能

pom.xml配置依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency>  资源文件配置信息 不管是

基于Java EE技术的公司职员信息管理系统查询与统计功能的设计与实现

获取项目源文件,技术交流与指导联系Q:1225467431 摘要 二十一世纪的今天,随着互联网的普及与发展,计算机技术已经广泛的应用于人们的生产办公中,特别是信息的处理加工,更大大的推进着企业的生产效率的增长,企业管理信息化已经成为一个必然的趋势. 本课题研究并开发基于J2EE多层框架的人事信息管理系统.此系统不同于以往的人事信息管理软件,而是在互联网的大背景下,实现以数据库服务器来维护人事信息数据,浏览器客户端来动态访问服务器,这种采用B/S架构的管理系统,有利于充分利用互联网覆盖范围广,通信

词频统计-功能一

一.完成一个小程序 我 拿到这个题目之后,就决定用最不熟悉的c#来实现,因为老师说不懂的去学才会有进步.布置任务后的第二天就开始去图书馆借了两本书<c#从入门到精髓>,<c#项目实战>,拿到书之后看了入门书<c#从入门到精髓>,看书的过程时痛苦的,因为发现大二选修课学的c#全交还给老师了,只能重头再学了.唯一有点印象的就是窗口应用程序,基于UI的设计. 写代码首先需要工具,由于电脑上没有visual studio的安装包,当时求助了度娘. 如果没有安装包的同学们,可以借

atitit. 统计功能框架的最佳实践(1)---- on hibernate criteria

atitit. 统计功能框架的最佳实践(1)---- on hibernate criteria 1. 关键字 1 2. 统计功能框架普通有有些条件选项...一个日期选项..一个日期类型(日,周,月份,年等) 1 3. 元数据的位置,不需要绑定class 1 4. 设置聚合字段... @reduce(" sum(timLen) "),@reduce(" Avg(timLen) ") 2 5. 设置groupby  字段  @GroupBy 2 6. 设置groupb

Jxl、JxCell图表导出功能的实现

最近接触过许多报表导出功能,也用过多种工具进行导出功能的实现,但对于图表的导出一直没有仔细的去展开研究和探讨,直到最近略微整理了下这方面的需求和技术攻克. 首先导出excel功能的实现主要有JXL.JXCELL.POI等工具.目前只实现了JXL和JXCELL. JXL: 先介绍下JXL: jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文支持非常好

词频统计-功能二

一.完成一个小程序 今天的任务是处理一本英文小说.我处理的英文小说是<war and peace> 本想着用多线程来解决的,发现看书还不是太懂,并不能真正动手编程. 我在编程的过程中主要遇到了以下两个问题: 1.在对整个英文小说进行单词总数统计遇见困难. 2.字符串数组的空字符串处理问题.因为我开始用空格来代替标点等不是英文单词来进行对单词分割,其中用到了正则表达式,后来统计对单词出现频数时,发现空格竟然排第一名,有4万多空格. 最终我的处理方式是使用List泛型集合的ForEach方法循环获