0

我正在为我们内部 CRM 中的管理仪表板创建图表,但是我在添加趋势线时遇到了一些麻烦。我似乎找不到任何内置函数的文档,因此尝试使用笨重且缓慢的 sql 查询来完成它。

    SELECT days.date, 
        (SELECT count(app.app_id) FROM li_appointments.li_appointments as app
            where app_datetime >= date_add(days.date, interval -7 day)
            and app_datetime <= days.date) / 5 as average
    FROM li_appointments.li_days as days
    where date >= date_add(date(now()), interval -30 day)
    and date <= date(now())

仅抓取图表上这一行的数据大约需要 12 秒。是否有任何本地方法可以使用 pChart 执行此操作,或者是否有更好的 PHP 选项可用?

这是我现在使用的完整代码,没有趋势线:

   $getapps = "SELECT days.date, app.app_id as scheduled
   ,cnc.app_id as cnc
   ,dnc.app_id as dnc
   ,cancel.app_id as cancel
   ,covered.app_id as covered
   FROM li_appointments.li_days as days
   left Join (SELECT date(app_datetime) as app_datetime
               , count(app_id) as app_id 
               FROM li_appointments.li_appointments
               group by date(app_datetime)) 
       as app on days.date = app.app_datetime
   left Join (SELECT date(app_datetime) as app_datetime
               , count(app_id) as app_id 
               FROM li_appointments.li_appointments
               Where app_id in (select app_id from li_app_cnc)
               group by date(app_datetime)) 
       as cnc on days.date = cnc.app_datetime
   left Join (SELECT date(app_datetime) as app_datetime
               , count(app_id) as app_id 
               FROM li_appointments.li_appointments
               Where app_id in (select app_id from li_app_dnc)
               group by date(app_datetime)) 
       as dnc on days.date = dnc.app_datetime
   left Join (SELECT date(app_datetime) as app_datetime
               , count(app_id) as app_id 
               FROM li_appointments.li_appointments
               Where app_id in (select app_id from li_app_canceled)
               group by date(app_datetime)) 
       as cancel on days.date = cancel.app_datetime
   left Join (SELECT date(app_datetime) as app_datetime
               , count(app_id) as app_id 
               FROM li_appointments.li_appointments
               Where terp_id is not null and terp_id <> ''
               group by date(app_datetime)) 
       as covered on days.date = covered.app_datetime
   Where date > adddate(now(), Interval -30 day)
   and date <= date(now())
   group by date";

   $result = mysql_query($getapps );

   $date = array();
   $scheduled = array();
   $cnc = array();
   $dnc = array();
   $canceled = array();
   $covered = array();

   while($row = mysql_fetch_array($result)){
       array_push($date, $row['date']);
       array_push($scheduled, $row['scheduled']);
       array_push($cnc, $row['cnc']);
       array_push($dnc, $row['dnc']);
       array_push($canceled, $row['cancel']);
       array_push($covered, $row['covered']);
    }

     /* CAT:Line chart */ 

    /* pChart library inclusions */ 
    include("../class/pData.class.php"); 
    include("../class/pDraw.class.php"); 
    include("../class/pImage.class.php"); 

    /* Create and populate the pData object */ 
    $MyData = new pData();   
    $MyData->addPoints($scheduled,"Scheduled"); 
    $MyData->addPoints($cnc,"CNC"); 
    $MyData->addPoints($dnc,"DNC"); 
    $MyData->addPoints($canceled,"canceled"); 
    $MyData->addPoints($covered,"covered"); 
    $MyData->setSerieTicks("Probe 2",4); 
    $MyData->setSerieTicks("Probe 3",4); 
    $MyData->setAxisName(0,"Appointments"); 
    $MyData->addPoints($date,"Labels"); 
    $MyData->setSerieDescription("Labels","Months"); 
    $MyData->setAbscissa("Labels"); 
4

0 回答 0