多线图中的笔刷仅适用于一条线 [英] brush in multiline chart is working for only one line

查看:50
本文介绍了多线图中的笔刷仅适用于一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多条线图绘制两条线并使用画笔和d3 v4缩放,当我进行画笔绘制时,只有一条线在移动,而另一条线则保持不变.由于我只是自己开始学习这项技术,因此我不知道必须进行哪些更改,以便在笔刷时两条线都可以移动.任何类型的建议都会有很大的帮助.

am working on multi line chart with two lines and with brush and zoom in d3 v4, when i brush my only one line is moving my another line remains constant. Since i have just started learning this on my own i don't have idea on what changes i have to make so that my both lines moves when i brush. Any kind of suggestions would be of a great help.

<!DOCTYPE html>
<meta charset="utf-8">
<style>

 .line {
        fill: none;
        stroke: steelblue;
        stroke-width: 1.5px;
    }
.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}

</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 110, left: 40},
    margin2 = {top: 430, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

var parseDate = d3.timeParse("%Y-%m-%d");

var x = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
//brush
    x2 = d3.scaleTime().range([0, width]),
    y2 = d3.scaleLinear().range([height2, 0]);

var xAxis = d3.axisBottom(x),
    yAxis = d3.axisLeft(y),
    //brush
    xAxis2 = d3.axisBottom(x2);

//slider that grey selection one
var brush = d3.brushX()
    .extent([[0, 0], [width, height2]])
    .on("brush end", brushed);

var zoom = d3.zoom()
    .scaleExtent([1, Infinity])
    .translateExtent([[0, 0], [width, height]])
    .extent([[0, 0], [width, height]])
    .on("zoom", zoomed);

    var line = d3.line()
        .x(function (d) { return x(d.date); })
        .y(function (d) { return y(d.extra); });
    
    var line1 = d3.line()
        .x(function(d) { return x(d.date); })
        .y(function(d) { return y(d.Speed); });

//slider line
    var line2 = d3.line()
        .x(function (d) { return x2(d.date); })
        .y(function (d) { return y2(d.Speed); });

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

        

    var Line_chart = svg.append("g")
        .attr("class", "focus")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
        .attr("clip-path", "url(#clip)");

     var focus = svg.append("g")
         .attr("class", "focus")
         .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

d3.csv("data/morley.csv", type, function (error, data) {
  if (error) throw error;

   data.sort(function(a,b){
       return a.date-b.date
   });
  
  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain([0, d3.max(data, function(d) {
      return Math.max(d.Speed, d.extra); })]);
      //slider
   x2.domain(x.domain());
   y2.domain(y.domain());

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

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

    Line_chart.append("path")
        .datum(data)
        .attr("class", "line")
        .style("stroke", "green")
        .attr("d", line);

    Line_chart.append("path")
        .datum(data)
        .attr("class", "line")
        .style("stroke", "blue")
        .attr("d", line1);

//slider line
    context.append("path")
        .datum(data)
        .attr("class", "line")
        .attr("d", line2);

  context.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

//slider
  context.append("g")
      .attr("class", "brush")
      .call(brush)
      .call(brush.move, x.range());

  svg.append("rect")
      .attr("class", "zoom")
      .attr("width", width)
      .attr("height", height)
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .call(zoom);
 
});


//selection in slider
function brushed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; 
  var s = d3.event.selection || x2.range();
  x.domain(s.map(x2.invert, x2));
  Line_chart.select(".line").attr("d", line);
  focus.select(".axis--x").call(xAxis);
  svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
}

 
function zoomed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; 
  var t = d3.event.transform;
  x.domain(t.rescaleX(x2).domain());
  Line_chart.select(".line").attr("d", line);
  focus.select(".axis--x").call(xAxis);
  context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}

   

function type(d) {
  d.date = parseDate(d.date);
  d.extra = +d.extra;
  d.Speed=+d.Speed
  return d;
}

</script>

推荐答案

brushed()zoomed()函数中,您仅在更新绿线(附加日期).您必须标识每一行(例如,使用特定的类),并使用它们各自的d3.line函数来更新它们:

In the brushed() and zoomed() functions, you are only updating the green line (extra VS. date). You have to identify each line (for example with a specific class), and update both using their respective d3.line functions:

Line_chart.append("path")
    .datum(data)
    .attr("class", "line line-extra") // <-- class added here
    .style("stroke", "green")
    .attr("d", line);

Line_chart.append("path")
    .datum(data)
    .attr("class", "line line-speed") // <-- and here
    .style("stroke", "blue")
    .attr("d", line1);

[...]

function updateLines() {
  Line_chart.select(".line-extra").attr("d", line);
  Line_chart.select(".line-speed").attr("d", line1);
}

function brushed() {
  [...]
  updateLines();
}

function zoomed() {
  [...]
  updateLines();
}

这篇关于多线图中的笔刷仅适用于一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