var d3 = window.d3 // hook on with a temp namespace d3.eesur = {} d3.eesur.singleValueDonut = function createDonut () { // getter/setters var height = null var width = null var innnerRadius = 45 var outerRadius = 90 var colors = ['#f43530', '#eee'] function exports (selection) { selection.each(function (datum) { var remainingPercent = 100 - datum.value var pies = d3.pie().sort(null)([datum.value, remainingPercent]) // use the DOM container as the default width if (width === null) { width = +d3.select(this).style('width').slice(0, -2) } var arc = d3.arc() .innerRadius(innnerRadius) .outerRadius(outerRadius) .startAngle(function (d) { return d.startAngle; }) .endAngle(function (d) { return d.endAngle; }) // add the title d3.select(this).append('h5') .attr('class', 'center caps') .text(datum.subject) // add the svg var svg = d3.select(this) .append('svg') .attr('width', width) .attr('height', height) .append('g') .attr('transform', ("translate(" + (width / 2) + ", " + (height / 2) + ")")) svg.selectAll('path') .data(pies) .enter().append('path') .attr('d', arc) .attr('fill', function (d) { return colors[d.index]; }) // add svg.append('text') .attr('transform', "translate(-15, 5)") .text(datum.value + '%') }) } exports.height = function (value) { if (!arguments.length) { return height } height = value return this } exports.width = function (value) { if (!arguments.length) { return width } width = value return this } exports.innnerRadius = function (value) { if (!arguments.length) { return innnerRadius } innnerRadius = value return this } exports.outerRadius = function (value) { if (!arguments.length) { return outerRadius } outerRadius = value return this } exports.colors = function (value) { if (!arguments.length) { return colors } colors = value return this } return exports }