I have tried to search but cannot find a solution.
My data look like this :
var data = [
{ "hour":"10",
"percentage":"50"
},
{ "hour":"11",
"percentage":"20"
},
{ "hour":"3",
"percentage":"90"
},
{ "hour":"55",
"percentage":"40"
},
{ "hour":"6",
"percentage":"70"
},
{ "hour":"8",
"percentage":"40"
}
];
I draw donut chart according to this data. I need :
To divide chart into 12 equal parts like clock.
I have color range to describe percentage, but what if there is no data in my hour attribute ?
I am new to D3JS and I cannot figure out the logic. Below is my donut chart.
Thanks in advanced.
var data = [
{ "hour":"10",
"percentage":"50"
},
{ "hour":"11",
"percentage":"20"
},
{ "hour":"3",
"percentage":"90"
},
{ "hour":"55",
"percentage":"40"
},
{ "hour":"6",
"percentage":"70"
},
{ "hour":"8",
"percentage":"40"
}
];
var can = d3.select("body").append("svg").attr("height",1000).attr("width",1000);
//var svg = d3.select(can[0]);
var r =100;
var p = Math.PI*2;
var color = d3.scale.linear()
.domain([0,100])
.range(["white","red"]);
var group = can.append("g")
.attr("transform","translate(100,100)");
var arc = d3.svg.arc()
.innerRadius(r - 30)
.outerRadius(r)
//.startAngle(0)
.endAngle(p-1);
var pie = d3.layout.pie()
.sort(null)
.value(function (d){return d.percentage;});
var arcs = group.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc")
.attr('fill',function(d){return color(d.data.percentage)})
.on("mouseover", function(d){
div.style("display", "inline")
.text(d.data.percentage + ", " + d.data.hour)
.style("left", (d3.event.pageX - 34) + "px")
.style("top", (d3.event.pageY - 12) + "px");
})
.on("mouseout", mouseout);
arcs.append("path")
.attr("d", arc)
.style("fill", function (d) {
return color(d.data.percentage);
});
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("display", "none");
function mouseout() {
div.style("display", "none");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>