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