在直流系列图表中设置Y轴范围

时间:2014-04-05 17:18:25

标签: d3.js dc.js

我正在使用dc.js来构建系列图表。我无法根据需要设置Y轴的开始和结束。有人可以建议如何实现从90而不是0开始的Y轴?理想情况下,我想将Y轴启动设置为数据值的最小值,并以数据值的最大值结束。

代码:

d3.csv("data/compareData.txt", function(data) {

  ndx = crossfilter(data);
  runDimension = ndx.dimension(function(d) {return [+d3.time.format.iso.parse(d.timestamp), +d.meterid]; });
  runGroup = runDimension.group().reduceSum(function(d) { return +d.voltagemagnitude*100; });

  testChart
    .width(768)
    .height(480)
    .chart(function(c) { return dc.lineChart(c).interpolate('basis'); })
    .x(d3.time.scale().domain([1366621166000, 1366621179983]))
    .y(d3.scale.linear().domain([90, 100]))
    .brushOn(false)
    .yAxisLabel("Measured Speed km/s")
    .xAxisLabel("Run")
    .elasticY(true)
    .dimension(runDimension)
    .group(runGroup)
    .mouseZoomable(false)
    .seriesAccessor(function(d) {return "PMU: " + d.key[1];})
    .keyAccessor(function(d) {return +d.key[0];})
    .valueAccessor(function(d) {return +d.value;})
    .legend(dc.legend().x(350).y(350).itemHeight(13).gap(5).horizontal(1).legendWidth(140).itemWidth(70));
  testChart.yAxis().tickFormat(function(d) {return d3.format(',d')(d);});
  testChart.margins().left += 40;

  dc.renderAll();

});

2 个答案:

答案 0 :(得分:5)

我想你想要这样的东西:

var runMin = +runDimension.bottom(1)[0].voltagemagnitude*100;
var runMax = +runDimension.top(1)[0].voltagemagnitude*100;

然后您可以为y轴设置域:

.y(d3.scale.linear().domain([runMin, runMax]))

请注意,bottomtop会返回一个数据行,因此您需要从中提取值并按照与reduceSum相同的方式对其进行转换。

答案 1 :(得分:3)

我遇到了类似的问题,只需删除.yAxisPadding即可。不完全是你的问题的答案,但希望用于阅读这篇文章的其他人。

相关问题