将水平条形图旋转到垂直图表

时间:2014-08-09 23:30:00

标签: d3.js

Saw this graph on JSFiddle。我用自己的数据修改了它,但是我想要一个垂直条形图而不是水平条形图。 Here's what it looks like now。我试着摆弄X轴和Y轴,但似乎无法做到正确。任何帮助将不胜感激。

这是我的代码:

var margins = {
    top: 12,
    left: 48,
    right: 24,
    bottom: 40
},

legendPanel = {
    width: 180
},
width = 400 - margins.left - margins.right - legendPanel.width,
    height = 200 - margins.top - margins.bottom,
    dataset = [{
        data: [{
            year: '2009',
            count: 80
        }, {
            year: '2010',
            count: 79
        }, {
            year: '2011',
            count: 65
        }, 
                {
            year: '2012',
            count: 70
        },
                    {
            year: '2013',
            count: 72
        } ,
                    {
            year: '2014*',
            count: 38
        } 
              ],
        name: 'Male'
    }, {
        data: [{
            year: '2009',
            count: 15
        }, {
            year: '2010',
            count: 17
        }, {
            year: '2011',
            count: 18
        }, {
            year: '2012',
            count: 20
        }, {
            year: '2013',
            count: 17
        },

              {
            year: '2014*',
            count: 8
        }],
        name: 'Female'
    }

    ],
    series = dataset.map(function (d) {
        return d.name;
    }),
    dataset = dataset.map(function (d) {
        return d.data.map(function (o, i) {
            // Structure it so that your numeric
            // axis (the stacked amount) is y
            return {
                y: o.count,
                x: o.year
            };
        });
    }),
    stack = d3.layout.stack();

stack(dataset);

var dataset = dataset.map(function (group) {
    return group.map(function (d) {
        // Invert the x and y values, and y0 becomes x0
        return {
            x: d.y,
            y: d.x,
            x0: d.y0
        };
    });
}),
    svg = d3.select('#sex')
        .append('svg')
        .attr('width', width + margins.left + margins.right + legendPanel.width)
        .attr('height', height + margins.top + margins.bottom)
        .append('g')
        .attr('transform', 'translate(' + margins.left + ',' + margins.top + ')'),
    xMax = d3.max(dataset, function (group) {
        return d3.max(group, function (d) {
            return d.x + d.x0;
        });
    }),
    xScale = d3.scale.linear()
        .domain([0, xMax])
        .range([0, 200]),
    months = dataset[0].map(function (d) {
        return d.y;
    }),
    _ = console.log(months),
    yScale = d3.scale.ordinal()
        .domain(months)
        .rangeRoundBands([0, height], .1),
    xAxis = d3.svg.axis()
        .scale(xScale)
        .orient('bottom'),
    yAxis = d3.svg.axis()
        .scale(yScale)
        .orient('left'),

    colours = d3.scale.category20c(),
    groups = svg.selectAll('g')
        .data(dataset)
        .enter()
        .append('g')
        .style('fill', function (d, i) {
        return colours(i);
    }),

    rects = groups.selectAll('rect')
        .data(function (d) {
        return d;
    })
        .enter()
        .append('rect')
        .attr('x', function (d) {
        return xScale(d.x0);
    })
        .attr('y', function (d, i) {
        return yScale(d.y);
    })
        .attr('height', function (d) {
        return yScale.rangeBand();
    })
        .attr('width', function (d) {
        return xScale(d.x);
    })
        .on('mouseover', function (d) {
        var xPos = parseFloat(d3.select(this).attr('x')) / 2 + width / 2;
        var yPos = parseFloat(d3.select(this).attr('y')) + yScale.rangeBand() / 2;

        d3.select('#tooltip')
            .style('left', xPos + 'px')
            .style('top', yPos + 'px')
            .select('#value')
            .text(d.x);

        d3.select('#tooltip').classed('hidden', false);
    })
        .on('mouseout', function () {
        d3.select('#tooltip').classed('hidden', true);
    })

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

svg.append('g')
    .attr('class', 'axis')
    .call(yAxis);

svg.append('rect')
    .attr('fill', 'white')
    .attr('width', 160)
    .attr('height', 30 * dataset.length)
    .attr('x', width + margins.left)
    .attr('y', 0);

//X AXIS LABEL    
svg.append("text")
    .attr("class", 'x label')
    .attr("y", height + 35)
    .attr("x", width / 2)
    .text("Victims")
    .style("font-size","13px")
    .style("font-weight", "600"); 

/*
//FOOTNOTE
svg.append("text")
    .attr("class", 'x label')
    .attr("y", height + 35)
    .attr("x", width + 65)
    .text("*As of July 2014")
    .style("font-size","11px")
    .style("font-style", "italic"); 
*/


//LEGEND
series.forEach(function (s, i) {
    svg.append('text')
        .attr('fill', 'black')
        .attr('x', width + margins.left + 18)
        .attr('y', i * 24 + 24)
        .text(s);

    svg.append('rect')
        .attr('fill', colours(i))
        .attr('width', 20)
        .attr('height', 20)
        .attr('x', width + margins.left + 75)
        .attr('y', i * 24 + 6);

});

0 个答案:

没有答案