5.28. Example - Horizontal Stacked Bar Chart

This example shows a horizontal stacked 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.

Example 5.28. Horizontal Stacked Bar Chart

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

$column_names = array(
                 'Beef', 'Fish', 'Pork', 'Chicken', 'Butter',
                                                         'Cheese',
                                                               'Ice Cream');
//                   |       |       |       |       |       |       |
$data = array(
    array('1910',   48.5,   11.2,   38.2,   11.0,   18.4,    3.9,    1.9),
    array('1930',   33.7,   10.2,   41.1,   11.1,   17.6,    4.7,    9.7),
    array('1950',   44.6,   11.9,   43.0,   14.3,   10.9,    7.7,   17.4),
    array('1970',   79.6,   11.7,   48.1,   27.4,    5.4,   11.4,   17.8),
    array('1990',   63.9,   14.9,   46.4,   42.4,    4.0,   24.6,   15.8),
);
$plot = new PHPlot(800, 500);
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
$plot->SetTitle("U.S. Annual Per-Capita Consumption\n"
              . "of Selected Meat and Dairy Products");
$plot->SetLegend($column_names);
#  Move the legend to the lower right of the plot area:
$plot->SetLegendPixels(700, 300);
$plot->SetDataValues($data);
$plot->SetDataType('text-data-yx');
$plot->SetPlotType('stackedbars');
$plot->SetXTitle('Pounds Consumed Per Capita');
#  Show data value labels:
$plot->SetXDataLabelPos('plotstack');
#  Rotate data value labels to 90 degrees:
$plot->SetXDataLabelAngle(90);
#  Format the data value labels with 1 decimal place:
$plot->SetXDataLabelType('data', 1);
#  Specify a whole number for the X tick interval:
$plot->SetXTickIncrement(20);
#  Disable the Y tick marks:
$plot->SetYTickPos('none');
$plot->DrawGraph();

SourceForge.net Logo

This version of the manual was produced for the PHPlot Sourceforge project web service site, which requires the logo on each page.

To download a logo-free copy of the manual, see the PHPlot project downloads area.