3

I have a simple line chart that contains 2 series, with the x-axis being a date and the y-axis an integer. The code that illustrates this is as follows:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" language="javascript" src="flot/jquery.js"></script>
    <script type="text/javascript" language="javascript" src="flot/jquery.flot.js"></script>
    <style type="text/css">
        #overview-plot24 {
            width: 94%;
            margin-left: 20px;
            height: 220px;
        }
    </style>

    <script type="text/javascript">
        $(function() {
            var plotOptions = {
                //Options go here
                xaxis: {
                    mode: "time",
                    tickLength: 5,
                    reserveSpace: true,
                    autoscaleMargin: 0.01
                },
                yaxis: {
                    min: 0
                },
                legend: {
                    show: false
                },
                series: {
                    lineWidth: 1,
                    shadowSize: 0
                },
                grid: {
                    hoverable: true,
                    clickable: true
                }
            };

            var plot2 = $.plot(
                    '#overview-plot24', [
                        {
                            label: "Alerts",
                            color: "#FC8200",
                            data: [
                                    [Date.parse("2013-03-19 15:00"), 9650],
                                    [Date.parse("2013-03-19 16:00"), 33124],                                
                                    [Date.parse("2013-03-19 17:00"), 27806],
                                    [Date.parse("2013-03-19 18:00"), 24143],
                                    [Date.parse("2013-03-19 19:00"), 7573],
                            ]},
                        {
                            label: "Scores",
                            color: "#8000FF",
                            data: [                            
                                    [Date.parse("2013-03-19 15:00"), 26407],
                                    [Date.parse("2013-03-19 16:00"), 93973],
                                    [Date.parse("2013-03-19 17:00"), 77760],                                
                                    [Date.parse("2013-03-19 18:00"), 68715],
                                    [Date.parse("2013-03-19 19:00"), 20383],
                            ]
                        }
                    ],
                    plotOptions
            );
        });
    </script>
</head>

<body>
    <div id="overview-plot24"></div>
</body>
</html>

This is correctly rendered in Chrome and Opera, but the series fails to be rendered on Safari and Firefox. Correct renderingChrome renders is correctly. Bad renderingSafari and Firefox renders it incorrectly.

This is perplexing as the examples on the flot web page render correctly on all browsers and I'm struggling to see where my code differs!

For those interested in running the code directly, zip archive containing all you need is available.

CadentOrange
  • 3,263
  • 1
  • 34
  • 52
  • This isn't the problem, but note that your lists have a comma after the last item. Modern browsers don't care, but that sort of thing (in object literals especially) will cause errors in older ones, like IE7. – DNS Mar 20 '13 at 13:22
  • That's not a problem for me as I am fortunate enough to be able to target IE9 or later. – CadentOrange Mar 20 '13 at 14:06

3 Answers3

5

The problem is not in Flot or its rendering but in the JavaScript, specifically in the format you use for date.

Format you're using for date is not valid (see horizontal axis in the rendered chart) because that format 2013-03-19 19:00 is recognized by IE and Chrome but not FF and Safari.

The only format you're sure every browser will read is YYYY-MM-DDTHH:mm:ss.sssZ so in your case you should change string in your code to 2013-03-19T19:00. Take a look to this and this posts on SO for other details (or this little tutorial about dates).

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
3

Could this be a problem with the Date.parse function not working as you expect on Safari and Firefox? I think the date formats that are recognised are implementation dependent, and may not be consistent across different browsers.

See this question for more details

Community
  • 1
  • 1
codebox
  • 19,927
  • 9
  • 63
  • 81
  • That was the problem. Changing the date format made it render correctly. It is strange that there was no error in the Javascript console. – CadentOrange Mar 20 '13 at 13:03
  • Date.parse just returns 'NaN' rather than throwing an error if it can't understand the date, and I guess Flot doesn't throw errors for NaN values either. – codebox Mar 20 '13 at 13:05
1

I had this same issue and, although I fixed the date format it didn't solve the problem. The issue turned out to be a bug in Flot where it will not graph the data if you specify min and max options for time values on the x-axis.

Once I removed the min and max it worked like a charm in all browsers.

Alex W
  • 37,233
  • 13
  • 109
  • 109