javascript - Data-driven Axis-Label Hyperlinks in D3 -


i'm trying add data-driven hyperlinks y-axis labels d3.js charts i'm creating. however, despite best efforts search online solution problem, haven't yet found exact solution (or i'm misunderstanding in solutions i've seen).

to keep things simple, created basic horizontal bar chart use example. here's code:

    var table = [           { id: 1, name: 'entity 1', value: 10 },           { id: 2, name: 'entity 2', value: 20 },           { id: 3, name: 'entity 3', value: 30 },         ];      var height = 100;     var width = 300;     var leftmargin =  100;     var rightmargin = 10;     var topmargin = 10;     var bottommargin = 50;      var x = d3.scale.linear()         .domain([0, 30])         .range([0, width]);      var y = d3.scale.ordinal()         .domain(table.map(function(d, i) {             return d.name; }))         .rangeroundbands([0, height]);      var chart = d3         .select('body')         .append('svg')         .attr('height', height + topmargin + bottommargin)         .attr('width', width + leftmargin + rightmargin);      var bars = chart         .selectall('rect')         .data(table)         .enter()         .append('rect')             .attr('x', x(0) + leftmargin)             .attr('y', function(d, i) {                 return y(d.name) + topmargin; })             .attr('height', y.rangeband())             .attr('width', function(d, i) {                 return x(d.value); });      var xaxis = d3.svg         .axis()         .scale(x)         .orient('bottom');      chart         .append('g')         .attr("transform", "translate(" + leftmargin + "," + (height + 15) + ")")         .call(xaxis);      var yaxis = d3.svg         .axis()         .scale(y)         .orient('left');      chart         .append('g')         .attr("transform", "translate(" + (leftmargin - 5) + "," + topmargin + ")")         .call(yaxis); 

here chart code produces (and yes, i've intentionally left unstyled keep things simple):

enter image description here

essentially, need demonstrate how modify code y-axis labels hyperlinks of format "http://www.test.com/entity/{entityid}", "{entityid"} integer id associated each row of data".

i'm fine either svg:a hyperlinks or javascript on-click solution.

i've looked @ solutions involve making plot elements (e.g. bars) hyperlinkable, solution doesn't solve specific problem. in addition, have multiple charts on same page, i'm not solution involving d3.selectall('text') work, unless there way filter y-axis labels on specific chart.

any can provide appreciated!

i feel selectall way go. if don't want select text elements, add class text or 1 of text containers.

chart     .append('g')     .attr("transform", "translate(" + (leftmargin - 5) + "," + topmargin + ")")     .attr("class", "entitylink") //***this new***     .call(yaxis);  //*** new *** d3.selectall(".entitylink text")   .on("click", function() { alert("hooray"); }); 

css

.entitylink text {     fill: blue;     cursor: pointer; } 

full example: https://jsfiddle.net/memrr22q/

you'll have figure out how right data click event, seems easy.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -