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 |
(...skipping 12 matching lines...) Expand all Loading... |
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 | 30 |
31 /** | 31 /** |
32 * @constructor | 32 * @constructor |
33 * @param {number=} totalValue | 33 * @param {number} size |
34 * @param {function(number):string=} formatter | 34 * @param {function(number):string=} formatter |
35 */ | 35 */ |
36 WebInspector.PieChart = function(totalValue, formatter) | 36 WebInspector.PieChart = function(size, formatter) |
37 { | 37 { |
38 var shadowSize = WebInspector.PieChart._ShadowSizePercent; | 38 var shadowSize = WebInspector.PieChart._ShadowSizePercent; |
39 this.element = document.createElementWithClass("div", "pie-chart"); | 39 this.element = document.createElementWithClass("div", "pie-chart"); |
40 var svg = this._createSVGChild(this.element, "svg"); | 40 var svg = this._createSVGChild(this.element, "svg"); |
41 svg.setAttribute("width", (100 * (1 + 2 * shadowSize)) + "%"); | 41 svg.setAttribute("width", (100 * (1 + 2 * shadowSize)) + "%"); |
42 svg.setAttribute("height", (100 * (1 + 2 * shadowSize)) + "%"); | 42 svg.setAttribute("height", (100 * (1 + 2 * shadowSize)) + "%"); |
43 this._group = this._createSVGChild(svg, "g"); | 43 this._group = this._createSVGChild(svg, "g"); |
44 var shadow = this._createSVGChild(this._group, "circle"); | 44 var shadow = this._createSVGChild(this._group, "circle"); |
45 shadow.setAttribute("r", 1 + shadowSize); | 45 shadow.setAttribute("r", 1 + shadowSize); |
46 shadow.setAttribute("cy", shadowSize); | 46 shadow.setAttribute("cy", shadowSize); |
47 shadow.setAttribute("fill", "hsl(0,0%,70%)"); | 47 shadow.setAttribute("fill", "hsl(0,0%,70%)"); |
48 var background = this._createSVGChild(this._group, "circle"); | 48 var background = this._createSVGChild(this._group, "circle"); |
49 background.setAttribute("r", 1); | 49 background.setAttribute("r", 1); |
50 background.setAttribute("fill", "hsl(0,0%,92%)"); | 50 background.setAttribute("fill", "hsl(0,0%,92%)"); |
51 if (totalValue) { | 51 this._totalElement = this.element.createChild("div", "pie-chart-foreground")
; |
52 var totalString = formatter ? formatter(totalValue) : totalValue; | 52 this._formatter = formatter; |
53 this._totalElement = this.element.createChild("div", "pie-chart-foregrou
nd"); | 53 this._slices = []; |
54 this._totalElement.textContent = totalString; | |
55 this._totalValue = totalValue; | |
56 } | |
57 this._lastAngle = -Math.PI/2; | 54 this._lastAngle = -Math.PI/2; |
58 this.setSize(100); | 55 this._setSize(size); |
59 } | 56 } |
60 | 57 |
61 WebInspector.PieChart._ShadowSizePercent = 0.02; | 58 WebInspector.PieChart._ShadowSizePercent = 0.02; |
62 | 59 |
63 WebInspector.PieChart.prototype = { | 60 WebInspector.PieChart.prototype = { |
64 /** | 61 /** |
65 * @param {number} value | 62 * @param {number} totalValue |
66 */ | 63 */ |
67 setTotal: function(value) | 64 setTotal: function(totalValue) |
68 { | 65 { |
69 this._totalValue = value; | 66 for (var i = 0; i < this._slices.length; ++i) |
| 67 this._slices[i].remove(); |
| 68 this._slices = []; |
| 69 this._totalValue = totalValue; |
| 70 var totalString; |
| 71 if (totalValue) |
| 72 totalString = this._formatter ? this._formatter(totalValue) : totalV
alue; |
| 73 else |
| 74 totalString = ""; |
| 75 this._totalElement.textContent = totalString; |
70 }, | 76 }, |
71 | 77 |
72 /** | 78 /** |
73 * @param {number} value | 79 * @param {number} value |
74 */ | 80 */ |
75 setSize: function(value) | 81 _setSize: function(value) |
76 { | 82 { |
77 this._group.setAttribute("transform", "scale(" + (value / 2) + ") transl
ate(" + (1 + WebInspector.PieChart._ShadowSizePercent) + ",1)"); | 83 this._group.setAttribute("transform", "scale(" + (value / 2) + ") transl
ate(" + (1 + WebInspector.PieChart._ShadowSizePercent) + ",1)"); |
78 var size = value + "px"; | 84 var size = value + "px"; |
79 this.element.style.width = size; | 85 this.element.style.width = size; |
80 this.element.style.height = size; | 86 this.element.style.height = size; |
81 if (this._totalElement) | 87 if (this._totalElement) |
82 this._totalElement.style.lineHeight = size; | 88 this._totalElement.style.lineHeight = size; |
83 }, | 89 }, |
84 | 90 |
85 /** | 91 /** |
86 * @param {number} value | 92 * @param {number} value |
87 * @param {string} color | 93 * @param {string} color |
88 */ | 94 */ |
89 addSlice: function(value, color) | 95 addSlice: function(value, color) |
90 { | 96 { |
91 var sliceAngle = value / this._totalValue * 2 * Math.PI; | 97 var sliceAngle = value / this._totalValue * 2 * Math.PI; |
92 if (!isFinite(sliceAngle)) | 98 if (!isFinite(sliceAngle)) |
93 return; | 99 return; |
94 sliceAngle = Math.min(sliceAngle, 2 * Math.PI * 0.9999); | 100 sliceAngle = Math.min(sliceAngle, 2 * Math.PI * 0.9999); |
95 var path = this._createSVGChild(this._group, "path"); | 101 var path = this._createSVGChild(this._group, "path"); |
96 var x1 = Math.cos(this._lastAngle); | 102 var x1 = Math.cos(this._lastAngle); |
97 var y1 = Math.sin(this._lastAngle); | 103 var y1 = Math.sin(this._lastAngle); |
98 this._lastAngle += sliceAngle; | 104 this._lastAngle += sliceAngle; |
99 var x2 = Math.cos(this._lastAngle); | 105 var x2 = Math.cos(this._lastAngle); |
100 var y2 = Math.sin(this._lastAngle); | 106 var y2 = Math.sin(this._lastAngle); |
101 var largeArc = sliceAngle > Math.PI ? 1 : 0; | 107 var largeArc = sliceAngle > Math.PI ? 1 : 0; |
102 path.setAttribute("d", "M0,0 L" + x1 + "," + y1 + " A1,1,0," + largeArc
+ ",1," + x2 + "," + y2 + " Z"); | 108 path.setAttribute("d", "M0,0 L" + x1 + "," + y1 + " A1,1,0," + largeArc
+ ",1," + x2 + "," + y2 + " Z"); |
103 path.setAttribute("fill", color); | 109 path.setAttribute("fill", color); |
| 110 this._slices.push(path); |
104 }, | 111 }, |
105 | 112 |
106 /** | 113 /** |
107 * @param {!Element} parent | 114 * @param {!Element} parent |
108 * @param {string} childType | 115 * @param {string} childType |
109 * @return {!Element} | 116 * @return {!Element} |
110 */ | 117 */ |
111 _createSVGChild: function(parent, childType) | 118 _createSVGChild: function(parent, childType) |
112 { | 119 { |
113 var child = document.createElementNS("http://www.w3.org/2000/svg", child
Type); | 120 var child = document.createElementNS("http://www.w3.org/2000/svg", child
Type); |
114 parent.appendChild(child); | 121 parent.appendChild(child); |
115 return child; | 122 return child; |
116 } | 123 } |
117 } | 124 } |
OLD | NEW |