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