Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var myCanvas1 = d3.select("#chart1")
- .append("svg")
- .attr("width", svgWidth + margin.left + margin.right)
- .attr("height", svgHeight + margin.top + margin.bottom)
- .style("background", "aliceblue")
- .append("g")
- .attr("transform","translate(" + margin.left + "," + margin.top + ")")
- .append("g");
- //append rectangles to svg container
- var Bar = myCanvas1.selectAll("rect")
- .data(dataArray1)
- .enter()
- .append("rect")
- .style("fill", "steelblue")
- .attr("x", function(d, i) { return x(i); })
- .attr("width", x.bandwidth())
- .attr("y", function(d) { return (svgHeight - y(+d.balance)); } )
- .attr("height", function(d) { return y(+d.balance); })
- //.on("mouseover", function() {d3.select(this).attr("opacity", 0.5)})
- //.on("mouseout", function() {d3.select(this).attr("opacity", 1)});
- //function for button click event
- function changeData(myDataArray) {
- var Bars = myCanvas1.selectAll("rect");
- var NewBars = Bars.data(eval(myDataArray));
- //enter new data
- NewBars.enter()
- .append("rect")
- .style("fill", "steelblue")
- //.on("mouseover", function() {d3.select(this).attr("opacity", 0.5)})
- //.on("mouseout", function() {d3.select(this).attr("opacity", 1)})
- .transition()
- .duration(duration1)
- .attr("x", function(d, i) { return x(i); })
- .attr("width", x.bandwidth())
- .attr("y", function(d) { return (svgHeight - y(+d.balance)); } )
- .attr("height", function(d) { return y(+d.balance); });
- //exit data
- NewBars.exit()
- .remove();
- //update data
- NewBars.transition()
- .duration(duration1)
- .style("fill", "steelblue")
- .attr("x", function(d, i) { return x(i); })
- .attr("width", x.bandwidth())
- .attr("y", function(d) { return (svgHeight - y(+d.balance)); } )
- .attr("height", function(d) { return y(+d.balance); });
- //mouseover and mouseout event functions
- d3.select("#chart1").select("svg").selectAll("rect").on("mouseover", function(d) { d3.select(this).attr("opacity", 0.3)});
- d3.select("#chart1").select("svg").selectAll("rect").on("mouseout", function(d) { d3.select(this).attr("opacity", 1)});
- };
- //mouseover and mouseout event functions
- d3.select("#chart1").select("svg").selectAll("rect").on("mouseover", function(d) { d3.select(this).style("fill", "red")});
- d3.select("#chart1").select("svg").selectAll("rect").on("mouseout", function(d) { d3.select(this).style("fill", "green")});
- //function click button to change opacity in all rects
- function OpacityNow() {
- d3.select("#chart1").select("svg").selectAll("rect").style("opacity", 0.3);
- };
- //function click button to change color in all rects
- function ColorNow() {
- d3.select("#chart1").select("svg").selectAll("rect").style("fill", "green");
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement