5.27. Example - Horizontal Bar Chart
Prev | Chapter 5. PHPlot Examples | Next

## 5.27. Example - Horizontal Bar Chart

This example shows a horizontal bar chart. As always, the X axis is horizontal, and the Y axis is vertical. But the data array contains the X value for each implicit Y. The data type 'text-data-yx' indicates this is a horizontal plot.

This example also has a tiled background image under the plot area.

Example 5.27. Horizontal Bar Chart

    <?php
    # PHPlot Example - Horizontal Bars
    require_once 'phplot.php';

    $data = array(
      array('San Francisco CA', 20.11),
      array('Reno NV', 7.5),
      array('Phoenix AZ', 8.3),
      array('New York NY', 49.7),
      array('New Orleans LA', 64.2),
      array('Miami FL', 52.3),
      array('Los Angeles CA', 13.2),
      array('Honolulu HI', 18.3),
      array('Helena MT', 11.3),
      array('Duluth MN', 31.0),
      array('Dodge City KS', 22.4),
      array('Denver CO', 15.8),
      array('Burlington VT', 36.1),
      array('Boston MA', 42.5),
      array('Barrow AL', 4.2),
    );

    $plot = new PHPlot(800, 800);
    $plot->SetImageBorderType('plain'); // Improves presentation in the manual
    $plot->SetTitle("Average Annual Precipitation (inches)\n"
                  . "Selected U.S. Cities");
    $plot->SetBackgroundColor('gray');
    #  Set a tiled background image:
    $plot->SetPlotAreaBgImage('images/drop.png', 'centeredtile');
    #  Force the X axis range to start at 0:
    $plot->SetPlotAreaWorld(0);
    #  No ticks along Y axis, just bar labels:
    $plot->SetYTickPos('none');
    #  No ticks along X axis:
    $plot->SetXTickPos('none');
    #  No X axis labels. The data values labels are sufficient.
    $plot->SetXTickLabelPos('none');
    #  Turn on the data value labels:
    $plot->SetXDataLabelPos('plotin');
    #  No grid lines are needed:
    $plot->SetDrawXGrid(FALSE);
    #  Set the bar fill color:
    $plot->SetDataColors('salmon');
    #  Use less 3D shading on the bars:
    $plot->SetShading(2);
    $plot->SetDataValues($data);
    $plot->SetDataType('text-data-yx');
    $plot->SetPlotType('bars');
    $plot->DrawGraph();

Prev | Up | Next
5.26. Example - Custom Bar Colors Using the Data Color Callback | Home | 5.28. Example - Horizontal Stacked Bar Chart