Skip to content

Commit 02d727d

Browse files
authored
feat: add negative value support for the LineGraph (#164)
* feat: add negative value support for the LineGraph * remove package-lock.json * refine
1 parent c69313a commit 02d727d

4 files changed

Lines changed: 37 additions & 11 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ css/
44
cjs/
55
node_modules
66
.DS_Store
7-
*.log
7+
*.log
8+
.idea/

components/LineGraph/LineGraph.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ const defaultProps = {
8989
isXTime: true,
9090
};
9191

92+
const getYDomainMin = data => {
93+
const dataMin = d3.min(data, d => d3.min(d.slice(0, d.length - 1)));
94+
if (dataMin > 0) {
95+
return 0;
96+
}
97+
return dataMin;
98+
};
99+
92100
class LineGraph extends Component {
93101
componentDidMount() {
94102
const {
@@ -162,8 +170,10 @@ class LineGraph extends Component {
162170
? nextProps.data
163171
: _.flatten(nextProps.datasets);
164172
this.x.domain(d3.extent(data, d => d[d.length - 1]));
165-
this.y.domain([0, d3.max(data, d => d3.max(d.slice(0, d.length - 1)))]);
166-
173+
this.y.domain([
174+
getYDomainMin(data),
175+
d3.max(data, d => d3.max(d.slice(0, d.length - 1))),
176+
]);
167177
this.updateEmptyState(
168178
nextProps.data.length > 0 ? nextProps.data : nextProps.datasets
169179
);
@@ -324,10 +334,9 @@ class LineGraph extends Component {
324334
.scaleLinear()
325335
.range([this.height, 0])
326336
.domain([
327-
0,
337+
getYDomainMin(flatData),
328338
d3.max(flatData, d => d3.max(d.slice(0, d.length - 1))) || 10,
329339
]);
330-
331340
this.line = d3
332341
.line()
333342
.x(d => this.x(d[d.length - 1]))

components/LineGraph/LineGraph.story.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,15 @@ storiesOf('LineGraph', module)
152152
.addWithInfo('Static', ` Static Example. `, () => (
153153
<LineGraph
154154
datasets={[
155-
[[43, 1507563000000], [27, 1507563900000], [33, 1507564800000]],
156155
[
157-
[48.633333333333, 1507563004000],
156+
[43, 1507563000000],
157+
[-27, 1507563900000],
158+
[33, 1507564800000],
159+
],
160+
[
161+
[-48.633333333333, 1507563004000],
158162
[21, 1507563900140],
159-
[38, 1507564830000],
163+
[-38, 1507564830000],
160164
],
161165
]}
162166
onHover={action('Hover')}
@@ -167,8 +171,20 @@ storiesOf('LineGraph', module)
167171
.addWithInfo('Number values for X', ` Static Example. `, () => (
168172
<LineGraph
169173
datasets={[
170-
[[45, 12], [23, 14], [33, 18], [31, 20], [12, 21]],
171-
[[48.633333333333, 11], [21, 15], [38, 16], [21, 19], [31, 21]],
174+
[
175+
[45, 12],
176+
[23, 14],
177+
[33, 18],
178+
[31, 20],
179+
[12, 21],
180+
],
181+
[
182+
[48.633333333333, 11],
183+
[21, 15],
184+
[38, 16],
185+
[21, 19],
186+
[31, 21],
187+
],
172188
]}
173189
onHover={action('Hover')}
174190
onMouseOut={action('Mouseout')}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "carbon-addons-data-viz-react",
3-
"version": "1.24.0",
3+
"version": "1.25.0",
44
"description": "Carbon Data Visualization",
55
"main": "cjs/index.js",
66
"module": "es/index.js",

0 commit comments

Comments
 (0)