D3.js Dc.js将事件监听器绑定到轴刻度标签

时间:2018-02-07 09:46:47

标签: javascript d3.js dc.js

我有一个基本需要将事件监听器绑定到我的图表中每个tick的标签(不是轴标签,而是每个tick上的数据标签)。 我们以此图表为例。

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

我想说点什么,

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

到轴上的每个水果名称(例如,当我点击" apple"或" banana" ...文本时,它应该听我的点击。我一直在尝试了几次尝试但都失败了,例如:

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

你能告诉我,无论如何能解决这个问题吗? 谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

@Gordon

感谢您转发给我正确答案:

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

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

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

答案 1 :(得分:0)

只需在调用text

时选择所有xAxis元素即可
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>