score:1
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>
Source: stackoverflow.com
Related Query
- How to create a multi-series line chart with series for each unique item in 1st column?
- How can I create a doughnut chart with rounded edges only on one end of each segment?
- How do I create a multiline chart using d3js with json formatted for nvd3?
- d3: tooltips on multi series line chart at each line when mouse hover event
- How to create a % difference arrow line with value in a bar chart using D3.js
- how to display data from 1st point on words on y axis for line chart in d3.js
- How do you get JSON data and create a line chart with d3.js?
- D3 Multi Series Line chart not working with correct xAxis values
- How to draw a multi line series graph using d3.js with json data using angular directives
- How can I create a basic bar chart with unique counts on the y axis and a category on the x axis?
- How to create a line chart with vertical line and different backgrounds?
- How can I create a xy line chart with c3?
- How to create a dashed line with arrows on each dash in d3?
- How to assign a specific id to each line in a multi-line chart with multiple y-axes
- D3 Multi Series Line Chart with Clickable Legend
- How to render multi line chart with DC.js with the following type of data - JAVASCRIPT (DC.js)
- How to implement tooltip for D3 line chart with data from 2 arrays?
- d3js - How to plot a multi line chart chart for multiple groups of items that can be updated?
- D3 Multi Series Line chart with non time x axis
- In d3js, how to plot line chart from csv file, taking data from seperate columns for each row
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- how to create labels for data in donut chart using d3.js
- How to create stacked row chart with one row with dc.js?
- dc.js Series Chart multi line
- How to create a reusable component in D3 with an API that manipulates each instance of the component?
- Multi series chart (D3) with missing values
- How do I implement d3's enter update exit pattern for an area chart with focus and context?
- D3: How to use exit().remove() for Multi-Series Line Chart
- In D3, how can I create multiple elements for each data element based on value in data?
- Error in A Simple D3 Line chart with Legend and Tooltips for D3v 3.5.13
More Query from same tag
- How to create a custom element in d3
- Adding labels to D3 force graph
- Dart D3 line using Package JS
- d3JS: Bisecting a Nested Array
- d3.js graphing sub arrays from JSON
- Math.random() - set a range of minimum to maximum
- d3js axis dates start and end value only
- Gathering some data for a network graph in D3. How should I format it?
- Change d3.js line graph stroke colour above a data point
- d3.js nodes and edges format
- Is it possible to create a d3/svg circular canvas/container?
- d3-force - Nodes are dragged away from links, ticks not run
- c3js position of data labels
- How to Uniformly distribute svg circles in d3
- Update d3 data react hooks
- d3js - my line chart is not working
- How can I create a basic bar chart with unique counts on the y axis and a category on the x axis?
- Programmatically control brush and pass relevant information
- Creating .exe file with pyinstaller and Matlab engine
- d3.js change array data mapping from normalized to regular stacked chart
- How to add a tooltip to D3.js subplot
- How to load JSON to D3 in Tree Diagram
- How to highlight a source node and all its target nodes and corresponding links in d3 hierarchical edge bundling
- Making a variable from a csv file
- Importing json data using D3.json function
- Update D3 Line Graph from List Options
- d3 grid lines, have done some research but still stuck
- Draw a frequency plot in d3 with JSON data
- Mongo Query: flatten field and split into a new document
- D3: Render Flare Graph Titles avoiding text overlap