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

Feature/dev 4215f #1386

Merged
merged 9 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"annotations": [],
"type": "chart",
"debugSvg": false,
"chartMessage": {
Expand All @@ -11,6 +12,7 @@
"animate": false,
"fontSize": "medium",
"lineDatapointStyle": "hover",
"lineDatapointColor": "Same as Line",
"barHasBorder": "false",
"isLollipopChart": false,
"lollipopShape": "circle",
Expand All @@ -21,13 +23,16 @@
"tipRounding": "top",
"isResponsiveTicks": false,
"general": {
"showDownloadButton": false
"showDownloadButton": false,
"showMissingDataLabel": true,
"showSuppressedSymbol": true,
"showZeroValueDataLabel": true
},
"padding": {
"left": 5,
"right": 5
},
"suppressedData": [],
"preliminaryData": [],
"yAxis": {
"hideAxis": false,
"displayNumbersOnBar": false,
Expand All @@ -50,8 +55,11 @@
"rightAxisTickColor": "#333",
"numTicks": "",
"axisPadding": 0,
"scalePadding": 10,
"tickRotation": 0,
"anchors": [],
"shoMissingDataLabel": true,
"showMissingDataLine": true,
"label": "Y-Axis Label Example"
},
"boxplot": {
Expand Down Expand Up @@ -96,7 +104,7 @@
"xAxis": {
"sortDates": false,
"anchors": [],
"type": "date",
"type": "date-time",
"showTargetLabel": true,
"targetLabel": "Target",
"hideAxis": false,
Expand All @@ -116,9 +124,10 @@
"maxTickRotation": 0,
"dataKey": "Date",
"label": "X-Axis Example Label",
"dateParseFormat": "%d/%m/%Y",
"dateDisplayFormat": "%d/%m/%Y",
"tickWidthMax": 91
"dateParseFormat": "%m/%d/%Y",
"dateDisplayFormat": "%m/%d/%Y",
"tickWidthMax": 91,
"padding": 6
},
"table": {
"label": "Data Table",
Expand All @@ -131,6 +140,9 @@
"indexLabel": "",
"download": true,
"showVertical": false,
"dateDisplayFormat": "",
"showMissingDataLabel": true,
"showSuppressedSymbol": true,
"show": true
},
"orientation": "vertical",
Expand All @@ -139,6 +151,7 @@
"legend": {
"hide": false,
"behavior": "highlight",
"axisAlign": true,
"singleRow": false,
"colorCode": "",
"reverseLabelOrder": false,
Expand All @@ -150,11 +163,15 @@
"dynamicLegendChartMessage": "Select Options from the Legend",
"lineMode": false,
"verticalSorted": false,
"highlightOnHover": false,
"hideSuppressedLabels": false,
"seriesHighlight": [],
"position": "right",
"label": "Data Type"
},
"brush": {
"height": 25,
"active": true,
"data": [
{
"Date": "1/15/2016",
Expand Down Expand Up @@ -228,8 +245,7 @@
"Data 4": "150",
"Monthly-Goal": "140"
}
],
"active": false
]
},
"exclusions": {
"active": false,
Expand Down Expand Up @@ -265,20 +281,21 @@
"series": [
{
"dataKey": "Monthly-Goal",
"type": "Line",
"type": "Bar",
"axis": "Left",
"tooltip": true
},
{
"dataKey": "Data 2",
"type": "Line",
"type": "Bar",
"axis": "Left",
"tooltip": true
}
],
"tooltips": {
"opacity": 90,
"singleSeries": false
"singleSeries": false,
"dateDisplayFormat": ""
},
"forestPlot": {
"startAt": 0,
Expand Down Expand Up @@ -321,13 +338,56 @@
"leftWidthOffset": 0,
"rightWidthOffset": 0,
"showZeroLine": false,
"hideDateCategoryCol": false,
"leftLabel": "",
"rightLabel": ""
"rightLabel": "",
"hideDateCategoryCol": false
},
"area": {
"isStacked": false
},
"sankey": {
"title": {
"defaultColor": "black"
},
"iterations": 1,
"rxValue": 0.9,
"overallSize": {
"width": 900,
"height": 700
},
"margin": {
"margin_y": 25,
"margin_x": 0
},
"nodeSize": {
"nodeWidth": 26,
"nodeHeight": 40
},
"nodePadding": 55,
"nodeFontColor": "black",
"nodeColor": {
"default": "#ff8500",
"inactive": "#808080"
},
"linkColor": {
"default": "#ffc900",
"inactive": "#D3D3D3"
},
"opacity": {
"nodeOpacityDefault": 1,
"nodeOpacityInactive": 0.1,
"LinkOpacityDefault": 1,
"LinkOpacityInactive": 0.1
},
"storyNodeFontColor": "#006778",
"storyNodeText": [],
"nodeValueStyle": {
"textBefore": "(",
"textAfter": ")"
},
"data": []
},
"suppressedData": [],
"height": "332",
"data": [
{
Expand Down Expand Up @@ -403,8 +463,9 @@
"Monthly-Goal": "140"
}
],
"visualizationType": "Line",
"visualizationType": "Bar",
"filters": [],
"validated": 4.23,
"dynamicMarginTop": 0
"dynamicMarginTop": 0,
"version": "4.24.4"
}
6 changes: 3 additions & 3 deletions packages/chart/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
-->

<!-- GENERIC CHART TYPES -->
<!-- <div class="react-container" data-config="/examples/feature/annotations/index.json"></div> -->
<!-- <div class="react-container" data-config="/examples/test.json"></div> -->
<!-- <div class="react-container" data-config="/examples/private/rvr.json"></div> -->
<!-- <div class="react-container" data-config="/examples/feature/filters/url-filter.json"></div> -->
<!-- <div class="react-container" data-config="/examples/feature/bar/additional-column-tooltip.json"></div> -->
<!-- <div class="react-container" data-config="https://cdc.gov/poxvirus/mpox/modules/data-viz/mpx-trends_1.json"></div> -->
Expand All @@ -64,7 +65,7 @@
<!-- <div class="react-container" data-config="/examples/feature/forecasting/combo-forecasting.json"></div> -->
<!-- <div class="react-container" data-config="/examples/feature/forecasting/effective_reproduction.json"></div> -->
<!-- <div class="react-container" data-config="/examples/feature/area/area-chart-date.json"></div> -->
<div class="react-container" data-config="/examples/feature/area/area-chart-category.json"></div>
<!-- <div class="react-container" data-config="/examples/feature/area/area-chart-category.json"></div> -->
<!-- <div class="react-container" data-config="/examples/feature/scatterplot/scatterplot.json"></div> -->
<!-- <div class="react-container" data-config="/examples/feature/deviation/planet-deviation-config.json"></div> -->
<!-- <div class="react-container" data-config="/examples/feature/boxplot/boxplot.json"></div> -->
Expand Down Expand Up @@ -135,7 +136,6 @@
<!-- GENERIC CHART TYPES -->
<!-- <div class="react-container" data-config="/examples/gallery/paired-bar/paired-bar-chart.json"></div> -->
<!-- <div class="react-container" data-config="/examples/feature/line/line-chart.json"></div> -->
<div class="react-container" data-config="/examples/feature/annotations/index.json"></div>

<!-- HORIZONTAL BAR CHARTS -->
<!-- <div class="react-container" data-config="/examples/gallery/bar-chart-horizontal/horizontal-bar-chart-with-numbers-on-bar.json"></div> -->
Expand Down
154 changes: 154 additions & 0 deletions packages/chart/src/components/BrushChart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { Group } from '@visx/group'
import { useContext, useEffect, useRef, useState } from 'react'
import ConfigContext from '../ConfigContext'
import * as d3 from 'd3'
import { Text } from '@visx/text'

function BrushChart({ xMax, yMax }) {
const { tableData, config, parseDate, setBrushConfig, getTextWidth } = useContext(ConfigContext)
const [brushState, setBrushState] = useState({ isBrushing: false, selection: [] })
const [tooltip, showTooltip] = useState(false)
const svgRef = useRef()
const brushheight = 25
const borderRadius = 15

const tooltipText = 'Drag edges to focus on a specific segment '
const textWidth = getTextWidth(tooltipText, `normal ${16 / 1.1}px sans-serif`)

const calculateGroupTop = (): number => {
return Number(yMax) + config.xAxis.axisBBox + brushheight * 1.5
}

const handleMouseOver = () => {
// show tooltip text only once before brush triggered
if (brushState.selection[0] === 0 && xMax === brushState.selection[1]) {
showTooltip(true)
}
}
const handleMouseLeave = () => {
// hide tooltip text if brush was triggered
if (brushState.selection[0] !== 0 || brushState.selection[1] !== xMax) {
showTooltip(false)
}
showTooltip(false)
}

let brushHandle = (g, selection, firstDate, lastDate) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be const?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

const textWidth = getTextWidth(firstDate, `normal ${16 / 1.1}px sans-serif`)
return g
.selectAll('.handle--custom')
.data([{ side: 'left' }, { side: 'right' }])
.join(enter => {
const handleGroup = enter.append('g').attr('class', 'handle--custom')
// Define the path data for left and right handles
const leftHandlePath = 'M0.5,10A6,6 0 0 1 6.5,16V14A6,6 0 0 1 0.5,20ZM2.5,18V12M4.5,18V12'
const rightHandlePath = 'M6.5,10A6,6 0 0 0 0.5,16V14A6,6 0 0 0 6.5,20ZM4.5,18V12M2.5,18V12'
handleGroup
.append('text')
.attr('x', d => (d.side === 'left' ? 0 : -textWidth))
.attr('y', 30)
.text(d => (d.side === 'left' ? firstDate : lastDate))
.attr('font-size', '13px')

// handleGroup // ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this unused code?

// .append('path')
// .attr('class', 'handle--custom')
// .attr('cursor', 'ew-resize')
// .attr('fill', 'blue')
// .attr('d', d => (d.side === 'left' ? rightHandlePath : leftHandlePath))
// .attr('transform', (d, i) => `scale(1.2), translate(${d.side === 'left' ? '-9' : '0'} ,-12)`)
// .style('pointer-events', 'all')

return handleGroup
})

.attr('display', 'block')
.attr('transform', selection === null ? null : (d, i) => `translate(${selection[i]},${'10'})`)
}

useEffect(() => {
const svg = d3 // prettier-ignore
.select(svgRef.current)
.attr('overflow', 'visible')

// append background rect
svg
.append('rect') // prettier-ignore
.attr('fill', '#949494')
.attr('stroke', '#c5c5c5')
.attr('stroke-width', 2)
.attr('ry', borderRadius)
.attr('rx', borderRadius)
.attr('height', brushheight)
.attr('width', xMax)

// calculate x
const x = d3
.scaleTime()
.domain(d3.extent(tableData, d => parseDate(d[config.runtime.originalXAxis.dataKey])))
.range([0, xMax])

const brushHanlder = event => {
const selection = event?.selection

if (selection) {
// invert pixel values into data values
const [x0, x1] = selection.map(x.invert)
// filter out data based on inverted data values
const newFilteredData = tableData.filter(d => parseDate(d[config.runtime.originalXAxis.dataKey]) >= x0 && parseDate(d[config.runtime.originalXAxis.dataKey]) <= x1)
const firstDate = newFilteredData[0][config.runtime.originalXAxis.dataKey] ?? ''
const lastDate = newFilteredData[newFilteredData.length - 1][config.runtime.originalXAxis.dataKey] ?? ''
// add custom blue colored handlers to each corners of brush
svg.selectAll('.handle--custom').remove()
svg.selectAll('.handle--custom').remove()
// append handler
svg.call(brushHandle, selection, firstDate, lastDate)
// update the brush state to add filtered data based on selection
setBrushConfig(prev => {
return {
...prev,
isBrushing: true,
data: newFilteredData
}
})
setBrushState(() => {
return {
isBrushing: true,
selection
}
})
}
}
// create brush with "start ,brush, end'" states
const brush = d3
.brushX()
.extent([
[0, 0],
[xMax, brushheight]
])
.on('start brush end', brushHanlder)

const defaultSelection = [0, xMax]

const brushGroup = svg.append('g').call(brush).call(brush.move, defaultSelection)
brushGroup.select('.overlay').style('pointer-events', 'none')
// svg.call(brushHandle, selection, firstDate, lastDate)

brushGroup.selectAll('.selection').attr('fill', '#474747').attr('fill-opacity', 1).attr('rx', borderRadius).attr('ry', borderRadius)

return () => {
svg.selectAll('*').remove() // Cleanup on component unmount
}
}, [config])

return (
<Group onMouseLeave={handleMouseLeave} onMouseOver={handleMouseOver} className='brush-container' left={Number(config.runtime.yAxis.size)} top={calculateGroupTop()}>
<Text pointerEvents='visiblePainted' display={tooltip ? 'block' : 'none'} fontSize={16} x={(Number(xMax) - Number(textWidth)) / 2} y={-10}>
Drag edges to focus on a specific segment
</Text>
<svg width={'100%'} height={brushheight * 3} ref={svgRef}></svg>
</Group>
)
}

export default BrushChart
Loading
Loading