nvd3: remove y-axis tick labels from discrete bar chart
Asked Answered
P

2

5

I am trying to remove y-axis tick labels from a discrete bar chart in nvd3.

I already looked into the nvd3 source, but can't find an obvious function I could change. Can anyone point me to the solution?

Update: Code

function Transform(value1, value2, chart_name,value3) {
value3 = typeof value3 !== 'undefined' ? value3 : 'no_header';
var chart = nv.models.discreteBarChart()
    .x(function (d) {
    return d.label;
})

    .y(function (d) {
    return d.value;
})

    .staggerLabels(true)
    .tooltips(false)
    .showValues(true);

var a = [];
var f = [{
    values: []
}];
d3.csv("../../csv/master.csv").get(function (error, rows) {

 if (error){
    console.log(error);
    loadError("Uh Oh. This  data set is missing... Try going <a href='/'>back to the start</a>");
    return;
    }

    for (var i = 0; i < rows.length; i++) {
        a.push(rows[i]);
    }
    console.log(a[0].agency);
    for (var key = 0; key < a.length; key++) {
        var b = a[key];
        for (var c in b) {
            if (c != "agency" && c != "division" && c != "pod") {
                var d = b[c];
                a[key][c] = +d;
            }
        }
        console.log(a[0].changes);
        }
  try {
    var e = $.grep(a, function (v) {
      return v.division == {{division|safe}};
        });
    var k = $.grep(e, function(v) {
      return v.agency == {{agency|safe}};
      })[0];
    console.log(k.division);
      }
  catch (TypeError) { loadError("Uh Oh. We could not find this combination\
      of Agency & Division. Try going back to the <a href='/pitchview'>selection menu</a>");
    return;
  }
  if (k.agency){
    for (var g in k) {
        if (g == value1 || g == value2 || g == value3) {
            var h = {
                "label": g,
                    "value": k[g]
            };
            f[0].values.push(h);
        }
    }


    d3.select('#' + chart_name + ' svg')
        .datum(f)
        .transition().duration(500)
        .call(chart)
    nv.utils.windowResize(chart.update);
    return chart;
  } else {
    loadError("Uh Oh. We could not find this combination of Agency &\
        Division. Try going back to the <a href='/pitchview'>selection menu</a>");}});
}
Paddy answered 21/6, 2013 at 10:52 Comment(3)
Have you tried something like chart.yAxis.ticks(0)?Bordello
Yes tried that, after the tooltips option - but did not work. Is this the wrong position?Paddy
This would be a separate line of code, not an option to set.Bordello
P
21

There's a variable in the nvd3 discrete bar-function that removes the y axis (including labels). Use this in your code to remove them:

.showYAxis(false)

A similar variable exists for the x-axis.

Pula answered 6/11, 2013 at 15:59 Comment(1)
This should have been the accepted answer! The above one is more of a hack!Outgoings
T
4

use chart.yAxis.tickValues(0), It will turn off the yticks and keep the y Label

Theriault answered 8/1, 2015 at 9:47 Comment(1)
Great answer~ Plus 1!Dullard

© 2022 - 2024 — McMap. All rights reserved.