An alternative implementation of d3's quantitative scales
An alternative to d3's quantitative scales that handles multiple axes a little more conveniently.
I've found that in larger d3 projects I tend to create a few duplicate scales across multiple charts, when really they'd be easier to manage/update them as a group: being passed around into each chart as required, responding to updates being made in other parts of the code.
$ npm install --save axes
var linedata = require'./linedata.json'var bardata = require'./bardata.json'var d3 = require'd3'var axes = require'axes'def'barX'domain0 bardatalengthdef'barY'domain0 d3maxbardatadef'lineX'domain0 linedatalengthdef'lineY'domain0 d3maxlinedataroot// Alias your scales so they play nice// with the code you're giving it.axesbarX2 // 0.5axesalias x: 'barX' x2 // 0.5// Throw them into your chartsrequire'./barchart'axes: axesaliasx: 'barX'y: 'barY'require'./linechart'axes: axesaliasx: 'lineX'x: 'lineY'// Use `axis.map` for alternative value// mappings.var angle = axesbarXmapreturn nvalue * MathPI * 2angle value: 2 // 3.14159265...
Returns an anonymous scale, which is very similar to
d3.scale.linear, but a
more limited API.
Takes an 2-element array defining the minimum and maximum input values for the scale.
Takes an 2-element array defining the minimum and maximum output values for the scale.
Returns a number between
range depending on how far it is
The "update" event is called on
handler every time the axis'
domain properties are updated.
Creates a copy of the axis, so that you can change its
values without altering the original one.
Returns a scale that maps its output according to
map. The initial value will
be scaled based on
axis's output. You can update these values in the
original scale and the scale's range will update accordingly too.
The returned scale essentially boils down to:
axismapmappern === mapperaxisn
Returns a new group of axes.
Returns a named scale, attached to this group.
Returns the group of axes.
def methods on each group member will be called from
the group, to make for easier chaining.
Creates a copy of the group's member called
Returns a copy of the group, while preserving the original references to each
map is an object: the keys determine the new name, and the values
determine the old one.
var axes = require'axes'def'oldX'range0 100var aliased = axesaliasoldX: 'newX'axesoldX0.5 // 50aliasednewX0.5 // 50aliasedoldX0.5 // Object #<Object> has no method 'oldX'
Copies the whole group, copying each member reference as well so you can make can changes to this copy without having to worry about altering the other scales.