Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the domain and scale on an yaxis on a discreteBarChart nvd3.js

Tags:

d3.js

nvd3.js

I'm using d3.js charts in one of my applications. Here they are in this image See Charts

For Y axis on Money chart (See in the Image), I want maximum value rounded to 400, no matter what maximum bar size is here it is $358.72, but I want to keep bar at 358.72 but on Y Axis it would be 400 Limit.

Code for it is here

tradesChart = [ 
      {
        key: "Cumulative Return",
        values: [
                        {
            "label" : "6E",
            "value" : 100       },
                    {
            "label" : "NG",
            "value" : 100       },
                    {
            "label" : "TF",
            "value" : 67        }        ]
      }
    ];
        nv.addGraph(function() {

      var chart = nv.models.discreteBarChart()
          .x(function(d) { return d.label })
          .y(function(d) { return d.value/100 })
          .staggerLabels(true)
          .showValues(true)
          .valueFormat(d3.format('.0%'))

      chart.yAxis.tickFormat(function(d) { return d3.format('d')(d*100) + '%'; });

      d3.select('#chart-trades svg')
        .datum(tradesChart)
        .transition().duration(500)
        .call(chart);

      nv.utils.windowResize(chart.update);

      return chart;
    });

Kindly Help me to solve this out

like image 845
Nawaz Avatar asked Feb 12 '13 20:02

Nawaz


1 Answers

You need to modify the domain of the Y-axis scale. Usually it is derived from the maximum value of the data with a statement like the following:

yScale.domain([0, d3.max(data, function (d) { return d.v; }) ]);

In your case, you should modify it to be more like this instead:

yScale.domain([0, 400]);

Alternatively, if you want to set the maximum value from the data or a minimum static value, you could do something like the following:

yScale.domain([0, d3.max(data, function (d) { return d.v > 400 ? d.v : 400; }) ]);

A full example jsfiddle is here.

That's how to do it with D3.js, I'm not familiar with the venerable nvd3.js lib, so I'm not sure how to access the scale, but i'll take a look and see if I can find it.

like image 96
Scott Willeke Avatar answered Jan 04 '23 04:01

Scott Willeke