score:2
This answer expands on @Gerardo's solution, making the chart adjust dynamically, and dealing with a few dc.js quirks.
First, the original author of this example fudged a few things just to get stuff working. If you want truly dynamic axes, you'll need to show dc.js how to calculate the bar widths for itself:
magnitudeChart
.xUnits(dc.units.fp.precision(0.1))
and remove .gap(65)
- that kind of magic number is always a sign that someone couldn't figure out how to get things calculated automatically.
There are a few quirks with how dc.js deals with bar charts. For one, it doesn't really account for its own bar width or centering when it sets the domain - it uses exactly what you give it. You'll notice that the rightmost bar is missing when automatically calculating the maximum. (This happens with elasticX(true)
too.)
So let's turn off bar-centering and add the 0.1 bar width to the domain each time we calculate it.
magnitudeChart
.centerBar(false)
.x(d3.scale.linear().domain([2, d3.max(data, d=>d.mag)+0.1]))
Next, crossfilter does not automatically remove empty bins, so we can wrap the group in a fake group to do that:
function remove_empty_bins(source_group) {
return {
all:function () {
return source_group.all().filter(function(d) {
return d.value != 0;
});
}
};
}
magnitudeChart
.group(remove_empty_bins(magValueGroupCount))
And we can have the chart recalculate its domain every time it's redrawn like so:
.on('preRedraw', function(chart) {
chart.x().domain([2, d3.max(chart.group().all(), kv=>kv.key) + 0.1]);
chart.rescale();
});
Fork of the block: http://bl.ocks.org/gordonwoodhull/7675bb4bb67354cfdc7c6841106d7b31
score:3
Change this line:
.x(d3.scale.linear().domain([0.5, 7.5]))
To:
.x(d3.scale.linear().domain([2, d3.max(data, d=>d.mag)]))
Source: stackoverflow.com
Related Query
- DC.js xAxis range, show only a subset
- d3.js: show only part of data on xAxis
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- Show d3 node text only on hover
- Display only whole values along y axis in d3.js when range is 0-1 or 0-2
- Format Y axis values, original figures in millions, only want to show first three digits
- Month Path only calculating month, not actual date range
- d3.js nvd3 date on x axis: only some dates are show
- D3 ordinal scale only returning extremes. Why isn't it interpolating between range and domain?
- How to have d3 tickFormat of (day, time) but only show time if day is the same?
- LeafletJs only show one country
- Rickshaw only show gridlines on some points
- d3 show labels only for ticks with data in a bar chart
- D3 - Show only middle and last tick marks on axis
- d3js v4: Only show labels on x-axis
- Show text only after transition is complete d3.js
- d3 - Show only values using d3.nest()
- How to show only specific node level in d3 tree layout
- Modify D3.js Circle Progress Graph to show range of percentages
- NVD3 LineChart: Some xAxis dates do not show at the beginning and end of the axis, despite I am using the tickValues() method
- Use only a subset of CSV columns
- In d3.js, while importing csv files using a row conversion, how can I "slice" the data to only include a range of rows?
- nvd3 - force all xaxis labels to show on line chart
- Show numbers as times in xAxis of linechart in dc.js
- Only show first 25 data items in bar chart with d3.js
- Selecting only a subset in d3.select
- Only select subset in d3
- Show only max values in d3 bar chart
- How to show labels in first two rings only in a zoomable sunburst partition with rotated labels?
- How to show only limited number of records in box plot dc.js
More Query from same tag
- D3js highlight bar one by one continuously
- d3 resummarise nested object
- D3: Multiple Values on Y-Axis
- sync d3.js map and leaflet map
- JSON to Javascript hierarchy (array?)
- suggestion about how to adapt a function that wrap text in d3
- Processing nested json data into data joins
- svg element does not show up
- How to change opacity on second click?
- draw horizontal lines for bars in nvd3 multi bar chart
- Beginner in d3, making US county map
- D3js-Topojson : how to move from pixelized to Bézier curves?
- Issues showing tooltips in a dynamic d3 stack bar chart
- How to use coffeescript or javascript to read value of json file / parse to be use in d3 java script
- Add timestamp format to line graph x-axes
- How to achieve this transition effect in d3?
- d3.js stack layout upgrading from v3 to v4
- How to perform a centering of a node in D3.js version 4?
- Time axis in d3.js with specific time zone
- How to create a tooltip in d3.js while mouseover?
- Example CSS for D3 script with R package r2d3?
- How to implement a smooth transition when new bands are created in d3?
- Expanding canvas to full browser window
- D3 tree with boxes
- How to create floor plans that work nice with mobile and desktop browsers?
- Zero-index problem and filtering issue in shiny
- D3 / design - bar graph,seeking snazzy way to indicate "N/A"
- How to make data() key function to work with TypeScript?
- Why is the d3.js group element hopping at first drag?
- Drawing voronoi diagram from a csv file with d3.js