NVD3 - 在折线图上更改X标签轴

时间:2013-12-19 00:39:07

标签: nvd3.js

我有一个简单的折线图,其格式为:

[
    {
        label: "lebel1",
        x: 0,
        y: 128
    },
    {
        label: "lebel1",
        x: 1,
        y: 128
    },
    ....
    {
        label: "lebel2",
        x: 25,
        y: 128
    },
    ....
    {
        label: "lebel8",
        x: 285,
        y: 128
    },
    .... 

}

我把它传递给我的nvd3对象:

nv.addGraph(function() 
{
    var chart = nv.models.lineChart();

    chart.xAxis
        .axisLabel("My X-Axis")
        .ticks(36)
        .tickFormat(function(d) { return d; });

    chart.yAxis
        .axisLabel('Voltage (v)')
        .tickFormat(d3.format('.02f'));

    d3.select('div svg')
        .datum(myData)
        .transition().duration(500)
        .call(chart);

    nv.utils.windowResize(function() { d3.select(gridSvgId).call(chart) });

    return chart;
});

如何显示我的x轴刻度: *八个标签:label1 - label8

而不是将网格划分为可变数量的行?

1 个答案:

答案 0 :(得分:3)

尝试这样的事情

chart.xAxis.tickValues(['Label 1','Label 2','Label 3','Label 4','Label 5','Label 6','Label 7','Label 8']);

或者如果你想从数据集中获取它,你可以尝试这样的事情,

chart.xAxis.tickValues(function(d) {
    // do all you stuff and return an array
    var dataset = ['Build Array from data'];

    return dataset;
};)

希望有所帮助

相关问题