在条形图中使用序数比例('d3.scale.ordinal')作为x轴

Aru*_*run 18 javascript bar-chart d3.js crossfilter dc.js

我有一个这样的数据数组:

var data = [{"Alphabet_Type":"a"}, {"Alphabet_Type":"b"}, {"Alphabet_Type":"a"}];
Run Code Online (Sandbox Code Playgroud)

我在用:

  • dc.js
  • crossfilter.js
  • d3.js

我想创建一个条形图:

  • x轴具有字母名称,和
  • y轴具有字母表的出现次数.

问题:如何在x轴上绘制带有有序刻度的条形图?

我的代码:

var data = [{"Alphabet_Type":"a"}, {"Alphabet_Type":"b"}, {"Alphabet_Type":"a"}];

var Filter = crossfilter(data);
var Dimension = Filter.dimension(function (d) { return d.Alphabet_Type; });
var Group = Dimension.group();

dc.barChart("#bar-chart")
    .width(800) 
    .height(275) 
    .transitionDuration(0) 
    .margins({ top: 10, right: 50, bottom: 30, left: 40 })
    .dimension(Dimension) 
    .group(Group) 
    .elasticY(false)
    .elasticX(false)
    .x(d3.scale.linear().domain([-1, 10]))
    .y(d3.scale.linear().domain([0, 5]))
    .centerBar(true)
    .renderHorizontalGridLines(true)
    .renderVerticalGridLines(true)
    .brushOn(false);
Run Code Online (Sandbox Code Playgroud)

最后,我有另一个问题,那Alphabet_Type就是没有可预测的值.所以我需要获取Alphabet_Typevia crossfilter.js的值并将这些值合并到域中.我怎样才能做到这一点?

reb*_*ace 34

两件事情:

  1. dc.units.ordinal作为参数传递给.xUnits().
  2. 序数比例函数传递给.x().

这就是我的意思:

.x(d3.scale.ordinal().domain(["", "a", "b", "c"])) // Need empty val to offset first value
.xUnits(dc.units.ordinal) // Tell dc.js that we're using an ordinal x-axis
Run Code Online (Sandbox Code Playgroud)

看到这个JSFiddle:http://jsfiddle.net/reblace/tbamW/156/

出于某种原因,我无法上班renderHorizontalGridLines()renderVerticalGridLines()上班,但其余的都很好.


Aru*_*run 7

添加

.x(d3.scale.ordinal().domain(data.map(function (d) {return d.Alphabet_Type; })))
Run Code Online (Sandbox Code Playgroud)

解决了这个问题.