-
Notifications
You must be signed in to change notification settings - Fork 7
added prop rotateAngle to avoid the overlapping x-axis tick values #322
Changes from 8 commits
ade981b
3a9d13e
756d613
79d79eb
f0183ce
beb094a
bc666bc
cf44461
95fdf8a
e57dbf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -120,6 +120,12 @@ const beforeInit = (control) => { | |||||||||||||||||||||||||
console.warn('allowCalibration for x-axis is a new feature that is currently a work in progress and may have stability issues. Use it at your own risk.'); | ||||||||||||||||||||||||||
getAxesDataRange({}, constants.X_AXIS, control.config); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
if (!utils.isDefined(control.config.axis.x.ticks.tickLabelsRotation)) { | ||||||||||||||||||||||||||
control.config.axis.x.ticks.tickLabelsRotation = 0; | ||||||||||||||||||||||||||
} else if (!utils.validTickLabelRotations.has(control.config.axis.x.ticks.tickLabelsRotation)) { | ||||||||||||||||||||||||||
console.warn(`${control.config.axis.x.ticks.tickLabelsRotation} is an invalid value for tickLabelsRotation. Valid values are: 0, -45. Resorting to the default value of 0`); | ||||||||||||||||||||||||||
control.config.axis.x.ticks.tickLabelsRotation = 0; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Can we simplify like above ? |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
updateAxesDomain(control.config); | ||||||||||||||||||||||||||
updateXAxisDomain(control.config); | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -893,7 +893,25 @@ const createAxes = (axis, scale, config, canvasSVG) => { | |||||||||||||||||
)})`, | ||||||||||||||||||
) | ||||||||||||||||||
.call(axis.x) | ||||||||||||||||||
.call(resetD3FontSize); | ||||||||||||||||||
.call(resetD3FontSize) | ||||||||||||||||||
.attr('id', 'x') | ||||||||||||||||||
.selectAll('text') | ||||||||||||||||||
.style('text-anchor', () => { | ||||||||||||||||||
if (!utils.isDefined(config.axis.x.ticks.tickLabelsRotation)) { | ||||||||||||||||||
config.axis.x.ticks.tickLabelsRotation = 0; | ||||||||||||||||||
return 'middle'; | ||||||||||||||||||
} | ||||||||||||||||||
const rotation = config.axis.x.ticks.tickLabelsRotation; | ||||||||||||||||||
if (rotation === 0) { | ||||||||||||||||||
return 'middle'; | ||||||||||||||||||
} if (rotation !== 0) { | ||||||||||||||||||
return 'end'; | ||||||||||||||||||
} if (!utils.validTickLabelRotations.has(rotation)) { | ||||||||||||||||||
return 'middle'; | ||||||||||||||||||
} | ||||||||||||||||||
return 'middle'; | ||||||||||||||||||
sdadn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
}) | ||||||||||||||||||
.attr('transform', () => `rotate(${config.axis.x.ticks.tickLabelsRotation})`); | ||||||||||||||||||
canvasSVG | ||||||||||||||||||
.append('g') | ||||||||||||||||||
.classed(styles.axis, true) | ||||||||||||||||||
|
@@ -922,7 +940,6 @@ const createAxes = (axis, scale, config, canvasSVG) => { | |||||||||||||||||
.call(resetD3FontSize); | ||||||||||||||||||
} | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* X Axis's position vertically relative to the canvas | ||||||||||||||||||
* | ||||||||||||||||||
|
@@ -1122,6 +1139,13 @@ const getXAxisHeight = (config) => { | |||||||||||||||||
const dummy = d3.select('body').append('div'); | ||||||||||||||||||
const svg = dummy.append('svg'); | ||||||||||||||||||
const group = svg.append('g').call(axis); | ||||||||||||||||||
if (config.axis.x.ticks && config.axis.x.ticks.tickLabelsRotation !== 0) { | ||||||||||||||||||
// Add extra padding for rotated tick labels | ||||||||||||||||||
group.selectAll('.tick text').attr('transform', `rotate(${config.axis.x.ticks.tickLabelsRotation})`); | ||||||||||||||||||
const rotatedTickLabelsHeight = group.node().getBoundingClientRect().height; | ||||||||||||||||||
dummy.remove(); | ||||||||||||||||||
return rotatedTickLabelsHeight + constants.DEFAULT_OVERLAPPING_PADDING; | ||||||||||||||||||
} | ||||||||||||||||||
const { height } = group.node().getBoundingClientRect(); | ||||||||||||||||||
dummy.remove(); | ||||||||||||||||||
return height; | ||||||||||||||||||
|
@@ -1205,10 +1229,16 @@ const getAxisLabelWidth = (label, axis, config) => { | |||||||||||||||||
* @param {string} label - Label text | ||||||||||||||||||
* @returns {number} label height | ||||||||||||||||||
*/ | ||||||||||||||||||
const getAxisLabelHeight = (label) => { | ||||||||||||||||||
const getAxisLabelHeight = (label, tickLabelsRotation) => { | ||||||||||||||||||
const dummy = d3.select('body').append('div'); | ||||||||||||||||||
const svg = dummy.append('svg'); | ||||||||||||||||||
const grouper = svg.append('g'); | ||||||||||||||||||
|
||||||||||||||||||
if (tickLabelsRotation !== 0) { | ||||||||||||||||||
// Adding extra padding for rotated labels | ||||||||||||||||||
grouper.attr('transform', `rotate(${tickLabelsRotation})`); | ||||||||||||||||||
} | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
buildAxisLabel(grouper, label); | ||||||||||||||||||
const { height } = grouper.node().getBoundingClientRect(); | ||||||||||||||||||
dummy.remove(); | ||||||||||||||||||
|
@@ -1293,7 +1323,8 @@ const calculateAxesLabelSize = (config) => { | |||||||||||||||||
config.axisLabelWidths.y2 = 0; | ||||||||||||||||||
if (config.showLabel) { | ||||||||||||||||||
if (config.axis.x.label) { | ||||||||||||||||||
config.axisLabelHeights.x = getAxisLabelHeight(config.axis.x.label); | ||||||||||||||||||
const tickLabelsRotation = config.axis.x.ticks && config.axis.x.ticks.tickLabelsRotation; | ||||||||||||||||||
config.axisLabelHeights.x = getAxisLabelHeight(config.axis.x.label, tickLabelsRotation); | ||||||||||||||||||
} | ||||||||||||||||||
if (config.axis.y.label) { | ||||||||||||||||||
config.axisLabelWidths.y = constants.DEFAULT_CHARACTER_SVG_ELEMENT_WIDTH; | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,4 +316,5 @@ export default { | |
DEFAULT_LEGEND_LINE_WIDTH: 28, | ||
DEFAULT_LEGEND_LINE_WIDTH_WITH_SYMBOL: 18, | ||
LEGEND_LINE_POSITION: 1.5, | ||
DEFAULT_OVERLAPPING_PADDING: 5, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be reviewed by UX: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming this is relative to the bottom of the tick labels, the typical value seems to be half of this. I'd like to see what 2.5 looks like. |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,12 @@ const getEpocFromDateString = (dateISO) => parseInt(new Date(dateISO).getTime(), | |
* @returns {object typeof Date} - Date object based on epoc | ||
*/ | ||
const getDateFromEpoc = (epocDate) => new Date(epocDate); | ||
/** | ||
* Enum for tick label rotations. | ||
* @readonly | ||
* @enum {number} | ||
*/ | ||
const validTickLabelRotations = new Set([0, -45]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
|
||
/** | ||
* @enum {Function} | ||
|
@@ -242,4 +248,5 @@ export default { | |
notEmpty, | ||
parseDateTime, | ||
sanitize, | ||
validTickLabelRotations, | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,8 @@ | ||||||||||||||||
# ChangeLog | ||||||||||||||||
|
||||||||||||||||
## Unreleased | ||||||||||||||||
* Added | ||||||||||||||||
* Added examples for the Xaxis Overlapping tick values. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Need a line break for consistency ! |
||||||||||||||||
|
||||||||||||||||
## 1.6.0 - (September 25, 2023) | ||||||||||||||||
|
||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Carbon from '@cerner/carbon-graphs/lib/js/carbon'; | ||
|
||
const getLineTimeseriesConfig = (id) => ({ | ||
bindTo: id, | ||
axis: { | ||
x: { | ||
type: Carbon.helpers.AXIS_TYPE.TIME_SERIES, | ||
label: 'Datetime', | ||
lowerLimit: new Date(2016, 0, 1).toISOString(), | ||
upperLimit: new Date(2016, 0, 12).toISOString(), | ||
ticks: { | ||
values: [ | ||
new Date(2016, 0, 1).toISOString(), | ||
new Date(2016, 0, 2).toISOString(), | ||
new Date(2016, 0, 3).toISOString(), | ||
new Date(2016, 0, 4).toISOString(), | ||
new Date(2016, 0, 5).toISOString(), | ||
new Date(2016, 0, 6).toISOString(), | ||
new Date(2016, 0, 7).toISOString(), | ||
new Date(2016, 0, 8).toISOString(), | ||
new Date(2016, 0, 9).toISOString(), | ||
new Date(2016, 0, 10).toISOString(), | ||
new Date(2016, 0, 11).toISOString(), | ||
], | ||
tickLabelsRotation: -45, | ||
format: '%Y, %X', | ||
}, | ||
}, | ||
y: { | ||
label: 'Line Set A', | ||
lowerLimit: 10, | ||
upperLimit: 30, | ||
}, | ||
y2: { | ||
show: false, | ||
label: 'Line Set B', | ||
lowerLimit: 0, | ||
upperLimit: 250, | ||
}, | ||
}, | ||
showLabel: true, | ||
showLegend: true, | ||
showShapes: true, | ||
showVGrid: true, | ||
showHGrid: true, | ||
locale: Carbon.helpers.LOCALE.es_ES, | ||
}); | ||
|
||
export default getLineTimeseriesConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import LineGraph from '../../../example/Graphs-Carbon/General/Axes/XAxisTickLabelOrientation?dev-site-example'; | ||
import GraphConfig from '@cerner/terra-graphs-docs/lib/example-datasets/graphConfigObjects/General/generalDefaultXAxisOverlapping.js?dev-site-codeblock'; | ||
require('details-polyfill'); | ||
|
||
# X-Axis Tick Label orientation | ||
|
||
This is a simple timeseries line graph with x-axis ticks label orientation `tickLabelsRotation = -45`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a simple timeseries line graph with x-axis ticks label orientation `tickLabelsRotation = -45`. Tick label rotation is used to prevent labels from overlapping in a narrow viewport or due to long tick labels. |
||
|
||
## Getting Started | ||
|
||
- Install with [npmjs](https://www.npmjs.com): | ||
- `npm i @cerner/carbon-graphs --save-dev` | ||
|
||
## Usage | ||
```js | ||
import Carbon from '@cerner/carbon-graphs/dist/js/carbon-graphs.js'; | ||
import '@cerner/carbon-graphs/dist/css/carbon-graphs.css'; | ||
``` | ||
|
||
## Example | ||
<LineGraph /> | ||
|
||
<details> | ||
<summary style={{fontSize: 24 }}> | ||
<b>Data</b> | ||
</summary> | ||
|
||
### Graph Config object | ||
<GraphConfig /> | ||
|
||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import Carbon from '@cerner/carbon-graphs/lib/js/carbon'; | ||
import utils from '@cerner/carbon-graphs/lib/js/helpers/utils'; | ||
import '@cerner/terra-graphs-docs/lib/terra-graphs-src/components/Graph.module.scss'; | ||
import ExampleGraphContainer from '@cerner/terra-graphs-docs/lib/terra-dev-site/ExampleGraphContainer/ExampleGraphContainer'; | ||
import getGraphConfig from '@cerner/terra-graphs-docs/lib/example-datasets/graphConfigObjects/General/generalDefaultXAxisOverlapping'; | ||
|
||
/* | ||
Please refer to the documentation below to see the graphConfig and data objects | ||
*/ | ||
|
||
const graphConfig = utils.deepClone(getGraphConfig('#XAxisTickLabelOrientation')); | ||
|
||
const XAxisTickLabelOrientationGeneralExample = () => { | ||
React.useEffect(() => { | ||
Carbon.api.graph(graphConfig); | ||
}, []); | ||
return ( | ||
<ExampleGraphContainer id="XAxisTickLabelOrientation" /> | ||
); | ||
}; | ||
|
||
export default XAxisTickLabelOrientationGeneralExample; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you generate wdio screenshots for this test? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import utils from '@cerner/carbon-graphs/lib/js/helpers/utils'; | ||
import LineGraph from '@cerner/terra-graphs-docs/lib/terra-graphs-src/components/Line/LineGraph'; | ||
import '@cerner/terra-graphs-docs/lib/terra-dev-site/ExampleGraphContainer/ExampleGraphContainer.module.scss'; | ||
import getGraphConfig from '@cerner/terra-graphs-docs/lib/example-datasets/graphConfigObjects/General/generalDefaultXAxisOverlapping'; | ||
import exampleData1 from '@cerner/terra-graphs-docs/lib/example-datasets/dataObjects/Line/dataset1'; | ||
import exampleData2 from '@cerner/terra-graphs-docs/lib/example-datasets/dataObjects/Line/dataset2'; | ||
import exampleData3 from '@cerner/terra-graphs-docs/lib/example-datasets/dataObjects/Line/dataset3'; | ||
import exampleData4 from '@cerner/terra-graphs-docs/lib/example-datasets/dataObjects/Line/dataset4'; | ||
import exampleData5 from '@cerner/terra-graphs-docs/lib/example-datasets/dataObjects/Line/dataset5'; | ||
import exampleData6 from '@cerner/terra-graphs-docs/lib/example-datasets/dataObjects/Line/dataset6'; | ||
|
||
const graphConfig = utils.deepClone(getGraphConfig('#xAxisTicksVlauesOverlapping')); | ||
const dataset = [ | ||
utils.deepClone(exampleData1), | ||
utils.deepClone(exampleData2), | ||
utils.deepClone(exampleData3), | ||
utils.deepClone(exampleData4), | ||
utils.deepClone(exampleData5), | ||
utils.deepClone(exampleData6), | ||
]; | ||
|
||
const timeoutArray = [0, 750, 750 * 2, 750 * 3, 750 * 4, 750 * 5, 750 * 6]; | ||
|
||
export default () => ( | ||
<> | ||
<div id="tooltip" className="initial-tooltip" /> | ||
<LineGraph graphID="xAxisTicksVlauesOverlapping" graphConfig={graphConfig} dataset={dataset} timeout={timeoutArray} /> | ||
; | ||
</> | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a condition where if
config.axis.x.ticks.tickLabelsRotation
is undefined, then it should be set to 0 as the default value.