d3 gauge no animation
by rdenver6
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.min.js"></script>
<div class="chart-gauge">
</div>
CSS
.chart-gauge {
width: 400px;
margin: 10px auto;
}
.chart-color1 {
fill: #DEA82C;
}
.chart-color2 {
fill: #E9621A;
}
.chart-color3 {
fill: #e92213;
}
.needle,
.needle-center {
fill: #464A4F;
}
JavaScript
(function() {
var Needle, arc, arcEndRad, arcStartRad, barWidth, chart, chartInset, degToRad, el, endPadRad, height, i, margin, needle, numSections, padRad, percToDeg, percToRad, percent, radius, ref, sectionIndx, sectionPerc, startPadRad, svg, totalPercent, width;
percent = .65;
barWidth = 40;
numSections = 3;
sectionPerc = 1 / numSections / 2;
padRad = 0.05;
chartInset = 10;
totalPercent = .75;
el = d3.select('.chart-gauge');
margin = {
top: 20,
right: 20,
bottom: 30,
left: 20
};
width = el[0][0].offsetWidth - margin.left - margin.right;
height = width;
radius = Math.min(width, height) / 2;
percToDeg = function(perc) {
return perc * 360;
};
percToRad = function(perc) {
return degToRad(percToDeg(perc));
};
degToRad = function(deg) {
return deg * Math.PI / 180;
};
svg = el.append('svg').attr('width', width + margin.left + margin.right).attr('height', height + margin.top + margin.bottom);
chart = svg.append('g').attr('transform', "translate(" + ((width + margin.left) / 2) + ", " + ((height + margin.top) / 2) + ")");
var stop = 0;
for (sectionIndx = i = 1, ref = numSections; 1 <= ref ? i <= ref : i >= ref; sectionIndx = 1 <= ref ? ++i : --i) {
if (stop == 1) {
break;
}
arcStartRad = percToRad(totalPercent);
arcEndRad = arcStartRad + percToRad(sectionPerc);
totalPercent += sectionPerc;
startPadRad = sectionIndx === 0 ? 0 : padRad / 2;
endPadRad = sectionIndx === numSections ? 0 : padRad / 2;
arc = d3.svg.arc().outerRadius(radius - chartInset).innerRadius(radius - chartInset - barWidth).startAngle(arcStartRad + startPadRad).endAngle(arcEndRad - endPadRad);
chart.append('path').attr('class', "arc chart-color" + sectionIndx).attr('d', arc);
}
stop = 1;
Needle = (function() {
function Needle(len, radius1) {
this.len = len;
this.radius = radius1;
}
Needle.prototype.drawOn = function(el, perc) {
...