如何获得条纹灰色背景类似于100%barchart? [英] How to get striped grey background resembling 100% to the barchart?

查看:136
本文介绍了如何获得条纹灰色背景类似于100%barchart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是



代码:

  var data = [
{
age:低于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 height +))
.call(xAxis);

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

svg.selectAll(。bar)
.data(data)
.enter()。append(rect)
.attr ,bar)
.attr(x,function(d){return x(d.age);})
.attr(width,x.rangeBand b $ b .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 ,0);
});


解决方案

这是它的工作原理。



更新了 小提琴

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

创建 svg 元素 background-bar



x y 保持与您之前定义的相同。



height 变量定义。



希望这有助于:)


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);   
});

解决方案

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 :)

这篇关于如何获得条纹灰色背景类似于100%barchart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