1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
// CircleChart creates a donut/pie chart of colored sections.
//
// Initialize the chart with a size and inner radius to get a blank chart.
// To populate with data, first initialize the segments. The class names you
// provide for the segments will allow you to style them. You can then update
// the chart with new values (in the same order as the segments) at any time.
//
// SVG:
//
// - There is a single background path for the background.
// - There is a path for each segment.
// - If you want to put something inside the middle of the chart you can use `centerElement`.
//
// <div class="circle-chart">
// <svg width="120" height="120" viewBox="0 0 120 120">
// <path class="background" d="..."/>
// <path class="segment segment-class-name-1" d="..."/>
// <path class="segment segment-class-name-2" d="..."/>
// ...
// </svg>
// <div class="center"></div>
// </div>
WI.CircleChart = class CircleChart extends WI.View
{
constructor({size, innerRadiusRatio})
{
super();
this._data = [];
this._size = size;
this._radius = (size / 2) - 1;
this._innerRadius = innerRadiusRatio ? Math.floor(this._radius * innerRadiusRatio) : 0;
this.element.classList.add("circle-chart");
this._chartElement = this.element.appendChild(createSVGElement("svg"));
this._chartElement.setAttribute("width", size);
this._chartElement.setAttribute("height", size);
this._chartElement.setAttribute("viewBox", `0 0 ${size} ${size}`);
this._pathElements = [];
this._values = [];
this._total = 0;
let backgroundPath = this._chartElement.appendChild(createSVGElement("path"));
backgroundPath.setAttribute("d", this._createCompleteCirclePathData(this._size / 2, this._radius, this._innerRadius));
backgroundPath.classList.add("background");
}
// Public
get size() { return this._size; }
get centerElement()
{
if (!this._centerElement) {
this._centerElement = this.element.appendChild(document.createElement("div"));
this._centerElement.classList.add("center");
this._centerElement.style.width = this._centerElement.style.height = this._radius + "px";
this._centerElement.style.top = this._centerElement.style.left = (this._radius - this._innerRadius) + "px";
}
return this._centerElement;
}
get segments()
{
return this._segments;
}
set segments(segmentClassNames)
{
for (let pathElement of this._pathElements)
pathElement.remove();
this._pathElements = [];
for (let className of segmentClassNames) {
let pathElement = this._chartElement.appendChild(createSVGElement("path"));
pathElement.classList.add("segment", className);
this._pathElements.push(pathElement);
}
}
get values()
{
return this._values;
}
set values(values)
{
console.assert(!values.length || values.length === this._pathElements.length, "Should have the same number of values as segments");
this._values = values;
this._total = 0;
for (let value of values)
this._total += value;
}
clear()
{
this.values = new Array(this._values.length).fill(0);
}
// Protected
layout()
{
super.layout();
if (this.layoutReason === WI.View.LayoutReason.Resize)
return;
if (!this._values.length)
return;
const center = this._size / 2;
let startAngle = -Math.PI / 2;
let endAngle = 0;
for (let i = 0; i < this._values.length; ++i) {
let value = this._values[i];
let pathElement = this._pathElements[i];
if (value === 0)
pathElement.removeAttribute("d");
else if (value === this._total)
pathElement.setAttribute("d", this._createCompleteCirclePathData(center, this._radius, this._innerRadius));
else {
let angle = (value / this._total) * Math.PI * 2;
endAngle = startAngle + angle;
pathElement.setAttribute("d", this._createSegmentPathData(center, startAngle, endAngle, this._radius, this._innerRadius));
startAngle = endAngle;
}
}
}
// Private
_createCompleteCirclePathData(c, r1, r2)
{
const a1 = 0;
const a2 = Math.PI * 1.9999;
let x1 = c + Math.cos(a1) * r1,
y1 = c + Math.sin(a1) * r1,
x2 = c + Math.cos(a2) * r1,
y2 = c + Math.sin(a2) * r1,
x3 = c + Math.cos(a2) * r2,
y3 = c + Math.sin(a2) * r2,
x4 = c + Math.cos(a1) * r2,
y4 = c + Math.sin(a1) * r2;
return [
"M", x1, y1, // Starting position.
"A", r1, r1, 0, 1, 1, x2, y2, // Draw outer arc.
"Z", // Close path.
"M", x3, y3, // Starting position.
"A", r2, r2, 0, 1, 0, x4, y4, // Draw inner arc.
"Z" // Close path.
].join(" ");
}
_createSegmentPathData(c, a1, a2, r1, r2)
{
const largeArcFlag = ((a2 - a1) % (Math.PI * 2)) > Math.PI ? 1 : 0;
let x1 = c + Math.cos(a1) * r1,
y1 = c + Math.sin(a1) * r1,
x2 = c + Math.cos(a2) * r1,
y2 = c + Math.sin(a2) * r1,
x3 = c + Math.cos(a2) * r2,
y3 = c + Math.sin(a2) * r2,
x4 = c + Math.cos(a1) * r2,
y4 = c + Math.sin(a1) * r2;
return [
"M", x1, y1, // Starting position.
"A", r1, r1, 0, largeArcFlag, 1, x2, y2, // Draw outer arc.
"L", x3, y3, // Connect outer and inner arcs.
"A", r2, r2, 0, largeArcFlag, 0, x4, y4, // Draw inner arc.
"Z" // Close path.
].join(" ");
}
};
|