javascript - 在 nvd3 结构中使用 d3 和 json 的多折线图

标签 javascript jquery canvas d3.js svg

我正在尝试使用 d3 和 json 格式的数据创建多折线图。出于某种原因,我的代码无法正常工作。我一直无法找出我做错的地方。网上查了很多例子,都没有用。我可能对代码视而不见。

在示例中,我只尝试绘制两条线,但不知道线的数量。所以我不能硬编码。有人对我做错的地方有任何提示吗?

Jsfiddle 代码链接。 http://jsfiddle.net/6qrcmsnj/3/

这里也是代码。

var w = 700;
var h = 600;
var pad = 80;

var time_format = d3.time.format("%I:%M:%S");

var gd = example_data(4);


var xScale = d3.time.scale()
    .domain(d3.extent(gd[0].data, function(d) { return d[0]; }))
    .range([pad, w - pad]);

var yScale = d3.scale.linear()
    .domain([0, d3.max(gd[0].data, function(d) { return d[1]; })*2])
    .range([h - pad, pad]);

var canvas = d3.select("body")
    .append("svg")
        .attr("class", "chart")
        .attr("width", w)
        .attr("height", h);

var xaxis = d3.svg.axis()
    .scale(xScale)
    .orient("bottom")
    .tickFormat(time_format);

var yaxis = d3.svg.axis()
    .scale(yScale)
    .orient("left");

var line = d3.svg.line()
    .x(function(d) { return xScale(d[0]); })
    .y(function(d) { return yScale(d[1]); })
    .interpolate("linear");

// Just for a grey background.
canvas.append("rect")
    .attr("width", "100%")
    .attr("height", "100%")
    .attr("fill", "#E8E8E8");

// Add x-axis.
canvas.append("g")
    .attr("class", "axis")
    .attr("transform","translate(0," + (h - pad) + ")")
    .call(xaxis)
    .selectAll("text")
        .style("text-anchor", "end")
        .attr("dx", "-.8em")
        .attr("dy", ".15em")
        .attr("transform", function(d) {
                return "rotate(-65)"
                });

// Add y-axis
canvas.append("g")
    .attr("class", "axis")
    .attr("transform", "translate(" + pad + ", 0)")
    .call(yaxis);

// Add line
canvas.selectAll("path")
    .data(gd)
    .enter().append("path")
    .attr("d", line(gd.data))
    .attr("stroke", "black")
    .attr("stroke-width", 1)
    .attr("fill", "none");

function example_data(val) {

    if (val == "4") {
        return [
            { "label" : "Data_1", "data" : [ [ 1404438592000, 21.09 ], [ 1404438593000, 10.85 ], [ 1404438594000, 15.74 ], [ 1404438595000, 20.86 ], [ 1404438596000, 10.83 ], [ 1404438597000, 8.92 ], [ 1404438598000, 23.68 ], [ 1404438599000, 20.68 ], [ 1404438600000, 14.68 ], [ 1404438601000, 4.65 ]] }, 
            { "label" : "Data_2", "data" : [ [ 1404438592000, 3.219 ], [ 1404438593000, 1.641 ], [ 1404438594000, 6.68 ], [ 1404438595000, 2.543 ], [ 1404438596000, 8.522 ], [ 1404438597000, 4.616 ], [ 1404438598000, 9.703 ], [ 1404438599000, 3.737 ], [ 1404438600000, 8.752 ], [ 1404438601000, 1.791 ]]}
        ];
    }           
}

最佳答案

@LarThoren 方法肯定会起作用,但对于 d3 我们希望避免显式循环。您的版本遗漏了两件事:

canvas.selectAll(".series") //<-- don't selectAll with "path", there are other paths in the axis and you selection needs to be more specific
  .data(gd)
  .enter()
  .append("path")
  .attr("d", function(d){ //<-- gd is out of scope here, you need to use the accessor function
    return line(d.data);
  }) 
  .attr("class", "series")
  .attr("stroke", "black")
  .attr("stroke-width", 1)
  .attr("fill", "none");

