NVD3 - Changing X Label Axis on a Line Graph

浪尽此生 提交于 2019-12-01 05:32:12

问题


I have a simple line graph with data in the format:

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

}

and I pass this into my nvd3 object:

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;
});

How can I have my x-axis ticks to show: * eight labels: label1 - label8

Rather than have the grids broken up into a variable number of lines?


回答1:


Try something like this

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

or if you want to get it from the dataset, you could try something like this,

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

    return dataset;
};)

Hope it helps



来源:https://stackoverflow.com/questions/20671447/nvd3-changing-x-label-axis-on-a-line-graph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!