Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
111 lines (99 sloc) 3.85 KB
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>Auto-aligned axis labels for dc.js Row Chart - Into the Void</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter2/1.3.14/crossfilter.js" type='text/javascript'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dc/2.1.1/dc.min.js" type='text/javascript'></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/dc/2.1.1/dc.min.css" rel='stylesheet' type='text/css'>
</head>
<body>
<div id="rowChartContainer">
</div>
<style type="text/css">
/* Change Font size and color for axis labels */
text.x-axis-label, .y-axis-label {
fill: #777;
font-size: 0.9em;
}
/* Dark font color on bar chart and pie chart */
.dc-chart g.row text {
fill: #2c3e50;
font-size: 0.85em;
pointer-events: inherit;
}
</style>
<script>
// Simple data for demo
var data = [
{fruit: "orange", size: 4},
{fruit: "orange", size: 3},
{fruit: "orange", size: 4},
{fruit: "mango", size: 5},
{fruit: "banana", size: 2},
{fruit: "banana", size: 1},
{fruit: "apple", size: 2},
{fruit: "apple", size: 3},
{fruit: "apple", size: 3}
];
// Setup Crossfilter and dc.js rowChart object
var cf = crossfilter(data);
var fruitDim = cf.dimension(function(d) { return d.fruit; });
var fruitGroup = fruitDim.group(); // Default is to reduce by counts
var rowChartObj = dc.rowChart("#rowChartContainer");
rowChartObj
.width(350)
.height(250)
.dimension(fruitDim)
.group(fruitGroup)
.label(function(d) { return d.key; })
.margins({top: 10, right: 50, bottom: 40, left: 30}) // Add padding to bottom for axis label
.colors(d3.scale.category10())
.labelOffsetX(4)
.elasticX(true)
.xAxis().ticks(5);
// Functions to add x-label & y-label to Row Charts (Unsupported by dc.js)
var addXLabel = function(chartToUpdate, displayText) {
var textSelection = chartToUpdate.svg()
.append("text")
.attr("class", "x-axis-label")
.attr("text-anchor", "middle")
.attr("x", chartToUpdate.width() / 2)
.attr("y", chartToUpdate.height() - 2)
.text(displayText);
var textDims = textSelection.node().getBBox();
var chartMargins = chartToUpdate.margins();
// Dynamically adjust positioning after reading text dimension from DOM
textSelection
.attr("x", chartMargins.left + (chartToUpdate.width()
- chartMargins.left - chartMargins.right) / 2)
.attr("y", chartToUpdate.height() - Math.ceil(textDims.height) / 2);
};
var addYLabel = function(chartToUpdate, displayText) {
var textSelection = chartToUpdate.svg()
.append("text")
.attr("class", "y-axis-label")
.attr("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.attr("x", -chartToUpdate.height() / 2)
.attr("y", 10)
.text(displayText);
var textDims = textSelection.node().getBBox();
var chartMargins = chartToUpdate.margins();
// Dynamically adjust positioning after reading text dimension from DOM
textSelection
.attr("x", -chartMargins.top - (chartToUpdate.height()
- chartMargins.top - chartMargins.bottom) / 2)
.attr("y", Math.max(Math.ceil(textDims.height), chartMargins.left
- Math.ceil(textDims.height) - 5));
};
// Bind addXLabel & addYlabel as callbacks to postRender
rowChartObj.on("postRender", function(chart) {
addXLabel(chart, "Counts");
addYLabel(chart, "Type of Fruit");
});
dc.renderAll();
</script>
</body>
</html>
You can’t perform that action at this time.