我正在制作一个简单的柱状图,以获得一年的收入总额。下面是使用dc.js构建条形图的代码片段
//INITIALIZE CROSSFILTER AND RELATED DIMENSIONS/AGGREGATES
var data = crossfilter([
{revenue: 1487946,year: 2016},
{revenue: 2612,year: 2016},
{revenue: 2908,year: 2015},
{revenue: 1648171,year: 2015 }]);
var dimension = data.dimension(function(d) {return d.year;});
var group = dimension.group().reduceSum(function(d) {return d.revenue;});
// MAKE A DC BAR CHART
dc.barChart("#bar-chart")
.dimension(dimension)
.group(group)
.x(d3.scale.ordinal().domain(dimension)) // Need the empty val to offset the first value
.xUnits(dc.units.ordinal) // Tell Dc.js that we're using an ordinal x axis
.brushOn(false)
.centerBar(false)
.gap(70);
console.log(dc.version);
dc.renderAll();必须注意的是,为了简单起见,年份被呈现为序数。
在dc.js版本为1.7.5的情况下,输出不清楚:

。
对于dc.js版本-2.x(测试版),输出要好得多:

由于其他冲突,目前不能使用dc.js 2.x。对于使用dc.js 1.7.5改进条形图有什么想法吗?
这就是可以使用的JS Fiddle!提前谢谢。
发布于 2016-02-12 17:17:46
在下面的脚本中添加了注释:
dc.barChart("#bar-chart")
.width(300) //give it a width
.margins({top: 10, right: 10, bottom: 20, left: 100})//give it margin left of 100 so that the y axis ticks dont cut off
.dimension(dimension)
.group(group)
.x(d3.scale.ordinal().domain(dimension)) // Need the empty val to offset the first value
.xUnits(dc.units.ordinal) // Tell Dc.js that we're using an ordinal x axis
.brushOn(false)
.centerBar(false)
.gap(7);//give it a gap of 7 instead of 70工作代码here
发布于 2016-02-13 17:00:47
临时答案:避免序数轴
这可能是使用dc.js 1.7.5时的一个小故障。到目前为止,我能够通过将年视为线性刻度并适当地处理刻度来呈现图形。
让我们打开这个问题,以防有人找到更好的答案!
https://stackoverflow.com/questions/35358320
复制相似问题