MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
Asked Answered
S

2

20

Data:

nvd3TestData = [
      {
        values:[
          {x:"M",y:1},
          {x:"T",y:2},
          {x:"W",y:3},
          {x:"R",y:3},
          {x:"F",y:4},
          {x:"S",y:5},
          {x:"U",y:6}
        ],
        key:"Apples"
      },
      {
        values:[
          {x:"M",y:5},
          {x:"T",y:2},
          {x:"W",y:6},
          {x:"R",y:8},
          {x:"F",y:2},
          {x:"S",y:4},
          {x:"U",y:1}
        ],
        key:"Zebras"
      }
    ]

Creating the chart (pulled from an angularjs directive):

nv.addGraph -> chart = nv.models.multiBarChart() .stacked(true) .showControls(false)

chart.xAxis
  .axisLabel(attrs.xAxisLabel)

chart.yAxis
  .axisLabel(attrs.yAxisLabel)
  .tickFormat(d3.format(',r'))


console.log element


d3.select(element[0].children[0])
  .datum(nvd3TestData)
  .call(chart)

nv.utils.windowResize(chart.update)

Output:

Output

Expected output would have all 7 labels: M T W R F S U

Salic answered 14/6, 2013 at 4:57 Comment(0)
A
26

I looked on nvd3.org and couldn't find any real documentation, but checking the source, I found https://github.com/novus/nvd3/blob/master/src/models/multiBarChart.js which showed a boolean chart parameter of "reduceXTicks" with a comment indicating it would do what you want. I tried it out with one of the example charts and it worked. Specifically, I used:

chart
   .reduceXTicks(true)
Arredondo answered 14/6, 2013 at 6:9 Comment(4)
Thanks! Not sure how I missed that when I looked over the source.Salic
I think you meant .reduceXTicks(false)Brest
This works, but for my case, the labels are overlapping each other :-(Selfwill
@Selfwill stagger them?Arrington
C
2

There is no proper documentation, but you can acheive it. Just add the below line to your chart reduceXTicks: false

Rest will follow. Thanks

Cursory answered 27/4, 2017 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.