简体   繁体   中英

Bind data to axis ticks d3.js

I would bind data to my axis ticks (generated by ordinal scale) to use it later on mouseover event.

I don't want to display it, just bind it:

  //data what I would like to bind on each tick (eg. "<0,1" tick binded to 10
//"<0,2" tick binded to 28
//"<0,3" tick binded to 4
//...
  var data = [10,28,4,6,10,65,87,54, 9, 1,45];
//what is display on tick
  this.xLabelsValues = ["<0,1", "<0,2", "<0,3", "<0,4", "<0,5", 
                        "<0,6", "<0,7", "<0,8", "<0,9", "<1", "=1"];

  this.x = d3.scale.ordinal()
    .rangeRoundBands([0, this.width], .1);

    this.x.domain(chart.xLabelsValues);

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

//my event
 this.svg.selectAll(".tick").on("mouseover", this.mouseover);

  this.mouseover = function(d, e){
   //I want my binded data here!
};

Is that possible or I've to trick something?

The ticks are already bound to the tick values.

Something like this trick to set some extra data:

d3.selectAll(".tick")[0].forEach(function(tick){
          //set the data all ticks
          d3.select(tick)[0][0].myData = {foo:"bar"} 


        });

get data from ticks

d3.selectAll(".tick")[0].forEach(function(tick){
          //set the data


          console.log(d3.select(tick)[0][0].myData)
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM