我已经检查了各种教程,但找不到我的用例。
以下是我的数据集的一个子集,格式为CSV:
Provider,Service date,Unique patients seen
10800,2007-12-03,1
10800,2008-03-21,9
10800,2008-04-16,3
10800,2008-04-18,6
11451,2008-06-27,24
11451,2008-07-10,1
11451,2008-07-14,31
11451,2008-07-15,6
12980,2008-06-17,24
12980,2008-06-27,14
12980,2008-06-28,24
13907,2008-05-04,23
13907,2008-05-05,20
13907,2008-05-08,6
14618,2008-08-27,27
14618,2008-09-04,21
14618,2008-09-05,20
我希望每个独特的提供者都有一个系列,该系列的X轴为服务日期,Y轴为患者。
到目前为止,我已经查看了multi-series line chart tutorial,但我无法说服它使用我拥有的数据而不是现在使用的数据。
任何建议或阅读?
tarek:)
答案 0 :(得分:1)
您可以在http://jsfiddle.net/YHW6H/1/看到一个有效的例子。
我做了以下几点来强制你的数据。您需要使用d3.csv
来读取您的csv文件,但我对该示例的值进行了硬编码。
// 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);
不幸的是,您提供的数据并不像图表那么有趣,因为提供商并没有真正重叠。一旦你加载了所有数据,它应该看起来更好:)
答案 1 :(得分:0)
简短的回答是将您的数据重新处理为d3可以处理的格式。
不包含示例的道歉,但在移动设备上输入代码是猪
答案 2 :(得分:0)
这是一个完整的工作示例,包括上面的数据。图表存在一个问题,即不同的提供商没有不同的颜色。就个人而言,我不确定如何解决这个问题。谢谢大家的帮助。
<!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>