gpt4 book ai didi

javascript - D3 v4 : charting tool: How to send horizontal gridlines behind columns

转载 作者:行者123 更新时间:2023-11-30 10:58:25 25 4
gpt4 key购买 nike

我正在使用 D3 图表工具 绘制图表。问题是我无法在列后面发送水平网格线。请看截图。

enter image description here

This approach here确实将网格线发送到栏后,但与此同时,所有列标签都消失了!请看下面的截图:

enter image description here

代码

public function evd_unitary_growth_plan_chart( $data_str ){
ob_start(); ?>

<style> /* set the CSS */

.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}

.grid line {
stroke: lightgrey;
stroke-opacity: 0.6;
shape-rendering: crispEdges;
}

.grid path {
stroke-width: 0;
}

.axis {
font-size: 13px;
font-family: 'Roboto';
color: #394041;
}

</style>

<script type="text/javascript">
var h = 300;
var w = 750;
var barPadding = 2;

function barColor(data_month, current_month) {
if( parseInt(data_month) >= current_month)
return "#008600";
else
return "#c4c4c4";
}

// set the dimensions and margins of the graph
var margin = {top: 30, right: 20, bottom: 30, left: 40},
width = w - margin.left - margin.right,
height = h - margin.top - margin.bottom;

var data = <?php echo $data_str ?>;

// set the ranges
var x = d3.scaleBand().range([0, width]).padding(0.2);
var y = d3.scaleLinear().range([height, 0]);

var svg = d3.select("#ecbg_unitary").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// Scale the range of the data in the domains
x.domain(data.map(function(d) { return d.month; }));

var y_domain_upperBound = d3.max(data, function(d) { return d.points; });
y_domain_upperBound = Math.round(y_domain_upperBound / 10) * 10 + 10;
y.domain([0, y_domain_upperBound]);

// Create Y-Axis tick array to draw grid lines
var yTicks = [];
var tickInterval = 5;
for(var i = 0; i <= y_domain_upperBound; i = i + 5) {
yTicks.push(i);
}

// gridlines in y axis function
function draw_yAxis_gridlines() {
return d3.axisLeft(y)
.tickValues(yTicks);
}

// append the rectangles for the bar chart
svg.selectAll("rect")
.data(data)
.enter().append("rect")
.attr("x", function(d) {
return x(d.month);
})
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d.points); })
.attr("height", function(d) { return height - y(d.points); })
.attr("fill", function(d){return barColor(d.data_month_number, d.current_month_number)});



// column labels
svg.selectAll("text")
.data(data)
.enter()
.append("text")
.text(function(d) {
return d.points;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return x(d.month) + x.bandwidth() / 2;
})
.attr("y", function(d) {
return y(d.points) - 10;
})
.attr("font-family", "Roboto")
.attr("font-size", "13px")
.attr("font-weight", "bold")
.attr("fill", "#394041");

// add the x Axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));


// add the Y gridlines
svg.append("g")
.attr("class", "grid axis")
.call(draw_yAxis_gridlines()
.tickSize(-width)
);

</script>

<?php return ob_get_clean();
}

最佳答案

JDunken 是对的,你画东西的顺序将决定什么在什么上面,所以你想在矩形之前画水平线,使它们出现在矩形后面。

如果那会弄乱您的标签,这可能与我在您的另一篇文章中提出的关于不只使用 "text" 的观点之一有关。选择列标签。

请记住,当您在 <svg> 上添加轴时这些轴通常包括 <text>元素,所以当你稍后调用 svg.selectAll("text")准备绑定(bind)您的列标签,这个选择很可能会选择一些 <text>坐标轴中的元素,结果不可预测! svg.selectAll("text")将这样做 - 选择所有 <text> svg 中的元素.

您可以改为选择您为列标签保留的类别,例如:

svg.selectAll(".column-label")
.data(data)
.enter()
.append("text")
.attr("class","column-label")

希望对你有帮助

关于javascript - D3 v4 : charting tool: How to send horizontal gridlines behind columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59072258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com