使用数据值在D3 JS条形图中添加背景色

大师110

在此处输入图片说明

我创建了一个非常简单的条形图。现在,我想为创建的条形图添加一些样式。如示例所示,当x值大于200时,我想添加红色。我尝试了各种样式填充和背景,但无法获得预期的结果。知道现在该如何处理吗?

添加了代码

<script>


var mydata = {
		"min": 68.9813,
		"avg": 177.5037,
		"max": 672.6713,
		"values": [{
			"bin": -50.0,
			"percent": 0.0,
			"samples": 0
		}, {
			"bin": 0.0,
			"percent": 0.0,
			"samples": 0
		}, {
			"bin": 50.0,
			"percent": 6.7028,
			"samples": 309
		}, {
			"bin": 100.0,
			"percent": 32.2897,
			"samples": 2407
		}, {
			"bin": 150.0,
			"percent": 32.4565,
			"samples": 3207
		}, {
			"bin": 200.0,
			"percent": 17.1745,
			"samples": 2064
		}, {
			"bin": 250.0,
			"percent": 6.1833,
			"samples": 940
		}, {
			"bin": 300.0,
			"percent": 2.4971,
			"samples": 444
		}, {
			"bin": 350.0,
			"percent": 1.2438,
			"samples": 279
		}, {
			"bin": 400.0,
			"percent": 0.9262,
			"samples": 182
		}, {
			"bin": 450.0,
			"percent": 0.2781,
			"samples": 71
		}, {
			"bin": 500.0,
			"percent": 0.0962,
			"samples": 24
		}, {
			"bin": 550.0,
			"percent": 0.074,
			"samples": 25
		}, {
			"bin": 600.0,
			"percent": 0.0535,
			"samples": 24
		}, {
			"bin": 650.0,
			"percent": 0.0243,
			"samples": 6
		}, {
			"bin": 700.0,
			"percent": 0.0,
			"samples": 0
		}, {
			"bin": 750.0,
			"percent": 0.0,
			"samples": 0
		}, {
			"bin": 800.0,
			"percent": 0.0,
			"samples": 0
		}, {
			"bin": 850.0,
			"percent": 0.0,
			"samples": 0
		}, {
			"bin": 900.0,
			"percent": 0.0,
			"samples": 0
		}, {
			"bin": 950.0,
			"percent": 0.0,
			"samples": 0
		}, {
			"bin": 1000.0,
			"percent": 0.0,
			"samples": 0
		}],
		"index": 7,
		"time_h": 13.8529,
		"stddev": 67.8836,
		"samples": 9982
	};


//set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleBand()
          .range([0, width])
          .padding(0.1);
var y = d3.scaleLinear()
          .range([height, 0]);


function make_x_gridlines() {		
    return d3.axisBottom(x)
        .ticks(2)
}

// gridlines in y axis function
function make_y_gridlines() {		
    return d3.axisLeft(y)
        .ticks(10)
}
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("#co_histogram").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 + ")")
          .style('fill', 'black');;

// get the data
// d3.csv("sales.csv", function(error, data) {
//   if (error) throw error;

//   // format the data
//   data.forEach(function(d) {
//     d.sales = +d.sales;
//   });

  // Scale the range of the data in the domains
  x.domain(mydata.values.map(function(d) { return d.bin; }));
  y.domain([0, d3.max(mydata.values, function(d) { return d.percent; })]);

  // append the rectangles for the bar chart
  svg.selectAll(".bar")
      .data(mydata.values)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.bin) + (x.bandwidth()-4) / 2; })
      .attr("width", Math.min(x.bandwidth(),5))
      .attr("y", function(d) { return y(d.percent); })
      .attr("height", function(d) { return height - y(d.percent); });


  
  
  svg.append("g")			
  .attr("class", "grid")
  .attr("transform", "translate(0," + height + ")")
  .call(make_x_gridlines()
      .tickSize(-height)
      .tickFormat("")
  );

// add the Y gridlines
svg.append("g")			
  .attr("class", "grid")
  .call(make_y_gridlines()
      .tickSize(-width)
      .tickFormat("")
  );


// add the x Axis
svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

// add the y Axis
svg.append("g")
    .call(d3.axisLeft(y));




</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="../js/d3.v4.min.js"></script>

杰拉尔多·富塔多

您可以通过在黑条之前附加一个简单的SVG矩形来实现。要水平定位矩形,只需使用x比例尺即可:

var redBox = svg.append("rect")
    .attr("x", x(200) + x.bandwidth() / 2)
    .attr("y", 0)
    .attr("width", x(x.domain()[x.domain().length - 1]) - x(200) + x.bandwidth() / 2)
    .attr("height", height)
    .attr("fill", "red")
    .attr("opacity", 0.2);

