Looking to use d3.js to create a simple list from data in a box, complete with a button to toggle the visibility of the box and its content. Ran into two errors during implementation:
1) The list is starting at item 2 for some reason - where's the first one?
2) Clicking the button to hide the box doesn't hide the content, just the box itself.
Any suggestions on how to fix this?
jsFiddle: here
new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn JavaScript", done: false },
{ text: "Learn Vue", done: false },
{ text: "Play around in JSFiddle", done: true },
{ text: "Build something awesome", done: true }
]
},
mounted : function() {
this.start();
},
methods: {
start : function(){
var margin = {top: 10, right: 10, bottom: 30, left: 10},
width = 960,
height = 500;
var active = true;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var box = svg.append("rect")
.attr("x", 20)
.attr("y", 20)
.attr("rx", 20)
.attr("ry", 20)
.attr("id", "list")
.attr("height", 400)
.attr("width", 500)
.style("stroke", "#112233")
.style("fill", "#DDD")
.style("stroke-width", 1);
var items = svg.selectAll("rect")
.append("g")
.attr("id", "texts")
.data(this.todos)
.enter()
.append("text")
.style("fill", "#f00")
.attr("x", 50)
.attr("y", function (d, i) {
return 80 + (i) * 20;
})
.text(function (d, i) {
return (1 + i) + ") " + d.text;
});
svg.append("circle")
.attr("cx", 20 + 500 - 10)
.attr("cy", 20 + 10)
.attr("r", 20)
.attr("id", "closer")
.style("stroke", "#fdf8fe")
.style("fill", "red")
.style("stroke-width", 1)
.on("click", function (d, a, i) {
active = (active) ? false : true,
newOpacity = active ? 1 : 0;
console.log(newOpacity)
d3.select("#list").style("opacity", newOpacity);
d3.select("#texts").style("opacity", newOpacity);
});
},
toggle: function(todo){
todo.done = !todo.done
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<h2>Todos:</h2>
<div id="boxList"></div>
</div>