@sb1/ffe-chart-donut-react 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 SpareBank 1
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @sb1/ffe-chart-donut-react
2
+
3
+ ## Install
4
+
5
+ ```bash
6
+ npm install --save @sb1/ffe-chart-donut-react
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```css
12
+ @import 'path/to/node_modules/@sb1/ffe-chart-donut-react/lib/ffe-chart-donut';
13
+ ```
14
+
15
+ Run Styleguidist from the repository root to see live examples and documentation,
16
+ or see the markdown files next to the component code in `src/`.
@@ -0,0 +1,99 @@
1
+ import React from 'react';
2
+ import { node, number, string } from 'prop-types';
3
+ var NON_BREAKING_SPACE = "\xA0";
4
+ var RADIUS = 150;
5
+ var CIRCUMFERENCE = 2 * Math.PI * RADIUS;
6
+
7
+ function ChartDonut(_ref) {
8
+ var name = _ref.name,
9
+ percentage = _ref.percentage,
10
+ firstLabel = _ref.firstLabel,
11
+ lastLabel = _ref.lastLabel,
12
+ label = _ref.label;
13
+ var offset = CIRCUMFERENCE - CIRCUMFERENCE / 100 * percentage;
14
+ /*
15
+ The rendered circle consists of two half-circles with a gap between them.
16
+ That means we have to rotate one of them to create this gap and we also have to
17
+ completely remove one of the circles once the other one grows so large that there is
18
+ not enough room to fit the other. The numbers used to rotate, adjust and hide
19
+ half-circles below come from visually inspecting the result at different percentages
20
+ and is not the result of any kind of complex math. If any aspect of this SVG is
21
+ changed, please verify that it looks right for the entire 0-100 percentage range.
22
+ */
23
+
24
+ return /*#__PURE__*/React.createElement("div", {
25
+ className: "ffe-chart-donut"
26
+ }, /*#__PURE__*/React.createElement("svg", {
27
+ viewBox: "0 0 360 360",
28
+ className: "ffe-chart-donut__circle",
29
+ style: {
30
+ transform: "scale(-1, 1) rotate(".concat(percentage ? 275 : -90, "deg)")
31
+ }
32
+ }, percentage < 95.7 && /*#__PURE__*/React.createElement("circle", {
33
+ className: "ffe-chart-donut--vann",
34
+ fill: "none",
35
+ strokeWidth: "15",
36
+ strokeLinecap: "round",
37
+ strokeLinejoin: "round",
38
+ strokeMiterlimit: "10",
39
+ cx: "50%",
40
+ cy: "50%",
41
+ r: RADIUS,
42
+ strokeDasharray: CIRCUMFERENCE,
43
+ strokeDashoffset: CIRCUMFERENCE - offset + (percentage ? 22 : 0)
44
+ })), /*#__PURE__*/React.createElement("svg", {
45
+ viewBox: "0 0 360 360",
46
+ className: "ffe-chart-donut__circle",
47
+ style: {
48
+ transform: "rotate(".concat(percentage ? -85 : -90, "deg)")
49
+ }
50
+ }, percentage > 3.2 && /*#__PURE__*/React.createElement("circle", {
51
+ className: "ffe-chart-donut--frost",
52
+ fill: "none",
53
+ strokeWidth: "15",
54
+ strokeLinecap: "round",
55
+ strokeLinejoin: "round",
56
+ strokeMiterlimit: "10",
57
+ cx: "50%",
58
+ cy: "50%",
59
+ r: RADIUS,
60
+ strokeDasharray: CIRCUMFERENCE + (percentage === 100 ? 22 : 0),
61
+ strokeDashoffset: offset + (percentage ? 30 : 0)
62
+ })), /*#__PURE__*/React.createElement("div", {
63
+ className: "ffe-chart-donut__description"
64
+ }, /*#__PURE__*/React.createElement("div", {
65
+ className: "ffe-chart-donut__name ffe-strong-text"
66
+ }, name), firstLabel && lastLabel && /*#__PURE__*/React.createElement("div", {
67
+ className: "ffe-chart-donut__fractions"
68
+ }, /*#__PURE__*/React.createElement("div", {
69
+ className: "ffe-chart-donut__fraction ffe-chart-donut__fraction--vann"
70
+ }, /*#__PURE__*/React.createElement("div", {
71
+ className: "ffe-chart-donut__amount ffe-strong-text"
72
+ }, "".concat(Number(100 - percentage).toFixed(0)).concat(NON_BREAKING_SPACE, "%")), /*#__PURE__*/React.createElement("div", {
73
+ className: "ffe-chart-donut__type ffe-micro-text"
74
+ }, firstLabel)), /*#__PURE__*/React.createElement("div", {
75
+ className: "ffe-chart-donut__fraction ffe-chart-donut__fraction--frost"
76
+ }, /*#__PURE__*/React.createElement("div", {
77
+ className: "ffe-chart-donut__amount ffe-strong-text"
78
+ }, "".concat(Number(percentage).toFixed(0)).concat(NON_BREAKING_SPACE, "%")), /*#__PURE__*/React.createElement("div", {
79
+ className: "ffe-chart-donut__type ffe-micro-text"
80
+ }, lastLabel))), !firstLabel && !lastLabel && label));
81
+ }
82
+
83
+ ChartDonut.propTypes = {
84
+ /** Short text labeling left value, like "empty", "said yes" etc */
85
+ firstLabel: string,
86
+
87
+ /** Short text labeling right value, like "full", "said no" etc */
88
+ lastLabel: string,
89
+
90
+ /** React node to be inserted directly under the chart's name, alternative to first/last label */
91
+ label: node,
92
+
93
+ /** Short text labeling the graph in total, like "percentage", "voted this year" etc */
94
+ name: string.isRequired,
95
+
96
+ /** The percentage for the right-most value */
97
+ percentage: number.isRequired
98
+ };
99
+ export default ChartDonut;
@@ -0,0 +1,37 @@
1
+ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
+
3
+ import React from 'react';
4
+ import { shallow } from 'enzyme';
5
+ import ChartDonut from '../src';
6
+ var defaultProps = {
7
+ firstLabel: 'Foo',
8
+ lastLabel: 'Bar',
9
+ name: 'Baz',
10
+ percentage: 42
11
+ };
12
+
13
+ var renderShallow = function renderShallow() {
14
+ var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
15
+ return shallow( /*#__PURE__*/React.createElement(ChartDonut, _extends({}, defaultProps, props)));
16
+ };
17
+
18
+ describe('ChartDonut', function () {
19
+ it('renders two SVGs', function () {
20
+ var el = renderShallow();
21
+ expect(el.find('svg')).toHaveLength(2);
22
+ });
23
+ it('renders html for the name of the circle', function () {
24
+ var el = renderShallow();
25
+ expect(el.find('.ffe-chart-donut__name')).toHaveLength(1);
26
+ expect(el.find('.ffe-chart-donut__name').text()).toBe('Baz');
27
+ });
28
+ it('renders html for the labels and their percentages', function () {
29
+ var el = renderShallow();
30
+ expect(el.find('.ffe-chart-donut__type')).toHaveLength(2);
31
+ expect(el.find('.ffe-chart-donut__amount')).toHaveLength(2);
32
+ expect(el.find('.ffe-chart-donut__type').at(0).text()).toBe('Foo');
33
+ expect(el.find('.ffe-chart-donut__amount').at(0).text()).toMatch(/58/);
34
+ expect(el.find('.ffe-chart-donut__type').at(1).text()).toBe('Bar');
35
+ expect(el.find('.ffe-chart-donut__amount').at(1).text()).toMatch(/42/);
36
+ });
37
+ });
package/es/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import ChartDonut from './ChartDonut';
2
+ export default ChartDonut;
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _propTypes = require("prop-types");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ var NON_BREAKING_SPACE = "\xA0";
15
+ var RADIUS = 150;
16
+ var CIRCUMFERENCE = 2 * Math.PI * RADIUS;
17
+
18
+ function ChartDonut(_ref) {
19
+ var name = _ref.name,
20
+ percentage = _ref.percentage,
21
+ firstLabel = _ref.firstLabel,
22
+ lastLabel = _ref.lastLabel,
23
+ label = _ref.label;
24
+ var offset = CIRCUMFERENCE - CIRCUMFERENCE / 100 * percentage;
25
+ /*
26
+ The rendered circle consists of two half-circles with a gap between them.
27
+ That means we have to rotate one of them to create this gap and we also have to
28
+ completely remove one of the circles once the other one grows so large that there is
29
+ not enough room to fit the other. The numbers used to rotate, adjust and hide
30
+ half-circles below come from visually inspecting the result at different percentages
31
+ and is not the result of any kind of complex math. If any aspect of this SVG is
32
+ changed, please verify that it looks right for the entire 0-100 percentage range.
33
+ */
34
+
35
+ return /*#__PURE__*/_react.default.createElement("div", {
36
+ className: "ffe-chart-donut"
37
+ }, /*#__PURE__*/_react.default.createElement("svg", {
38
+ viewBox: "0 0 360 360",
39
+ className: "ffe-chart-donut__circle",
40
+ style: {
41
+ transform: "scale(-1, 1) rotate(".concat(percentage ? 275 : -90, "deg)")
42
+ }
43
+ }, percentage < 95.7 && /*#__PURE__*/_react.default.createElement("circle", {
44
+ className: "ffe-chart-donut--vann",
45
+ fill: "none",
46
+ strokeWidth: "15",
47
+ strokeLinecap: "round",
48
+ strokeLinejoin: "round",
49
+ strokeMiterlimit: "10",
50
+ cx: "50%",
51
+ cy: "50%",
52
+ r: RADIUS,
53
+ strokeDasharray: CIRCUMFERENCE,
54
+ strokeDashoffset: CIRCUMFERENCE - offset + (percentage ? 22 : 0)
55
+ })), /*#__PURE__*/_react.default.createElement("svg", {
56
+ viewBox: "0 0 360 360",
57
+ className: "ffe-chart-donut__circle",
58
+ style: {
59
+ transform: "rotate(".concat(percentage ? -85 : -90, "deg)")
60
+ }
61
+ }, percentage > 3.2 && /*#__PURE__*/_react.default.createElement("circle", {
62
+ className: "ffe-chart-donut--frost",
63
+ fill: "none",
64
+ strokeWidth: "15",
65
+ strokeLinecap: "round",
66
+ strokeLinejoin: "round",
67
+ strokeMiterlimit: "10",
68
+ cx: "50%",
69
+ cy: "50%",
70
+ r: RADIUS,
71
+ strokeDasharray: CIRCUMFERENCE + (percentage === 100 ? 22 : 0),
72
+ strokeDashoffset: offset + (percentage ? 30 : 0)
73
+ })), /*#__PURE__*/_react.default.createElement("div", {
74
+ className: "ffe-chart-donut__description"
75
+ }, /*#__PURE__*/_react.default.createElement("div", {
76
+ className: "ffe-chart-donut__name ffe-strong-text"
77
+ }, name), firstLabel && lastLabel && /*#__PURE__*/_react.default.createElement("div", {
78
+ className: "ffe-chart-donut__fractions"
79
+ }, /*#__PURE__*/_react.default.createElement("div", {
80
+ className: "ffe-chart-donut__fraction ffe-chart-donut__fraction--vann"
81
+ }, /*#__PURE__*/_react.default.createElement("div", {
82
+ className: "ffe-chart-donut__amount ffe-strong-text"
83
+ }, "".concat(Number(100 - percentage).toFixed(0)).concat(NON_BREAKING_SPACE, "%")), /*#__PURE__*/_react.default.createElement("div", {
84
+ className: "ffe-chart-donut__type ffe-micro-text"
85
+ }, firstLabel)), /*#__PURE__*/_react.default.createElement("div", {
86
+ className: "ffe-chart-donut__fraction ffe-chart-donut__fraction--frost"
87
+ }, /*#__PURE__*/_react.default.createElement("div", {
88
+ className: "ffe-chart-donut__amount ffe-strong-text"
89
+ }, "".concat(Number(percentage).toFixed(0)).concat(NON_BREAKING_SPACE, "%")), /*#__PURE__*/_react.default.createElement("div", {
90
+ className: "ffe-chart-donut__type ffe-micro-text"
91
+ }, lastLabel))), !firstLabel && !lastLabel && label));
92
+ }
93
+
94
+ ChartDonut.propTypes = {
95
+ /** Short text labeling left value, like "empty", "said yes" etc */
96
+ firstLabel: _propTypes.string,
97
+
98
+ /** Short text labeling right value, like "full", "said no" etc */
99
+ lastLabel: _propTypes.string,
100
+
101
+ /** React node to be inserted directly under the chart's name, alternative to first/last label */
102
+ label: _propTypes.node,
103
+
104
+ /** Short text labeling the graph in total, like "percentage", "voted this year" etc */
105
+ name: _propTypes.string.isRequired,
106
+
107
+ /** The percentage for the right-most value */
108
+ percentage: _propTypes.number.isRequired
109
+ };
110
+ var _default = ChartDonut;
111
+ exports.default = _default;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ var _react = _interopRequireDefault(require("react"));
4
+
5
+ var _enzyme = require("enzyme");
6
+
7
+ var _src = _interopRequireDefault(require("../src"));
8
+
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+
11
+ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
12
+
13
+ var defaultProps = {
14
+ firstLabel: 'Foo',
15
+ lastLabel: 'Bar',
16
+ name: 'Baz',
17
+ percentage: 42
18
+ };
19
+
20
+ var renderShallow = function renderShallow() {
21
+ var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
22
+ return (0, _enzyme.shallow)( /*#__PURE__*/_react.default.createElement(_src.default, _extends({}, defaultProps, props)));
23
+ };
24
+
25
+ describe('ChartDonut', function () {
26
+ it('renders two SVGs', function () {
27
+ var el = renderShallow();
28
+ expect(el.find('svg')).toHaveLength(2);
29
+ });
30
+ it('renders html for the name of the circle', function () {
31
+ var el = renderShallow();
32
+ expect(el.find('.ffe-chart-donut__name')).toHaveLength(1);
33
+ expect(el.find('.ffe-chart-donut__name').text()).toBe('Baz');
34
+ });
35
+ it('renders html for the labels and their percentages', function () {
36
+ var el = renderShallow();
37
+ expect(el.find('.ffe-chart-donut__type')).toHaveLength(2);
38
+ expect(el.find('.ffe-chart-donut__amount')).toHaveLength(2);
39
+ expect(el.find('.ffe-chart-donut__type').at(0).text()).toBe('Foo');
40
+ expect(el.find('.ffe-chart-donut__amount').at(0).text()).toMatch(/58/);
41
+ expect(el.find('.ffe-chart-donut__type').at(1).text()).toBe('Bar');
42
+ expect(el.find('.ffe-chart-donut__amount').at(1).text()).toMatch(/42/);
43
+ });
44
+ });
@@ -0,0 +1,77 @@
1
+ .ffe-chart-donut {
2
+ height: 200px;
3
+ position: relative;
4
+ text-align: left;
5
+ width: 200px;
6
+
7
+ &--vann {
8
+ stroke: @ffe-farge-vann;
9
+ }
10
+
11
+ &--frost {
12
+ stroke: @ffe-farge-frost-70;
13
+ }
14
+
15
+ &__circle {
16
+ height: 100%;
17
+ margin: 0 auto;
18
+ position: absolute;
19
+ width: 100%;
20
+
21
+ @media (min-width: @breakpoint-md) {
22
+ margin: inherit;
23
+ width: 200px;
24
+ }
25
+ }
26
+
27
+ &__description {
28
+ left: calc(~'50% - 75px');
29
+ position: absolute;
30
+ top: 60px;
31
+ width: 150px;
32
+
33
+ @media (min-width: @breakpoint-md) {
34
+ left: 25px;
35
+ }
36
+ }
37
+
38
+ &__name {
39
+ color: @ffe-farge-koksgraa;
40
+ text-align: center;
41
+
42
+ .native & {
43
+ @media (prefers-color-scheme: dark) {
44
+ color: @ffe-farge-graa;
45
+ }
46
+ }
47
+ }
48
+
49
+ &__fractions {
50
+ display: flex;
51
+ justify-content: space-around;
52
+ }
53
+
54
+ &__fraction {
55
+ > .ffe-chart-donut__amount {
56
+ color: @ffe-farge-koksgraa;
57
+ text-align: center;
58
+
59
+ .native & {
60
+ @media (prefers-color-scheme: dark) {
61
+ color: @ffe-farge-graa;
62
+ }
63
+ }
64
+ }
65
+
66
+ &:last-child {
67
+ > .ffe-chart-donut__type {
68
+ padding-left: 20px;
69
+ }
70
+
71
+ > .ffe-chart-donut__amount {
72
+ border-left: 1px solid @ffe-farge-graa;
73
+ padding-left: 20px;
74
+ }
75
+ }
76
+ }
77
+ }
package/lib/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _ChartDonut = _interopRequireDefault(require("./ChartDonut"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ var _default = _ChartDonut.default;
13
+ exports.default = _default;
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@sb1/ffe-chart-donut-react",
3
+ "version": "4.0.0",
4
+ "description": "Donut chart in SVG",
5
+ "keywords": [
6
+ "ffe"
7
+ ],
8
+ "license": "MIT",
9
+ "author": "SpareBank 1",
10
+ "files": [
11
+ "lib",
12
+ "es",
13
+ "less"
14
+ ],
15
+ "main": "lib/index.js",
16
+ "module": "es/index.js",
17
+ "sideEffects": false,
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "ssh://git@github.com:SpareBank1/designsystem.git"
21
+ },
22
+ "scripts": {
23
+ "build": "npm run build:cjs && npm run build:es && npm run build:less",
24
+ "build:cjs": "babel -d lib/. --root-mode=upward --env-name=cjs --ignore=*.spec.js src/.",
25
+ "build:es": "babel -d es/. --root-mode=upward --env-name=es --ignore=*.spec.js src/.",
26
+ "build:less": "copyfiles -f src/ffe-chart-donut.less lib/",
27
+ "lint": "eslint src/.",
28
+ "test": "jest",
29
+ "test:watch": "jest --watch"
30
+ },
31
+ "jest": {
32
+ "setupTestFrameworkScriptFile": "../../test-setup.js"
33
+ },
34
+ "dependencies": {
35
+ "prop-types": "^15.6.0"
36
+ },
37
+ "devDependencies": {
38
+ "copyfiles": "^2.1.0",
39
+ "enzyme": "^3.7.0",
40
+ "enzyme-adapter-react-16": "^1.7.0",
41
+ "jest": "^26.0.0",
42
+ "react": "^16.9.0",
43
+ "react-dom": "^16.9.0"
44
+ },
45
+ "peerDependencies": {
46
+ "react": ">=16.9.0"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "gitHead": "fae3e64f7a01bc033be2edbf20cf4803556c5fd6"
52
+ }