【问题标题】:D3.js Dc.js Binding Event listener to axis tick labelD3.js Dc.js 绑定事件侦听器到轴刻度标签
【发布时间】:2018-02-07 09:46:47
【问题描述】:

我有一个基本需求,将事件侦听器绑定到图表中每个刻度的标签(不是轴标签,而是每个刻度上的数据标签)。 让我们以这张图表为例。

http://dc-js.github.io/dc.js/examples/ordinal-bar.html

我想写一些类似的东西,

 *.on('click', function(){console.log("click!")}) 

到轴上的每个水果名称(例如,当我单击“苹果”或“香蕉”...文本时,它应该听我的点击。我尝试了几次但都失败了,例如:

d3.selectAll("g.axis.x g.tick text").on("click", function(){console.log("Mouse click")})

你能帮我看看是否可以解决这个问题吗? 感谢您的帮助!

【问题讨论】:

标签: javascript d3.js dc.js


【解决方案1】:

@戈登

感谢您在此处将正确答案转发给我:

DC.js Barchart- provide tooltip or title on the X axis labels

原因:dc.js 在其 CSS 中阻止了其轴上的指针事件。
解决方案:添加 css

.dc-chart g.axis text {
    pointer-events: auto;
}

【讨论】:

    【解决方案2】:

    调用xAxis时只需选择所有text元素

    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
      .selectAll('text')
      .style('cursor', 'pointer')
      .on('click', barClick);
    
    function barClick(){
        alert('click')
    }
    

    这是一个小提琴:

    var margin = {
        top: 40,
        right: 20,
        bottom: 30,
        left: 40
      },
      width = 960 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;
    
    var formatPercent = d3.format(".0%");
    
    var x = d3.scale.ordinal()
      .rangeRoundBands([0, width], .1);
    
    var y = d3.scale.linear()
      .range([height, 0]);
    
    var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom");
    
    var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .tickFormat(formatPercent);
    
    
    
    var svg = d3.select("body").append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    
    
    
    var data = JSON.parse(document.getElementById('data').innerHTML);
    
    //d3.tsv("data.tsv", type, function(error, data) {
    x.domain(data.map(function(d) {
      return d.letter;
    }));
    y.domain([0, d3.max(data, function(d) {
      return d.frequency;
    })]);
    
    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
      .selectAll('text')  
      .style('cursor', 'pointer')
      .on('click', barClick);
    
    function barClick() {
      alert('click')
    }
    
    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Frequency");
    
    svg.selectAll(".bar")
      .data(data)
      .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) {
        return x(d.letter);
      })
      .attr("width", x.rangeBand())
      .attr("y", function(d) {
        return y(d.frequency);
      })
      .attr("height", function(d) {
        return height - y(d.frequency);
      })
    
    //});
    
    function type(d) {
      d.frequency = +d.frequency;
      return d;
    }
    body {
      font: 10px sans-serif;
    }
    
    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }
    
    .bar {
      fill: orange;
    }
    
    .bar:hover {
      fill: orangered;
    }
    
    .x.axis path {
      display: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <script type="application/json" id="data">
      [{
          "letter": "A",
          "frequency": 0.08167
        },
        {
          "letter": "B",
          "frequency": 0.01492
        },
        {
          "letter": "C",
          "frequency": 0.02780
        },
        {
          "letter": "D",
          "frequency": 0.04253
        },
        {
          "letter": "E",
          "frequency": 0.12702
        },
        {
          "letter": "F",
          "frequency": 0.02288
        },
        {
          "letter": "G",
          "frequency": 0.02022
        },
        {
          "letter": "H",
          "frequency": 0.06094
        },
        {
          "letter": "I",
          "frequency": 0.06973
        },
        {
          "letter": "J",
          "frequency": 0.00153
        },
        {
          "letter": "K",
          "frequency": 0.00747
        },
        {
          "letter": "L",
          "frequency": 0.04025
        },
        {
          "letter": "M",
          "frequency": 0.02517
        },
        {
          "letter": "N",
          "frequency": 0.06749
        },
        {
          "letter": "O",
          "frequency": 0.07507
        },
        {
          "letter": "P",
          "frequency": 0.01929
        },
        {
          "letter": "Q",
          "frequency": 0.00098
        },
        {
          "letter": "R",
          "frequency": 0.05987
        },
        {
          "letter": "S",
          "frequency": 0.06333
        },
        {
          "letter": "T",
          "frequency": 0.09056
        },
        {
          "letter": "U",
          "frequency": 0.02758
        },
        {
          "letter": "V",
          "frequency": 0.01037
        },
        {
          "letter": "W",
          "frequency": 0.02465
        },
        {
          "letter": "X",
          "frequency": 0.00150
        },
        {
          "letter": "Y",
          "frequency": 0.01971
        },
        {
          "letter": "Z",
          "frequency": 0.00074
        }
      ]
    </script>

    【讨论】:

    • 这是一个仅限 d3 的答案。可能还不清楚,但问题是关于 dc.js。
    • @Gordon 哦,对不起。我在那里感到困惑。 OP 可以做的是使用 d3.js 而不是 dc.js 创建轴和刻度。我不知道 dc.js 是如何工作的,但这应该可以。谢谢和欢呼:)
    • 谢谢,aditya.kk29。但是,我关心的是DC.js构建的图表。比如使用提供的页面中的示例,我们如何将事件监听器绑定到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多