如何使用 nvd3.js 设置图表的 Y 轴值

How to set the value of the Y-AXIS of Graphs with nvd3.js

我正在尝试使用 nvd3.js 设置 y 轴 的值: 这是我在 Y 轴上得到的: [0,0.1,0.2,0.3,...,0.9,1]

我想要的是只显示 0 和 1。 我试过 chart.yRange([0,1]) 但对我不起作用。知道如何实现这一点。

这是我拥有的:

这就是我想要得到的:

这是代码:

line: function () {
        var self = this,
            dim_x = this.pivot.rows.groupby.length,
            dim_y = this.pivot.cols.groupby.length;

        var rows = this.pivot.get_rows_with_depth(dim_x),
            labels = _.pluck(rows, 'title');

        var data = _.map(this.pivot.get_cols_leaves(), function (col) {
            var values = _.map(rows, function (row, index) {
                return {x: index, y: self.pivot.get_values(row.id,col.id)[0] || 0};
            });
            var title = _.map(col.path, function (p) {
                return p || _t('Undefined');
            }).join('/');
            if (dim_y === 0) {
                title = self.pivot.measures[0].string;
            }
            return {values: values, key: title};
        });

        nv.addGraph(function () {
            // var chart = nv.models.lineWithFocusChart()
            var chart = nv.models.lineChart()
                .x(function (d,u) { return u; });
            /// get the max and the min of the chart
            var i;
            var max,min;
            for(i=0;i<data[0].values.length;i++){
            //alert("Y:"+data[0].values[i].y);
            if (i==0) {
                max=data[0].values[i].y;min=data[0].values[i].y;
            }
            if (max<data[0].values[i].y) {
                max=data[0].values[i].y;
            }
            if (min>data[0].values[i].y) {
                min=data[0].values[i].y;
            }
            }


            chart.xAxis.tickFormat(function (d,u) {return labels[d];});
            if ((min == 0) &(max==1)){
                //here i check when the max is 1 and the min is 0
            }

            //alert("TICKS:"+chart.yAxis.ticks())

            d3.select(self.svg)
                .attr('width', self.width)
                .attr('height', self.height)
                .datum(data)
                .call(chart);
            return chart;
          });
    }

在 jsfiddle 中查看我的示例代码。net/varatharaj/442o9kxh

在您的 y 轴图表对象上调用 tickValues([0,1])