Plugin Directory

Changeset 217596


Ignore:
Timestamp:
03/14/2010 08:24:32 PM (16 years ago)
Author:
anukit
Message:

trying to avoid out of memory errors when building charts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cyclopress/branches/cp2/classes/cy_google_charts.php

    r217586 r217596  
    9696
    9797        // get all the chart data
    98         $data = array();
     98        $x_data = array();
     99        $y_data = array();
    99100        if (count($rides)) {
    100101           
     
    104105                $r->load($row);
    105106       
    106                 $data[$r->get_startdate('Y-m-d_H:i:s')] = number_format($row[$chart->field], 1);
     107                $x_data[] = $r->get_startdate('Y-m-d_H:i:s');
     108                $y_data[] = number_format($row[$chart->field], 1);
     109                //$data[$r->get_startdate('Y-m-d_H:i:s')] = number_format($row[$chart->field], 1);
    107110       
    108111            }
     
    117120        // send off to google and cache the images
    118121        // check if it's a line chart or a bar chart and retrieve the right one
    119         $chart->url = $this->get_line_chart($data, false, true);
    120         $chart->thumb_url = $this->get_line_chart($data, true, true);
     122        $chart->url = $this->get_line_chart($x_data, $y_data, false, true);
     123        $chart->thumb_url = $this->get_line_chart($x_data, $y_data, true, true);
    121124       
    122125        // save the chart's new hash and url
     
    132135     * Using only a Google URL is not desirable due to API request limits.
    133136     *
    134      * @param array $data An associative array. The keys are the X data, values are the Y data.
     137     * @param array $x_data The X data.
     138     * @param array $y_data The Y data.
    135139     * @param boolean $is_thumb True if the chart URL to be returned should be a thumbnail. False for full size.
    136140     * @param boolean $is_by_date True if the X data represents dates.
    137141     */
    138     public function get_line_chart($data, $is_thumb=false, $is_by_date=true) {
     142    public function get_line_chart(&$x_data, &$y_data, $is_thumb=false, $is_by_date=true) {
    139143       
    140144        // no data? get out of here...
    141         if ( !count($data) ) {
     145        if ( !count($y_data) ) {
    142146            // alternatively return a URL to a "not found" image
    143147            return '<span class="error">Unable to get chart image. There was no data in your query.</span>';
    144148        }
    145 
    146         // get each data set separately
    147         $x_data = array_keys($data);
    148         $y_data = array_values($data);
    149149
    150150        // get max/min
     
    152152        $max = max($y_data);
    153153       
     154        // counts
     155        $num_x_data = count($x_data);
     156        $num_y_data = count($y_data);
     157       
    154158        // get nice tick ranges for the data
    155159        $range = $max - $min;
    156160        $smart_min = ($min > ($range*0.1)) ? floor($min - ($range*0.1)) : 0; // avoid too much space at the bottom of the chart
    157         list($x_tick_range, $x_lower_bound, $x_upper_bound) = $this->get_chart_step_size(7, (count($x_data)-1), 1);
     161        list($x_tick_range, $x_lower_bound, $x_upper_bound) = $this->get_chart_step_size(7, ($num_x_data-1), 1);
    158162        list($y_tick_range, $y_lower_bound, $y_upper_bound) = $this->get_chart_step_size(8, $max, $smart_min);
    159163
     
    164168        // map dates onto x labels if they're dates
    165169        if ($is_by_date) {
    166             $x_date_labels = array();
     170            $x_labels = array();
    167171            foreach($x_label_positions as $xpos) {
    168                 $index = round($xpos / 100 * (count($x_data) - 1) );
     172                $index = round($xpos / 100 * ($num_x_data - 1) );
    169173                $time = strtotime(str_replace('_', ' ', $x_data[$index]));
    170                 $x_date_labels[] = urlencode(date("M j, 'y", $time));
     174                $x_labels[] = urlencode(date("M j, 'y", $time));
    171175            }
    172             $x_labels = $x_date_labels;
    173176        }
    174177
Note: See TracChangeset for help on using the changeset viewer.