gpt4 book ai didi

javascript - 使用 d3.chart.js 在 d3.js 中绘制折线图

转载 作者:行者123 更新时间:2023-11-29 15:39:55 24 4
gpt4 key购买 nike

我想将 d3.chart() 用于我已经编写的图表。我找到了 d3.chart() 的示例,用于 circlebarcharts 但不是用于 line charts。我的图表是折线图,我需要在 d3.charts()

中使用以下代码
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);

但在尝试这样使用时遇到问题

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3.v3.min.js"></script>
<script type="text/javascript" src="d3.chart.min.js"></script>

</head>
<body>
<div id="vis"></div>
<script type="text/javascript">


d3.chart("linechart", {

initialize: function() {
// create a base scale we will use later.
var chart = this;
chart.w = chart.base.attr('width') || 200;
chart.h = chart.base.attr('height') || 150;
chart.w = +chart.w;
chart.h = +chart.h;

chart.x = d3.scale.linear()
.range([0, chart.w]);

chart.y = d3.scale.linear()
.range([chart.h,0]);

chart.base.classed('line', true);

this.areas = {};
chart.areas.lines = chart.base.append('g')
.classed('lines', true)
.attr('width', chart.w)
.attr('height', chart.h)

chart.line = d3.svg.line()
.x(function(d) { return chart.x(d.x);})
.y(function(d) { return chart.y(d.y);});


this.layer("lines", chart.areas.lines, {
dataBind: function(data) {
// update the domain of the xScale since it depends on the data
chart.y.domain([d3.min(data,function(d){return d.y}),d3.max(data,function(d){return d.y})])
chart.x.domain(d3.extent(data, function(d) { return d.x; }));

// return a data bound selection for the passed in data.
return this.append("path")
.datum(data)
.attr("d", chart.line)
.attr('stroke','#1ABC9C')
.attr('stroke-width','2')
.attr('fill','none');
},
insert: function() {

return null;

},

});
},

// configures the width of the chart.
// when called without arguments, returns the
// current width.
width: function(newWidth) {
if (arguments.length === 0) {
return this.w;
}
this.w = newWidth;
return this;
},

// configures the height of the chart.
// when called without arguments, returns the
// current height.
height: function(newHeight) {
if (arguments.length === 0) {
return this.h;
}
this.h = newHeight;
return this;
},

});

var data = [
{x: 0,y:190},
{x: 1,y:10},{x: 2,y:40},{x: 3,y:90},
{x: 4,y:30},{x: 5,y:20},{x: 6,y:10}
];

var chart1 = d3.select("#vis")
.append("svg")
.chart("linechart")
.width(720)
.height(320)

chart1.draw(data);
</script>
</body>
</html>

错误:

Uncaught Error: [d3.chart] Layer selection not properly bound.

我也得到了行和错误。

注意this link获取d3.chart.min.jsthis link 获取 d3.v3.min.js

更新: 我从@LarsKotthoff 的回答中得到了答案,但图像有所不同。检查此链接Before apply D3After apply D3 .

最佳答案

看起来您混淆了 insertdataBind 操作——在前者中,您应该附加新元素,而后者仅绑定(bind)数据.通过以下修改,您的代码对我来说工作正常。

dataBind: function(data) {
// update the domain of the xScale since it depends on the data
chart.y.domain([d3.min(data,function(d){return d.y}),d3.max(data,function(d){return d.y})])
chart.x.domain(d3.extent(data, function(d) { return d.x; }));

// return a data bound selection for the passed in data.
return this.selectAll("path").data([data]);
},
insert: function() {
return this.append("path")
.attr("d", chart.line)
.attr('stroke','#1ABC9C')
.attr('stroke-width','2')
.attr('fill','none');

}

请注意,这不适用于多行——要做到这一点,请将 .data([data]) 更改为 .data(data) 并使用嵌套数组,例如[[{x:0,y:0},...], [{x:1,y:1},...], ...]

关于javascript - 使用 d3.chart.js 在 d3.js 中绘制折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21043862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com