对Chart的generateBars()函数进行PHPGraphLib修改

时间:2014-08-26 18:03:07

标签: php charts phpgraphlib

您好我使用phpGraphLib生成图表,但我遇到了问题。

我想添加2个数组作为数据,我希望它们在另一个数据列之后显示一个数据列。 所以我将常量MULTI_OFFSET_TWO = 24更改为MULTI_OFFSET_TWO = 100,并在generateBars()函数中进行了此修改: $ xStart + = 2 * $ this-> bar_width + $ this-> space_width; (乘以2)。 这一切都还可以,但是x bar不会改变它的长度,它会保持不变,所以有些列会显示在X栏之外。我需要修改什么来达到我想要的目标?

这是generateBars函数的源代码:

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
        }
    }
}

0 个答案:

没有答案