简体   繁体   English

将规则添加到使用d3.svg.axis的图表

[英]Adding rules to charts that use d3.svg.axis

I'm using a line chart that's essentially a copy of the code at http://bl.ocks.org/3883245 . 我使用的折线图基本上是http://bl.ocks.org/3883245上代码的副本。 I would like to add horizontal rules to the graph, however when I try to access the calculated tick values via yAxis.tickValues() I only get a null response. 我想在图表中添加水平规则,但是当我尝试通过yAxis.tickValues()访问计算的tick值时,我只得到一个null响应。 Am I going about that correctly? 我正确地谈到了吗?

tickValues is used to set custom, externally determined tick positions. tickValues用于设置自定义的,外部确定的刻度位置。 So if you let the axis choose the values then this property will properly be null. 因此,如果您让轴选择值,则此属性将正确为null。

The easiest way to add grid lines is actually to add another axis! 添加网格线的最简单方法实际上是添加另一个轴! In the example you linked immediately following the lines: 在示例中,您紧跟以下行链接:

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

you can add the following to append a grid: 您可以添加以下内容以附加网格:

svg.append("g").attr("class", "xaxisgrid").call( xAxis.tickFormat("").tickSize(450) );

svg.select("g.xaxisgrid").selectAll(".tick")
    .style('stroke', "#000")
    .style('opacity', 0.4)
    .filter(function(d, i){ return d3.select(this).classed('minor');} )
        .style('opacity', 0.1);

svg.select("g.xaxisgrid .domain").style('fill', 'none');

The code is a bit rough, but should get you started. 代码有点粗糙,但应该让你开始。 Basically I am slightly modifying the axis generation function(xAxis) to generate only ticks, and then I am making the ticks really long. 基本上我稍微修改轴生成函数(xAxis)以仅生成刻度,然后我使刻度非常长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM