gpt4 book ai didi

d3.js - 如何在d3面积图中叠加颜色

转载 作者:行者123 更新时间:2023-12-02 04:14:52 33 4
gpt4 key购买 nike

我正在制作面积图,例如 http://bl.ocks.org/mbostock/3883195我想在它上面添加一个附加功能。我想根据高度或其他一些变量有不同的颜色。有什么办法可以做到这一点吗?

  var area = d3.svg.area().interpolate("step-before")
.x(function(d) { return x(d.decile/10);})
.y0(height)
.y1(function(d) { return y(d.count);});

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 + ")");


d3.tsv("mark_vis.tsv", function(error, data){
if(error) throw error;
data = getCount(data);
console.log(data);

// data[0] = {decile : 1, count:0.34, other_variable:0.3}


svg.append("path").datum(data).attr("class", "area").attr("d", area);
//I wish I could do something like this .style("fill", function(d){return d.other_variable};);

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

svg.append("g").attr("class", "y axis").call(yAxis)
.append("text")
.attr("transform","rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.style("text-anchor", "end")
.text("%");

});

最佳答案

首先,您需要制作数据 block 。

此处制作数据集 block 的标准是使用字段关闭(即任何 data.close > 150)

  var bucket = [];
var prev = undefined;
var datanew = [];
data.forEach(function(d){
if (d.close > 150){
if (prev && prev.close < 150){
datanew.push(bucket);
bucket = [];
}
bucket.push(d);
} else {
if (prev && prev.close > 150){
datanew.push(bucket);
bucket = [];
}
bucket.push(d);
}
prev = d;
});
datanew.push(bucket); //datanew will hold the chunk of data with close > 150.

在当前数据集中,它将生成 8 个 block 。

  1. block 1(数据集 > 150)
  2. Chunk2(数据集 < 150)
  3. Chunk3(数据集 > 150)

..等等

现在在制作路径时:

 datanew.forEach(function(d){//iterate through the chunks
console.log(d)
if (d[0].close>150)
c = "red";//less than 150 so make it red as this chunk is for less than 150
else
c="blue";//greater than 150 so make it blue as this chunk is for greater than 150
svg.append("path")
.datum(d)
.attr("class", "area")
.attr("stroke", c)//color selected above
.style("fill", c)
.attr("d", area);
});

完整的工作代码在这里

var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%d-%b-%y").parse;

var x = d3.time.scale()
.range([0, width]);

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

var area = d3.svg.area()
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.close); });

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 + ")");

d3.tsv("https://gist.githubusercontent.com/cyrilcherian/6baf8967e3fceaa4fec1/raw/02ab25f4b7956b86be41523932da434529377fdb/data.tsv", function(error, data) {
if (error) throw error;

data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});

x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]);

var bucket = [];
var prev = undefined;
var datanew = [];
data.forEach(function(d){
if (d.close > 150){
if (prev && prev.close < 150){
datanew.push(bucket);
bucket = [];
}
bucket.push(d);
} else {
if (prev && prev.close > 150){
datanew.push(bucket);
bucket = [];
}
bucket.push(d);
}
prev = d;
});
datanew.push(bucket); //datanew will hold the chunk of data with close > 150.

datanew.forEach(function(d){//iterate through the chunks
console.log(d)
if (d[0].close>150)
c = "red";//less than 150 so make it red as this chunk is for less than 150
else
c="blue";//greater than 150 so make it blue as this chunk is for greater than 150
svg.append("path")
.datum(d)
.attr("class", "area")
.attr("stroke", c)//color selected above
.style("fill", c)
.attr("d", area);
});


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

svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
});
body {
font: 10px sans-serif;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

.area {
fill: steelblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

希望这有帮助!

关于d3.js - 如何在d3面积图中叠加颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34441305/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com