There is this example that shows how you can make an elastic X axis that removes the empty bins using a fake group and the chart.elasticX(true) method.
I'm trying to make this work with a stacked barchart but I face a problem. I have slightly modified the code of the above example to use a stacked group for the barchart. (I have added an Earned column in the data, made a fake group for it and assigned it to the chart.stack method). But for certain "Earned" values there is a d3.js error:
Uncaught TypeError: Cannot read property '1' of undefined.
Update: This issue is related with this answer which states that "The stack method expects your data to be equally length-ed". It is also related with this answer that proposes the creation of a combined group to overcome the problem.
Here is the modified example code
<!DOCTYPE html>
<html lang="en">
<head>
<title>dc.js - Filtering Example</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dc/2.0.0/dc.min.css"/>
</head>
<body>
<div class="container">
<script type="text/javascript" src="header.js"></script>
<p>Example demonstrating using a "<a href="https://github.com/dc-js/dc.js/wiki/FAQ#fake-groups">Fake Group</a>" to remove
the empty bars of an ordinal bar chart when their values drop to zero.</p>
<p>(Note the use of <code><a href="https://github.com/dc-js/dc.js/blob/develop/web/docs/api-latest.md#dc.coordinateGridMixin+elasticX">.elasticX(true)</a></code>
to force calculation of the X domain each round.)</p>
<div id="chart-ring-year"></div>
<div id="chart-hist-spend"></div>
<div id="chart-row-spenders"></div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.12/d3.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dc/2.0.0/dc.min.js"></script>
<script type="text/javascript">
var yearRingChart = dc.pieChart("#chart-ring-year"),
spendHistChart = dc.barChart("#chart-hist-spend"),
spenderRowChart = dc.rowChart("#chart-row-spenders");
// use static or load via d3.csv("spendData.csv", function(error, spendData) {/* do stuff */});
var spendData = [
{Name: 'Mr A', Spent: '$40', Earned: '$70', Year: 2011},
{Name: 'Mr B', Spent: '$10', Earned: '$20', Year: 2011},
{Name: 'Mr C', Spent: '$40', Earned: '$40', Year: 2011},
{Name: 'Mr A', Spent: '$70', Earned: '$170', Year: 2012},
{Name: 'Mr B', Spent: '$20', Earned: '$30', Year: 2012},
{Name: 'Mr B', Spent: '$50', Earned: '$30', Year: 2013},
{Name: 'Mr C', Spent: '$30', Earned: '$70', Year: 2013}
];
// normalize/parse data
spendData.forEach(function(d) {
d.Spent = d.Spent.match(/\d+/);
d.Earned = d.Earned.match(/\d+/);
});
function remove_empty_bins(source_group) {
return {
all:function () {
return source_group.all().filter(function(d) {
return d.value != 0;
});
}
};
}
// set crossfilter
var ndx = crossfilter(spendData),
yearDim = ndx.dimension(function(d) {return +d.Year;}),
spendDim = ndx.dimension(function(d) {return Math.floor(d.Spent/10);}),
earnDim = ndx.dimension(function(d) {return Math.floor(d.Earned/10);}),
nameDim = ndx.dimension(function(d) {return d.Name;}),
spendPerYear = yearDim.group().reduceSum(function(d) {return +d.Spent;}),
spendPerName = nameDim.group().reduceSum(function(d) {return +d.Spent;}),
spendHist = spendDim.group().reduceCount(),
earnHist = earnDim.group().reduceCount(),
nonEmptyHist = remove_empty_bins(spendHist)
nonEmptyEarnHist = remove_empty_bins(earnHist)
yearRingChart
.width(200).height(200)
.dimension(yearDim)
.group(spendPerYear)
.innerRadius(50);
spendHistChart
.width(300).height(200)
.dimension(spendDim)
.group(nonEmptyHist)
.stack(nonEmptyEarnHist)
.x(d3.scale.ordinal())
.xUnits(dc.units.ordinal)
.elasticX(true)
.elasticY(true);
spendHistChart.xAxis().tickFormat(function(d) {return d*10}); // convert back to base unit
spendHistChart.yAxis().ticks(2);
spenderRowChart
.width(350).height(200)
.dimension(nameDim)
.group(spendPerName)
.elasticX(true);
dc.renderAll();
</script>
</div>
</body>
</html>
However if you change the Earned values to
var spendData = [
{Name: 'Mr A', Spent: '$40', Earned: '$70', Year: 2011},
{Name: 'Mr B', Spent: '$10', Earned: '$20', Year: 2011},
{Name: 'Mr C', Spent: '$40', Earned: '$40', Year: 2011},
{Name: 'Mr A', Spent: '$70', Earned: '$170', Year: 2012},
{Name: 'Mr B', Spent: '$20', Earned: '$30', Year: 2012},
{Name: 'Mr B', Spent: '$50', Earned: '$50', Year: 2013}, // This is the only change Earned from '$30' to '$50'
{Name: 'Mr C', Spent: '$30', Earned: '$70', Year: 2013}
]
Then it works fine.
I face the same d3 error for my case so I tried to reproduce it with a simple example.
There is also this related question
Welp, you found the answer already; it's just a matter of applying it.
This is indeed the same question that can be solved with a combined group.
combinedGroup = combine_groups(nonEmptyHist,nonEmptyEarnHist)
function sel_stack(i) {
return function(d) {
return d.value[i];
};
}
spendHistChart
.group(combinedGroup, 'spend', sel_stack(0))
.stack(combinedGroup, 'earn', sel_stack(1))
I don't know of another way to deal with this; d3.stack
requires arrays of the same size and dc.js will also assume that the key/value arrays correspond to each other.