score:1

Accepted answer

you can see a working example at http://jsfiddle.net/yhw6h/1/.

i did the following to coerce your data a bit. you'll want to use d3.csv to read in your csv file, but i hard coded the values for the example.

// you would use d3.csv('filename.csv', function (data) {...})
// in order to populate the data variable, i'm just hard coding it here

var data = [
    {provider:'10800', 'service date': '2007-12-03', 'unique patients seen':'1'},
    {provider:'10800', 'service date': '2008-03-21', 'unique patients seen':'9'},
    {provider:'10800', 'service date': '2008-04-16', 'unique patients seen':'3'},
    {provider:'10800', 'service date': '2008-04-18', 'unique patients seen':'6'},
    {provider:'11451', 'service date': '2008-06-27', 'unique patients seen':'24'},
    {provider:'11451', 'service date': '2008-07-10', 'unique patients seen':'1'},
    {provider:'11451', 'service date': '2008-07-14', 'unique patients seen':'31'},
    {provider:'11451', 'service date': '2008-07-15', 'unique patients seen':'6'},
    {provider:'12980', 'service date': '2008-06-17', 'unique patients seen':'24'},
    {provider:'12980', 'service date': '2008-06-27', 'unique patients seen':'14'},
    {provider:'12980', 'service date': '2008-06-28', 'unique patients seen':'24'},
    {provider:'13907', 'service date': '2008-05-04', 'unique patients seen':'23'},
    {provider:'13907', 'service date': '2008-05-05', 'unique patients seen':'20'},
    {provider:'13907', 'service date': '2008-05-08', 'unique patients seen':'6'},
    {provider:'14618', 'service date': '2008-08-27', 'unique patients seen':'27'},
    {provider:'14618', 'service date': '2008-09-04', 'unique patients seen':'21'},
    {provider:'14618', 'service date': '2008-09-05', 'unique patients seen':'20'}    
];

// first we need to coerce the data into the right formats and make the
// names a little more sane
data = data.map( function (d) { 
    return { 
      provider: +d.provider,   // the + sign will coerce strings to number values
      date: new date(d['service date']),
      patients: +d['unique patients seen'] }; 
});   

// then we need to nest the data on provider since we want to only draw one
// line per provider
data = d3.nest().key(function(d) { return d.provider; }).entries(data);

unfortunately the data you provided wasn't that interesting to graph since the providers didn't really overlap. once you get all your data loaded it should look nicer :)

score:0

the short answer is to re-process your data into a format that d3 can process.

apologies for not including an example, but typing code on a mobile is a pig

score:0

here is a complete, working example, including the data above. there is a problem with the graph in that different providers do not have different colours. personally, i'm not sure how to fix that.. thank you all for your help.

<!doctype html>
<meta charset="utf-8">
<style>

body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispedges;
}

.x.axis path {
  display: none;
}

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>

var margin = {top: 20, right: 80, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.time.scale()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var color = d3.scale.category10();

var xaxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yaxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var line = d3.svg.line()
    .interpolate("basis")
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.patients); });

var svg = d3.select("body").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 + ")");

d3.csv("https://raw.github.com/gist/3925458/c48eb92a3a8a23bfee762b514a4c1256ff0be10d/gistfile1.txt", function(error, data) {

    data = data.map( function (d) {
        return { 
            provider: +d.provider,   // the + sign will coerce strings to number values
            date: new date(d['service date']),
            patients: +d['unique patients seen'] }; 
    });

  providers = d3.nest().key(function(d) { return d.provider; }).entries(data);


  x.domain(d3.extent(data, function(d) { return d.date; }));

  y.domain([
    d3.min(providers, function(c) { return d3.min(c.values, function(v) { return v.patients; }); }),
    d3.max(providers, function(c) { return d3.max(c.values, function(v) { return v.patients; }); })
  ]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xaxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yaxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("number of patients seen");

  var provider = svg.selectall(".provider")
      .data(providers)
    .enter().append("g")
      .attr("class", "provider");

  provider.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .style("stroke", function(d) { return color(d.name); });

  provider.append("text")
      .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
      .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.patients) + ")"; })
      .attr("x", 3)
      .attr("dy", ".35em")
      .text(function(d) { return d.value.provider; });
});

</script>

Related Query