Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. var myCanvas1 = d3.select("#chart1")
  2. .append("svg")
  3. .attr("width", svgWidth + margin.left + margin.right)
  4. .attr("height", svgHeight + margin.top + margin.bottom)
  5. .style("background", "aliceblue")
  6. .append("g")
  7. .attr("transform","translate(" + margin.left + "," + margin.top + ")")
  8. .append("g");
  9.  
  10. //append rectangles to svg container
  11. var Bar = myCanvas1.selectAll("rect")
  12. .data(dataArray1)
  13. .enter()
  14. .append("rect")
  15. .style("fill", "steelblue")
  16. .attr("x", function(d, i) { return x(i); })
  17. .attr("width", x.bandwidth())
  18. .attr("y", function(d) { return (svgHeight - y(+d.balance)); } )
  19. .attr("height", function(d) { return y(+d.balance); })
  20. //.on("mouseover", function() {d3.select(this).attr("opacity", 0.5)})
  21. //.on("mouseout", function() {d3.select(this).attr("opacity", 1)});
  22.  
  23. //function for button click event
  24. function changeData(myDataArray) {
  25.  
  26. var Bars = myCanvas1.selectAll("rect");
  27. var NewBars = Bars.data(eval(myDataArray));
  28. //enter new data
  29. NewBars.enter()
  30. .append("rect")
  31. .style("fill", "steelblue")
  32. //.on("mouseover", function() {d3.select(this).attr("opacity", 0.5)})
  33. //.on("mouseout", function() {d3.select(this).attr("opacity", 1)})
  34. .transition()
  35. .duration(duration1)
  36. .attr("x", function(d, i) { return x(i); })
  37. .attr("width", x.bandwidth())
  38. .attr("y", function(d) { return (svgHeight - y(+d.balance)); } )
  39. .attr("height", function(d) { return y(+d.balance); });
  40. //exit data
  41. NewBars.exit()
  42. .remove();
  43. //update data
  44. NewBars.transition()
  45. .duration(duration1)
  46. .style("fill", "steelblue")
  47. .attr("x", function(d, i) { return x(i); })
  48. .attr("width", x.bandwidth())
  49. .attr("y", function(d) { return (svgHeight - y(+d.balance)); } )
  50. .attr("height", function(d) { return y(+d.balance); });
  51.  
  52. //mouseover and mouseout event functions
  53. d3.select("#chart1").select("svg").selectAll("rect").on("mouseover", function(d) { d3.select(this).attr("opacity", 0.3)});
  54. d3.select("#chart1").select("svg").selectAll("rect").on("mouseout", function(d) { d3.select(this).attr("opacity", 1)});
  55. };
  56.  
  57. //mouseover and mouseout event functions
  58. d3.select("#chart1").select("svg").selectAll("rect").on("mouseover", function(d) { d3.select(this).style("fill", "red")});
  59. d3.select("#chart1").select("svg").selectAll("rect").on("mouseout", function(d) { d3.select(this).style("fill", "green")});
  60.  
  61. //function click button to change opacity in all rects
  62. function OpacityNow() {
  63. d3.select("#chart1").select("svg").selectAll("rect").style("opacity", 0.3);
  64. };
  65. //function click button to change color in all rects
  66. function ColorNow() {
  67. d3.select("#chart1").select("svg").selectAll("rect").style("fill", "green");
  68. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement