[英]Why are some bars are off center in d3 bar chart?
I am building a bar chart using d3 and svg elements. 我正在使用d3和svg元素构建条形图。 I am using RangeBands for placing the bars but for some reason some of the bars aren't ending up in the correct place off center bar char (see the link below I cant attach the image, it should look like the other image centered bar chart . The code I'm using the following code to place the bars let
我正在使用RangeBands放置条,但由于某些原因,某些条没有以正确的位置终止于中心条字符附近 (请参阅下面的链接,我无法附加图像,它看起来应与其他以图像为中心的条形图一样)我正在使用以下代码来放置酒吧的代码
private xAxisGroup: d3.Selection<SVGElement>;
xScale = d3.scale.ordinal()
.domain(viewModel.dataPoints.map(d => d.category))
.rangeRoundBands
([BarChart.Config.Axis.padding, width], BarChart.Config.xScalePadding, 0.2);
let xAxis = d3.svg.axis()
.scale(xScale)
.orient('bottom');
this.xAxisGroup
.attr('transform', 'translate(0, ' + (height - BarChart.Config.Axis.padding) + ')')
.call(xAxis);
d3.select("g.xAxis").selectAll("text")
.attr('fill', d => categoryColor(d) )
.attr('font-weight','bold');
and the actual placement of the bars: 以及钢筋的实际位置:
let bars = this.backgroundBarGroup
.selectAll(".bar")
.data(staticViewModel.dataPoints);
bars.
enter()
.append("rect")
.classed("bar", true);
bars.attr({
width: xScale.rangeBand(),
height: (dataPoint: BarChartDataPoint) => height - BarChart.Config.Axis.padding - yScale(<number>dataPoint.minValue),
y: (dataPoint: BarChartDataPoint) => yScale(<number>dataPoint.value),
x: (dataPoint: BarChartDataPoint) => xScale(dataPoint.category)+18,
})
.style({
'fill-opacity': 0.5,
'stroke-opacity': 0.5,
fill: (dataPoint: BarChartDataPoint) => dataPoint.color,
stroke: (dataPoint: BarChartDataPoint) => dataPoint.strokeColor,
"stroke-width": (dataPoint: BarChartDataPoint) => `${dataPoint.strokeWidth}px`,
});
any help would be really appreciated! 任何帮助将非常感激! Thanks!
谢谢!
Because you need to remove the bars
in front of .attr
因为您需要删除
.attr
前面的bars
let bars = this.backgroundBarGroup
.selectAll(".bar")
.data(staticViewModel.dataPoints);
bars.
enter()
.append("rect")
.classed("bar", true)
.attr({
width: xScale.rangeBand(),
height: (dataPoint: BarChartDataPoint) => height - BarChart.Config.Axis.padding - yScale(<number>dataPoint.minValue),
y: (dataPoint: BarChartDataPoint) => yScale(<number>dataPoint.value),
x: (dataPoint: BarChartDataPoint) => xScale(dataPoint.category)+18,
})
.style({
'fill-opacity': 0.5,
'stroke-opacity': 0.5,
fill: (dataPoint: BarChartDataPoint) => dataPoint.color,
stroke: (dataPoint: BarChartDataPoint) => dataPoint.strokeColor,
"stroke-width": (dataPoint: BarChartDataPoint) => `${dataPoint.strokeWidth}px`,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.