5.40. Example - Bubbles Plot
Prev | Chapter 5. PHPlot Examples | Next

## 5.40. Example - Bubbles Plot

This example shows a bubbles plot. A data array for a bubbles plot uses the data-data-xyz data type, in which three coordinate values specify each point: X and Y are the position on the plot, and Z maps to the bubble diameter.

In this example, X is the flavor, Y is the age group, and Z represents the percentage of participants who liked that flavor. Note that there are two missing points in the Y=1 row. No bubbles are drawn at X=3,Y=1 or X=5,Y=1. Presumably, no-one under 12 would try the pear or kiwi flavors, so there are no valid data points there. This is different from a value Z=0, which would be plotted with a bubble of the smallest size.

This plot type was added in PHPlot-5.5.0.

This example also shows the use of custom Y labels. A function `get_label()` is defined to map the Y tick value into a string from an array of strings. This method allows the display of arbitrary labels along the Y axis. See SetYLabelType for details.

Example 5.40. Bubbles Plot

    <?php
    # PHPlot Example - Bubble Plot
    require_once 'phplot.php';

    # Array of custom labels for the Y axis. See the get_label callback.
    $y_labels = array("", "Age\n12 and under", "Age 13-17", "Age 18-29",
                          "Age 30-39", "Age 40-54", "Age\n55 and older");

    # Return the string for a Y label:
    function get_label($value, $labels)
    {
        if (isset($labels[(int)$value])) return $labels[(int)$value];
        return $value;
    }

    #                       <=12    13-17   17-28   30-39   40-54    >=55
    $data = array(
        array('Cherry', 1,   1, 2,   2, 4,   3, 3,   4, 3,   5, 5,   6, 6),
        array('Apple',  2,   1, 9,   2, 7,   3, 4,   4, 7,   5, 3,   6, 7),
        array('Pear',   3,  '', 2,   2, 2,   3, 3,   4, 4,   5, 3,   6, 2),
        array('Grape',  4,   1, 8,   2, 5,   3, 5,   4, 6,   5, 3,   6, 4),
        array('Kiwi',   5,  '', 0,   2, 3,   3, 4,   4, 4,   5, 5,   6, 2),
        array('Banana', 6,   1, 5,   2, 4,   3, 6,   4, 3,   5, 3,   6, 4),
    );

    $plot = new PHPlot(600, 600);
    $plot->SetTitle("Flavor Preference By Age Group");
    $plot->SetDataType('data-data-xyz');
    $plot->SetDataValues($data);
    $plot->SetPlotType('bubbles');
    $plot->SetDataColors('yellow'); // Use same color for all data sets
    $plot->SetDrawPlotAreaBackground(True);
    $plot->SetPlotBgColor('plum');
    $plot->SetLightGridColor('red'); // Change grid color to make it visible
    $plot->SetImageBorderType('plain');
    $plot->SetPlotBorderType('full');
    $plot->SetXTickIncrement(1); // For grid line spacing
    $plot->SetYTickIncrement(1);
    $plot->SetPlotAreaWorld(0, 0, 6.5, 6.5);
    # Establish the handler for the Y label text:
    $plot->SetYLabelType('custom', 'get_label', $y_labels);
    $plot->SetXTickPos('both'); // Tick marks on both sides
    $plot->SetYTickPos('both'); // Tick marks on top and bottom too
    $plot->SetXDataLabelPos('both'); // X axis data labels top and bottom
    $plot->SetYTickLabelPos('both'); // Y axis labels left and right
    $plot->SetDrawXGrid(True);
    $plot->DrawGraph();

Prev | Up | Next
5.39. Example - Embedding Image with EncodeImage | Home | 5.41. Example - Pie Chart Label Types