我试图让我的数据与 jQuery 滑块交互。我的数据用钢蓝色的垂直线表示,但 jQuery 滑块仅与浅灰色垂直线 xAxis 交互,因为我使用 xAxis x.axis 及其路径设置它。它只随滑块移动,而不是钢蓝中的实际垂直线。我不能使用 d3 滑块,我被这个 d3 v2 困住了。我问了一个关于垂直线的问题:(How to interact JQuery slider with d3 vertical lines for every data point),但建议对这个滑块问题提出一个单独的问题:(任何输入都会有帮助!这是代码:
// define dimensions of graph
var m = [80, 80, 80, 80]; // margins
var w = 1000 - m[1] - m[3]; // width
var h = 350 - m[0] - m[2]; // height
// create a simple data array that we'll plot with a line (this array represents only the Y values, X will just be the index location)
var data = [0];
for (var i = 1; i < 50; i++) {
var sign = Math.random() > 0.5 ? +1 : -1;
data.push(data[i - 1] + sign * Math.random());
}
// X scale will fit all values from data[] within pixels 0-w
var x = d3.scale.linear().domain([0, data.length]).range([0, w]);
// Y scale will fit values from 0-10 within pixels h-0 (Note the inverted domain for the y-scale: bigger is up!)
var y = d3.scale.linear().domain([d3.min(data), d3.max(data)]).range([h, 0]);
// Add an SVG element with the desired dimensions and margin.
var graph = d3.select("#graph").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
// create yAxis
var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(3);
// Add the x-axis.
graph.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
// create left yAxis
var yAxisLeft = d3.svg.axis().scale(y).ticks(4).orient("left");
// Add the y-axis to the left
graph.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(-25,0)")
.call(yAxisLeft);
var circle = graph.selectAll("circle")
.data(data);
circle.enter()
.append("circle")
.attr("cx", function(d, i) {
return x(i)
})
.attr("cy", function(d) {
return y(d)
})
.attr("class", "circle")
.attr("r", 2)
.attr("fill", "red");
holdtom
相关分类