使用underscorejs和d3数据进行过滤

时间:2015-08-14 00:10:14

标签: javascript d3.js underscore.js

我想将我的键值对数组过滤成两组,并为每组绘制折线图。我使用underscore.js来过滤数据。我正在使用d3.js来绘制折线图。当我在D3中添加spectrum1Data作为数据对象时,它不会绘制折线图 - 有人可以查看下面的代码并让我知道我缺少的是什么吗?

我的数据如下所示

spectrum_data = [
    {
        "SpectrumName": "Spectrum1",
        "Mass": "27.19",
        "Intensity": "20.2"
    },
    {
        "SpectrumName": "Spectrum1",
        "Mass": "11.39",
        "Intensity": "10.7"
    },
    {
        "SpectrumName": "Spectrum2",
        "key": "value",
        "Intensity": "12.9"
    },
    {
        "SpectrumName": "Spectrum2",
        "Mass": "21.83",
        "Intensity": "30.9"
    }];

用于过滤频谱1数据的下划线JS功能如下所示。

var spectrum1Data = _(spectrum_data).chain()
        .filter(function(x){ return x.SpectrumName=="Spectrum1"}).value()[0];

绘制线的d3.js函数如下所示

var line = d3.svg.line()
     .interpolate("cardinal")
    .x(function(d) {//console.log(xScale(parseInt(d.Mass)));
        return xScale(d.Mass); })
    .y(function(d) {//console.log(xScale(d.Intensity));
        return yScale(d.Intensity); });

focus.append("path")
  .datum(spectrum1Data)
  .attr("class", "line")
  .attr("d", line)

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<meta charset="utf-8">
<style>

svg {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  fill:none;
  stroke:#000;
  shape-rendering: crispEdges;
}

.line {
  fill: none;
  stroke-width: 1.5px;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>

var spectrum_data = [
    {
        "SpectrumName": "Spectrum1",
        "Mass": "27.19",
        "Intensity": "20.2"
    },
    {
        "SpectrumName": "Spectrum1",
        "Mass": "11.39",
        "Intensity": "10.7"
    },
    {
        "SpectrumName": "Spectrum2",
        "Mass": "30",
        "Intensity": "12.9"
    },
    {
        "SpectrumName": "Spectrum2",
        "Mass": "21.83",
        "Intensity": "30.9"
    }];
var margin = {top: 20, right: 80, bottom: 30, left: 50},
    width = 900 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;


var x = d3.scale.linear()
    .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("cardinal")
    .x(function(d) { return x(d.Intensity); })
    .y(function(d) { return y(d.Mass); });

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

  color.domain(d3.keys(spectrum_data[0]).filter(function(key) { return key == "SpectrumName"; }));

  //filter data based on spectrumname
  var nested_data = d3.nest().key(function(d) { return d.SpectrumName; }).entries(spectrum_data);

  console.log(nested_data);

  x.domain([d3.min(nested_data, function(d) { return d3.min(d.values, function (d) { return d.Intensity; }); }),
             d3.max(nested_data, function(d) { return d3.max(d.values, function (d) { return d.Intensity; }); })]);
  y.domain([0, d3.max(nested_data, function(d) { return d3.max(d.values, function (d) { return d.Mass; }); })]);


  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);

  var Spectrum = svg.selectAll(".spectrum")
      .data(nested_data, function(d) { return d.key; })
    .enter().append("g")
      .attr("class", "spectrum");

  Spectrum.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .style("stroke", function(d) { return color(d.key); });

</script>

您可以使用d3.js的d3.nest()进行过滤,而不是使用underscore.js。这是包含数据绘制折线图的代码。