pie.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.ChartPieView = Em.View.extend({
  20. w:90,
  21. h:90,
  22. data:[300, 500],
  23. id:null,
  24. palette: new Rickshaw.Color.Palette({ scheme: 'munin'}),
  25. stroke: 'black',
  26. strokeWidth: 2,
  27. donut:d3.layout.pie().sort(null),
  28. existCenterText: false,
  29. centerTextColor: 'black',
  30. r:function () {
  31. return Math.min(this.get('w'), this.get('h')) / 2 - this.get('strokeWidth');
  32. }.property('w', 'h'),
  33. outerR: Em.computed.alias('r'),
  34. innerR:function () {
  35. return 0; // this.get('r') - 20;
  36. }.property('r'),
  37. arc:function () {
  38. return d3.svg.arc().innerRadius(this.get('innerR')).outerRadius(this.get('outerR'));
  39. }.property(),
  40. didInsertElement:function () {
  41. this._super();
  42. this.appendSvg();
  43. },
  44. selector: Em.computed.format('#{0}', 'elementId'),
  45. appendSvg:function () {
  46. var thisChart = this;
  47. var svg = d3.select(thisChart.get('selector')).append("svg:svg")
  48. .attr("id", thisChart.get('id'))
  49. .attr("width", thisChart.get('w'))
  50. .attr("height", thisChart.get('h'))
  51. .attr("stroke", thisChart.get('stroke'))
  52. .attr("stroke-width", thisChart.get('strokeWidth'));
  53. // set percentage data in center if there exist a center text
  54. if(thisChart.get('existCenterText')){
  55. this.set('svg', svg
  56. .append("svg:g")
  57. .attr("render-order", 1)
  58. .append("svg:text")
  59. .style('fill', thisChart.get('centerTextColor'))
  60. .attr("stroke", thisChart.get('centerTextColor'))
  61. .attr("font-size", 17)
  62. .attr("transform", "translate(" + thisChart.get('w') / 2 + "," + ((thisChart.get('h') / 2) + 3) + ")")
  63. .attr("text-anchor", "middle")
  64. .text(function(d) {
  65. return thisChart.get('data')[0] + '%';
  66. })
  67. );
  68. }
  69. this.set('svg', svg
  70. .append("svg:g")
  71. .attr("transform", "translate(" + thisChart.get('w') / 2 + "," + thisChart.get('h') / 2 + ")"));
  72. this.set('arcs', thisChart.get('svg').selectAll("path")
  73. .data(thisChart.donut(thisChart.get('data')))
  74. .enter().append("svg:path")
  75. .attr("fill", function (d, i) {
  76. return thisChart.palette.color(i);
  77. })
  78. .attr("d", thisChart.get('arc'))
  79. );
  80. }
  81. });