d3js:在气泡图中使用来自输入的特定参数突出显示气泡

3

我有一个d3脚本,其中的数据格式如下:

var data = [{name: "A", rank: 0, student_percentile: 100.0, 
             admit_probability: 24},
            {name: "B", rank: 45, student_percentile: 40.3, 
             admit_probability: 24},
            {name: "C", rank: 89, student_percentile: 89.7, 
             admit_probability: 24},
            {name: "D", rank: 23, student_percentile: 10.9, 
             admit_probability: 24},
            {name: "E", rank: 56, student_percentile: 30.3, 
             admit_probability: 24}];

当页面首次加载时,我会用这个数据制作一个气泡图。之后,用户可以输入(从A到E)。图表的x轴由“学生百分位”组成,y轴排名组成。在接收到用户输入后,我想突出显示此名称的气泡(我拥有用户提供的输入的排名和“学生百分位”)。

现在,我不明白如何使用从输入中接收到的cx=xscale(学生百分位),cy=yscale(排名)过滤圆圈。

我拥有的脚本如下:

var svg;
var margin = 40,
    width = 600,
    height = 400;

xscale = d3.scaleLinear()
                  .domain(
                        d3.extent(data, function(d) { return +d.student_percentile; })
                    )
                  .nice() 
                  .range([0, width]);

yscale = d3.scaleLinear()
                  .domain(d3.extent(data, function(d) { return +d.rank; }))
                  .nice()
                  .range([height, 0]);

    var xAxis = d3.axisBottom().scale(xscale);

    var yAxis = d3.axisLeft().scale(yscale);

    svg = d3.select('.chart')
                    .classed("svg-container", true)
                    .append('svg')
                    .attr('class', 'chart')
                    .attr("viewBox", "0 0 680 490")
                    .attr("preserveAspectRatio", "xMinYMin meet")
                    .classed("svg-content-responsive", true)
                    .append("g")
                    .attr("transform", "translate(" + margin + "," + margin + ")");

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

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

    // var legend = svg.append("g")
    //  .attr('class', 'legend')
    var color = d3.scaleOrdinal(d3.schemeCategory10);

    var local = d3.local();
    circles = svg.selectAll(null)
          .data(data)
          .enter()
          .append("circle")
          .attr("cx", width / 2)
          .attr("cy", height / 2)
          .attr("opacity", 0.3)
          .attr("r", 20)
          .style("fill", function(d){
            if(+d.admit_probability <= 40){
                return "red";
            }
            else if(+d.admit_probability > 40 && +d.admit_probability <= 70){
                return "yellow";
            }
            else{
                return "green";
            }
          })
          .attr("cx", function(d) {
            return xscale(+d.student_percentile);
          })
          .attr("cy", function(d) {
            return yscale(+d.rank);
          })
          .on('mouseover', function(d, i) {
            local.set(this, d3.select(this).style("fill"));
            d3.select(this)
              .transition()
              .duration(1000)
              .ease(d3.easeBounce)
              .attr("r", 32)
              .style("fill", "orange")
              .style("cursor", "pointer")
              .attr("text-anchor", "middle");
            }
           )
          .on('mouseout', function(d, i) {
            d3.select(this).style("fill", local.get(this));
            d3.select(this).transition()
              .style("opacity", 0.3)
              .attr("r", 20)
              .style("cursor", "default")
            .transition()
            .duration(1000)
            .ease(d3.easeBounce)
          });

    texts = svg.selectAll(null)
      .data(data)
      .enter()
      .append('text')
      .attr("x", function(d) {
        return xscale(+d.student_percentile);
      })
      .attr("text-anchor", "middle")
      .attr("y", function(d) {
        return yscale(+d.rank);
      })
      .text(function(d) {
        return +d.admit_probability;
      })
      .attr("pointer-events", "none")
      .attr("font-family", "sans-serif")
      .attr("font-size", "12px")
      .attr("fill", "red");

    svg.append("text")
        .attr("transform", "translate(" + (width / 2) + " ," + (height + margin) + ")")
        .style("text-anchor", "middle")
        .text("Percentile");

    svg.append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 0 - margin)
        .attr("x",0 - (height / 2))
        .attr("dy", "1em")
        .style("text-anchor", "middle")
        .text("Rank");

提前感谢你!

1个回答

3

您可以处理 keyup 输入事件,并使用本地的d3.filter 方法过滤您的circle 选择。请查看以下示例代码以获取更多帮助。

d3.select('#user-input').on('keyup', function() {
  var value = d3.event.target.value;

  circles.filter(function(circle) {
    return circle.name === value.trim().toUpperCase();
  })
  .each(function() {
    local.set(this, d3.select(this).style("fill"));
  })
  .transition()
  .duration(1000)
  .ease(d3.easeBounce)
  .attr("r", 32)
  .style("fill", "orange")
  .style("cursor", "pointer")
  .attr("text-anchor", "middle");

  circles.filter(function(circle) {
    return circle.name !== value.trim().toUpperCase();
  })
  .transition()
  .attr("r", 20)
  .style("cursor", "default")
  .style("fill", function() { return  local.get(this) || d3.select(this).style("fill"); })
  .transition()
  .duration(1000)
  .ease(d3.easeBounce)
});

