javascript - I cannot scale my font-size with D3 domain and range...? -
i looking scale book 'titles' in below dataset depending on number of 'weeks' have been on best-sellers list...so if book's 1st week on list, title appear in 18pt, , if book's 20th week on list (or maximum number week) title appear in 72pt.
e.g. of dataset:
var hardcover_fiction = [ { title: "missing you", author: "harlan coben", rank: 1, last: 0, weeks: 1 }, { title: "raising steam", author: "terry pratchett", rank: 2, last: 0, weeks: 20 } ];
e.g. of d3 code:
.style("font-size", function(d) { return textscale(d.weeks) + "px"; }) var textscale = d3.scale.linear() .domain([1, d3.max(hardcover_fiction.weeks)] .range([18, 72]);
i'm pretty stumped...any appreciated! :)
you're using d3.max
incorrectly. first argument has array. second (optional) argument function extract relevant value each element in array.
the call d3.max(hardcover_fiction.weeks)
returns 0 because hardcover_fiction.weeks
doesn't exist. need
d3.max(hardcover_fiction, function(d){return d.weeks;})
the rest of code snippet should work expected.
Comments
Post a Comment