- Stop charts with no visible plots from crashing.
[dygraphs.git] / tests / underlay-callback.html
CommitLineData
e7746234
EC
1<html>
2 <head>
3 <title>Custom underlay callback</title>
4 <!--[if IE]>
a2b2c3a1 5 <script type="text/javascript" src="../excanvas.js"></script>
e7746234 6 <![endif]-->
d37dca40
DV
7 <script type="text/javascript" src="../strftime/strftime-min.js"></script>
8 <script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script>
e7746234
EC
9 <script type="text/javascript" src="../dygraph-canvas.js"></script>
10 <script type="text/javascript" src="../dygraph.js"></script>
11 <script type="text/javascript" src="data.js"></script>
12 </head>
13 <body>
14 <p>Should draw a two-colored background, split horizontally at 2.25:</p>
15 <div id="div_g" style="width:600px; height:300px;"></div>
16
17 <div id="status" style="width:100%; height:200px;"></div>
18
19 <script type="text/javascript">
20 s = document.getElementById("status");
21
22 g = new Dygraph(
23 document.getElementById("div_g"),
24 NoisyData, {
25 rollPeriod: 7,
26 showRoller: true,
27 errorBars: true,
28
04143109 29 underlayCallback: function(canvas, area, g) {
fdcf1ce4
RK
30 // Selecting a date in the middle of the graph.
31 var splitDate = new Date("2006-11-19").getTime();
5879c20d 32 var coords = g.toDomCoords(splitDate, 2.25);
04143109 33
5879c20d 34 // splitX and splitY are the coordinates on the canvas for (2006-11-19, 2.25).
fdcf1ce4
RK
35 var splitX = coords[0];
36 var splitY = coords[1];
37
5879c20d
RK
38 // The drawing area doesn't start at (0, 0), it starts at (area.x, area.y).
39 // That's why we subtract them from splitX and splitY. This gives us the
40 // actual distance from the upper-left hand corder of the graph itself.
41 var leftSideWidth = splitX - area.x;
42 var rightSideWidth = area.w - leftSideWidth;
43 var topHeight = splitY - area.y;
44 var bottomHeight = area.h - topHeight;
2fc82862 45
fdcf1ce4 46 // fillRect(x, y, width, height)
2fc82862 47 // Top section: y = (2.25, +Infinity)
5879c20d 48 // left: (x < 2006-11-19)
e7746234 49 canvas.fillStyle = 'lightblue';
5879c20d 50 canvas.fillRect(area.x, area.y, leftSideWidth, topHeight);
fdcf1ce4 51
5879c20d 52 // right: (x > 2006-11-19)
fdcf1ce4
RK
53 canvas.fillStyle = 'orange';
54 canvas.fillRect(splitX, area.y, rightSideWidth, topHeight);
55
56 // Bottom section: y = (-Infinity, 2.25)
57 canvas.fillStyle = 'pink';
58 canvas.fillRect(area.x, splitY, area.w, bottomHeight);
e7746234
EC
59 }
60 }
61 );
62 </script>
63 </body>
64</html>