我想基于两个条件从一个现有矢量(数据集中)创建多个矢量
现在,我有一个数据集(//Width and height
var w = 600;
var h = 250;
var dataset = [5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
11, 12, 15, 20, 18, 17, 16, 18, 23, 25];
var xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
.rangeRound([0, w])
.paddingInner(0.05);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset)])
.range([0, h]);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create groups to hold the bars and text for each data point
var groups = svg.selectAll(".groups")
.data(dataset)
.enter()
.append("g")
.attr("class", "gbar");
//Create bars
groups.append("rect")
.attr("class", "actualRect")
.attr("x", function (d, i) {
return xScale(i);
})
.attr("y", function (d) {
return h - yScale(d);
})
.attr("width", xScale.bandwidth())
.attr("height", function (d) {
return yScale(d);
})
.attr("fill", function (d) {
return "rgb(0, 0, " + Math.round(d * 10) + ")";
});
//Create labels
groups.append("text")
.text(function (d) {
return d;
})
/*.style("pointer-events", "none")*/
.attr("text-anchor", "middle")
.attr("x", function (d, i) {
return xScale(i) + xScale.bandwidth() / 2;
})
.attr("y", function (d) {
return h - yScale(d) + 14;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white");
// Create container rect
// The goal is to be able to hover *above* a bar, but only highlight the visible blue bar to orange.
// I don't understand how to select (this).('actualRect'), instead of (this).("containerRect")
groups.append("rect")
.attr("class", "containerRect")
.attr("x", function (d, i) {
return xScale(i);
})
.attr("y", 0)
.attr("width", xScale.bandwidth())
.attr("height", h)
.attr("fill", "none")
.style("pointer-events", "all")
.on("mouseover", function () {
d3.select(this) // trying to target (this) -> .actualBar
.attr("fill", "orange");
});
),其中包含带有四个级别的向量“包装”:“容器”,“托盘”,“容器和托盘”或“其他”。现在,我想基于两个条件基于向量// user.ts
export class User {
id: number;
name: string;
}
// inside the component:
getUsers() : Observable<User[]> {
return this.http.get<User[]>(this.api+'/get-users').pipe(
tap(users => {
localStorage.setItem("cache_users", JSON.stringify(users));
})
);
}
创建3个向量:
第一个向量:如果DCE_unformat
的值为DCE_unformat$Packaging
,如果包装为== 1
的值为DCE_unformat$Packaging == "container"
,否则为== -1
第二个和第三个向量相同,但比其他两个包装类型相同。
现在我为每个将要完成的条件写了一个带有嵌套if语句的for循环,但是对于将来的任务,我想知道这样做是否更方便。
== "other"
答案 0 :(得分:0)
您可以通过矢量化的方式进行操作。
DCE_unformat$P1 <- as.integer(DCE_unformat$Packaging == "pallet")
DCE_unformat$P2 <- as.integer(DCE_unformat$Packaging == "container")
DCE_unformat$P3 <- as.integer(DCE_unformat$Packaging == "container en pallet")
i <- DCE_unformat$Packaging == "other"
DCE_unformat$P1[i] <- -1
DCE_unformat$P2[i] <- -1
DCE_unformat$P3[i] <- -1