OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 /** | |
32 * @unrestricted | |
33 */ | |
34 UI.PieChart = class { | |
35 /** | |
36 * @param {number} size | |
37 * @param {function(number):string=} formatter | |
38 * @param {boolean=} showTotal | |
39 */ | |
40 constructor(size, formatter, showTotal) { | |
41 this.element = createElement('div'); | |
42 this._shadowRoot = UI.createShadowRootWithCoreStyles(this.element, 'ui_lazy/
pieChart.css'); | |
43 var root = this._shadowRoot.createChild('div', 'root'); | |
44 var svg = this._createSVGChild(root, 'svg'); | |
45 this._group = this._createSVGChild(svg, 'g'); | |
46 var background = this._createSVGChild(this._group, 'circle'); | |
47 background.setAttribute('r', 1.01); | |
48 background.setAttribute('fill', 'hsl(0, 0%, 90%)'); | |
49 this._foregroundElement = root.createChild('div', 'pie-chart-foreground'); | |
50 if (showTotal) | |
51 this._totalElement = this._foregroundElement.createChild('div', 'pie-chart
-total'); | |
52 this._formatter = formatter; | |
53 this._slices = []; | |
54 this._lastAngle = -Math.PI / 2; | |
55 this._setSize(size); | |
56 } | |
57 | |
58 /** | |
59 * @param {number} totalValue | |
60 */ | |
61 setTotal(totalValue) { | |
62 for (var i = 0; i < this._slices.length; ++i) | |
63 this._slices[i].remove(); | |
64 this._slices = []; | |
65 this._totalValue = totalValue; | |
66 var totalString; | |
67 if (totalValue) | |
68 totalString = this._formatter ? this._formatter(totalValue) : totalValue; | |
69 else | |
70 totalString = ''; | |
71 if (this._totalElement) | |
72 this._totalElement.textContent = totalString; | |
73 } | |
74 | |
75 /** | |
76 * @param {number} value | |
77 */ | |
78 _setSize(value) { | |
79 this._group.setAttribute('transform', 'scale(' + (value / 2) + ') translate(
1, 1) scale(0.99, 0.99)'); | |
80 var size = value + 'px'; | |
81 this.element.style.width = size; | |
82 this.element.style.height = size; | |
83 } | |
84 | |
85 /** | |
86 * @param {number} value | |
87 * @param {string} color | |
88 */ | |
89 addSlice(value, color) { | |
90 var sliceAngle = value / this._totalValue * 2 * Math.PI; | |
91 if (!isFinite(sliceAngle)) | |
92 return; | |
93 sliceAngle = Math.min(sliceAngle, 2 * Math.PI * 0.9999); | |
94 var path = this._createSVGChild(this._group, 'path'); | |
95 var x1 = Math.cos(this._lastAngle); | |
96 var y1 = Math.sin(this._lastAngle); | |
97 this._lastAngle += sliceAngle; | |
98 var x2 = Math.cos(this._lastAngle); | |
99 var y2 = Math.sin(this._lastAngle); | |
100 var largeArc = sliceAngle > Math.PI ? 1 : 0; | |
101 path.setAttribute('d', 'M0,0 L' + x1 + ',' + y1 + ' A1,1,0,' + largeArc + ',
1,' + x2 + ',' + y2 + ' Z'); | |
102 path.setAttribute('fill', color); | |
103 this._slices.push(path); | |
104 } | |
105 | |
106 /** | |
107 * @param {!Element} parent | |
108 * @param {string} childType | |
109 * @return {!Element} | |
110 */ | |
111 _createSVGChild(parent, childType) { | |
112 var child = parent.ownerDocument.createElementNS('http://www.w3.org/2000/svg
', childType); | |
113 parent.appendChild(child); | |
114 return child; | |
115 } | |
116 }; | |
OLD | NEW |