I have a data array like this:
var data = [{"Alphabet_Type":"a"}, {"Alphabet_Type":"b"}, {"Alphabet_Type":"a"}];
I am using:
I want to create a bar chart with:
Question: How can I plot a bar chart with an ordinal scale on the x-axis?
My code:
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);
Finally, I have another problem which is that Alphabet_Type
is not going to have predictable values. So I need to fetch the values for Alphabet_Type
via crossfilter.js and incorporate those values into the domain. How can I do this?
The d3. scaleOrdinal() function is used to create and return the ordinal scale which has the specified range and domain. If the domain and range are not given then both are set to empty array. These types of scales have a discrete domain and range.
D3 creates a function myScale which accepts input between 0 and 10 (the domain) and maps it to output between 0 and 600 (the range).
scaleLinear() method is used to create a visual scale point. This method is used to transform data values into visual variables.
Scales are functions that map from an input domain to an output range. Ordinal scales have a discrete domain, such as a set of names or categories.
Two things:
dc.units.ordinal
as the parameter to .xUnits()
..x()
.Here's what I mean:
.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
See this JSFiddle: http://jsfiddle.net/reblace/tbamW/156/
For some reason, I couldn't get renderHorizontalGridLines()
or renderVerticalGridLines()
to work, but the rest is good.
Adding
.x(d3.scale.ordinal().domain(data.map(function (d) {return d.Alphabet_Type; })))
solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With