完整的工作代码:

var w = 700;
var h = 600;
var pad = 80;

var time_format = d3.time.format("%I:%M:%S");

var gd = example_data(4);
	
var xScale = d3.time.scale()
	.domain(d3.extent(gd[0].data, function(d) { return d[0]; }))
	.range([pad, w - pad]);

var yScale = d3.scale.linear()
	.domain([0, d3.max(gd[0].data, function(d) { return d[1]; })*2])
	.range([h - pad, pad]);

var canvas = d3.select("body")
	.append("svg")
		.attr("class", "chart")
		.attr("width", w)
		.attr("height", h);

var xaxis = d3.svg.axis()
	.scale(xScale)
	.orient("bottom")
	.tickFormat(time_format);

var yaxis = d3.svg.axis()
	.scale(yScale)
	.orient("left");

var line = d3.svg.line()
	.x(function(d) { return xScale(d[0]); })
	.y(function(d) { return yScale(d[1]); })
	.interpolate("linear");

// Just for a grey background.
canvas.append("rect")
	.attr("width", "100%")
	.attr("height", "100%")
	.attr("fill", "#E8E8E8");

// Add x-axis.
canvas.append("g")
	.attr("class", "axis")
	.attr("transform","translate(0," + (h - pad) + ")")
	.call(xaxis)
	.selectAll("text")
		.style("text-anchor", "end")
		.attr("dx", "-.8em")
		.attr("dy", ".15em")
		.attr("transform", function(d) {
				return "rotate(-65)"
				});

// Add y-axis
canvas.append("g")
	.attr("class", "axis")
	.attr("transform", "translate(" + pad + ", 0)")
	.call(yaxis);

// Add line
canvas.selectAll(".series")
	.data(gd)
	.enter()
	.append("path")
	.attr("d", function(d){
    	return line(d.data);
	})
	.attr("class", "series")
	.attr("stroke", "black")
	.attr("stroke-width", 1)
	.attr("fill", "none");

function example_data(val) {
	
	if (val == "4") {
		return [
			{ "label" : "Data_1", "data" : [ [ 1404438592000, 21.09 ], [ 1404438593000, 10.85 ], [ 1404438594000, 15.74 ], [ 1404438595000, 20.86 ], [ 1404438596000, 10.83 ], [ 1404438597000, 8.92 ], [ 1404438598000, 23.68 ], [ 1404438599000, 20.68 ], [ 1404438600000, 14.68 ], [ 1404438601000, 4.65 ]] }, 
			{ "label" : "Data_2", "data" : [ [ 1404438592000, 3.219 ], [ 1404438593000, 1.641 ], [ 1404438594000, 6.68 ], [ 1404438595000, 2.543 ], [ 1404438596000, 8.522 ], [ 1404438597000, 4.616 ], [ 1404438598000, 9.703 ], [ 1404438599000, 3.737 ], [ 1404438600000, 8.752 ], [ 1404438601000, 1.791 ]]}
		];
	}			
}
.axis path,
.axis line {
	fill: none;
	stroke: black;
	shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

关于javascript - 在 nvd3 结构中使用 d3 和 json 的多折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32933326/

相关文章:

javascript - 如果用户不确认,则撤消模糊时的文本更改

javascript - 从不同的键创建多层对象:value pairs

jquery - 在特定链接上扩展搜索栏

javascript - toDataURL 返回错误值,其中包含大量 "A"

javascript - 如何显示/隐藏 Canvas 的一部分?

javascript - 将 context.drawImage() 与 MediaStream 结合使用

javascript - 检测数据集中异常的方法

javascript - VueJs : How to pass a 'computed function' value from child component to the parent component using $emit(optionalPayload) and $on?

javascript - 使用 jQuery 删除 HTML 表格行

javascript - JSON 对象回复解析