application/controllers/Chart.php
/**
     * Draw chart for value and period
     * 
     * @param int    $symbolId      Contains ID of symbol
     * @param string $valueName     Contains name of value
     * @param string $period        Contains name of period
     * @param string $fileName      Contains name of file
     * @param int    $ratioSymbolId Contains ID of ratio symbol
     * @return bool 
     */
Draw chart for value and period


Draw chart for value and period
private function _drawChartForValueAndPeriod($symbolId, $valueName, $period, $fileName, $ratioSymbolId = null) {
        $fileName = BASEPATH . '../' . $fileName;
        if ($period == 'hour') { 
            $lastValue = $this->value_model->get_last_minute_value($symbolId);
        } elseif ($period == 'day') { 
            $lastValue = $this->value_model->get_last_hour_value($symbolId);
        } else { 
            $lastValue = $this->value_model->get_last_day_value($symbolId);
        }
        if (!$lastValue) {
            return false;
        }
        if (!file_exists($fileName) || filemtime($fileName) < $lastValue['time']) {
            $end = time();
            $begin = strtotime("-1 $period", $end);
            if ($period == 'hour') { 
                $values = $this->value_model->get_minute_values($symbolId, $begin, $end);
            } elseif ($period == 'day') { 
                $values = $this->value_model->get_hour_values($symbolId, $begin, $end);
            } else { 
                $values = $this->value_model->get_day_values($symbolId, $begin, $end);
            }
            $xPoints = array();
            $yPoints = array();
            $dateFormat = ($period == 'month') ? 'd/m' : 'H:i';
            if ($ratioSymbolId) {
                if ($period == 'hour') { 
                    $ratioValues = $this->value_model->get_minute_values($ratioSymbolId, $begin, $end);
                } elseif ($period == 'day') { 
                    $ratioValues = $this->value_model->get_hour_values($ratioSymbolId, $begin, $end);
                } else { 
                    $ratioValues = $this->value_model->get_day_values($ratioSymbolId, $begin, $end);
                }
                $cnt = count($values);
                $ratioCnt = count($ratioValues);
                if ($ratioCnt < $cnt) {
                    $cnt = $ratioCnt;
                }
                for ($i = 0; $i < $cnt; $i++) {
                    $value = $values[$i];
                    $ratioValue = $ratioValues[$i];
                    if ($ratioValue['bid']) {
                        $xPoints[] = date($dateFormat, $value['time']);
                        $yPoints[] = round($value['bid'] / $ratioValue['bid'], 2);
                    }
                }
            } else {
                foreach ($values as $value) {
                    $xPoints[] = date($dateFormat, $value['time']);
                    $yPoints[] = floatval($value['bid']);
                }
            }
            if ($xPoints) {
                $title = $valueName . ' (Last ' . $period . ')';
                $this->_drawChart($xPoints, $yPoints, 850, 300, $title, $fileName);
            } else {
                return false;
            }
        }
        return true;
    }
private function _drawChartForValueAndPeriod ( $ symbolId , $ valueName , $ period , $ fileName , $ ratioSymbolId = null ) { $ fileName = BASEPATH . '../' . $ fileName ; if ( $ period == 'hour' ) { $ lastValue = $ this -> value_model -> get_last_minute_value ( $ symbolId ) ; } elseif ( $ period == 'day' ) { $ lastValue = $ this -> value_model -> get_last_hour_value ( $ symbolId ) ; } else { $ lastValue = $ this -> value_model -> get_last_day_value ( $ symbolId ) ; } if ( ! $ lastValue ) { return false ; } if ( ! file_exists ( $ fileName ) || filemtime ( $ fileName ) < $ lastValue [ 'time' ] ) { $ end = time ( ) ; $ begin = strtotime ( " -1  $ period " , $ end ) ; if ( $ period == 'hour' ) { $ values = $ this -> value_model -> get_minute_values ( $ symbolId , $ begin , $ end ) ; } elseif ( $ period == 'day' ) { $ values = $ this -> value_model -> get_hour_values ( $ symbolId , $ begin , $ end ) ; } else { $ values = $ this -> value_model -> get_day_values ( $ symbolId , $ begin , $ end ) ; } $ xPoints = array ( ) ; $ yPoints = array ( ) ; $ dateFormat = ( $ period == 'month' ) ? 'd/m' : 'H:i' ; if ( $ ratioSymbolId ) { if ( $ period == 'hour' ) { $ ratioValues = $ this -> value_model -> get_minute_values ( $ ratioSymbolId , $ begin , $ end ) ; } elseif ( $ period == 'day' ) { $ ratioValues = $ this -> value_model -> get_hour_values ( $ ratioSymbolId , $ begin , $ end ) ; } else { $ ratioValues = $ this -> value_model -> get_day_values ( $ ratioSymbolId , $ begin , $ end ) ; } $ cnt = count ( $ values ) ; $ ratioCnt = count ( $ ratioValues ) ; if ( $ ratioCnt < $ cnt ) { $ cnt = $ ratioCnt ; } for ( $ i = 0 ; $ i < $ cnt ; $ i ++ ) { $ value = $ values [ $ i ] ; $ ratioValue = $ ratioValues [ $ i ] ; if ( $ ratioValue [ 'bid' ] ) { $ xPoints [ ] = date ( $ dateFormat , $ value [ 'time' ] ) ; $ yPoints [ ] = round ( $ value [ 'bid' ] / $ ratioValue [ 'bid' ] , 2 ) ; } } } else { foreach ( $ values as $ value ) { $ xPoints [ ] = date ( $ dateFormat , $ value [ 'time' ] ) ; $ yPoints [ ] = floatval ( $ value [ 'bid' ] ) ; } } if ( $ xPoints ) { $ title = $ valueName . ' (Last ' . $ period . ')' ; $ this -> _drawChart ( $ xPoints , $ yPoints , 850 , 300 , $ title , $ fileName ) ; } else { return false ; } } return true ; }
Draw chart for value and period
Draw chart for value and period
// hour // day // month // Check last value // Check last date of image file // hour // day // month // Get ratio values // hour // day // month // Count values // Fill X and Y arrays for ratio chart // Fill X and Y arrays for simple chart // Check points