带有序数轴的dc.js条形图

时间:2016-02-12 08:51:48

标签: javascript d3.js dc.js

我正在制作一个简单的条形图,获得一年的收入总和。下面是使用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,输出不明确:

Untidy bar chart using dc.js 1.7.5

使用dc.js版本 - 2.x(beta),输出效果要好得多:

enter image description here

由于其他冲突,目前无法使用dc.js 2.x.有关使用dc.js 1.7.5更好地制作条形图的想法吗?

这是JS Fiddle玩的!提前谢谢。

2 个答案:

答案 0 :(得分:0)

在下面的文章中添加了评论:

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

答案 1 :(得分:0)

临时答案:避免顺序轴

使用dc.js 1.7.5可能会出现故障。至于现在,我能够通过将年度视为线性比例并适当地处理蜱来呈现图形。

让我们打开这个问题,以防有人找到更好的答案!