-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(BarChart): truncated label tooltip sample added
- Loading branch information
1 parent
7b4b599
commit d1f0a3b
Showing
2 changed files
with
109 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/viz/src/BarChart/stories/CustomEchartsOptions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { css, keyframes } from "@emotion/css"; | ||
import { theme } from "@hitachivantara/uikit-react-core"; | ||
import { HvBarChart, HvBarChartProps } from "@hitachivantara/uikit-react-viz"; | ||
|
||
const appear = keyframes` | ||
0% { | ||
opacity: 0; | ||
} | ||
100% { | ||
opacity: 1; | ||
} | ||
`; | ||
|
||
const styles = { | ||
root: css({ | ||
position: "absolute", | ||
width: "fit-content", | ||
boxShadow: theme.colors.shadow, | ||
backgroundColor: theme.colors.atmo1, | ||
padding: theme.space.sm, | ||
display: "flex", | ||
fontFamily: theme.fontFamily.body, | ||
fontWeight: theme.fontWeights.normal, | ||
fontSize: theme.fontSizes.sm, | ||
color: theme.colors.secondary, | ||
animation: `${appear} .2s ease-in-out`, | ||
}), | ||
}; | ||
|
||
const tooltipId = "viz-truncated-tooltip"; | ||
|
||
const events: HvBarChartProps["onEvents"] = { | ||
mouseover: (params, instance) => { | ||
if (params.componentType !== "yAxis") return; | ||
if (params.targetType !== "axisLabel") return; | ||
|
||
const fullValue = params.value; | ||
const targetValue = params.event?.target; | ||
const displayValue = targetValue?.style?.text; | ||
|
||
if (fullValue === displayValue) return; | ||
|
||
// Create tooltip | ||
const tooltip = document.createElement("div"); | ||
tooltip.setAttribute("id", tooltipId); | ||
tooltip.className = `${styles.root} ${css({ | ||
left: `${targetValue?.transform[4]}px`, | ||
top: `${targetValue?.transform[5]}px`, | ||
})}`; | ||
tooltip.innerText = fullValue; | ||
|
||
// Add tooltip | ||
instance?.getDom().appendChild(tooltip); | ||
}, | ||
mouseout: (params) => { | ||
if (params.componentType !== "yAxis") return; | ||
if (params.targetType !== "axisLabel") return; | ||
|
||
// Remove tooltip | ||
document.getElementById(tooltipId)?.remove(); | ||
}, | ||
}; | ||
|
||
export const CustomEchartsOptions = () => { | ||
const formatter = (value?: string | number) => `${Number(value) / 1000}k`; | ||
|
||
return ( | ||
<HvBarChart | ||
data={{ | ||
Group: [ | ||
"Group 1 with a very very long label", | ||
"Group 2", | ||
"Group 3 with a very very long label", | ||
], | ||
Sales: [2300, 1000, 7800], | ||
Target: [2100, 7700, 3000], | ||
}} | ||
groupBy="Group" | ||
measures={["Sales", "Target"]} | ||
xAxis={{ | ||
labelFormatter: formatter, | ||
}} | ||
tooltip={{ | ||
valueFormatter: formatter, | ||
}} | ||
horizontal | ||
stack="total" | ||
onOptionChange={(option) => { | ||
// Truncate axis label | ||
option.yAxis[0] = { | ||
...option.yAxis[0], | ||
triggerEvent: true, | ||
axisLabel: { | ||
...option.yAxis[0].axisLabel, | ||
overflow: "truncate", | ||
width: 80, | ||
}, | ||
}; | ||
return option; | ||
}} | ||
onEvents={events} | ||
/> | ||
); | ||
}; |