如何使用d3js动态创建追加svg行?

时间:2016-08-04 10:55:18

标签: javascript d3.js svg

我正在尝试将标记绘制到可缩放/可缩放的图形中。 我目前的版本位于https://jsfiddle.net/n2enn80o/

var array1 = [[0, 50], [1, 40], [2, 30], [3, 20], [5, 10]];

var result = array1.map(function(e) {
  this.num = (this.num || 0) + e[1];
  return [e[0], this.num];
}, {});

console.log(result);

我已经能够在示例中为我的d3绘图添加静态线,我想要实现的是使标记线与文本“test”一起随图形一起移动。

我所尝试的是使用带有线生成器的d3.svg.line(),但它最终没有获得对d的引用。我尝试的另一个版本是创建一个如下所示的函数,但是没有获得d的引用,并将该行绘制为零。这里我尝试绘制线条,当且仅当数据中存在标记标记时。默认情况下,当填充数据时,我确保它具有Number.NaN,当我不需要绘制标记时。

raw = {"l2":[-58,-44,-31,-21,-17,-16,-16,-15,-15,-14,-13,-12,-11,-10,-9,-7,-5,-3,-1,2,4,8,11,14,17,20,24,28,32,37,41,46,51,56,60,64,69,73,77,82,84,87,87,87,87,85,82,77,73,66,60,53,46,39,33,26,20,13,8,4,-1,-6,-10,-14,-16,-19,-22,-25,-26,-27,-28,-29,-30,-31,-31,-32,-32,-32,-32,-31,-31,-31,-31,-31,-30,-31,-30,-30,-30,-30,-29,-28,-27,-27,-27,-27,-27,-26,-26,-26,-26,-26,-26,-26,-26,-25,-25,-25,-25,-25,-25,-25,-25,-25,-24,-24,-24,-25,-25,-24,-23,-23,-23,-23,-23,-23,-23,-23,-23,-22,-22,-22,-22,-22,-22,-21,-21,-21,-21,-20,-20,-20,-20,-19,-18,-16,-13,-10,-6,-2,1,4,7,9,12,14,16,16,16,15,13,9,6,2,-1,-4,-7,-10,-11,-12,-14,-15,-16,-17,-18,-19,-19,-20,-20,-20,-20,-20,-20,-20,-24,-32,-45,-54,-50,-25,25,97,182,252,270,220,126,35,-25,-53,-65,-71,-70,-60,-46,-32,-22,-18,-16,-16,-15,-14,-13,-13,-12,-11,-10,-8,-7,-5,-3,-1,1,4,7,10,14,17,21,24,28,32,36,40,45,50,55,59,64,69,72,77,81,84,87,88,88,87,84,82,77,73,67,61,54,47,39,33,26,20,14,9,4,-1,-6,-9,-13,-16,-19,-22,-24,-26,-28,-29,-29,-30,-30,-31,-32,-32,-32,-31,-31,-32,-31,-31,-31,-31,-30,-30,-29,-29,-29,-29,-28,-28,-27,-27,-28,-28,-26,-26,-26,-26,-26,-26,-26,-26,-25,-25,-25,-25,-25,-25,-26,-26,-25,-24,-24,-24,-24,-24,-24,-24,-23,-23,-23,-23,-23,-23,-23,-24,-22,-22,-22,-22,-22,-22,-22,-21,-22,-20,-20,-20,-20,-20,-19,-18,-16,-13,-10,-8,-3,0,4,7,9,11,13,15,16,16,16,14,11,6,2,-2,-4,-7,-9,-11,-12,-14,-16,-17,-18,-19,-19,-19,-19,-20,-20,-20,-20,-20,-20,-23,-30,-43,-54,-53,-30,17,88,172,245,271,230,138,45,-19,-50,-63,-70,-70,-61,-48,-34,-23,-18,-17,-16,-16,-15,-14,-13,-13,-11,-10,-8,-7,-5,-3,-1,1,4,7,9,13,16,20,23,27,31,36,40,44,49,54,59,63,68,73,76,81,83,86,88,87,87,85,82,78,73,68,62,55,48,40,33,27,20,14,9,4,0,-5,-9,-13,-16,-19,-22,-24,-26,-28,-29,-29,-29,-30,-31,-31,-32,-31,-32,-32,-32,-32], "startts":1357714800000,"marker1":[50,100]};

