使用.csv使用d3.js构建多线图表

时间:2014-04-04 08:16:38

标签: javascript csv d3.js

我正在使用以下JavaScript绘制多线图。我遵循了Link

但是没有数据显示出来。我也是整个JavaScript

的新手

我的.csv文件的格式如下,我想从第二列开始阅读,我想在Y轴和favorite_count处绘制retweet_countcreated_at X轴:

TSV格式

idtweet         created_at                      favorite_count  retweet_count 
 1311613481     Wed Mar 11 17:24:26 +0000 2009      0           1 
 10293166856    Wed Mar 10 22:57:09 +0000 2010      0           1
 10881883917    Mon Mar 22 17:21:47 +0000 2010      0           1

我的代码,我无法理解我应该在哪里放置Y轴favourite_countretweet_count值?

我的代码

<script src="http://d3js.org/d3.v3.js"></script>
<script src="d3.legend.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 parseDate = d3.time.format("%a %b %d %H:%M:%S %Z %Y").parse;

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.created_at); })
    .y(function(d) { return y(d.temperature); });

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.tsv("/home/hduser/Downloads/MyTSV.tsv", function(error, data) {
  color.domain(d3.keys(data[1]).filter(function(key) { return key !== "created_at"; }));

  data.forEach(function(d) {
    d.created_at = parseDate(d.created_at);
  });

  var cities = color.domain().map(function(name) {
    return {
      name: name,
      values: data.map(function(d) {
        return {created_at: d.created_at, temperature: +d[name]};
      })
    };
  });

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

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

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

  var city = svg.selectAll(".city")
      .data(cities)
    .enter().append("g")
      .attr("class", "city");

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

  city.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.created_at) + "," + y(d.value.temperature) + ")"; })
      .attr("x", 2)
      .attr("dy", ".35em")
      .text(function(d) { return d.name; });


  legend = svg.append("g")
    .attr("class","legend")
    .attr("transform","translate(50,30)")
    .style("font-size","12px")
    .call(d3.legend)

  setTimeout(function() { 
    legend
      .style("font-size","20px")
      .attr("data-style-padding",10)
      .call(d3.legend)
  },1000)

});

任何人都可以指导我应该遵循哪个方向?我是这个概念的新手。

谢谢。

0 个答案:

没有答案