var svg;
var margin = 40,
  width = 600,
  height = 400;

var data = [{
  name: "A",
  rank: 0,
  student_percentile: 100.0,
  admit_probability: 24
}, {
  name: "B",
  rank: 45,
  student_percentile: 40.3,
  admit_probability: 24
}, {
  name: "C",
  rank: 89,
  student_percentile: 89.7,
  admit_probability: 24
}, {
  name: "D",
  rank: 23,
  student_percentile: 10.9,
  admit_probability: 24
}, {
  name: "E",
  rank: 56,
  student_percentile: 30.3,
  admit_probability: 24
}];

xscale = d3.scaleLinear()
  .domain(
    d3.extent(data, function(d) {
      return +d.student_percentile;
    })
  )
  .nice()
  .range([0, width]);

yscale = d3.scaleLinear()
  .domain(d3.extent(data, function(d) {
    return +d.rank;
  }))
  .nice()
  .range([height, 0]);

var xAxis = d3.axisBottom().scale(xscale);

var yAxis = d3.axisLeft().scale(yscale);

svg = d3.select('.chart')
  .classed("svg-container", true)
  .append('svg')
  .attr('class', 'chart')
  .attr("viewBox", "0 0 680 490")
  .attr("preserveAspectRatio", "xMinYMin meet")
  .classed("svg-content-responsive", true)
  .append("g")
  .attr("transform", "translate(" + margin + "," + margin + ")");

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

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

// var legend = svg.append("g")
//  .attr('class', 'legend')
var color = d3.scaleOrdinal(d3.schemeCategory10);

var local = d3.local();
circles = svg.selectAll(null)
  .data(data)
  .enter()
  .append("circle")
  .attr("cx", width / 2)
  .attr("cy", height / 2)
  .attr("opacity", 0.3)
  .attr("r", 20)
  .style("fill", function(d) {
    if (+d.admit_probability <= 40) {
      return "red";
    } else if (+d.admit_probability > 40 && +d.admit_probability <= 70) {
      return "yellow";
    } else {
      return "green";
    }
  })
  .attr("cx", function(d) {
    return xscale(+d.student_percentile);
  })
  .attr("cy", function(d) {
    return yscale(+d.rank);
  })
  .on('mouseover', function(d, i) {
    local.set(this, d3.select(this).style("fill"));
    d3.select(this)
      .transition()
      .duration(1000)
      .ease(d3.easeBounce)
      .attr("r", 32)
      .style("fill", "orange")
      .style("cursor", "pointer")
      .attr("text-anchor", "middle");
  })
  .on('mouseout', function(d, i) {
    d3.select(this).transition()
      .style("opacity", 0.3)
      .style("fill", local.get(this))
      .attr("r", 20)
      .style("cursor", "default")
      .transition()
      .duration(1000)
      .ease(d3.easeBounce)
  });

texts = svg.selectAll(null)
  .data(data)
  .enter()
  .append('text')
  .attr("x", function(d) {
    return xscale(+d.student_percentile);
  })
  .attr("text-anchor", "middle")
  .attr("y", function(d) {
    return yscale(+d.rank);
  })
  .text(function(d) {
    return +d.admit_probability;
  })
  .attr("pointer-events", "none")
  .attr("font-family", "sans-serif")
  .attr("font-size", "12px")
  .attr("fill", "red");

svg.append("text")
  .attr("transform", "translate(" + (width / 2) + " ," + (height + margin) + ")")
  .style("text-anchor", "middle")
  .text("Percentile");

svg.append("text")
  .attr("transform", "rotate(-90)")
  .attr("y", 0 - margin)
  .attr("x", 0 - (height / 2))
  .attr("dy", "1em")
  .style("text-anchor", "middle")
  .text("Rank");

d3.select('#user-input').on('keyup', function() {
 var value = d3.event.target.value;
  
  circles.filter(function(circle) {
   return circle.name === value.trim().toUpperCase();
  })
  .each(function() {
   local.set(this, d3.select(this).style("fill"));
  })
  .transition()
    .duration(1000)
    .ease(d3.easeBounce)
    .attr("r", 32)
    .style("fill", "orange")
    .style("cursor", "pointer")
    .attr("text-anchor", "middle");
    
  circles.filter(function(circle) {
   return circle.name !== value.trim().toUpperCase();
  })
  .transition()
  .attr("r", 20)
  .style("cursor", "default")
  .style("fill", function() { return local.get(this) || d3.select(this).style("fill") })
  .transition()
  .duration(1000)
  .ease(d3.easeBounce)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
<div class="chart"></div>
<h2>Type "A", "B", "C", "D", or "E" in input below</h2>
<input type="text" id="user-input">


非常好!非常感谢:D - Yesha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接