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

[DEV-7977a] Update scatter plot tooltips #1349

Merged
merged 1 commit into from
Jun 14, 2024
Merged
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
25 changes: 17 additions & 8 deletions packages/chart/src/components/ScatterPlot/ScatterPlot.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import React, { useContext } from 'react'
import ConfigContext from '../../ConfigContext'
import { Group } from '@visx/group'
import { formatNumber as formatColNumber } from '@cdc/core/helpers/cove/number'

const ScatterPlot = ({ xScale, yScale, getXAxisData, getYAxisData }) => {
const { colorScale, transformedData: data, config, formatNumber, seriesHighlight, colorPalettes } = useContext(ConfigContext)
const ScatterPlot = ({ xScale, yScale }) => {
const { transformedData: data, config, tableData, formatNumber, seriesHighlight, colorPalettes } = useContext(ConfigContext)

// TODO: copied from line chart should probably be a constant somewhere.
const circleRadii = 4.5
const hasMultipleSeries = Object.keys(config.runtime.seriesLabels).length > 1
// tooltips for additional columns
const additionalColumns = Object.entries(config.columns)
.filter(([_, value]) => value.tooltips)
.map(([_, value]) => [value.label || value.name, value.name])

const handleTooltip = (item, s) => `<div>
.map(([_, value]) => [
value.label || value.name,
value.name,
{
addColPrefix: value.prefix,
addColSuffix: value.suffix,
addColRoundTo: value.roundToPlace,
addColCommas: value.commas
}
])
const handleTooltip = (item, s, dataIndex) => `<div>
${config.legend.showLegendValuesTooltip && config.runtime.seriesLabels && hasMultipleSeries ? `${config.runtime.seriesLabels[s] || ''}<br/>` : ''}
${config.xAxis.label}: ${formatNumber(item[config.xAxis.dataKey], 'bottom')} <br/>
${config.yAxis.label}: ${formatNumber(item[s], 'left')}<br/>
${additionalColumns.map(([label, name]) => `${label} : ${formatNumber(item[name], 'left')}<br/>`).join('')}
${additionalColumns.map(([label, name, options]) => `${label} : ${formatColNumber(tableData[dataIndex][name], 'left', false, config, options)}<br/>`).join('')}
</div>`

return (
Expand All @@ -42,9 +51,9 @@ const ScatterPlot = ({ xScale, yScale, getXAxisData, getYAxisData }) => {
cx={xScale(item[config.xAxis.dataKey])}
cy={yScale(item[s])}
fill={displayArea ? seriesColor : 'transparent'}
fillOpacity={transparentArea ? .25 : 1}
fillOpacity={transparentArea ? 0.25 : 1}
style={pointStyles}
data-tooltip-html={handleTooltip(item, s)}
data-tooltip-html={handleTooltip(item, s, dataIndex)}
data-tooltip-id={`cdc-open-viz-tooltip-${config.runtime.uniqueId}`}
tabIndex={-1}
/>
Expand Down