0

Hi I am using phpGraphLib to generate charts but I have a problem.

I want to add 2 arrays as data and I want them to be displayed one data column after another data column. So I changed the constant MULTI_OFFSET_TWO=24 to MULTI_OFFSET_TWO=100, and in generateBars() function I made this modification: $xStart += 2*$this->bar_width + $this->space_width; (multiplied by 2). This is all ok but x bar does not change its length, it stays the same, so some of the columns are displayed out of the X bar. What else do I need to modify to reach what I want?

This is the source code of generateBars function:

function generateBars() 
{
    $this->finalizeColors();

    $barCount = 0;
    $adjustment = 0;
    if ($this->bool_user_data_range && $this->data_min >= 0) {
        $adjustment = $this->data_min * $this->unit_scale;
    }

    //reverse array to order data sets in order of priority
    $this->data_array = array_reverse($this->data_array);

    //set different offsets based on number of data sets
    $dataset_offset = 0;
    switch ($this->data_set_count) {
        case 2: 
            $dataset_offset = $this->bar_width * (self::MULTI_OFFSET_TWO / 100); 
            break;
        case 3: 
            $dataset_offset = $this->bar_width * (self::MULTI_OFFSET_THREE / 100); 
            break;
        case 4: 
            $dataset_offset = $this->bar_width * (self::MULTI_OFFSET_FOUR / 100); 
            break;
        case 5: 
            $dataset_offset = $this->bar_width * (self::MULTI_OFFSET_FIVE / 100); 
            break;
    }

    foreach ($this->data_array as $data_set_num => $data_set) {
        $lineX2 = null;
        $xStart = $this->y_axis_x1 + ($this->space_width / 2);
        foreach ($data_set as $key => $item) {
            $hideBarOutline = false;

            $x1 = round($xStart + ($dataset_offset * $data_set_num));
            $x2 = round(($xStart + $this->bar_width) + ($dataset_offset * $data_set_num));
            $y1 = round($this->x_axis_y1 - ($item * $this->unit_scale) + $adjustment);
            $y2 = round($this->x_axis_y1);

            //if we are using a user specified data range, need to limit what's displayed
            if ($this->bool_user_data_range) {
                if ($item <= $this->data_range_min) {
                    //don't display, we are out of our allowed display range!
                    $y1 = $y2;
                    $hideBarOutline = true;
                } elseif ($item >= $this->data_range_max) {
                    //display, but cut off display above range max
                    $y1 = $this->x_axis_y1 - ($this->actual_displayed_max_value * $this->unit_scale) + $adjustment; 
                }
            }   
            //draw bar 
            if ($this->bool_bars) {
                if ($this->bool_gradient) {
                    //draw gradient if desired
                    $this->drawGradientBar($x1, $y1, $x2, $y2, $this->multi_gradient_colors_1[$data_set_num], $this->multi_gradient_colors_2[$data_set_num], $data_set_num);
                } else {
                    imagefilledrectangle($this->image, $x1, $y1,$x2, $y2,  $this->multi_bar_colors[$data_set_num]);
                }
                //draw bar outline  
                if ($this->bool_bar_outline && !$hideBarOutline) { 
                    imagerectangle($this->image, $x1, $y2, $x2, $y1, $this->outline_color); 
                }
            }
            // draw line
            if ($this->bool_line) {
                $lineX1 = $x1 + ($this->bar_width / 2); //MIDPOINT OF BARS, IF SHOWN
                $lineY1 = $y1;
                if (isset($lineX2)) {
                    imageline($this->image, $lineX2, $lineY2, $lineX1, $lineY1, $this->line_color[$data_set_num]);
                    $lineX2 = $lineX1;
                    $lineY2 = $lineY1;
                } else {
                    $lineX2 = $lineX1;
                    $lineY2 = $lineY1;
                }   
            }
            // display data points
            if ($this->bool_data_points) {
                //dont draw datapoints here or will overlap poorly with line
                //instead collect coordinates
                $pointX = $x1 + ($this->bar_width / 2); //MIDPOINT OF BARS, IF SHOWN
                $this->data_point_array[] = array($pointX, $y1);
            }
            // display data values
            if ($this->bool_data_values) {
                $dataX = ($x1 + ($this->bar_width / 2)) - ((strlen($item) * self::DATA_VALUE_TEXT_WIDTH) / 2);
                //value to be graphed is equal/over 0
                if ($item >= 0) {
                    $dataY = $y1 - self::DATA_VALUE_PADDING - self::DATA_VALUE_TEXT_HEIGHT;
                } else {
                    //check for item values below user spec'd range
                    if ($this->bool_user_data_range && $item <= $this->data_range_min) {
                        $dataY = $y1 - self::DATA_VALUE_PADDING - self::DATA_VALUE_TEXT_HEIGHT;
                    } else {
                        $dataY = $y1 + self::DATA_VALUE_PADDING;
                    }
                }
                //add currency sign, formatting etc
                if ($this->data_format_array) {
                    $item = $this->applyDataFormats($item);
                }
                if ($this->data_currency) {
                    $item = $this->applyDataCurrency($item);
                }
                //recenter data position if necessary
                $dataX -= ($this->data_additional_length * self::DATA_VALUE_TEXT_WIDTH) / 2;
                imagestring($this->image, 2, $dataX, $dataY, $item,  $this->data_value_color);
            }
            //write x axis value 
            if ($this->bool_x_axis_values) {
                if ($data_set_num == $this->data_set_count - 1) {
                    if ($this->bool_x_axis_values_vert) {
                        if ($this->bool_all_negative) {
                            //we must put values above 0 line
                            $textVertPos = round($this->y_axis_y2 - self::AXIS_VALUE_PADDING);
                        } else {
                            //mix of both pos and neg numbers
                            //write value y axis bottom value (will be under bottom of grid even if x axis is floating due to
                            $textVertPos = round($this->y_axis_y1 + (strlen($key) * self::TEXT_WIDTH) + self::AXIS_VALUE_PADDING);
                        }
                        $textHorizPos = round($xStart + ($this->bar_width / 2) - (self::TEXT_HEIGHT / 2));

                        //skip and dispplay every x intervals
                        if ($this->x_axis_value_interval) {
                            if ($this->x_axis_value_interval_counter < $this->x_axis_value_interval) {
                                $this->x_axis_value_interval_counter++;
                            } else {
                                imagestringup($this->image, 2, $textHorizPos, $textVertPos, $key,  $this->x_axis_text_color);
                                $this->x_axis_value_interval_counter = 0;
                            }
                        }
                        else {
                            imagestringup($this->image, 2, $textHorizPos, $textVertPos, $key,  $this->x_axis_text_color);
                        }
                    }
                    else {
                        if ($this->bool_all_negative) {
                            //we must put values above 0 line
                            $textVertPos = round($this->y_axis_y2 - self::TEXT_HEIGHT - self::AXIS_VALUE_PADDING);
                        } else {
                            //mix of both pos and neg numbers
                            //write value y axis bottom value (will be under bottom of grid even if x axis is floating due to
                            $textVertPos = round($this->y_axis_y1 + (self::TEXT_HEIGHT * 2 / 3) - self::AXIS_VALUE_PADDING);
                        }
                        //horizontal data keys
                        $textHorizPos = round($xStart + ($this->bar_width / 2) - ((strlen($key) * self::TEXT_WIDTH) / 2));


                        //skip and dispplay every x intervals
                        if ($this->x_axis_value_interval) {
                            if ($this->x_axis_value_interval_counter < $this->x_axis_value_interval) {
                                $this->x_axis_value_interval_counter++;
                            } else {
                                imagestring($this->image, 2, $textHorizPos, $textVertPos, $key,  $this->x_axis_text_color);
                                $this->x_axis_value_interval_counter = 0;
                            }
                        } else {
                            imagestring($this->image, 2, $textHorizPos, $textVertPos, $key,  $this->x_axis_text_color);
                        }
                    }
                }
            }
            $xStart += $this->bar_width + $this->space_width;//modification: multiplied by 2
        }
    }
}
Ani
  • 1
  • 2
  • Rather than telling us changes you made, it would be better to actually post your code and use comments to indicate where you made those changes. – skrrgwasme Aug 26 '14 at 18:12
  • the code is too large, that is why I didn't paste it. Instead I am giving the source code here: https://github.com/elliottb/phpgraphlib/blob/master/phpgraphlib.php The changes I have made are in lines 69 and 511 respectively. – Ani Aug 26 '14 at 19:41
  • Can you reduce the code to a [MCVE](http://stackoverflow.com/help/mcve) and post that instead? – skrrgwasme Aug 26 '14 at 20:29

0 Answers0