forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Utility function to render chart tooltips (apache#27950)
- Loading branch information
1 parent
467e612
commit b549977
Showing
23 changed files
with
512 additions
and
425 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
57 changes: 57 additions & 0 deletions
57
superset-frontend/packages/superset-ui-core/src/utils/tooltip.ts
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,57 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { t } from '../translation'; | ||
|
||
const TRUNCATION_STYLE = ` | ||
max-width: 300px; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
`; | ||
|
||
export function tooltipHtml( | ||
data: string[][], | ||
title?: string, | ||
focusedRow?: number, | ||
) { | ||
const titleRow = title | ||
? `<span style="font-weight: 700;${TRUNCATION_STYLE}">${title}</span>` | ||
: ''; | ||
return ` | ||
<div> | ||
${titleRow} | ||
<table> | ||
${data.length === 0 ? `<tr><td>${t('No data')}</td></tr>` : ''} | ||
${data | ||
.map((row, i) => { | ||
const rowStyle = | ||
i === focusedRow ? 'font-weight: 700;' : 'opacity: 0.8;'; | ||
const cells = row.map((cell, j) => { | ||
const cellStyle = ` | ||
text-align: ${j > 0 ? 'right' : 'left'}; | ||
padding-left: ${j === 0 ? 0 : 16}px; | ||
${TRUNCATION_STYLE} | ||
`; | ||
return `<td style="${cellStyle}">${cell}</td>`; | ||
}); | ||
return `<tr style="${rowStyle}">${cells.join('')}</tr>`; | ||
}) | ||
.join('')} | ||
</table> | ||
</div>`; | ||
} |
115 changes: 115 additions & 0 deletions
115
superset-frontend/packages/superset-ui-core/test/utils/tooltip.test.ts
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,115 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { tooltipHtml } from '@superset-ui/core'; | ||
|
||
const TITLE_STYLE = | ||
'style="font-weight: 700;max-width:300px;overflow:hidden;text-overflow:ellipsis;"'; | ||
const TR_STYLE = 'style="opacity:0.8;"'; | ||
const TR_FOCUS_STYLE = 'style="font-weight:700;"'; | ||
const TD_TEXT_STYLE = | ||
'style="text-align:left;padding-left:0px;max-width:300px;overflow:hidden;text-overflow:ellipsis;"'; | ||
const TD_NUMBER_STYLE = | ||
'style="text-align:right;padding-left:16px;max-width:300px;overflow:hidden;text-overflow:ellipsis;"'; | ||
|
||
const data = [ | ||
['a', 'b', 'c'], | ||
['1', '2', '3'], | ||
]; | ||
|
||
function removeWhitespaces(text: string) { | ||
return text.replace(/\s/g, ''); | ||
} | ||
|
||
test('should return a table with the given data', () => { | ||
const title = 'Title'; | ||
const html = removeWhitespaces(tooltipHtml(data, title)); | ||
const expectedHtml = removeWhitespaces(` | ||
<div> | ||
<span ${TITLE_STYLE}>Title</span> | ||
<table> | ||
<tr ${TR_STYLE}> | ||
<td ${TD_TEXT_STYLE}>a</td> | ||
<td ${TD_NUMBER_STYLE}>b</td> | ||
<td ${TD_NUMBER_STYLE}>c</td> | ||
</tr> | ||
<tr ${TR_STYLE}> | ||
<td ${TD_TEXT_STYLE}>1</td> | ||
<td ${TD_NUMBER_STYLE}>2</td> | ||
<td ${TD_NUMBER_STYLE}>3</td> | ||
</tr> | ||
</table> | ||
</div>`); | ||
expect(html).toMatch(expectedHtml); | ||
}); | ||
|
||
test('should return a table with the given data and a focused row', () => { | ||
const title = 'Title'; | ||
const focusedRow = 1; | ||
const html = removeWhitespaces(tooltipHtml(data, title, focusedRow)); | ||
const expectedHtml = removeWhitespaces(` | ||
<div> | ||
<span ${TITLE_STYLE}>Title</span> | ||
<table> | ||
<tr ${TR_STYLE}> | ||
<td ${TD_TEXT_STYLE}>a</td> | ||
<td ${TD_NUMBER_STYLE}>b</td> | ||
<td ${TD_NUMBER_STYLE}>c</td> | ||
</tr> | ||
<tr ${TR_FOCUS_STYLE}> | ||
<td ${TD_TEXT_STYLE}>1</td> | ||
<td ${TD_NUMBER_STYLE}>2</td> | ||
<td ${TD_NUMBER_STYLE}>3</td> | ||
</tr> | ||
</table> | ||
</div>`); | ||
expect(html).toMatch(expectedHtml); | ||
}); | ||
|
||
test('should return a table with no data', () => { | ||
const title = 'Title'; | ||
const html = removeWhitespaces(tooltipHtml([], title)); | ||
const expectedHtml = removeWhitespaces(` | ||
<div> | ||
<span ${TITLE_STYLE}>Title</span> | ||
<table> | ||
<tr><td>No data</td></tr> | ||
</table> | ||
</div>`); | ||
expect(html).toMatch(expectedHtml); | ||
}); | ||
|
||
test('should return a table with the given data and no title', () => { | ||
const html = removeWhitespaces(tooltipHtml(data)); | ||
const expectedHtml = removeWhitespaces(` | ||
<div> | ||
<table> | ||
<tr ${TR_STYLE}> | ||
<td ${TD_TEXT_STYLE}>a</td> | ||
<td ${TD_NUMBER_STYLE}>b</td> | ||
<td ${TD_NUMBER_STYLE}>c</td> | ||
</tr> | ||
<tr ${TR_STYLE}> | ||
<td ${TD_TEXT_STYLE}>1</td> | ||
<td ${TD_NUMBER_STYLE}>2</td> | ||
<td ${TD_NUMBER_STYLE}>3</td> | ||
</tr> | ||
</table> | ||
</div>`); | ||
expect(html).toMatch(expectedHtml); | ||
}); |
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
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
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
Oops, something went wrong.