Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are various ways to do this in d3, with their own pluses and minuses.</p> <p>Frankly, this question is too vague for this forum, but I found the challenge you present kind of interesting. So I mocked up one way to do the Y axis. <a href="http://jsfiddle.net/7Jpw2/2/" rel="nofollow">http://jsfiddle.net/7Jpw2/2/</a></p> <p>It uses an ordinal scale for the states and another ordinal scale for the years overlaid on top of the first one</p> <p>This may not be a complete answer, but it should get you going pretty far.</p> <pre><code>var categories = [ { year:3, state:'NY' }, { year:3, state:'CA' }, { year:3, state:'TX' }, { year:2, state:'NY' }, { year:2, state:'CA' }, { year:2, state:'TX' }, { year:1, state:'NY' }, { year:1, state:'CA' }, { year:1, state:'TX' } ]; // IMPORTANT! This is a way to make each state @ year unique, by returning a concatenation of state and year categories.forEach(function(cat) { cat.toString = function() { return this.state + '_' + this.year } }); // These year values should ideally be extracted from categories array above, not hardcoded as below var years = ['Year 1', 'Year 2', 'Year 3']; var svg = d3.select('body').append('svg'); // Create 2 axes: // First a state-name axis // Then a year axis, in which everything except the text is invisible (via css fill:none) var statesScale = d3.scale.ordinal() .domain(categories) .rangeBands([50,300]); var statesAxis = d3.svg.axis() .orient('left') .tickFormat(function(d) { return d.state; }) .scale(statesScale) var yearsScale = d3.scale.ordinal() .domain(years) .rangeBands([50,300]); var yearsAxis = d3.svg.axis() .orient('left') .tickSize(50) // Distances the year label from state labels .scale(yearsScale) svg.append('g') .attr('class', 'states-axis') .attr('transform', 'translate(150,0)') .call(statesAxis); svg.append('g') .attr('class', 'years-axis') .attr('transform', 'translate(150,0)') .call(yearsAxis); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload