How to get striped grey background resembling 100% to the barchart?

吃可爱长大的小学妹 提交于 2019-12-25 02:24:33

问题


This is a follow-up question to this question.
How do I get the striped grey scale in the background which represents 100% and is parallel to Y-axis?


jsFiddle

Code:

var data = [
    {
        "age": "Under 18",
        "value": "582.13",
        "color": "#B3DA29"
    },
    {
        "age": "18 - 22",
        "value": "583.98",
        "color": "#78C0F2"
    },
    {
        "age": "23 - 34",
        "value": "603.00",
        "color": "#9B58B5"
    },
    {
        "age": "35 - 60",
        "value": "607.70",
        "color": "#E54C3C"
    },
    {
        "age": "60 - Above",
        "value": "610.00",
        "color": "#F2519D"
    }
];

var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 400 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10, "%");

var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");


x.domain(data.map(function(d) { return d.age; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);

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

var tooltip = d3.select("body").append("div")   
.attr("class", "tooltip")               
.style("opacity", 0);

svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.age); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return d.color; })
.attr("rx", 10)
.attr("ry", 10)
.on("mouseover", function(d) {      
    tooltip.transition().duration(200).style("opacity", .9);      
    tooltip.html("Count: " + d.value)  
    .style("left", (d3.event.pageX) + "px")     
    .style("top", (d3.event.pageY - 28) + "px");    
})                  
.on("mouseout", function(d) {       
    tooltip.transition().duration(500).style("opacity", 0);   
});

回答1:


Here's how it works.

Updated the fiddle.

svg.selectAll(".backgound-bar")
.data(data)
.enter()
.append("rect")
.attr("class", "background-bar")
.attr("x", function(d) { return x(d.age); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(800); })
.attr("height", height)
.style("fill", "#ddd")
.attr("rx", 10)
.attr("ry", 10)

Created a svg element with the class background-bar.

And the x and y remains the same as you've defined prior.

But the height of the bars are given to the height variable defined.

Hope this helps :)



来源:https://stackoverflow.com/questions/21823226/how-to-get-striped-grey-background-resembling-100-to-the-barchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!