Skip to content

Commit bca5ea8

Browse files
authored
feat: customize the yxis tick size and format (#167)
* feat: customize the Y axis tick size and format * fix issue * fix issue
1 parent 02d727d commit bca5ea8

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

components/LineGraph/LineGraph.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const propTypes = {
5454
* Set this prop to false to prevent x values from being converted to time.
5555
*/
5656
isXTime: PropTypes.bool,
57+
yAxisTickFormat: PropTypes.func,
58+
yAxisTickSize: PropTypes.number,
5759
};
5860

5961
const defaultProps = {
@@ -359,10 +361,13 @@ class LineGraph extends Component {
359361

360362
this.yAxis = d3
361363
.axisLeft()
362-
.ticks(4)
364+
.ticks(this.props.yAxisTickSize ? this.props.yAxisTickSize : 4)
363365
.tickSize(-this.width)
364366
.scale(this.y.nice());
365367

368+
if (this.props.yAxisTickFormat) {
369+
this.yAxis = this.yAxis.tickFormat(this.props.yAxisTickFormat);
370+
}
366371
this.renderAxes();
367372
this.renderLabels();
368373
this.renderOverlay();

components/LineGraph/LineGraph.story.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,42 @@ storiesOf('LineGraph', module)
168168
onBlur={action('Blur')}
169169
/>
170170
))
171+
.addWithInfo(
172+
'Static Custom Y Axis',
173+
` Static Custom Y Axis' Example. `,
174+
() => (
175+
<LineGraph
176+
datasets={[
177+
[
178+
[1, 1507563000000],
179+
[-1, 1507563900000],
180+
[0, 1507564800000],
181+
],
182+
[
183+
[-1, 1507563004000],
184+
[0, 1507563900140],
185+
[1, 1507564830000],
186+
],
187+
]}
188+
yAxisTickSize={2}
189+
yAxisTickFormat={val => {
190+
if (val === 1) {
191+
return 'running';
192+
}
193+
if (val === -1) {
194+
return 'stopped';
195+
}
196+
if (val === 0) {
197+
return 'unknown';
198+
}
199+
return '';
200+
}}
201+
onHover={action('Hover')}
202+
onMouseOut={action('Mouseout')}
203+
onBlur={action('Blur')}
204+
/>
205+
)
206+
)
171207
.addWithInfo('Number values for X', ` Static Example. `, () => (
172208
<LineGraph
173209
datasets={[

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.25.0",
3+
"version": "1.26.0",
44
"description": "Carbon Data Visualization",
55
"main": "cjs/index.js",
66
"module": "es/index.js",

0 commit comments

Comments
 (0)