计算 dc.js 中条形图中的条数
Calculating the number of bars in a bar chart in dc.js
我想根据条形图上出现的条数调整条形图的宽度。这是我的图表,它显示了足够多的记录,这些记录适合宽度并且显示得很好。
这是另一种情况,其中只有两个条以相同的宽度显示,这看起来很奇怪。
对于这种情况,我需要根据柱的数量调整图表的宽度,这样我的图表才会好看。
这是我的脚本部分
var width = ??; // Here how should I calculate width based on the number of bars?
redrawCustomerCodeBarChart
.width(width)
.height(400)
.margins({top: 10, right: 10, bottom: 70, left: 30})
.dimension(customerNamer)
.group(customerNameGroupr)
.x(d3.scale.ordinal().domain(newCoh.map(function (d) {return d.customerName; })))
.xUnits(dc.units.ordinal)
.elasticY(true)
.renderHorizontalGridLines(true)
.on('renderlet',function(chart){
chart.selectAll("g.x text")
.attr('dx', '-15')
.attr('transform', "rotate(-55)");
})
.on('filtered.monitor', function(d) {
// return d.key + ' (' + d.value + ')';
if(d.value !== undefined)
size= d.value;
})
.controlsUseVisibility(true);
在大多数 dc.js 图表中,数据是从 group.all()
读取的。所以柱数就是
customerNameGroupr.all().length
然后适当相乘
如果条形的数量可能会随着过滤内容而改变(例如使用 remove_empty_bins
),那么您需要在每次重绘之前设置宽度:
chart.on('preRedraw', function(chart) {
chart.width(chart.group().all().length * BAR_WIDTH);
});
不过那可能有点乱。
我想根据条形图上出现的条数调整条形图的宽度。这是我的图表,它显示了足够多的记录,这些记录适合宽度并且显示得很好。
这是另一种情况,其中只有两个条以相同的宽度显示,这看起来很奇怪。
对于这种情况,我需要根据柱的数量调整图表的宽度,这样我的图表才会好看。
这是我的脚本部分
var width = ??; // Here how should I calculate width based on the number of bars?
redrawCustomerCodeBarChart
.width(width)
.height(400)
.margins({top: 10, right: 10, bottom: 70, left: 30})
.dimension(customerNamer)
.group(customerNameGroupr)
.x(d3.scale.ordinal().domain(newCoh.map(function (d) {return d.customerName; })))
.xUnits(dc.units.ordinal)
.elasticY(true)
.renderHorizontalGridLines(true)
.on('renderlet',function(chart){
chart.selectAll("g.x text")
.attr('dx', '-15')
.attr('transform', "rotate(-55)");
})
.on('filtered.monitor', function(d) {
// return d.key + ' (' + d.value + ')';
if(d.value !== undefined)
size= d.value;
})
.controlsUseVisibility(true);
在大多数 dc.js 图表中,数据是从 group.all()
读取的。所以柱数就是
customerNameGroupr.all().length
然后适当相乘
如果条形的数量可能会随着过滤内容而改变(例如使用 remove_empty_bins
),那么您需要在每次重绘之前设置宽度:
chart.on('preRedraw', function(chart) {
chart.width(chart.group().all().length * BAR_WIDTH);
});
不过那可能有点乱。