admin管理员组

文章数量:1336289

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. Chrome renders is correctly. Safari 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.

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. Chrome renders is correctly. Safari 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.

Share asked Mar 20, 2013 at 12:54 CadentOrangeCadentOrange 3,3431 gold badge36 silver badges52 bronze badges 2
  • This isn't the problem, but note that your lists have a ma 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 Commented Mar 20, 2013 at 13:22
  • That's not a problem for me as I am fortunate enough to be able to target IE9 or later. – CadentOrange Commented Mar 20, 2013 at 14:06
Add a ment  | 

3 Answers 3

Reset to default 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).

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

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.

本文标签: javascriptFlot fails to render charts on certain browsersStack Overflow