Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(treemap): assign leaf text color based on custom colors #1881

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions packages/core/src/components/graphs/treemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ const findColorShade = (hex: string) => {
return null
}

const textFillColor = function () {
const correspondingLeaf = select(this.parentNode).select('rect.leaf') as any
const correspondingLeafFill = getComputedStyle(correspondingLeaf.node(), null).getPropertyValue(
'fill'
)
const cl = d3Color(correspondingLeafFill) as any
const textFillColor = function (data) {
const correspondingLeaf = select(this.parentNode).select('rect.leaf')
const correspondingLeafFill: string =
data.backgroundColor ??
getComputedStyle(correspondingLeaf.node() as Element, null).getPropertyValue('fill')
const cl = d3Color(correspondingLeafFill)

let colorShade: any

if (cl) {
colorShade = findColorShade(cl ? cl.hex() : null)
}
Expand Down Expand Up @@ -195,11 +196,11 @@ export class Treemap extends Component {

let parent = d
while (parent.depth > 1) parent = parent.parent
const color = hsl(this.model.getFillColor(parent.data.name))

return [
{
text: d.data.name,
color: color.l < 0.5 ? 'white' : 'black'
backgroundColor: this.model.getFillColor(parent.data.name)
}
]
},
Expand Down
20 changes: 20 additions & 0 deletions packages/docs/src/lib/treemap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ const options: TreemapChartOptions = {
height: '600px'
}

const customColorOptions: TreemapChartOptions = {
title: 'Treemap (Custom colors)',
height: '600px',
color: {
scale: {
Oceania: '#3ddbd9',
Europe: '#08bdba',
America: '#009d9a',
Australia: '#007d79',
Africa: '#005d5d',
Asia: '#004144'
}
}
}

const data: ChartTabularData = [
{
name: 'Oceania',
Expand Down Expand Up @@ -111,5 +126,10 @@ export const examples: Example[] = [
data,
options,
tags: ['test']
},
{
data,
options: customColorOptions,
tags: ['test']
}
]
Loading