简体   繁体   English

更新 D3 轴标签

[英]Updating D3 Axis Labels

I have three radio buttons that change the value on the x-axis for my d3 line graph.我有三个单选按钮,用于更改 d3 折线图的 x 轴上的值。 I have successfully been able to change the scales, however not the x-axis label.我已经成功地改变了比例,但不是 x 轴标签。 Everywhere I look I just see info for the tick labels, not the axes label (in the gif "Minute").我到处看我只看到刻度标签的信息,而不是轴标签(在 gif“分钟”中)。

动态量表

I have successfully been able to update and thus re-scale the axes as with the code below, however cannot figure out how to change the label.我已经成功地更新并因此重新缩放轴,如下面的代码,但是无法弄清楚如何更改标签。

    function updateGraph(graphData, timeScale){

    yDomain = d3.extent(graphData, function(d){return Number(d.amt)});

    switch(timeScale) {
        case 'min':
            xDomain = d3.extent(graphData, function(d){return Number(d.min)});
            break;
        case 'hour':
            xDomain = d3.extent(graphData, function(d){return Number(d.hour)});
            break;
        case 'day':
            xDomain = d3.extent(graphData, function(d){return Number(d.day)});
            break;
        default: break;

    }

    //Make the scale for the graphs dynamic
    yScale.domain(yDomain);

    xScale.domain(xDomain);

    vis = d3.select('#visualisation').transition();
    //add axes with proper scale, orientation, and position 

    var line = d3.svg.line()
    .x(function(d){

        switch(timeScale) {
            case 'min':
                return xScale(d.min);
                break;
            case 'hour':
                return xScale(d.hour);
                break;
            case 'day':
                return xScale(d.day);
                break;
            default: break;

        }

    })
    .y(function(d){
        return yScale(d.amt);
    })
    .interpolate('basis');

    var xAxisLabel = function(){
        switch(timeScale) {
            case 'min':
                return 'Minute';
                break;
            case 'hour':
                return 'Hours';
                break;
            case 'day':
                return 'Day';
                break;
            default: break;

        }
    }

    vis.select('.line')
        .duration(750)
        .attr('d', line(graphData))
    vis.select('.xaxis')
        .duration(750)
        .call(xAxis);
    vis.select('.yaxis')
        .duration(750)
        .call(yAxis);
    //Tried to hardcode with 'sugar' to no avail, would eventually like to use xAxisLabel declared above.
    vis.select('.text')
        .duration(750)
        .text('sugar');
}

I set the text when I first made the graph with the following code:我在第一次使用以下代码制作图表时设置了文本:

 vis.append('text')
        .attr('text-anchor', 'middle')  // this makes it easy to centre the text as the transform is applied to the anchor
        .attr('transform', 'translate('+ (WIDTH/2) +','+(HEIGHT+(MARGINS.bottom/3))+')')  // centre below axis
        .text(xAxisLabel);

Try this: first, create a variable for your text.试试这个:首先,为你的文本创建一个变量。

var textLabel = vis.append("text")
   .attr('text-anchor', 'middle')  // this makes it easy to centre the text as the transform is applied to the anchor
    .attr('transform', 'translate('+ (WIDTH/2) +','+(HEIGHT+(MARGINS.bottom/3))+')')  // centre below axis
    .text(xAxisLabel);

And then, after updating:然后,更新后:

textLabel.duration(750).text(xAxisLabel)

Alternatively, if the above solution don't work (because vis is a transition() selection), you can simply try this one, since you are selecting a DOM element:或者,如果上述解决方案不起作用(因为vis是一个transition()选择),您可以简单地尝试这个,因为您正在选择一个 DOM 元素:

vis.select('.text')[0][0]
    .duration(750)
    .textContent = (xAxisLabel);

If you still get an "is not a function" error, change vis for the original variable that you used to append the svg.如果您仍然收到“不是函数”错误,请更改用于附加 svg 的原始变量的vis

Edit: don't forget to set the class "text" to the text.编辑:不要忘记将类“文本”设置为文本。

As suggested by Gerardo in the comments, giving the label the class 'text' upon creation was the missing link.正如 Gerardo 在评论中所建议的,在创建时给标签类“文本”是缺失的链接。

var xAxisLabel = vis.append('text').attr("class", "text")//the rest of the code 

I was then able to change it in my update function as so:然后我可以在我的更新功能中更改它,如下所示:

    vis.select('.text')
    .duration(750)
    .text(xAxisLabel);

Thank you Gerardo!谢谢杰拉尔多!

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

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