98 |
- |
1 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
|
|
|
2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>5.34. Example - Overlaying Plots</title><link rel="stylesheet" type="text/css" href="phplotdoc.css" /><meta name="generator" content="DocBook XSL Stylesheets V1.78.1" /><link rel="home" href="index.html" title="PHPlot Reference Manual" /><link rel="up" href="examples.html" title="Chapter 5. PHPlot Examples" /><link rel="prev" href="ex-linepoints2.html" title="5.33. Example - Linepoints Plot with Data Value Labels" /><link rel="next" href="ex-legendshape.html" title="5.35. Example - Legend with Shape Markers" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.34. Example - Overlaying Plots</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-linepoints2.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-legendshape.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="ex-outbreak"></a>5.34. Example - Overlaying Plots</h2></div></div></div><p>
|
|
|
3 |
This example shows overlay plots, where multiple plots are drawn at the same
|
|
|
4 |
position on the same image. In this case, one plot contains a stacked bar
|
|
|
5 |
plot, and the second is a linepoints plot. The two plots also have
|
|
|
6 |
different Y axis scales.
|
|
|
7 |
</p><p>
|
|
|
8 |
See <a class="xref" href="adv-multiplot.html" title="4.8. Multiple Plots Per Image">Section 4.8, “Multiple Plots Per Image”</a> for more information.
|
|
|
9 |
</p><div class="example"><a id="example-outbreak"></a><p class="title"><strong>Example 5.34. Overlaying Plots</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/outbreak.png" alt="Overlay Plot Example" /></div></div><pre class="programlisting"><?php
|
|
|
10 |
# PHPlot Example: Plot Overlay (lines and stackedbars)
|
|
|
11 |
require_once 'phplot.php';
|
|
|
12 |
|
|
|
13 |
$title = '2009 Outbreak, Positive Tests';
|
|
|
14 |
|
|
|
15 |
# Note: Graph is based on the real thing, but the data is invented.
|
|
|
16 |
# Data for plot #1: stackedbars:
|
|
|
17 |
$y_title1 = 'Number of positive tests';
|
|
|
18 |
$data1 = array(
|
|
|
19 |
array('1/09', 200, 200, 300),
|
|
|
20 |
array('2/09', 300, 100, 700),
|
|
|
21 |
array('3/09', 400, 200, 800),
|
|
|
22 |
array('4/09', 500, 300, 1200),
|
|
|
23 |
array('5/09', 400, 400, 2500),
|
|
|
24 |
array('6/09', 500, 600, 3600),
|
|
|
25 |
array('7/09', 400, 1200, 3300),
|
|
|
26 |
array('8/09', 300, 1500, 2500),
|
|
|
27 |
array('9/09', 200, 1900, 800),
|
|
|
28 |
array('10/09', 100, 2000, 200),
|
|
|
29 |
array('11/09', 100, 2500, 100),
|
|
|
30 |
array('12/09', 100, 2700, 200),
|
|
|
31 |
);
|
|
|
32 |
$legend1 = array('Strain A', 'Strain B', 'Strain C');
|
|
|
33 |
|
|
|
34 |
# Data for plot #2: linepoints:
|
|
|
35 |
$y_title2 = 'Percent Positive';
|
|
|
36 |
$data2 = array(
|
|
|
37 |
array('1/09', 5),
|
|
|
38 |
array('2/09', 10),
|
|
|
39 |
array('3/09', 15),
|
|
|
40 |
array('4/09', 30),
|
|
|
41 |
array('5/09', 40),
|
|
|
42 |
array('6/09', 45),
|
|
|
43 |
array('7/09', 47),
|
|
|
44 |
array('8/09', 35),
|
|
|
45 |
array('9/09', 25),
|
|
|
46 |
array('10/09', 20),
|
|
|
47 |
array('11/09', 25),
|
|
|
48 |
array('12/09', 30),
|
|
|
49 |
);
|
|
|
50 |
$legend2 = array('% positive');
|
|
|
51 |
|
|
|
52 |
$plot = new PHPlot(800, 600);
|
|
|
53 |
$plot->SetImageBorderType('plain'); // For presentation in the manual
|
|
|
54 |
$plot->SetPrintImage(False); // Defer output until the end
|
|
|
55 |
$plot->SetTitle($title);
|
|
|
56 |
$plot->SetPlotBgColor('gray');
|
|
|
57 |
$plot->SetLightGridColor('black'); // So grid stands out from background
|
|
|
58 |
|
|
|
59 |
# Plot 1
|
|
|
60 |
$plot->SetDrawPlotAreaBackground(True);
|
|
|
61 |
$plot->SetPlotType('stackedbars');
|
|
|
62 |
$plot->SetDataType('text-data');
|
|
|
63 |
$plot->SetDataValues($data1);
|
|
|
64 |
$plot->SetYTitle($y_title1);
|
|
|
65 |
# Set and position legend #1:
|
|
|
66 |
$plot->SetLegend($legend1);
|
|
|
67 |
$plot->SetLegendPixels(5, 30);
|
|
|
68 |
# Set margins to leave room for plot 2 Y title on the right.
|
|
|
69 |
$plot->SetMarginsPixels(120, 120);
|
|
|
70 |
# Specify Y range of these data sets:
|
|
|
71 |
$plot->SetPlotAreaWorld(NULL, 0, NULL, 5000);
|
|
|
72 |
$plot->SetYTickIncrement(500);
|
|
|
73 |
$plot->SetXTickLabelPos('none');
|
|
|
74 |
$plot->SetXTickPos('none');
|
|
|
75 |
# Format Y tick labels as integers, with thousands separator:
|
|
|
76 |
$plot->SetYLabelType('data', 0);
|
|
|
77 |
$plot->DrawGraph();
|
|
|
78 |
|
|
|
79 |
# Plot 2
|
|
|
80 |
$plot->SetDrawPlotAreaBackground(False); // Cancel background
|
|
|
81 |
$plot->SetDrawYGrid(False); // Cancel grid, already drawn
|
|
|
82 |
$plot->SetPlotType('linepoints');
|
|
|
83 |
$plot->SetDataValues($data2);
|
|
|
84 |
# Set Y title for plot #2 and position it on the right side:
|
|
|
85 |
$plot->SetYTitle($y_title2, 'plotright');
|
|
|
86 |
# Set and position legend #2:
|
|
|
87 |
$plot->SetLegend($legend2);
|
|
|
88 |
$plot->SetLegendPixels(690, 30);
|
|
|
89 |
# Specify Y range of this data set:
|
|
|
90 |
$plot->SetPlotAreaWorld(NULL, 0, NULL, 50);
|
|
|
91 |
$plot->SetYTickIncrement(10);
|
|
|
92 |
$plot->SetYTickPos('plotright');
|
|
|
93 |
$plot->SetYTickLabelPos('plotright');
|
|
|
94 |
$plot->SetDataColors('black');
|
|
|
95 |
# Format Y tick labels as integers with trailing percent sign:
|
|
|
96 |
$plot->SetYLabelType('data', 0, '', '%');
|
|
|
97 |
$plot->DrawGraph();
|
|
|
98 |
|
|
|
99 |
# Now output the graph with both plots:
|
|
|
100 |
$plot->PrintImage();
|
|
|
101 |
</pre></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ex-linepoints2.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="examples.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ex-legendshape.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.33. Example - Linepoints Plot with Data Value Labels </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.35. Example - Legend with Shape Markers</td></tr></table></div></body></html>
|