2
建立在這個問題上:Access subkeys in multi-dimensional javascript object。d3.js使用.filter()從多個圖表上相應的x軸點返回數據
使用d3.js/dc.js,我有三個(或更多)圖表。所有圖表都具有相同的x軸(日期序列),因此任何圖表上的第n個數據點將完全對應於其他圖表的x軸上的第n個數據點。
當用戶點擊一個圖表中的一個點時,我需要從其他兩個圖表上的同一點獲取「y」數據,並返回一個數組或對象或字符串與chartID/y-datum從其他圖表,這樣的事情:
{"chart1":"30","chart2":"50","chart3":"10"}
下面是赫拉爾多·費塔朵的回答上面提及的問題借一個例子。我將如何修改Gerardo的示例以返回每個圖表中的數據點?
var data = [{x:20, y:30},
{x:30, y:60},
{x:40, y:40},
{x:50, y:90},
{x:60, y:20},
{x:70, y:90},
{x:80, y:90},
{x:90, y:10}];
draw("#svg1");
draw("#svg2");
draw("#svg3");
function draw(selector){
var width = 250,
height = 250;
var svg = d3.select(selector)
.append("svg")
.attr("width", width)
.attr("height", height);
var xScale = d3.scaleLinear()
.domain([0, 100])
.range([30, width - 10]);
var yScale = d3.scaleLinear()
.domain([0,100])
.range([height - 30, 10]);
\t
var circles = svg.selectAll("foo")
\t .data(data)
\t .enter()
\t .append("circle");
\t
circles.attr("r", 10)
\t .attr("fill", "teal")
\t .attr("cx", d=>xScale(d.x))
\t .attr("cy", d=>yScale(d.y));
var xAxis = d3.axisBottom(xScale);
var yAxis = d3.axisLeft(yScale);
svg.append("g").attr("transform", "translate(0,220)")
.attr("class", "xAxis")
.call(xAxis);
svg.append("g")
.attr("transform", "translate(30,0)")
.attr("class", "yAxis")
.call(yAxis);
}
d3.selectAll("circle").on("mouseover", function(){
var thisDatum = d3.select(this).datum();
d3.selectAll("circle").filter(d=>d.x == thisDatum.x && d.y == thisDatum.y).attr("fill", "firebrick");
}).on("mouseout", function(){
\t d3.selectAll("circle").attr("fill", "teal")
})
#svg1 {
float: left;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<div id="svg1"></div>
<div id="svg2"></div>
<div id="svg3"></div>
非常感謝,赫拉爾多。再次感謝一個優秀的演示。 – crashwap
不用擔心。那是你要的嗎?我看到你現在正在爲此掙扎一段時間...... –