410*_*one 7 html javascript graph d3.js
我想在我的D3图的x轴上呈现HTML.基本上,我希望轴上的每个标签都是数据中另一列的超链接.
我试过了
x.domain(data.map(function(d) { return "<a href=\"" + d.SiteUrl + "\">" + d.Name + "</a>"; }));
Run Code Online (Sandbox Code Playgroud)
但它根本不起作用.而不是获取超链接我得到实际的文本值:
Run Code Online (Sandbox Code Playgroud)<a href="http://example.com">Something</a>
我也尝试过添加
.tickFormat(function(d) { return "<a href=\"" + d.SiteUrl + "\">" + d.Name + "</a>"; })
Run Code Online (Sandbox Code Playgroud)
在x轴上,以及改变.attr("x", ...)
到
.attr("x", function(d) { return "<a href=\"" + d.SiteUrl + "\">" + x(d.Name) + "</a>"; })
Run Code Online (Sandbox Code Playgroud)
在图表上.
我错过了什么吗?
(基于https://groups.google.com/d/msg/d3-js/zoUjrm75iCg/EJg0YhnTw3YJ)
技巧是使用svg:foreignObject
(如@thatOneGuy 在评论中所述),然后您可以将任何 HTML 放置在轴标签内。下面的函数html
被传递data[i].name
,可以在其中放置任意 HTML。
var height = 500;
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(0).tickPadding(0).tickFormat(function(d) {return '';});
var tx = -5;
var ty = 2;
var tw = 50;
var th = 10;
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("g")
.append("svg:foreignObject")
.attr("width",tw)
.attr("height",th)
.attr("x", tx)
.attr("y", ty)
.append("xhtml:div")
.attr("class", "my-x-axis-label")
.html(function(schema) {return schema;});
Run Code Online (Sandbox Code Playgroud)
CSS:
div.my-x-axis-label {
font: 10px sans-serif;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1288 次 |
最近记录: |