这是您所做的更改的代码:

var mydata = {
  "min": 68.9813,
  "avg": 177.5037,
  "max": 672.6713,
  "values": [{
    "bin": -50.0,
    "percent": 0.0,
    "samples": 0
  }, {
    "bin": 0.0,
    "percent": 0.0,
    "samples": 0
  }, {
    "bin": 50.0,
    "percent": 6.7028,
    "samples": 309
  }, {
    "bin": 100.0,
    "percent": 32.2897,
    "samples": 2407
  }, {
    "bin": 150.0,
    "percent": 32.4565,
    "samples": 3207
  }, {
    "bin": 200.0,
    "percent": 17.1745,
    "samples": 2064
  }, {
    "bin": 250.0,
    "percent": 6.1833,
    "samples": 940
  }, {
    "bin": 300.0,
    "percent": 2.4971,
    "samples": 444
  }, {
    "bin": 350.0,
    "percent": 1.2438,
    "samples": 279
  }, {
    "bin": 400.0,
    "percent": 0.9262,
    "samples": 182
  }, {
    "bin": 450.0,
    "percent": 0.2781,
    "samples": 71
  }, {
    "bin": 500.0,
    "percent": 0.0962,
    "samples": 24
  }, {
    "bin": 550.0,
    "percent": 0.074,
    "samples": 25
  }, {
    "bin": 600.0,
    "percent": 0.0535,
    "samples": 24
  }, {
    "bin": 650.0,
    "percent": 0.0243,
    "samples": 6
  }, {
    "bin": 700.0,
    "percent": 0.0,
    "samples": 0
  }, {
    "bin": 750.0,
    "percent": 0.0,
    "samples": 0
  }, {
    "bin": 800.0,
    "percent": 0.0,
    "samples": 0
  }, {
    "bin": 850.0,
    "percent": 0.0,
    "samples": 0
  }, {
    "bin": 900.0,
    "percent": 0.0,
    "samples": 0
  }, {
    "bin": 950.0,
    "percent": 0.0,
    "samples": 0
  }, {
    "bin": 1000.0,
    "percent": 0.0,
    "samples": 0
  }],
  "index": 7,
  "time_h": 13.8529,
  "stddev": 67.8836,
  "samples": 9982
};


//set the dimensions and margins of the graph
var margin = {
    top: 20,
    right: 20,
    bottom: 30,
    left: 40
  },
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleBand()
  .range([0, width])
  .padding(0.1);
var y = d3.scaleLinear()
  .range([height, 0]);


function make_x_gridlines() {
  return d3.axisBottom(x)
    .ticks(2)
}

// gridlines in y axis function
function make_y_gridlines() {
  return d3.axisLeft(y)
    .ticks(10)
}
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
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 + ")")
  .style('fill', 'black');;

// get the data
// d3.csv("sales.csv", function(error, data) {
//   if (error) throw error;

//   // format the data
//   data.forEach(function(d) {
//     d.sales = +d.sales;
//   });

// Scale the range of the data in the domains
x.domain(mydata.values.map(function(d) {
  return d.bin;
}));
y.domain([0, d3.max(mydata.values, function(d) {
  return d.percent;
})]);

var redBox = svg.append("rect")
	.attr("x", x(200) + x.bandwidth()/2)
  .attr("y", 0)
  .attr("width", x(x.domain()[x.domain().length - 1]) - x(200) + x.bandwidth() / 2)
  .attr("height", height)
  .attr("fill", "red")
  .attr("opacity", 0.2);
 

// append the rectangles for the bar chart
svg.selectAll(".bar")
  .data(mydata.values)
  .enter().append("rect")
  .attr("class", "bar")
  .attr("x", function(d) {
    return x(d.bin) + (x.bandwidth() - 4) / 2;
  })
  .attr("width", Math.min(x.bandwidth(), 5))
  .attr("y", function(d) {
    return y(d.percent);
  })
  .attr("height", function(d) {
    return height - y(d.percent);
  });




svg.append("g")
  .attr("class", "grid")
  .attr("transform", "translate(0," + height + ")")
  .call(make_x_gridlines()
    .tickSize(-height)
    .tickFormat("")
  );

// add the Y gridlines
svg.append("g")
  .attr("class", "grid")
  .call(make_y_gridlines()
    .tickSize(-width)
    .tickFormat("")
  );


// add the x Axis
svg.append("g")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x));

// add the y Axis
svg.append("g")
  .call(d3.axisLeft(y));
<script src="https://d3js.org/d3.v4.min.js"></script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章