Skip to content

Commit caea674

Browse files
committed
Add top and bottom compression constants
1 parent fbfcd4d commit caea674

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Distributed under the MIT license. See ``LICENSE`` for more information.
136136
[travis-url]: https://travis-ci.org/dbader/node-datadog-metrics
137137
[wiki]: https://github.com/umairnadeem/react-line-graph/wiki
138138
139-
## TODO
139+
## Feature Requests
140140
- Put all helper methods in a class
141141
- Fix smoothing algorithm to be more precise using cubic Beziers
142142
- Clean up prop passing in InteractionLayer*
@@ -147,6 +147,7 @@ Distributed under the MIT license. See ``LICENSE`` for more information.
147147
- Refactor LineGraph's index.jsx with React Hooks*
148148
- Publish on NPM, update README tags
149149
- Allow multiple transformation functions in drawPath function, using piping
150+
- Add X-Y padding option
150151
151152
## Challenges
152153
- Make hovering compatible with responsive height/width: need 3 data points per axis (viewBox dimension, cursor position, current dimension)

spec/_constants/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
pointsWithIntegers: [[0, 10], [10, 70], [20, 45], [30, 100], [40, 10], [60, 80]],
33
pointsWithNegatives: [[0, -10], [-10, -70], [20, -45], [-30, 100], [40, -10], [-60, 80]],
4-
pointsWithDecimals: [[0.001, -10.92], [-10, -7230.87], [20.2100, -45.00000]],
4+
pointsWithDecimals: [[0.001, -10.92], [-10, -7230.87], [20.21, -45.0]],
55
pointsUnordered: [[20, 45], [0, 10], [40, 10], [10, 70], [60, 80], [30, 100]],
66
pointsRandom: (() => {
77
const data = [];
@@ -10,4 +10,5 @@ module.exports = {
1010
}
1111
return data;
1212
})(),
13+
pointsExtreme: [[0, 50], [10, 100], [20, 10], [30,110]],
1314
};

src/components/App.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import points from '../../spec/_constants';
44

55
const App = () => {
66
const [hovered, onHover] = useState([0, 0]);
7-
const data = points.pointsRandom; // Get random set of points
7+
const data = points.pointsExtreme; // Get random set of points
88
const props = {
99
data,
1010
smoothing: 0.4,
@@ -13,7 +13,7 @@ const App = () => {
1313
fillBelow: 'url(#grad1)',
1414
strokeWidth: 3,
1515
onHover,
16-
compression: 0.5,
16+
compression: 0,
1717
};
1818

1919
return (

src/components/_helpers/line.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ export const normalize = (points = [], compression = 0, xCeil = 0, yCeil = 0) =>
3030
Math.min(elem[0], accum[2]),
3131
Math.min(elem[1], accum[3]),
3232
]), [-Infinity, -Infinity, Infinity, Infinity]);
33+
// Top compression
34+
const topComp = 0.95;
35+
// Bottom compression
36+
const bottomComp = (1 - Math.min(Math.abs(compression), 1));
3337
const xDiff = (xMax - xMin) || 1;
3438
const yDiff = (yMax - yMin) || 1;
35-
const compFactor = (1 - Math.min(Math.abs(compression), 1));
3639
const xFactor = xCeil / xDiff;
37-
const yFactor = (yCeil / yDiff) * compFactor;
40+
const yFactor = ((yCeil * topComp) / yDiff) * bottomComp;
3841
const xConst = xCeil - (xFactor * xMax);
39-
const yConst = yCeil - (yFactor * yMax);
42+
const yConst = yCeil * topComp - (yFactor * yMax);
4043
return points.map(([x, y]) => [
4144
Number((x * xFactor + xConst).toFixed(2)),
4245
Number((y * yFactor + yConst).toFixed(2)),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { findCtrlPoint } from '../_services';
2+
3+
/**
4+
* A alternative transformation function that smooths out the lines using
5+
* cubic Bezier transformations
6+
* @param {Tuple} point - An array with two values, [x,y]
7+
* @param {Number} index - The index of the current point in the graph
8+
* @param {Array.<Array>} points - The entire points array
9+
* @param {Number} ratio - (optional) A smoothing ratio, from 0 to 1
10+
*/
11+
export const smoothAlt = (point = [0, 0], index = 0, points = [], ratio = 0) => `C${findCtrlPoint(
12+
ratio,
13+
points[index - 1],
14+
point,
15+
points[index + 1],
16+
).join(',')} ${point.join(',')}`;

0 commit comments

Comments
 (0)