You are not logged in.

  • Login

1

Sunday, January 1st 2012, 11:36pm

Flot mit Datum?

Hallo zusammen,

ich habe jetzt in meine Seite ein jquery flot eingebunden (http://code.google.com/p/flot/), klappt auch soweit gut, nur wird kein Datum angezeigt stattdessen irgendwelche zahlen (z.B. "1310000000"). So sieht es aus:




So sieht der Code aus:

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>';

2

Monday, January 2nd 2012, 11:54am

Setz mal bei deinen Optionen neben dem time-mode auch das gewünschte Format.


JavaScript Code

1
2
var options = {
        xaxis: { mode: "time" },


JavaScript Code

1
2
var options = {
        xaxis: { mode: "time", timeformat: "%d.%m.%y" },

3

Monday, January 2nd 2012, 12:36pm

Dankeschön für den Tipp, hat aber an der Ausgabe leider nichts geändert :(

4

Wednesday, January 4th 2012, 9:50am

Hat da keiner eine Lösung? :(

Habe mir zwar die Beispiele auf http://people.iola.dk/olau/flot/examples/ angeschaut, aber den Fehler trotzdem nicht gefunden...

5

Wednesday, January 4th 2012, 1:15pm

ich kanns leider immernoch nicht selber testen, aber eventuell kommt strtotime mit dem Attribut nicht klar? Je nachdem ob dein Timestamp als UNIX-Timestamp vorliegt oder als einfaches Datum?
anstatt:

Source code

1
$timestamp = strtotime(''.$row[datum].'');

einfach nur:

Source code

1
$timestamp = strtotime($row[datum]);


eine Frage am Rande:
Was bewirkt diese Zeile (24) bei dir? Ich kapier grad nicht, auf was du da hinauswillst

Source code

1
d[i][0] += 60 * 60 * 1000;

6

Wednesday, January 4th 2012, 2:28pm

Dein ersten Tipp hatte ich auch schon ausprobiert, gibt leider die gleiche Ausgabe :(

Die Zeile habe ich hiervon (Kommentierung gerade im Netz gefunden):

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	    }

7

Saturday, January 7th 2012, 5:47pm

Zeig doch mal deinen geänderten Code. Ich habs ausprobiert und vince hat die die Antwort eigentlich schon gepostet.
Und hast du den Zeitstempel mit tausend multipliziert?

Siehe dazu auch http://flot.googlecode.com/svn/trunk/API.txt Kapitel Time Series Data

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())


Hier meine Lösung in der fiddle

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"
  }
});



8

Monday, January 9th 2012, 12:50am

Dankeschön! Dein Script funktioniert aber komischerweise nur mit deinen Werten und ich finde meinen Fehler nicht? Wie kommst du auf deine Timestamps?

Folgende Werte liegen beispielsweise in der Datenbank vor:
2012-01-04 27.9
2012-01-05 27.4
2012-01-06 56.2
2012-01-07 97.1
2012-01-08 107

Der Code zur Ausgabe lautet wie oben schon gezeigt:

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>';


Lass ich mir das Ergebnis ausgeben bekomme ich folgende Ausgabe im Code:
var data= [[1325977200,107],[1325890800,97.1],[1325804400,56.2],[1325718000,27.4],[1325631600,27.9]];

Dann habe ich mal bei einem Generator überprüfen lassen, ob ein Timestamp (Beispielsweise "1325977200") überhaupt korrekt ist. Laut http://www.aritso.net/index.php ist 1325977200 = 08.01.2012 um 00:00:00 Uhr - Würde also logischerweise klappen.

Angezeigt wird jedoch folgendes:


Was mache ich falsch?

9

Monday, January 9th 2012, 9:06pm

Wie gesagt: Du musst deinen Timestamp noch mit tausend multiplizieren.

Source code

10
$timestamp = strtotime($row['datum']) * 1000;

10

Tuesday, January 10th 2012, 1:00am

Super Danke, genau das war der Fehler!

Zum Schluss: Wie bekomme ich jetzt dd.mm.jjjj? bisher wird beispielsweise 1.6.2011 angezeigt und nicht 01.06.2011 obwohl laut http://php.net/manual/en/function.date.php ja das "d" schon für zwei Zeichen steht, oder? (d Day of the month, 2 digits with leading zeros)

11

Thursday, January 12th 2012, 8:15am

Oder kann man bei Flots kein deutsches Datum ausgeben lassen?

12

Thursday, January 12th 2012, 8:11pm

Siehe dazu http://flot.googlecode.com/svn/trunk/API.txt Kapitel Time Series Data

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"]

13

Friday, January 13th 2012, 5:52pm

Super danke, hat geklappt! :)

Similar threads

Social bookmarks