D3线和图表在同一页面上打破

时间:2016-03-16 21:14:05

标签: javascript d3.js graph

我正在尝试在同一页面上渲染2个图表:图表图表和线性图表

在html中我有两个div,它们分别绑定到2个不同的图形,如下所示:     <div id="svg-container">     <div id="svg-container-avg"> 我在html中导入的第一个图是这个线性“平均”图形,它不显示路径或x轴。 如果我删除直接导入的图表图表,它工作正常。我会自动假设两个图之间存在某种依赖关系,但我找不到任何东西......

我的第一个导入,线性图:scns-avg.js文件包含此

// Set the dimensions of the canvas / graph
var margin = {
    top: 30,
    right: 20,
    bottom: 70,
    left: 50
  },
  width = 800 - margin.left - margin.right,
  height = 350 - margin.top - margin.bottom;

var x = d3.scale.linear().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

var xAxis = d3.svg.axis().scale(x)
  .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
  .orient("left").ticks(5);

var valueline = d3.svg.line()
  .x(function(d) {
    return x(d.scnsID);
  })
  .y(function(d) {
    return y(d.average);
  });

var svg2 = d3.select("#svg-container-avg")
  .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 + ")");

// Get the data
d3.json("scns-avg-data-retrieval.php", function(error, data) {
  data.forEach(function(d) {
    d.scnsID = d.scnsID;
    d.average = +d.average;
  });

  // Scale the range of the data
  x.domain(d3.extent(data, function(d) {
    return d.scnsID;
  }));
  y.domain([0, d3.max(data, function(d) {
    return d.average;
  })]);

  svg2.append("path") // Add the valueline path.
    .style("stroke", "rgba(13, 183, 196, 0.9)")
    .attr("d", valueline(data));

  svg2.selectAll("dot")
    .data(data)
    .enter().append("circle")
    .attr("class", "dot")
    .attr("r", 3.5)
    .attr("cx", function(d) {
      return x(d.scnsID);
    })
    .attr("cy", function(d) {
      return y(d.average);
    })


  svg2.append("g") // Add the X Axis
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

  svg2.append("g") // Add the Y Axis
    .attr("class", "y axis")
    .call(yAxis);

});
My Data structor in JSON: 
[{"date":"11-Mar-16","average":"3.18","scnsID":"2"},{"date":"12-Mar-16","average":"3.09","scnsID":"3"},{"date":"15-Mar-16","average":"3.16","scnsID":"4"},{"date":"17-Mar-16","average":"3.20","scnsID":"5"}]

当试图运行html页面并导入两个图形时,scns-avg.js在控制台中抛出错误:属性d =“M43,2.8124999999999902LNaN,15.46875000000002LNaN,5.6249999999999805L471,0”的值无效 这部分代码指向了valueline(数据)     svg2.append("path") .style("stroke", "rgba(13, 183, 196, 0.9)") .attr("d", valueline(data)); 这取决于这段代码,所以问题必须在这里,但现在6个小时我找不到解决方案..     var valueline = d3.svg.line() .x(function(d) { return x(d.scnsID); }) .y(function(d) { return y(d.average); });

我的图表图表接下来导入,如果禁用,线性图表正确呈现如下:

var margin = {top: 20, right: 20, bottom: 30, left: 40},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

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

        var y = d3.scale.linear()
        .range([height, 0]);

        var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom");

        var yAxis = d3.svg.axis()
        .scale(y)
        .orient("left")
        .ticks(5);

        var tip = d3.tip()
        .attr('class', 'd3-tip')
        .offset([-10, 0])
        .html(function(d) {
            return "<strong>Frequency:</strong> <span style='color:rgba(13, 183, 196, 0.9)'>" + d.average + "</span>";
        });

        var svg = d3.select("#svg-container").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 + ")");

        svg.call(tip);

// Get the data
d3.json("scns-data-retrieval.php", function(error, data) {
  if (error) throw error;

  data.forEach(function(d) {
    d.question = d.question;
    d.average = +d.average;
});

  x.domain(data.map(function(d) { return d.question; }));
  y.domain([0, 5]);
  legendSpace = width/5;

  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", 0 - margin.left)
  .attr("x", 0 - (height / 3))
  .attr("dy", ".71em")
  .style("text-anchor", "end")
  .text("Level of need for help:");

    svg.selectAll(".bar")
    .data(data)
    .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) { return x(d.question); })
    .attr("width", x.rangeBand())
    .attr("y", function(d) { return y(d.average); })
    .attr("height", function(d) { return height - y(d.average); })
    .on('mouseover', tip.show)
    .on('mouseout', tip.hide);
});

function type(d) {
  d.average = +d.average;
  return d;
};
data structure in JSON:
[{"question":1,"average":3.3333333333333},{"question":2,"average":2.5},{"question":3,"average":4},{"question":4,"average":2.75},{"question":5,"average":2.75},{"question":6,"average":2.75},{"question":7,"average":3},{"question":8,"average":3},{"question":9,"average":2.75},{"question":10,"average":3.25},{"question":11,"average":3.25},{"question":12,"average":3.5},{"question":13,"average":3},{"question":14,"average":3.25},{"question":15,"average":3.5},{"question":16,"average":3.5},{"question":17,"average":3.25},{"question":18,"average":3.75},{"question":19,"average":3.5},{"question":20,"average":3},{"question":21,"average":3},{"question":22,"average":3.5},{"question":23,"average":3.25},{"question":24,"average":3.75},{"question":25,"average":3.75},{"question":26,"average":3.75},{"question":27,"average":3.5},{"question":28,"average":2.75},{"question":29,"average":2.25},{"question":30,"average":3.5},{"question":31,"average":3},{"question":32,"average":3}]

我不能再这样做了,它让我发疯,所以我现在转向stackoverflow的力量!

1 个答案:

答案 0 :(得分:0)

经过一些疯狂的研究和反复试验,我已经解决了这个问题。

当你试图渲染超过1个图形时,如果它们是不同的类型而不是说 - 多个线性图形,但类似于带条形图的线性图形,变量范围变得混乱并且不受限制将javascript文件导入html后即可显示。

要修复此问题,每个图形js文件必须包含在(function(){所有图形代码都在这里})();

这会将变量范围限制为仅仅函数。