2 if ($("#sincos").length) {
3 var sin = [], cos = [];
5 for (var i = 0; i < 14; i += 0.5) {
6 sin.push([i, Math.sin(i) / i]);
7 cos.push([i, Math.cos(i)]);
10 var plot = $.plot($("#sincos"),
12 { data: sin, label: "sin(x)/x"},
13 { data: cos, label: "cos(x)" }
16 lines: { show: true },
17 points: { show: true }
19 grid: { hoverable: true, clickable: true, backgroundColor: { colors: ["#fff", "#eee"] } },
20 yaxis: { min: -1.2, max: 1.2 },
21 colors: ["#539F2E", "#3C67A5"]
24 function showTooltip(x, y, contents) {
25 $('<div id="tooltip">' + contents + '</div>').css({
30 border: '1px solid #fdd',
32 'background-color': '#dfeffc',
34 }).appendTo("body").fadeIn(200);
37 var previousPoint = null;
38 $("#sincos").bind("plothover", function (event, pos, item) {
39 $("#x").text(pos.x.toFixed(2));
40 $("#y").text(pos.y.toFixed(2));
43 if (previousPoint != item.dataIndex) {
44 previousPoint = item.dataIndex;
46 $("#tooltip").remove();
47 var x = item.datapoint[0].toFixed(2),
48 y = item.datapoint[1].toFixed(2);
50 showTooltip(item.pageX, item.pageY,
51 item.series.label + " of " + x + " = " + y);
55 $("#tooltip").remove();
61 $("#sincos").bind("plotclick", function (event, pos, item) {
63 $("#clickdata").text("You clicked point " + item.dataIndex + " in " + item.series.label + ".");
64 plot.highlight(item.series, item.datapoint);
70 if ($("#flotchart").length) {
72 for (var i = 0; i < Math.PI * 2; i += 0.25)
73 d1.push([i, Math.sin(i)]);
76 for (var i = 0; i < Math.PI * 2; i += 0.25)
77 d2.push([i, Math.cos(i)]);
80 for (var i = 0; i < Math.PI * 2; i += 0.1)
81 d3.push([i, Math.tan(i)]);
83 $.plot($("#flotchart"), [
84 { label: "sin(x)", data: d1},
85 { label: "cos(x)", data: d2},
86 { label: "tan(x)", data: d3}
89 lines: { show: true },
90 points: { show: true }
93 ticks: [0, [Math.PI / 2, "\u03c0/2"], [Math.PI, "\u03c0"], [Math.PI * 3 / 2, "3\u03c0/2"], [Math.PI * 2, "2\u03c0"]]
101 backgroundColor: { colors: ["#fff", "#eee"] }
107 if ($("#stackchart").length) {
109 for (var i = 0; i <= 10; i += 1)
110 d1.push([i, parseInt(Math.random() * 30)]);
113 for (var i = 0; i <= 10; i += 1)
114 d2.push([i, parseInt(Math.random() * 30)]);
117 for (var i = 0; i <= 10; i += 1)
118 d3.push([i, parseInt(Math.random() * 30)]);
120 var stack = 0, bars = true, lines = false, steps = false;
122 function plotWithOptions() {
123 $.plot($("#stackchart"), [ d1, d2, d3 ], {
126 lines: { show: lines, fill: true, steps: steps },
127 bars: { show: bars, barWidth: 0.6 }
134 $(".stackControls input").click(function (e) {
136 stack = $(this).val() == "With stacking" ? true : null;
139 $(".graphControls input").click(function (e) {
141 bars = $(this).val().indexOf("Bars") != -1;
142 lines = $(this).val().indexOf("Lines") != -1;
143 steps = $(this).val().indexOf("steps") != -1;
150 { label: "Internet Explorer", data: 12},
151 { label: "Mobile", data: 27},
152 { label: "Safari", data: 85},
153 { label: "Opera", data: 64},
154 { label: "Firefox", data: 90},
155 { label: "Chrome", data: 112}
158 if ($("#piechart").length) {
159 $.plot($("#piechart"), data,
175 function pieHover(event, pos, obj) {
178 percent = parseFloat(obj.series.percent).toFixed(2);
179 $("#hover").html('<span style="font-weight: bold; color: ' + obj.series.color + '">' + obj.series.label + ' (' + percent + '%)</span>');
182 $("#piechart").bind("plothover", pieHover);
186 if ($("#donutchart").length) {
187 $.plot($("#donutchart"), data,
202 // we use an inline data source in the example, usually data would
203 // be fetched from a server
204 var data = [], totalPoints = 300;
206 function getRandomData() {
208 data = data.slice(1);
211 while (data.length < totalPoints) {
212 var prev = data.length > 0 ? data[data.length - 1] : 50;
213 var y = prev + Math.random() * 10 - 5;
221 // zip the generated y values with the x values
223 for (var i = 0; i < data.length; ++i)
224 res.push([i, data[i]])
228 // setup control widget
229 var updateInterval = 30;
230 $("#updateInterval").val(updateInterval).change(function () {
231 var v = $(this).val();
232 if (v && !isNaN(+v)) {
234 if (updateInterval < 1)
236 if (updateInterval > 2000)
237 updateInterval = 2000;
238 $(this).val("" + updateInterval);
243 if ($("#realtimechart").length) {
245 series: { shadowSize: 1 }, // drawing is faster without shadows
246 yaxis: { min: 0, max: 100 },
247 xaxis: { show: false }
249 var plot = $.plot($("#realtimechart"), [ getRandomData() ], options);
252 plot.setData([ getRandomData() ]);
253 // since the axes don't change, we don't need to call plot.setupGrid()
256 setTimeout(update, updateInterval);