2

我检查了各种教程,但找不到我的用例。

这是我的数据集的一个子集,采用 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 轴作为患者看到的。

到目前为止,我已经查看了多系列折线图教程,但我无法说服它像现在一样使用我拥有的数据而不是列。

有什么建议或读物吗?

塔雷克:)

4

3 回答 3

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);

不幸的是,您提供的数据并没有那么有趣,因为提供者并没有真正重叠。一旦你加载了所有数据,它应该看起来更好:)

于 2012-10-21T01:16:48.210 回答
0

简短的回答是将您的数据重新处理为 d3 可以处理的格式。

抱歉没有包含示例,但在手机上输入代码是一头猪

于 2012-10-21T00:58:39.043 回答
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>
于 2012-10-21T02:38:41.953 回答