admin管理员组文章数量:1333176
Instead of numerical values on the x-axis, i want to set my attribute names. I am no Javascript hero. I am using the scatter Chart.
I believe it should be something like :
chart.xAxis.tickFormat(d3.format(String));
and then i can set:
chart.xAxis
.axisLabel('Attributes').staggerLabels(true).tickValues(["A", "B", "C"]);;
and then set the values:
data[i].values.push({
x : "A" ,
y : 29.765957771107,
size: Math.random(),
shape: shapes[j % 6]
} , ..
How to see my attributes "A", "B", etc on the x-axis ? I browsed the nvd3 source code also but not finding the right API call.
Instead of numerical values on the x-axis, i want to set my attribute names. I am no Javascript hero. I am using the scatter Chart.
I believe it should be something like :
chart.xAxis.tickFormat(d3.format(String));
and then i can set:
chart.xAxis
.axisLabel('Attributes').staggerLabels(true).tickValues(["A", "B", "C"]);;
and then set the values:
data[i].values.push({
x : "A" ,
y : 29.765957771107,
size: Math.random(),
shape: shapes[j % 6]
} , ..
How to see my attributes "A", "B", etc on the x-axis ? I browsed the nvd3 source code also but not finding the right API call.
Share Improve this question asked Sep 6, 2013 at 1:40 matthieu liebermatthieu lieber 6621 gold badge17 silver badges30 bronze badges3 Answers
Reset to default 3You can pass in a format function that returns label. This is an API of d3js.
var x_format = function(num) {
if (num === 2.3)
return "A";
else if (num === 5.6)
return "B";
else if (num === 45.2)
return "C";
};
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(x_format);
Check out this example that I make for scatterplot.
http://vida.io/documents/jSGz9TQEmzwMqQzhs
{(A, 2.3), (B, 5.6), (C, 45.2) }
Update: my answer with fixed value.
Update: hybrid bination between line, bar, scatterplot:
http://vida.io/documents/fN5FjsYaHaJSXEjz7
I have the same problem. My simple solution consists in avoiding the code declaring the x axis. In the nvd3 web also there is this solution: see http://nvd3/livecode/index.html#codemirrorNav for an example.
Rotating a bar chart into a column chart largely involves swapping x with y. However, a number of smaller incidental changes are also required. This is the cost of working directly with SVG rather than using a high-level visualization grammar like ggplot2 On the other hand, SVG offers greater customizability; and SVG is a web standard, so we can use the browser’s developer tools like the element inspector, and use SVG for things beyond visualization.
When renaming the x scale to the y scale, the range bees [height, 0] rather than [0, width]. This is because the origin of SVG’s coordinate system is in the top-left corner. We want the zero-value to be positioned at the bottom of the chart, rather than the top. Likewise this means that we need to position the bar rects by setting the "y" and "height" attributes, whereas before we only needed to set "width". (The default value of the "x" attribute is zero, and the old bars were left-aligned.)
var x = d3.scale.ordinal()
.domain(["A", "B", "C", "D", "E", "F"])
.range([0, 1, 2, 3, 4, 5]);
It would be tedious to enumerate the positions of each bar by hand, so instead we can convert a continuous range into a discrete set of values using rangeBands or rangePoints. The rangeBands method putes range values so as to divide the chart area into evenly-spaced, evenly-sized bands, as in a bar chart. The similar rangePoints method putes evenly-spaced range values as in a scatterplot.
For example:
var x = d3.scale.ordinal()
.domain(["A", "B", "C", "D", "E", "F"])
.rangeBands([0, width]);
For more information Click http://bost.ocks/mike/bar/3/
本文标签: javascriptHow to set a nvd3 axis to use strings instead of numerical values Stack Overflow
版权声明:本文标题:javascript - How to set a nvd3 axis to use strings instead of numerical values ? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742276386a2445276.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论