Guest User

Untitled

a guest
Aug 25th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. <svg #test width="300" height="300"></svg>
  2.  
  3. values: number[] = [100, 1000, 2500, 150];
  4. @ViewChild('test') test;
  5. ngOnChanges(changes) {
  6. this.renderPie(changes.values);
  7. }
  8. ngAfterViewInit() {
  9. this.renderPie(this.values);
  10. }
  11. renderPie(values: number[]) {
  12. let width = 300;
  13. let height = 300;
  14. let radius = Math.min(width, height) / 2;
  15.  
  16. let color = d3.scaleOrdinal(d3.schemeCategory20b);
  17.  
  18. let svg = d3.select(this.test)
  19. .append('svg')
  20. .attr('width', '100%')
  21. .attr('height', '100%')
  22. .attr('viewBox', '0 0 ' + width + ' ' + height)
  23. .append('g')
  24. .attr('transform', 'translate(' + (width / 2) + ',' + (height / 2) + ')');
  25.  
  26. let arc = d3.arc()
  27. .innerRadius(0)
  28. .outerRadius(radius);
  29.  
  30. let labelArc = d3.arc()
  31. .innerRadius(radius - 40)
  32. .outerRadius(radius - 40);
  33.  
  34. let pie = d3.pie()
  35. .value((d) => { return d.count; })
  36. .sort(undefined);
  37.  
  38. let g = svg.selectAll('.arc')
  39. .data(pie(values))
  40. .enter()
  41. .append('g')
  42. .attr('class', 'arc');
  43.  
  44. g.append('path')
  45. .attr('d', arc)
  46. .style('fill', (d) => { return color(d.data.label); });
  47.  
  48. g.append('text')
  49. .attr('transform', (d) => { return 'translate(' + labelArc.centroid(d) + ')'; })
  50. .attr('dy', '.35em')
  51. .text((d) => { return 'test'; });
  52. }
Add Comment
Please, Sign In to add comment