|
ResizablePiechart = function() { |
|
|
|
var data = [], |
|
json = null, |
|
chartWidth = 0, |
|
chartHeight = 0, |
|
domNode = null, |
|
svg = null, |
|
rootGroup = null, |
|
radius = 0, |
|
color = d3.scale.ordinal().range(colorbrewer.PuBuGn[9]), |
|
pie = d3.layout.pie(), |
|
arc = d3.svg.arc(), |
|
dispatch = d3.dispatch("pageDown", "pageUp"); |
|
|
|
function resizablepiechart(selection) { |
|
// initial setup |
|
domNode = selection.node(); // item dom node |
|
rootGroup = selection.append("svg"); |
|
|
|
arc.outerRadius(radius - 10) |
|
.innerRadius(0); |
|
|
|
pie.sort(null) |
|
.value(function(d) { |
|
return d.population; |
|
}); |
|
|
|
data = pie(json); |
|
|
|
cellSelection = rootGroup.selectAll("g").data(data) |
|
.enter(); |
|
|
|
rootGroup = cellSelection.append("g") |
|
.attr("class", "arc"); |
|
|
|
rootGroup.append("path") |
|
.attr("d", arc) |
|
.style("fill", function(d) { |
|
return color(d.data.age); |
|
}); |
|
|
|
rootGroup.append("text") |
|
.attr("transform", function(d) { |
|
return "translate(" + arc.centroid(d) + ")"; |
|
}) |
|
.attr("dy", ".35em") |
|
.style("text-anchor", "middle") |
|
.text(function(d) { |
|
return d.data.age; |
|
}); |
|
|
|
|
|
function handleResize() { |
|
chartWidth = parseInt(d3.select(domNode).style("width")); |
|
chartHeight = parseInt(d3.select(domNode).style("height")); |
|
|
|
radius = Math.min(chartWidth, chartHeight) / 2; |
|
|
|
rootGroup |
|
.attr("transform", "translate(" + chartWidth / 2 + "," + chartHeight / 2 + ")"); |
|
|
|
arc.outerRadius(radius - 10) |
|
.innerRadius(0); |
|
|
|
data = pie(json); |
|
|
|
d3.select(domNode) |
|
.attr("width", chartWidth + "px") |
|
.attr("height", chartHeight + "px"); |
|
|
|
rootGroup.selectAll("path") |
|
.each(function() { |
|
d3.select(this).call(position) |
|
}); |
|
} |
|
|
|
|
|
function position() { |
|
rootGroup.selectAll("path") |
|
.attr("d", arc) |
|
.style("fill", function(d) { |
|
return color(d.data.age); |
|
}); |
|
|
|
rootGroup.selectAll("text") |
|
.attr("transform", function(d) { |
|
return "translate(" + arc.centroid(d) + ")"; |
|
}) |
|
.text(function(d) { |
|
return d.data.age; |
|
}); |
|
} |
|
|
|
resizablepiechart.handleResize = handleResize; // make handleResize function publicly visible |
|
handleResize(); // call handleResize() to start |
|
} |
|
|
|
resizablepiechart.handleResize = function(evt) { // placeholder function that is overridden at runtime |
|
return virtualscroller; |
|
}; |
|
|
|
resizablepiechart.json = function(_) { |
|
if (!arguments.length) return json; |
|
json = _; |
|
return resizablepiechart; |
|
}; |
|
return resizablepiechart; |
|
}; |
|
|
|
var counter = 1; |
|
var piecharts = []; |
|
|
|
$(function() { |
|
var chartContainer = $("#chartContainer"); |
|
$("#addButton").click(function(e) { |
|
chartContainer.append("<div class='chart resizable ui-widget-content'><h3 class='ui-widget-header'>" + counter + ". Pie Chart</h3></div>"); |
|
var newChartDiv = $(".chart:last-of-type"); |
|
counter++; |
|
d3.csv("https://cdn.rawgit.com/KABA-CCEAC/kesign/master/public/data/piechart.csv", function(error, data) { |
|
var newNode = newChartDiv.resizable({}).draggable()[0]; |
|
var piechart = ResizablePiechart(); |
|
piechart.json(data); |
|
piecharts.push(piechart); |
|
d3.select(newNode).call(piechart); |
|
}) |
|
}); |
|
|
|
d3.select(window).on("resize", function() { |
|
_.each(piecharts, function(nextPieChart) { |
|
nextPieChart.handleResize(); |
|
}); |
|
}); |
|
|
|
$("#addButton").click(); |
|
$("#addButton").click(); |
|
}); |