简体   繁体   English

创建多个饼图/甜甜圈图

[英]creating multiple pie/donut charts

Here's the fiddle i've been working on. 这是我一直在努力的小提琴

I'm trying to figure out how to adjust this code so I can make multiple instances of my donut chart — scaled to various sizes depending upon the size of each object's "toursCreated" (but that's secondary). 我试图找出如何调整此代码的方法,以便可以制作甜甜圈图的多个实例-根据每个对象的“ toursCreated”的大小缩放到各种大小(但这是次要的)。

Right now I am just trying to understand how making multiples work — what are the code adjustments I need to make so I generate a pie chart for each object of data stored in the array of data. 现在,我只是想了解如何使倍数起作用-我需要进行哪些代码调整,因此我为存储在数据数组中的每个数据对象生成一个饼图。

here's the code so far... 这是到目前为止的代码...

 var width = 200, height = 150, radius = Math.min(width, height) / 2; var percentageFormat = d3.format("%"); var color = d3.scale.ordinal() .range(["#98abc5", "#8a89a6"]); var arc = d3.svg.arc() .outerRadius(radius) .innerRadius(40); var labelArc = d3.svg.arc() .outerRadius(radius - 18) .innerRadius(radius - 18); var pie = d3.layout.pie() .sort(null) .value(function(d) { return d.region; }); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); function render (data) { var tots = d3.sum(data, function(d) { console.log(d.region); return d.region; }); data.forEach(function(d) { d.percentage = d.region / tots; console.log(d.percentage); }); var g = svg.selectAll(".arc") .data(pie(data)) .enter().append("g") .attr("class", "arc"); g.append("path") .attr("d", arc) .style("fill", function(d) { return color(d.data.region); }); g.append("text") .attr("transform", function(d) { return "translate(" + labelArc.centroid(d) + ")"; }) .attr("dy", ".35em") .text(function(d) { return percentageFormat(d.data.percentage); }); console.log(data); var middle = data; console.log(middle); var innerG = svg.append("g") .attr("class", "innerArc") .append("circle") .attr("r", 40) .style("fill", "grey"); var salesPeopleTotal = svg.append("g") .attr("class", "sumTotal-text") .selectAll("text") .data([middle[0]]) .enter().append("text") .text(tots) .attr('y', "5") .attr('x', "-10") .style("fill", "white"); } var salesPeople = [ { "region": "West", "toursCreated": 644, "totalStaff": 644, "activeStaff": 399, "inactiveStaff": 245 }, { "region": "North-West", "toursCreated": 467, "totalStaff": 644, "activeStaff": 399, "inactiveStaff": 245 }, { "region": "South-West", "toursCreated": 320, "totalStaff": 644, "activeStaff": 399, "inactiveStaff": 245 } ]; var inactive = {"name": "inactive"}; inactive.region = salesPeople[0].inactiveStaff; var active = {"name": "active"}; active.region = salesPeople[0].activeStaff; var myArray = []; myArray.push(inactive); myArray.push(active); render(myArray); 

Maybe hold the d3 logic in a class, then create chart objects with the new operator. 也许将d3逻辑保存在一个类中,然后使用new运算符创建图表对象。

Ref: 参考:
http://www.w3schools.com/js/js_object_prototypes.asp http://www.w3schools.com/js/js_object_prototypes.asp

//if you use the new operator on a function, you get an object.
//var chartObj = new Chart({selector: '#myChartHtmlId svg', data: {}});
function Chart(initObject) {
  //set Chart properties.
  this.width = 200;
  this.height = 200;
  this.selector = initObject.selector;
  this.d3svg = d3.select(this.selector);
  this.data = initObj.data;

  for (prop in initObject) {
    //to overwrite defaults use:
    //new Chart({width: 500, height: 500});
    this[prop] = initObject[prop];
  }
  this.render();
}

//now once you've used:
//var chartObj = new Chart();
//you can now:
//chartObj.updateData(someVarWithData);
Chart.prototype.updateData = function(data) {
  this.data = data;
  this.render();
};

Chart.prototype.render = function() {
  var svg = this.d3sv;
  //continue d3 stuff.
  //access the object data with this.data
};

html: 的HTML:
....in the body obviously.. ....显然在体内..

<svg id="myChartHtmlId"></svg>

Then use: 然后使用:

var charts = [];

for (var i = 0, j = arrayOfChartInitObjects.length; i < j; i++) {
  var chartInitObj = arrayOfChartInitObjects[i];
  charts.push(new Chart(chartInitObj));
}

Please comment on my answer if any of this doesn't make sense/ you need further explanation. 如果这没有任何意义,请评论我的答案/您需要进一步的解释。

I hope this helps. 我希望这有帮助。

Rhys 里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM