score:5
I don't think you can zoom/pan on an ordinal axis.
One work around is that you make a linear scale and make it look like an ordinal scale.
Define your ordinals like this:
var ordinals = ["a", "b", "c"];
Instead of ordinal scale make a linear scale:
var yscale = d3.scale.linear()
.domain([0, ordinals.length])
.range([height - margin.top - margin.bottom, 0]);
Next when you make the yscale make use of the tick format to return the label of your choice.
var yAxis = d3.svg.axis()
.scale(yscale)
.orient('left')
.tickFormat(function(d) {
return ordinals[d];//now for 0 it will return 'a' for 1 b and so on...
})
.tickPadding(8);
Make your zoom to use the new y scale like this:
var zoom = d3.behavior.zoom()
.x(x)
.y(yscale)
.scaleExtent([1, 20])
.on("zoom", zoomed);
Hide all ticks without label
var hideTicksWithoutLabel = function() {
//hide ticks without label
svg.selectAll(".y .tick")[0].forEach(function(g) {
if (d3.select(g).select("text").text() == "") {
d3.select(g).style("display", "none");
} else {
d3.select(g).style("display", "");
}
})
}
Now call this function after zoom.
working code here
Source: stackoverflow.com
Related Query
- D3.js zoom with Ordinal axis not working - Issue with setup of scale in zoom behavior?
- D3: ordinal scale not working with array of objects
- dc.js bar chart with ordinal x axis scale doesn't render correctly
- d3 world map with country click and zoom almost working not quite
- d3.js scale time on x axis not working
- D3.js zoom is not working with mousewheel in safari
- limit number of tick marks in d3.js x axis with ordinal scale
- In D3 bar graph, y-axis ordinal scale is not aligning properly with bars
- X axis label not displayed in bar chart with scroll and zoom feature - d3js, Brushing
- d3 stacked area graph not working with log scale
- D3.JS- Format tick values with an ordinal scale axis
- D3 Line chart with ordinal scale issue
- Google maps with d3.js (v5) color scale not working
- D3js axis clipping not working with g element
- Inversion with ordinal scale
- d3 time scale x axis with unix timestamp
- Creating a Text Labeled x-Axis with an Ordinal Scale in D3.js
- Angular 4 + D3v4 : dragging circle not working correctly in conjunction with zooming
- d3 zoom not working as in example
- d3.select is not working with special Characters
- nvd3 scatter plot with ordinal scale
- Horizontal pan with ordinal x axis in d3
- D3 Zoom functionality is not working in Google chrome
- D3: .transition() not working with events
- D3 Zoom with Scrollbars used as panning, Scrollbar width and height adjusts to zoom scale
- With D3, how can I set attribute ("fill", "none") of axis path and line, but not text (without editing stylesheet)
- Convex hull not working with zoom/pan
- Remove event listener with d3js not working
- D3 graph with area fill not working
- javascript d3 line graph with ordinal x axis
More Query from same tag
- D3.js tooltip position combined with css overflow
- html d3.js, image renders in firefox but not in chrome
- dagreD3 - Are Renderer and DiGraph objects deprecated? How to do Transitions and Layouts?
- Add two different svg element in g element of svg using d3.js from a dataset
- Creating d3 bar chart
- Original Data Display on chart after filtering (DC.js)
- How to set intersection point of x and y axis
- Wrapping D3 functions inside objects doesn't work. Why?
- How to visualize simple network in R? With or without D3
- Disconnect d3 line chart?
- D3.js Pie Chart transitions
- Creating a d3 v4 histogram with an ordinal scale
- How to re-sort a barchart into it's initial state after sorting in d3js
- Find centroid of topoJSON path and use it to position a circle in D3
- D3: Passing object array data to a barchart on mouseover
- D3 Force Layout Graph - Self linking node
- d3v4 scatterplot - fixing the domain adding animations
- Sort data for d3.js path
- D3 Binding Text and SVG
- d3.js selectAll method does not select all elements?
- DC Bar Chart x-Axis Sort Alphabetically
- D3JS stacked bar-chart over a timescale
- D3: event functions with concatenated transitions
- How to calculate the x and y coordinates of individual points within a radial line?
- d3.js NVD3 trigger tooltip manually
- How to add HTML code to D3 tree chart at node?
- Add a Toggle function to d3 tree chart v4
- Find the points lying between Start and End Angle of an ARC.
- Voronoi cells are off-scale in a force-directed layout
- How to determine mouse's position is close enough to a path?