Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7999411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:29:57+00:00 2026-06-04T15:29:57+00:00

I am creating a flot graph that very closely resemble the StackOverflow reputation graph.

  • 0

I am creating a flot graph that very closely resemble the StackOverflow reputation graph. There is a small graph that shows a birds-eye level of the data which allows you to select a range and show it, zoomed, on a larger graph. When either graph is hovered, the legend text is updated to reflect the values at that x-position.

There are two problems I am encountering, and there are absolutely no errors to be found in my console, etc…

1: When I select a range on the “zoomer” graph, the larger “detail” graph zooms in properly, but the code that causes the legend text to update when an x-value is hovered stops working. The point Item mouse-overs (which cause a tooltip) still work and the two bits of code are in the same event. Weird.

2: The date format gets thrown out of whack once I zoom in. The dates are reverted to timestamps despite my timeformat being specified (and it actually works on the first plot, before a zoom takes place).


I have recorded a short screencast to show the problem in action:

http://www.screenr.com/KUX8


I have setup a JSFiddle that shows the date/time formatting issue. For some reason, in that environment, none of the hovering events are working as expected.

http://jsfiddle.net/Kzg3r/2/


Am I missing something that would cause these two issues?


//doPlot is originally called where ranges=null. This plots the entire universe
//of data on both charts. Once a range is selected, this is called again with
//ranges


var plot=null;
var plot2=null;
var updateLegendTimeout = null;
var latestPosition = null;   
var datasets=null;
var currRange=null;

function doPlot(ranges){
    currRange=ranges;

    clearTimeout(updateLegendTimeout);
    updateLegendTimeout = null;

    var options = { //Options for large graph
        lines: { show: true },
        points: { show: true },
        grid: { hoverable: true, clickable: false, autoHighlight: false, backgroundColor: { colors: ["#fff", "#eee"] }, markings: weekendAreas   },
        xaxis: {mode: 'time', timeformat: "%m/%d", minTickSize: [1, "day"]},
        crosshair: { mode: "x" },
        legend: {
            show: true,
            container: $('#legend'),
            noColumns: 2
        }
    };

    var options2 = {  //options for small zoomer graph
        lines: { show: true },
        points: { show: false },
        grid: { hoverable: true, clickable: true, autoHighlight: false, backgroundColor: { colors: ["#fff", "#eee"] }, markings: weekendAreas   },
        xaxis: {mode: 'time', timeformat: "%m/%d", minTickSize: [1, "month"]},
        crosshair: { mode: "x" },
        selection: { mode: "x" },
        legend: { show: false }
    };    


    if (currRange){ //if there is a range specified, extend options to include it.
        options = $.extend(options, {
            xaxis: { 
                min: currRange.xaxis.from, 
                max: currRange.xaxis.to 
            }
        });

    }

    //Plot Large Graph with (or without) given range
    plot = $.plot($("#placeholder"), datasets,options);


    //plot the zoomer graph, only if it is null (it is set to null before ajax request for new chart)
    if (!plot2) { 
        plot2 = $.plot($("#zoomer"), datasets, options2);


        $("#zoomer").unbind("plotselected").bind("plotselected", function (event, ranges) {

            //unbind/re-bind plotselected event to zoom in when a range is selected
            doPlot(ranges);

        }).unbind("plothover").bind("plothover",  function (event, pos, item) {

            //unbind/re-bind plothover event to to show tooltips and to change legend values
            latestPosition = pos;
            if (!updateLegendTimeout)
                updateLegendTimeout = setTimeout(function(){
                    updateLegend(plot2);
                }, 50);

            if (item) {
                if (previousPoint != item.dataIndex) {
                    previousPoint = item.dataIndex;

                    $("#tooltip").remove();
                    var x = item.datapoint[0].toFixed(2),
                        y = item.datapoint[1].toFixed(2);
                    var text = item.series.label.split(' = ');

                    showTooltip(item.pageX, item.pageY,
                                text[0] + ' ('+y+')');
                }
            }
            else {
                $("#tooltip").remove();
                previousPoint = null;            
            }
        });         
    }

    doBinds();          
}


function doBinds(){
    $("#placeholder").unbind("plothover").bind("plothover",  function (event, pos, item) {
        latestPosition = pos;
        if (!updateLegendTimeout)
            updateLegendTimeout = setTimeout(function(){
                updateLegend(plot);
            }, 50);

        if (item) {
            if (previousPoint != item.dataIndex) {
                previousPoint = item.dataIndex;

                $("#tooltip").remove();
                var x = item.datapoint[0].toFixed(2),
                    y = item.datapoint[1].toFixed(2);
                var text = item.series.label.split(' = ');

                showTooltip(item.pageX, item.pageY,
                            text[0] + ' ('+y+')');
            }
        }
        else {
            $("#tooltip").remove();
            previousPoint = null;            
        }    
    });  
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:29:58+00:00Added an answer on June 4, 2026 at 3:29 pm

    The issue with the ticks on your x-axis has to do with the way you extend the plot options in the doPlot function if there is a range selected. You need to call jQuery.extend with `true as the first argument so that it does a deep copy. Otherwise when you extend the options object with the x-axis min and max, it overwrites the x-axis object with the mode set to time. Replace that bit with this and it will fix that issue.

        if (currRange){ //if there is a range specified, extend options to include it.
            options = $.extend(true, options, {
                xaxis: { 
                    min: currRange.xaxis.from, 
                    max: currRange.xaxis.to 
                }
            });
    
        }
    

    EDIT:

    So, your first issue occurs because the legend items are removed and new ones added when the chart is redrawn. You store references to the original legend labels on document ready, but when the chart redraws, these are no longer attached to the DOM. You are updating the labels on those detached elements, and not on the newly created legend labels. Try re-initializing the legends collection when you update the legend like so:

    //THIS IS MOST LIKELY WHERE THE PROBLEM IS OCCURRING!!!!
    function updateLegend(thePlot) {
        legends = $('.legendLabel');   
        clearTimeout(updateLegendTimeout);
        updateLegendTimeout = null;
    
        var pos = latestPosition;
    
        var axes = thePlot.getAxes();
        if (pos.x < axes.xaxis.min || pos.x > axes.xaxis.max ||
            pos.y < axes.yaxis.min || pos.y > axes.yaxis.max)
            return;
    
        var i, j, dataset = thePlot.getData();
        for (i = 0; i < dataset.length; ++i) {
            var series = dataset[i];
    
            // find the nearest points, x-wise
            for (j = 0; j < series.data.length; ++j)
                if (series.data[j][0] > pos.x)
                    break;
    
            // now interpolate
            var y, p1 = series.data[j - 1], p2 = series.data[j];
            if (p1 == null)
                y = p2[1];
            else if (p2 == null)
                y = p1[1];
            else
                y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
    
            legends.eq(i).text(series.label.replace(/=.*/, "= " + y.toFixed(2)));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating Facebook event for specific page with FB Graph API There is a thread
I'm creating a graph using flot javascript library. I have enabled clickable event and
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I am using jQuery/FLOT to draw a graph, I would like for the user
I have had a lot of success creating graphs with Flot. However, I can
Creating a calculator-like dialog, I noticed that quickly clicking on a button in IE
Creating an installer for possible remote systems so that if they do not have
After seeing the cool new reputation tab on the stackoverflow user page, I was
Creating a patch is very easy in SubVersion, With Tortoise, you right-click and select
Creating a patch utility that will update my current website with my patch. when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.