KingFish
KingFish

Reputation: 9153

NVD3 - Changing X Label Axis on a Line Graph

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?

Upvotes: 4

Views: 8583

Answers (1)

shabeer90
shabeer90

Reputation: 5151

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

Upvotes: 3

Related Questions