2

我正在使用 d3 创建一个柱形图。我有 263 个数据点,显示所有列会使图表过于拥挤。为了过滤数据点,我只需抓取每第 n 个项目(从数组的反向开始,因此我确保获得最新的数据点)。

我将 y 轴刻度值定义为包含未过滤数据集的最小值和最大值,因此用户可以看到数据集的实际最小值和最大值。我在过滤数据之前计算最小值和最大值:

var v = new Array();
data.forEach(function (d) {
    d.date = parseDate(d.date);
    d.close = +d.close;
    v.push(d.close); // v holds ALL our values...before we filter them
});
yAxisValues = [Math.min.apply(null, v),Math.max.apply(null, v)];

if(data.length > 100){ // make it so that the chart isn't as crowded

        var len = data.length;
        var n = Math.round(len/100); // ideally, we want no more than 100 items

        var tempData = [];
        data = data.reverse();

        for( var k = 0; k < len; k += n ){

            tempData.push( data[ k ] );
        }
        data = tempData;
        data = data.reverse();
    }

但是,现在我的 y 轴搞砸了,-0.02 显示在 x 轴下方。我做错了什么?我的小提琴。(要查看 y 轴表现正常,只需注释掉我过滤数据的部分)

4

1 回答 1

1

您正在过滤之前创建 Y 轴,但您仍在过滤数据上创建比例:

var y = d3.scale.linear().range([height - 5, 5]);
// here is where it look at the min/max of the filtered data rather than the min/max of the original
y.domain(d3.extent(data, function (d) {
    return d.close;
}));
var yAxis = d3.svg.axis().scale(y).orient('left').tickValues(yAxisValues);

如果在过滤之前移动这部分应该没问题。

于 2013-03-26T21:02:51.440 回答