01.05.2017 Views

348957348957

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

var scale_y = d3.scale.linear()<br />

.domain([0, d3.max(column_data, function (d) {<br />

return d.quantity;<br />

})])<br />

.range([0, total_height]);<br />

var scale_x = d3.scale.ordinal()<br />

.domain(d3.range(column_data.length))<br />

.rangeRoundBands([0, total_width], 0.05);<br />

One of the main features of D3.js is its ability to do difficult and tedious calculations under the<br />

hood. A key part of this work is done in scaling plots. If you want to automatically map the range<br />

of your data to actual pixel dimensions in your graph, you can use the scale function to change<br />

either or both parameters without having to do any manual recalculations.<br />

From this snippet, you can see that the total height of the graph has been specified as a .range.<br />

This means you no longer need to calculate margins or positions or how the values map to fixed<br />

positions.<br />

The following section from Listing 10-1 shows that the greatest quantity value in the total height<br />

range is 25:<br />

var column_data = [{<br />

position: 0,<br />

quantity: 5<br />

}, {<br />

position: 1,<br />

quantity: 20<br />

}, {<br />

position: 2,<br />

quantity: 15<br />

}, {<br />

position: 3,<br />

quantity: 25<br />

}, {<br />

position: 4,<br />

quantity: 10<br />

}];<br />

var total_width = 400;<br />

var total_height = 200;<br />

By automatically mapping the range of your data (0[nd]25) to the actual pixel height of your graph,<br />

you can change either or both parameters without having to do any manual recalculations.<br />

Although you can use D3.js to automatically handle the placement of axis labels and tick<br />

marks, the library can also handle all calculations involved in date ranges. This functionality<br />

leaves you free to concentrate on the overall look and feel of the visualization, instead of<br />

having to tinker with its mechanics.<br />

Getting to know transitions and interactions<br />

The true beauty of D3.js lies in how you can use it to easily incorporate dynamic elements into<br />

your web-based data visualization. If you want to encourage users to explore and analyze the data<br />

in your dynamic visualization, create features that offer a lot of user interactivity. Also,<br />

incorporating transitions into your dynamic visualization can help you capture the interest of your<br />

audience. Transitions in D3.js build the aesthetic appeal of a data visualization by incorporating

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!