Skip to content

Commit

Permalink
feat: customize the yxis tick size and format (#167)
Browse files Browse the repository at this point in the history
* feat: customize the Y axis tick size and format

* fix issue

* fix issue
  • Loading branch information
yaoterry authored Mar 1, 2022
1 parent 02d727d commit bca5ea8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
7 changes: 6 additions & 1 deletion components/LineGraph/LineGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const propTypes = {
* Set this prop to false to prevent x values from being converted to time.
*/
isXTime: PropTypes.bool,
yAxisTickFormat: PropTypes.func,
yAxisTickSize: PropTypes.number,
};

const defaultProps = {
Expand Down Expand Up @@ -359,10 +361,13 @@ class LineGraph extends Component {

this.yAxis = d3
.axisLeft()
.ticks(4)
.ticks(this.props.yAxisTickSize ? this.props.yAxisTickSize : 4)
.tickSize(-this.width)
.scale(this.y.nice());

if (this.props.yAxisTickFormat) {
this.yAxis = this.yAxis.tickFormat(this.props.yAxisTickFormat);
}
this.renderAxes();
this.renderLabels();
this.renderOverlay();
Expand Down
36 changes: 36 additions & 0 deletions components/LineGraph/LineGraph.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,42 @@ storiesOf('LineGraph', module)
onBlur={action('Blur')}
/>
))
.addWithInfo(
'Static Custom Y Axis',
` Static Custom Y Axis' Example. `,
() => (
<LineGraph
datasets={[
[
[1, 1507563000000],
[-1, 1507563900000],
[0, 1507564800000],
],
[
[-1, 1507563004000],
[0, 1507563900140],
[1, 1507564830000],
],
]}
yAxisTickSize={2}
yAxisTickFormat={val => {
if (val === 1) {
return 'running';
}
if (val === -1) {
return 'stopped';
}
if (val === 0) {
return 'unknown';
}
return '';
}}
onHover={action('Hover')}
onMouseOut={action('Mouseout')}
onBlur={action('Blur')}
/>
)
)
.addWithInfo('Number values for X', ` Static Example. `, () => (
<LineGraph
datasets={[
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "carbon-addons-data-viz-react",
"version": "1.25.0",
"version": "1.26.0",
"description": "Carbon Data Visualization",
"main": "cjs/index.js",
"module": "es/index.js",
Expand Down

0 comments on commit bca5ea8

Please sign in to comment.