首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >d3:正在缩放的轴,而不是多行图.我怎样才能解决这个问题?

d3:正在缩放的轴,而不是多行图.我怎样才能解决这个问题?
EN

Stack Overflow用户
提问于 2016-03-07 18:50:42
回答 1查看 367关注 0票数 0

我正在根据y轴上的累积整数绘制x轴上的日期。我一直在尝试实现一个功能,以便能够缩放/缩放到图形中,以及移动它。然而,我没有做到这一点-轴正在改变,但图形不是。

这是我的密码:

代码语言:javascript
复制
var parseDate = d3.time.format("%d/%m/%Y").parse;
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");//.tickSize(-height);

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

// Define the line
var cumulativeline = d3.svg.line()
.interpolate("linear")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.cumulative); });

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right + 250)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", 
        "translate(" + margin.left  + "," + margin.top + ")");

svg.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);

// Get the data
d3.csv("file.csv", function(error, data) {
data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.cumulative = +d.cumulative;
});

var dataNest = d3.nest()
    .key(function(d) {return d.a_tradeidtype;})
    .entries(data);

x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.cumulative; })]);

// Loop through each a_tradeidtype / key
dataNest.forEach(function(d,i) { 
svg.append("path")
        .style("stroke", function() { // Add the colours dynamically
            return d.color = color(d.key); })
        .attr("class", classname)
        .attr("d", cumulativeline(d.values))
        .attr("clip-path", "url(#clip)");
});

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

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

// Create zooming component
var zoom = d3.behavior.zoom()
    .x(x)
    .y(y)
    .scaleExtent([1, 10])
    .on('zoom', zoomed);

svg.call(zoom);
});

function zoomed() {
  svg.select(".x.axis").call(xAxis);
  svg.select(".y.axis").call(yAxis);
}

我做错了什么?

EN

Stack Overflow用户

回答已采纳

发布于 2016-03-07 21:15:57

你不是在“放大”中更新你的行,而是用轴来更新。函数中需要这样的内容:

代码语言:javascript
复制
svg.selectAll("path")
        .attr("d", function(d) { return cumulativeline(d.values); })
;

我还怀疑,您可能需要将原始附加例程更改为如下所示,以便将数据连接到每条路径,以便上面的代码能够工作:

代码语言:javascript
复制
// Loop through each a_tradeidtype / key
svg.selectAll("path").data(dataNest)
        .enter()
        .append("path")
        .style("stroke", function(d) { // Add the colours dynamically
            return d.color = color(d.key); })
        .attr("class", classname)
        .attr("d", function(d) { return cumulativeline(d.values); })
        .attr("clip-path", "url(#clip)")
;

如果这不起作用,就把你的代码插进一个小玩意里。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35851597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档