【发布时间】:2017-07-23 10:47:12
【问题描述】:
我正在尝试拆分一些 D3 Graph Y 轴垂直彩色线。您可以在下图中看到当前的外观和外观:
到目前为止,我尝试的是为该 g 元素设置背景颜色,这样该行就不会显示在该特定区域,但它不起作用;我无法用background-color: gray; 设置背景颜色。
为了更好地了解 HTML 标记,您可以在下图中看到 彩色线条 用 D3 foreignObjects 表示,它们位于 下方 文本值。
有人知道如何分割这些彩色线条吗?
更新
在我的情况下仍然不起作用。我首先尝试渲染彩色线条,然后是文本值。这是我的代码:
//Add colored lines
this.graph.append("foreignObject")
.attr("id", "legendBackground" + segmentId)
.attr("class", "legendBackground")
.attr("height", this.graphHeight)
.attr("width", "2")
.attr("y", 0)
.attr('x', xOffset + 11)
.style('background-color', (d) => this.colors(segmentId));
this.updateLegend();
//Add text values
var yScale = this.getYScale(domain);
var yAxis = d3.svg.axis()
.scale(yScale)
.tickSize(value)
.tickFormat(d3.format(".1f"))
.tickPadding(this.isSmallScreen ? 6 : 12)
.orient("left");
/** If there is already an xAxis then update this one, do not redraw from scratch */
if (!this.graph.select(".y.axis" + (secondary ? "2" : "")).empty()) {
var t0 = this.graph.select(".y.axis" + (secondary ? "2" : ""))
.attr("transform", "translate(" + (secondary ? ( this.primaryAxis.width + this.padding.left + this.secondaryAxis.width + this.axisSpacing ) : ( this.primaryAxis.width + this.padding.left )) + ",0)")
.transition()
.call(yAxis);
} else {
var t0 = this.graph.insert("svg:g",":first-child")
.attr("class", secondary ? "y axis2" : "y axis")
.attr("transform", "translate(" + (secondary ? ( this.primaryAxis.width + this.padding.left + this.secondaryAxis.width + this.axisSpacing ) : ( this.primaryAxis.width + this.padding.left )) + ",0)")
.transition()
.call(yAxis);
}
【问题讨论】:
标签: javascript html css d3.js