admin管理员组文章数量:1405516
Here is a sample fiddle of a simple chart.
I am new to learning about D3.js, and I love it and am so impressed by it and its creator Mike Bostock.
I've created a webpage of charts whose values affect each other, sort of like in Bostock's amazing Rent Versus Buy calculator in the New York Times.
I've tried to create a slider (using JqueryUI) that changes an input value (like the $250,000 input box shown below), which affects the y-value of the chart. It works, but it's disappointing in appearance and isn't mobile-friendly.
I've been unable to figure out through tutorials or documentation how to create a slider like Bostock did, shown here:
I don't think Bostock used anything other than D3 to build his slider and styling.
I've tried to look at his source code but haven't had luck figuring it out.
In my sample fiddle, I'm using a JqueryUI slider ($("<div class='slider'></div>").insertAfter(input).slider()
) and some CSS.
Are there features of D3 that I haven't found yet that could help my chart look more like his?
If you could point me in the right direction for how to create (and style) the slider, the y-value label, and the axes, I'd really appreciate it.
Here is a sample fiddle of a simple chart.
I am new to learning about D3.js, and I love it and am so impressed by it and its creator Mike Bostock.
I've created a webpage of charts whose values affect each other, sort of like in Bostock's amazing Rent Versus Buy calculator in the New York Times.
I've tried to create a slider (using JqueryUI) that changes an input value (like the $250,000 input box shown below), which affects the y-value of the chart. It works, but it's disappointing in appearance and isn't mobile-friendly.
I've been unable to figure out through tutorials or documentation how to create a slider like Bostock did, shown here:
I don't think Bostock used anything other than D3 to build his slider and styling.
I've tried to look at his source code but haven't had luck figuring it out.
In my sample fiddle, I'm using a JqueryUI slider ($("<div class='slider'></div>").insertAfter(input).slider()
) and some CSS.
Are there features of D3 that I haven't found yet that could help my chart look more like his?
If you could point me in the right direction for how to create (and style) the slider, the y-value label, and the axes, I'd really appreciate it.
Share asked Feb 10, 2017 at 2:43 RyanRyan 24.2k35 gold badges209 silver badges397 bronze badges 1- I don't think there's any better way to learn d3 other than the docs. I'm assuming you're using v4. – tao Commented Feb 10, 2017 at 3:20
1 Answer
Reset to default 5Here is a very simple demo that I wrote from scratch, therefore not based on your code in the fiddle, showing the basic aspects of the dataviz you want to create.
In this demo I'm using only D3, no jQuery (mixing D3 with jQuery gives me headaches):
var width = 500,
height = 180,
padding = 16;
var svg = d3.select("#svg")
.append("svg")
.attr("width", width)
.attr("height", height);
var data = d3.range(1, 21);
var xScale = d3.scaleBand()
.domain(data)
.range([padding * 2, width - padding])
.padding(0.2);
var yScale = d3.scaleLinear()
.domain([0, d3.max(data, d => d)])
.range([height - padding, padding]);
var bars = svg.selectAll(".bars")
.data(data)
.enter()
.append("rect")
.attr("x", d => xScale(d))
.attr("width", xScale.bandwidth())
.attr("y", d => yScale(d))
.attr("height", d => height - padding - yScale(d))
.attr("fill", (d => d3.select("#slider").node().value == d ? "firebrick" : "teal"));
var xAxis = d3.axisBottom(xScale);
var yAxis = d3.axisLeft(yScale);
var gX = svg.append("g")
.attr("transform", "translate(0," + (height - padding) + ")")
.call(xAxis);
var gY = svg.append("g")
.attr("transform", "translate(" + padding * 2 + ",0)")
.call(yAxis);
d3.select("#slider").on("input", function() {
var currentValue = this.value;
yScale.domain([0, currentValue * 2])
bars.attr("y", d => yScale(d))
.attr("height", d => height - padding - yScale(d))
.attr("fill", (d => currentValue == d ? "firebrick" : "teal"));
gY.call(yAxis);
})
#slider {
width: 435px;
}
#sliderdiv{
padding-left: 40px;
}
<script src="https://d3js/d3.v4.min.js"></script>
<div id="svg"></div>
<div id="sliderdiv"><input id="slider" type="range" min="1" max="20" step="1" value="10"/></div>
Explanation:
In this example, I'm using a regular HTML input for the slider:
<input id="slider" type="range" min="1" max="20" step="1" value="10"/>
Then, we get the changes in that slider with this function:
d3.select("#slider").on("input", function() {
var currentValue = this.value;
yScale.domain([0, currentValue * 2])
bars.attr("y", d => yScale(d))
.attr("height", d => height - padding - yScale(d))
.attr("fill", (d => currentValue == d ? "firebrick" : "teal"));
gY.call(yAxis);
})
Inside that function, we have 3 main steps. First, we get the current value of the slider:
var currentValue = this.value;
And, using that value, we change the y scale:
yScale.domain([0, currentValue * 2])
That way, the selected bar will remain always at the same height, just like in Bostock's code. Then, we update the bars:
bars.attr("y", d => yScale(d))
.attr("height", d => height - padding - yScale(d))
.attr("fill", (d => currentValue == d ? "firebrick" : "teal"));
And, finally, the y axis:
gY.call(yAxis);
本文标签: javascriptHow to style slider on chart in D3js and show label of dynamic yvalueStack Overflow
版权声明:本文标题:javascript - How to style slider on chart in D3.js and show label of dynamic y-value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744917348a2632075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论