gwatts / jquery.sparkline

A plugin for the jQuery javascript library to generate small sparkline charts directly in the browser
http://omnipotent.net/jquery.sparkline/
1.24k stars 278 forks source link

The last bar of the very first barchart is not highlighted on mouseover #108

Open MarvMartian opened 10 years ago

MarvMartian commented 10 years ago

Repro: Modify the "simple" demo from the docs to render only one bar chart, as follows (note the large data element as the last bar, so we can see it; it is the only '10'):

screen shot 2014-01-07 at 1 15 44 pm

        // The last bar is not highlighted on mouseover (bug)
        var myvalues = [8,8,5,7,4,5,10];
        $('.dynamicbar').sparkline(myvalues, {type: 'bar', 
            barColor: 'green', highlightColor: 'red'} );

Only the very first bar chart has the issue. Subsequent identical bar charts added to the same demo file work as advertised. Same issue on Chrome 31, Safari 7, Firefox 26, IE11. Note: I'm using jquery-2.0.3.

MarvMartian commented 10 years ago

Simple, it is cognitive dissonance on the use of shapes.id = 0. Zero denotes a null value, but it's returned as the very first id from renderRegion (and since we work right to left on bars, the very first bar is the 0th and therefore it is forever "nullish" in nature and therefore won't highlight).

Here's a hack fix that I'm reluctant to commit (because it's a hack):

1328: shapes = this.renderRegion(i);

            // ********* insert after 1328
            if (shapes.id == 0) {
                // hack to avoid using '0' for id's, because a 0 id is used
                // for null values later
                shapes = this.renderRegion(i);  // better go get another shapes.id that isn't zero
            }
            // *********** end of insertion