在D3.js中创建带有序数比例的带文本标签的x轴

时间:2013-06-21 01:55:25

标签: javascript d3.js data-visualization

我在d3.js中构建一个带有序数x轴的条形图,其刻度应该用文本标记图表。任何人都可以解释序数规模"映射" x勾选到相应的栏位置?具体来说,如果我想用条形图中相应条形指定带有文本值数组的x刻度标签。

目前,我将域名设置如下:

    var labels = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"];
    var xScale = d3.scale.ordinal()
                .domain(labels)

但是,文本标签后显示的值为1-19。

正如此小提琴所见:

http://jsfiddle.net/chartguy/FbqjD/

Associated Fiddle源代码:

//Width and height
            var margin = {top: 20, right: 20, bottom: 30, left: 40};           
            var width = 600 - margin.left - margin.right;
            var height= 500-margin.top -margin.bottom;
            var w = width;
            var h = height;


            var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
                            11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];

            var labels = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"];
            var xScale = d3.scale.ordinal()
            .domain(labels)
                            .rangeRoundBands([margin.left, width], 0.05);
            var xAxis = d3.svg.axis().scale(xScale).orient("bottom");


            var yScale = d3.scale.linear()
                            .domain([0, d3.max(dataset)])
                            .range([h,0]);


            //Create SVG element
            var svg = d3.select("body")
                        .append("svg")
                        .attr("width", w)
                        .attr("height", h);

            //Create bars
            svg.selectAll("rect")
               .data(dataset)
               .enter()
               .append("rect")
               .attr("x", function(d, i) {
                    return xScale(i);
               })
               .attr("y", function(d) {
                    return yScale(d);
               })
               .attr("width", xScale.rangeBand())
               .attr("height", function(d) {
                    return h - yScale(d);
               })
               .attr("fill", function(d) {
                    return "rgb(0, 0, 0)";
               });

            svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + 0 + ")")
      .call(xAxis);

            //Create labels
            svg.selectAll("text")
               .data(dataset)
               .enter()
               .append("text")
               .text(function(d) {
                    return d;
               })
               .attr("x", function(d, i) {
                    return xScale(i) + xScale.rangeBand() / 2;
               })
               .attr("y", function(d) {
                    return h - yScale(d) + 14;
               });

1 个答案:

答案 0 :(得分:15)

您可以使用d3.svg.axis().tickValues(*array*)明确设置序号轴的刻度值。

但这是一种奇怪的方法,因为它会危险地分离您的键和值,这意味着您必须小心手动对齐刻度并确保您的数据正确对应。它有助于将键和值分组到单个对象中,然后使用以下格式:

       axis.domain(array.map(function (d) { return d.value; }))

映射您的轴域。

我已经重新设计了你的数据,并在我认为更多的d3 way中进行了操作。 (另请注意,我为了好玩而进行了一些其他更改,即改善了边距并清理了轴对齐等)。