5.21. Example - Stacked Area Plot

The stacked area plot is similar in appearance to the area plot, and this example is the same as in Section 5.3, “Example - Area Plot” except for the plot type and data values. In the stacked area plot, PHPlot accumulates the Y values at each X position, similar to the stacked bar plot, and fills the area between the resulting values. For example, in 1960, 30% of the candy sales were cherry, 10% were lime, and 6% were lemon. In the stacked area plot, this is represented simply as (30, 10, 6, ...), whereas in the area plot example we had to sum the values to get (100, 70, 60, 54, ...).

Note this plot type was added in PHPlot-5.1.1.

Example 5.21. Stacked Area Plot

Stacked Area Plot Example
<?php
# PHPlot Example: Stacked Area chart
require_once 'phplot.php';

$data = array(
  array('1960', 30, 10,  6, 38, 14,  2),
  array('1970', 20, 17,  9, 32,  2, 20),
  array('1980', 20, 14, 12, 27,  2, 25),
  array('1990',  5, 26, 15, 26, 18, 10),
  array('2000', 28,  0, 18, 16, 33,  5),
);

$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain');

$plot->SetPlotType('stackedarea');
$plot->SetDataType('text-data');
$plot->SetDataValues($data);

# Main plot title:
$plot->SetTitle('Candy Sales by Flavor');

# Set Y data limits, tick increment, and titles:
$plot->SetPlotAreaWorld(NULL, 0, NULL, 100);
$plot->SetYTickIncrement(10);
$plot->SetYTitle('% of Total');
$plot->SetXTitle('Year');

# Colors are significant to this data:
$plot->SetDataColors(array('red', 'green', 'blue', 'yellow', 'cyan', 'magenta'));
$plot->SetLegend(array('Cherry', 'Lime', 'Lemon', 'Banana', 'Apple', 'Berry'));

# Turn off X tick labels and ticks because they don't apply here:
$plot->SetXTickLabelPos('none');
$plot->SetXTickPos('none');

$plot->DrawGraph();