scotthmurray / d3-book

Code examples for “Interactive Data Visualization for the Web”
http://d3book.com
Other
2.41k stars 1.79k forks source link

BUG in https://github.com/alignedleft/d3-book/blob/master/chapter_12/01_enter_merge_exit.html #22

Closed wolfgangvonludwigsburg closed 7 years ago

wolfgangvonludwigsburg commented 7 years ago

If you click 20 x 'Remove a data value' (hence dataset is total empty), then click 'Add a new data value', ... 01_enter_merge_exit.html:136 Uncaught TypeError: Cannot read property 'key' of undefined

Change 136: var lastKeyValue = dataset[ dataset.length -1 ].key; to:

    var lastKeyValue = (dataset.length)
        ?   dataset[ dataset.length -1 ].key
        :   -1

Best Wishes, Wolfgang

scotthmurray commented 7 years ago

Wow, you scared me for a minute there! The book just came out in print yesterday — how awful if I'd had a major error in the code!

You are right, of course, but this is an edge case I'm not worried about nor trying to address in this example. Also, I don't want to complicate the example by introducing a new sort of syntax.

Thanks for the suggestion, and let me know if you discover anything else!

wolfgangvonludwigsburg commented 7 years ago

Thanks for your reply!

Me -sure- would'nt have discovered it actually. But while studying your examples concerning "General Update Pattern", I came across to automate it ... e.g.

    d3.interval( ()=>{
        let what = Math.round( Math.random() )
        if (what) {
            //Add a data value
            //...
            var newNumber    = ...
            var lastKeyValue = ...

            dataset.push({
                key     : lastKeyValue + 1,
                value   : newNumber
            });
        } else {
            //Remove a value
            dataset.shift();    //Remove one value from dataset
        }

        update()    //= "General Update Pattern" (without transitions) ...
    }, 100)

... and then wondered about the error break.

Yes, I do have some more concerns (not errors, but wishes), I'll would prepare and report them later to you ...