javascript - 点未显示成线/散点组合图 d3.js

标签 javascript d3.js

我在 d3.js 上使用,它工作正常。但我不明白为什么不显示分散序列。

这是我的代码:

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

body { font: 12px Arial;}

path { 
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}

</style>
<body>

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

<script>

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

// Parse the date / time
var parseDate = d3.time.format("%H-%M-%d-%b-%y").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

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

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

// Adds the plot
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 + ")");

lineSerie();
scatterSerie();

function lineSerie(){
var line = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.close); });
d3.csv("timeSeries.csv", function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.close = +d.close;
    });
  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain(d3.extent(data, function(d) { return d.close; }));

  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", ".60em")
      .style("text-anchor", "end")
      .text("CPU RunQueue");

  svg.append("path")
      .datum(data)
      .attr("class", "line")
      .attr("d", line);
});

}

function scatterSerie(){
d3.csv("scatterSerie.csv", function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.close = +d.close;
    });
    svg.selectAll("dot")
        .data(data)
        .enter().append("circle")
        .attr("r", 3.5)
        .attr("cx", function(d) { return x(d.date); })
        .attr("cy", function(d) { return y(d.close); });

});
}
</script>
</body>

系列:

date,close

05-17-14-Oct-14,223233.06250
05-18-14-Oct-14,223233.06250
05-19-14-Oct-14,223233.06250
05-20-14-Oct-14,223233.06250
05-21-14-Oct-14,223233.06250
05-22-14-Oct-14,223233.06250
05-23-14-Oct-14,223233.06250
05-24-14-Oct-14,223233.06250
05-25-14-Oct-14,223233.06250

线系列

date,close

10-27-02-Oct-14,223233.06250
10-28-02-Oct-14,223233.06250
10-29-02-Oct-14,223233.06250
10-30-02-Oct-14,223233.06250
10-31-02-Oct-14,223233.06250
10-32-02-Oct-14,223233.06250
10-33-02-Oct-14,223233.06250
10-34-02-Oct-14,223233.06250
10-35-02-Oct-14,223233.06250
10-36-02-Oct-14,223233.06250
10-37-02-Oct-14,223233.06250
10-38-02-Oct-14,223233.06250

显示了线系列,但未显示散点系列。 谁能告诉我出了什么问题吗?

提前致谢。

最佳答案

在您的 scatterSerie() 函数中,您的 select 语句不成立。 不存在“点”元素这样的东西。您需要像这样选择具有“dot”类的所有元素(想想 jQuery):

svg.selectAll(".dot")
    .data(data)
    .enter().append("circle")
    .attr('class','dot')
    .attr("r", 3.5)
    .attr("cx", function(d) { return x(d.date); })
    .attr("cy", function(d) { return y(d.close); });

不要忘记将“点”类设置为新添加的元素,否则您 future 的更新可能会失败。

您可能想要研究的另一件事是您的圈子的风格。目前,它们没有定义的描边颜色、描边宽度或填充。您可以将它们添加为 CSS 规则或将它们添加到您的 d3 代码中。

关于javascript - 点未显示成线/散点组合图 d3.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32652269/

相关文章:

javascript - 使用 jquery 进行文本旋转

javascript - d3 图表 + jQuery 数据表 : trouble reading nested array

javascript - 在 Rails 中,如何从我的 d3 js 代码访问 json?

javascript - D3 初始缩放行为中断

javascript - 验证 json 负载中元素的值

javascript - jsdom 未加载或未在页面中运行外部脚本

javascript - 如何在点击时关闭 jQuery Tools Overlay,无论在哪里?

javascript - Google 可视化图表在单独的 Div 中的多个实例 [跟进]

javascript - 平移后缩放路径对象

javascript - D3 弯曲链接,箭头未出现在屏幕上