|
|
JavaScript Code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?php echo '<script id="source"> $(function () { var d = ['; $result=mysql_query("SELECT * FROM tabelle WHERE user=$userid ORDER BY datum ASC"); while($row=mysql_fetch_array($result)) { $timestamp = strtotime(''.$row[datum].''); echo '['; echo $timestamp; echo ','; echo $row[wert]; echo '],'; } echo '];'; echo ' for (var i = 0; i < d.length; ++i) d[i][0] += 60 * 60 * 1000; function weekendAreas(axes) { var markings = []; var d = new Date(axes.xaxis.min); d.setUTCDate(d.getUTCDate() - ((d.getUTCDay() + 1) % 7)) d.setUTCSeconds(0); d.setUTCMinutes(0); d.setUTCHours(0); var i = d.getTime(); do { markings.push({ xaxis: { from: i, to: i + 2 * 24 * 60 * 60 * 1000 } }); i += 7 * 24 * 60 * 60 * 1000; } while (i < axes.xaxis.max); return markings; } var options = { xaxis: { mode: "time" }, selection: { mode: "x" }, grid: { markings: weekendAreas } }; var plot = $.plot($("#placeholder"), [{ data: d, label: "Gewicht", color: "green", lineWidth: 12}], [d], options); var overview = $.plot($("#overview"), [d], { series: { lines: { show: true, lineWidth: 1 , fill: true}, shadowSize: 0 }, xaxis: { ticks: [], mode: "time" }, yaxis: { ticks: [], min: 0, autoscaleMargin: 0.1 }, selection: { mode: "x" } }); $("#placeholder").bind("plotselected", function (event, ranges) { plot = $.plot($("#placeholder"), [d], $.extend(true, {}, options, { xaxis: { min: ranges.xaxis.from, max: ranges.xaxis.to } })); overview.setSelection(ranges, true); }); $("#overview").bind("plotselected", function (event, ranges) { plot.setSelection(ranges); }); }); </script>'; |

|
|
Source code |
1 |
$timestamp = strtotime(''.$row[datum].'');
|
|
|
Source code |
1 |
$timestamp = strtotime($row[datum]); |
|
|
Source code |
1 |
d[i][0] += 60 * 60 * 1000; |

|
|
JavaScript Code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// first correct the timestamps - they are recorded as the daily 27 // midnights in UTC+0100, but Flot always displays dates in UTC 28 // so we have to add one hour to hit the midnights in the plot 29 for (var i = 0; i < d.length; ++i) 30 d[i][0] += 60 * 60 * 1000; 31 32 // helper for returning the weekends in a period 33 function weekendAreas(axes) { 34 var markings = []; 35 var d = new Date(axes.xaxis.min); 36 // go to the first Saturday 37 d.setUTCDate(d.getUTCDate() - ((d.getUTCDay() + 1) % 7)) 38 d.setUTCSeconds(0); 39 d.setUTCMinutes(0); 40 d.setUTCHours(0); 41 var i = d.getTime(); 42 do { 43 // when we don't set yaxis, the rectangle automatically 44 // extends to infinity upwards and downwards 45 markings.push({ xaxis: { from: i, to: i + 2 * 24 * 60 * 60 * 1000 } }); 46 i += 7 * 24 * 60 * 60 * 1000; 47 } while (i < axes.xaxis.max); 48 49 return markings; 50 } |
Quoted
Time series data
================
Time series are a bit more difficult than scalar data because
calendars don't follow a simple base 10 system. For many cases, Flot
abstracts most of this away, but it can still be a bit difficult to
get the data into Flot. So we'll first discuss the data format.
The time series support in Flot is based on Javascript timestamps,
i.e. everywhere a time value is expected or handed over, a Javascript
timestamp number is used. This is a number, not a Date object. A
Javascript timestamp is the number of milliseconds since January 1,
1970 00:00:00 UTC. This is almost the same as Unix timestamps, except it's
in milliseconds, so remember to multiply by 1000!
You can see a timestamp like this
alert((new Date()).getTime())
|
|
JavaScript Code |
1 2 3 4 5 6 7 |
var data = [[1325349710000,49],[1325436110000,36],[1325522510000,25],[1325608910000,16],[1325695310000,9]]; $.plot($("#placeholder"), [data], { xaxis: { mode: "time", timeformat: "%d.%m.%y" } }); |
|
|
PHP Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
echo '<script id="source"> $(function () { var data = ['; $result=mysql_query("SELECT * FROM tabelle WHERE user=$userid ORDER BY datum DESC LIMIT 5"); while($row=mysql_fetch_array($result)) { $timestamp = strtotime($row[datum]); echo '['; echo $timestamp; echo ','; echo $row[wert]; echo '],'; } echo '];'; echo ' $.plot($("#placeholder"), [data], { xaxis: { mode: "time", timeformat: "%d.%m.%y" } }); }); </script>'; |
|
|
Source code |
10 |
$timestamp = strtotime($row['datum']) * 1000; |
Quoted
The following
specifiers are supported
%h: hours
%H: hours (left-padded with a zero)
%M: minutes (left-padded with a zero)
%S: seconds (left-padded with a zero)
%d: day of month (1-31), use %0d for zero-padding
%m: month (1-12), use %0m for zero-padding
%y: year (four digits)
%b: month name (customizable)
%p: am/pm, additionally switches %h/%H to 12 hour instead of 24
%P: AM/PM (uppercase version of %p)
Inserting a zero like %0m or %0d means that the specifier will be
left-padded with a zero if it's only single-digit. So %y-%0m-%0d
results in unambigious ISO timestamps like 2007-05-10 (for May 10th).
You can customize the month names with the "monthNames" option. For
instance, for Danish you might specify:
monthNames: ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"]