Im relatively new to d3 and nvd3 and wanted to create a simple scatterplot like, but with an ordinal y-axis. Thus, the y axis values โโare categorical strings. This is what I thought was necessary to do:
var xfun = function (d) { return d.Pos } //simple ints , yfun = function (d) { return d.Title } //the ordinal values var chart = nv.models.scatterChart() .showDistX(true) .showDistY(true) .color(d3.scale.category10().range()) .margin({ top: 30, right: 20, bottom: 50, left: 130 }) .tooltips(false) .x(xfun) .y(yfun); // create an ordinal scale with some test values var ys = d3.scale.ordinal() .domain(["this","is","an","ordinal","scale"]) .range(5); // tell nvd3 to use it chart.yAxis.scale(ys); // add to the page nv.addGraph(function () { d3.select(selector).datum(data).transition().duration(500).call(chart); nv.utils.windowResize(chart.update); return chart; });
However, no luck:
Error: Invalid value for <circle> attribute cy="NaN" d3.js:690 Error: Invalid value for <line> attribute y1="NaN" d3.js:690 Error: Invalid value for <line> attribute y2="NaN" d3.js:690 Error: Invalid value for <circle> attribute cy="NaN" d3.js:7532 Uncaught TypeError: Cannot read property '0' of undefined ..
And the y axis just shows the linear axis from -1 to 1. There are several circles in Yerevo, built on y = -1 and y = 1 (extremes).
To manually enter the correct values โโfor cy, I tried adding after the call (diagram):
d3.selectAll("#mychart circle").attr("cy", function(d){ return = ys(yfun(d)); });
But still the same mistake. How to get a normal scale for proper operation? Note. I also need it to update correctly when I use the nvd3 legend to switch between dataseries (which will contain different x / y data).
There is a related question on github, but no solution.
Update : after some debugging, I tried replacing chart.yAxis.scale(ys) with chart.scatter.y(ys) , and this will get rid of errors. I can also refuse the selectAll .
However, the y axis still shows a linear scale from 0.99-1.01, and all points are shown at y = 1. So, the step is closer, but not an ordinal scale.