使用d3图表的折线图

时间:2013-10-23 04:55:59

标签: javascript charts d3.js

我可以使用带有以下代码的d3图表生成折线图

function renderChart() {

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("%Y%m%d").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.date); })
    .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.csv("line.csv", function(error, data) {
  color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));

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

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

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

  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("Y - Axis");

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


});

}


Line.csv

date,temparature
20111001,124
20111002,58
20111003,49
20111004,100

此代码仅在输入格式为date,temparature

时有效

如何使其适用于以下格式(地点,温度)?

Line.csv

place,temparature
India,124
Us,58
Uk,49
China,100

2 个答案:

答案 0 :(得分:2)

你应该可以改变这个:

var x = d3.time.scale()
    .range([0, width]);

到此:

var x = d3.scale.ordinal()
    .range([0, width]);

然后用“地点”搜索并替换所有出现的“日期”并删除parseDate内容,你应该很好。

答案 1 :(得分:-2)

.on("click", function(d) {
    if (this._listenToEvents) {
        // Reset inmediatelly  
        d3.select(this).attr("transform", "translate(0,0)")

        //List item
        path.each(function() {
            this._listenToEvents = false;
        });
    }

    d3.selectAll("#" + id + " svg").remove();

    if (level == 1) {
        TransformChartData(chartData, options, 0, d[xVarName]);
        BuildBar(id, chartData, options, 0);
    } else {
        var nonSortedChart = chartData.sort(function(a, b) {
            return parseFloat(b[options[0].yaxis]) - parseFloat(a[options[0].yaxis]);
        });
        TransformChartData(nonSortedChart, options, 1, d[xVarName]);
        BuildBar(id, nonSortedChart, options, 1);
    }
});