var data =[];
for(var i=0;i<raw.l2.length;i++)
{
    var marker = Number.NaN;

    for(var j=0;j<raw.marker1.length;j++)
    {
        if(i==raw.marker1[j])
        {
            marker=i;
            break;
        }
    }
    var obj = {"date":raw.startts+(1/244.140625*1000)*i, "value":raw.l2[i]/75,"marker":marker}
    data.push(obj)
}

margin = {
    top: 20,
    right: 20,
    bottom: 20,
    left: 45
};

width = 1800 - margin.left - margin.right;
height = 600 - margin.top - margin.bottom;

var x = d3.time.scale()
    .domain(d3.extent(data, function (d) {
    return d.date;
}))
    .range([0, width]);

var y = d3.scale.linear()
    .domain(d3.extent(data, function (d) {
    return d.value;
}))
    .range([height, 0]);

var line = d3.svg.line()
    .x(function (d) {
    return x(d.date);
})
    .y(function (d) {
    return y(d.value);
});



var zoom = d3.behavior.zoom()
    .x(x)
    .y(y)
    .on("zoom", zoomed);

svg = d3.select('#chart')
    .append("svg:svg")
    .attr('width', width + margin.left + margin.right)
    .attr('height', height + margin.top + margin.bottom)
    .append("svg:g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .call(zoom);

svg.append("svg:rect")
    .attr("width", width)
    .attr("height", height)
    .attr("class", "plot");

var make_x_axis = function () {
    return d3.svg.axis()
        .scale(x)
        .orient("bottom")
        .ticks(5);
};

var make_y_axis = function () {
    return d3.svg.axis()
        .scale(y)
        .orient("left")
        .ticks(5);
};

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

svg.append("svg:g")
    .attr("class", "x axis")
    .attr("transform", "translate(0, " + height + ")")
    .call(xAxis);

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

svg.append("g")
    .attr("class", "y axis")
    .call(yAxis);


var marker = svg.append("line")
    .attr("x1",100)
    .attr("y1", 0)     
    .attr("x2",100)
    .attr("y2", height)
    .attr("class", "marker")

svg.append("text")
    .attr("x", 100)
    .attr("y", 0)
    .attr("dy", ".35em")
    .attr("dx", 5)
    .attr("class", "marker-text")
    .text(function(d) { return "test"; });

svg.append("g")
    .attr("class", "x grid")
    .attr("transform", "translate(0," + height + ")")
    .call(make_x_axis()
        .tickSize(-height, 0, 0)
        .tickFormat(""));

svg.append("g")
    .attr("class", "y grid")
    .call(make_y_axis()
    .tickSize(-width, 0, 0)
    .tickFormat(""));

var clip = svg.append("svg:clipPath")
    .attr("id", "clip")
    .append("svg:rect")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", width)
    .attr("height", height);

var chartBody = svg.append("g")
    .attr("clip-path", "url(#clip)");

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

function zoomed() {
    console.log(d3.event.translate);
    console.log(d3.event.scale);
    svg.select(".x.axis").call(xAxis);
    svg.select(".y.axis").call(yAxis);
    svg.select(".x.grid")
        .call(make_x_axis()
        .tickSize(-height, 0, 0)
        .tickFormat(""));
    svg.select(".y.grid")
        .call(make_y_axis()
        .tickSize(-width, 0, 0)
        .tickFormat(""));
    svg.select(".line")
        .attr("class", "line")
        .attr("d", line);
}

2 个答案:

答案 0 :(得分:1)

根据我对你问题的回复,我做了一些小改动。在缩放处理程序zoomed()内,您需要更改x1&amp; x2的{​​{1}}属性以及marker的{​​{1}}属性。

此功能会在每个缩放事件中触发,以根据您的缩放和轴移动内容或重绘内容。

x

小提琴:https://jsfiddle.net/kqnLfkvw/1/

答案 1 :(得分:0)

这可以通过使用d3轴来解决。 https://jsfiddle.net/3gww76s0/

提供了一个工作示例
var make_x_marker = function (scale=1, transform=[0,0]) {

    console.log(transform)
    var markers =[];
    var markerLabels={};
    for (i=0; i<raw.marker1.length; i++)
    {
    var value=transform[0]+scale*(1/244.140625*1000)*raw.marker1[i];
     markers.push(value)
     markerLabels[value]="A"+Math.floor(Math.random()*26);
    }

    return d3.svg.axis()
        .orient("top")
        .tickValues(markers)
        .tickSize(height-15)
        .tickFormat(function(d){return markerLabels[d]})


};