Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's another example similar to mbeasley's: <a href="http://jsfiddle.net/2N2rt/15/">http://jsfiddle.net/2N2rt/15/</a> which adds axis, colors, and flips the chart right side up. First, I massaged your data just a little bit. Works the best if you have an array of values and I used keys to make getting to the <code>name</code> and <code>values</code> properties easier.</p> <pre><code>var data = [ {name: 'John', values: [0,1,3,9, 8, 7]}, {name: 'Harry', values: [0, 10, 7, 1, 1, 11]}, {name: 'Steve', values: [3, 1, 4, 4, 4, 17]}, {name: 'Adam', values: [4, 77, 2, 13, 11, 13]} ]; </code></pre> <p>Generally in <code>d3</code> you set up the chart like this which determines the size of the actual graph and the surrounding margins.</p> <pre><code>var margin = {top: 20, right: 80, bottom: 30, left: 50}, width = 640 - margin.left - margin.right, height = 380 - margin.top - margin.bottom; </code></pre> <p>Then you can create your scales based on your data. Though you don't have to create them, they make positioning elements on the chart much easier by converting values to points. The <code>domain</code> is the min and max values that your data has, while the <code>range</code> is the min and max values of the size of your graph. Note that the <code>range</code> for the <code>y axis</code> gets reversed which puts (0,0) in the bottom left hand corner (usually 0 for the y axis is at the top). </p> <pre><code>var x = d3.scale.linear() .domain([0, d3.max(data, function(d) { return d.values.length - 1; })]) .range([0, width]); var y = d3.scale.linear() .domain([d3.min(data, function(d) { return d3.min(d.values); }), d3.max(data, function(d) { return d3.max(d.values); })]) .range([height, 0]); </code></pre> <p><code>d3</code> has a couple of features for automatically creating color scales for you. Just set it up with the keys that you want to use and the number of colors (I think there are 10 and 20 color options).</p> <pre><code>var color = d3.scale.category10() .domain(d3.keys(data[0]).filter(function(key) { return key === "name"; })); </code></pre> <p>Here we use our scales to setup the x and y axis. There are lots of different options for the axes. TickFormat is handy to change how the ticks look, <code>d3.format</code> has lots of different options so that you rarely have to create a custom formatter.</p> <pre><code>var xAxis = d3.svg.axis() .scale(x) .tickFormat(d3.format('d')) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); </code></pre> <p>Here we create a reusable function that knows how to draw a line. We'll pass each datapoint for each person into the line function later on. Note that <code>d</code> holds the current set of values that we are graphing and <code>i</code> is the index of the current values within our original data array.</p> <pre><code>var line = d3.svg.line() .interpolate("basis") .x(function(d, i) { return x(i); }) .y(function(d, i) { return y(d); }); </code></pre> <p>Finally we can start adding things to our chart, first creating and positioning the main svg element.</p> <pre><code>var svg = d3.select("#chart").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); </code></pre> <p>Then appending each axis.</p> <pre><code> svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis); </code></pre> <p>We append a group (<code>g</code>) for each person and associate the data with the group. We'll use this data when we draw the lines. We provide a <code>class</code> so that we can style the chart later if we want to.</p> <pre><code> var people = svg.selectAll(".people") .data(data) .enter().append("g") .attr("class", "people"); </code></pre> <p>Then finally draw the lines for each person.</p> <pre><code> people.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { return color(d.name); }); </code></pre> <p>I used your data to answer another question about drawing a grid when you have negative values. You can see that graph at <a href="http://jsfiddle.net/2y3er/2/">http://jsfiddle.net/2y3er/2/</a>.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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