-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
327 additions
and
111 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
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
272 changes: 227 additions & 45 deletions
272
packages/f2-next/src/components/tooltip/tooltipView.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 |
---|---|---|
@@ -1,60 +1,242 @@ | ||
import Component from '../../base/component'; | ||
import { jsx } from '../../jsx'; | ||
|
||
export default props => { | ||
const { records, context } = props; | ||
if (!records || !records.length) return null; | ||
const { width } = context; | ||
return ( | ||
<group | ||
style={{ | ||
width, | ||
}} | ||
> | ||
// view 的默认配置 | ||
const defaultStyle = { | ||
showTitle: false, | ||
showCrosshairs: false, | ||
crosshairsType: 'y', | ||
crosshairsStyle: { | ||
stroke: 'rgba(0, 0, 0, 0.25)', | ||
lineWidth: '2px', | ||
}, | ||
showTooltipMarker: true, | ||
background: { | ||
radius: '2px', | ||
fill: 'rgba(0, 0, 0, 0.65)', | ||
padding: ['6px', '10px'], | ||
}, | ||
titleStyle: { | ||
fontSize: '24px', | ||
fill: '#fff', | ||
textAlign: 'start', | ||
textBaseline: 'top', | ||
}, | ||
nameStyle: { | ||
fontSize: '24px', | ||
fill: 'rgba(255, 255, 255, 0.65)', | ||
textAlign: 'start', | ||
textBaseline: 'middle', | ||
}, | ||
valueStyle: { | ||
fontSize: '24px', | ||
fill: '#fff', | ||
textAlign: 'start', | ||
textBaseline: 'middle', | ||
}, | ||
joinString: ': ', | ||
showItemMarker: true, | ||
itemMarkerStyle: { | ||
width: '12px', | ||
radius: '6px', | ||
symbol: 'circle', | ||
lineWidth: '2px', | ||
stroke: '#fff', | ||
}, | ||
layout: 'horizontal', | ||
snap: false, | ||
}; | ||
|
||
function directionEnabled(mode: string, dir: string) { | ||
if (mode === undefined) { | ||
return true; | ||
} else if (typeof mode === 'string') { | ||
return mode.indexOf(dir) !== -1; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default class TooltipView extends Component { | ||
rootRef: any; | ||
itemsRef: any; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.rootRef = { current: null }; | ||
this.itemsRef = { current: null }; | ||
} | ||
didUpdate() { | ||
const { props, rootRef, itemsRef } = this; | ||
const group = rootRef.current; | ||
if (!group) { | ||
return; | ||
} | ||
const { records, coord } = props; | ||
if (!records || !records.length) return null; | ||
const firstRecord = records[0]; | ||
const { x } = firstRecord; | ||
const { left: coordLeft, width: coordWidth } = coord; | ||
const { width } = group.get('attrs'); | ||
const halfWidth = width / 2; | ||
const moveX = Math.min( | ||
Math.max(x - coordLeft - halfWidth, 0), | ||
coordWidth - halfWidth | ||
); | ||
itemsRef.current.moveTo(moveX, 0); | ||
} | ||
render() { | ||
const { props } = this; | ||
const { records, chart, coord, layout } = props; | ||
const { top: layoutTop } = layout; | ||
if (!records || !records.length) return null; | ||
const { | ||
left: coordLeft, | ||
top: coordTop, | ||
right: coordRight, | ||
bottom: coordBottom, | ||
width: coordWidth, | ||
} = coord; | ||
const firstRecord = records[0]; | ||
const { x, y, xField, yField, origin: firstOrigin } = firstRecord; | ||
const yScale = chart.getScale(yField); | ||
const { | ||
background, | ||
showTitle, | ||
titleStyle, | ||
showItemMarker = defaultStyle.showItemMarker, | ||
itemMarkerStyle: customItemMarkerStyle, | ||
nameStyle, | ||
valueStyle, | ||
joinString = defaultStyle.joinString, | ||
showCrosshairs = defaultStyle.showCrosshairs, | ||
crosshairsStyle, | ||
crosshairsType = defaultStyle.crosshairsType, | ||
} = props; | ||
const itemMarkerStyle = { | ||
...customItemMarkerStyle, | ||
...defaultStyle.itemMarkerStyle, | ||
}; | ||
|
||
return ( | ||
<group | ||
ref={this.rootRef} | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
// paddingLeft: '10px', | ||
flex: 1, | ||
}} | ||
attrs={{ | ||
radius: '2px', | ||
fill: 'rgba(0, 0, 0, 0.65)', | ||
left: coordLeft, | ||
top: layoutTop, | ||
}} | ||
> | ||
{records.map(record => { | ||
return ( | ||
<group | ||
<group | ||
ref={this.itemsRef} | ||
style={{ | ||
...defaultStyle.background, | ||
...background, | ||
}} | ||
attrs={{ | ||
...defaultStyle.background, | ||
...background, | ||
}} | ||
> | ||
{showTitle ? ( | ||
<text | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
// justifyContent: 'center', | ||
padding: ['6px', '10px', '6px', '10px'], | ||
// width: '100px' | ||
marginBottom: '6px', | ||
}} | ||
attrs={{ | ||
text: firstOrigin[xField], | ||
fontSize: '24px', | ||
fill: '#fff', | ||
textAlign: 'start', | ||
...titleStyle, | ||
}} | ||
> | ||
<circle | ||
/> | ||
) : null} | ||
<group | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
flexWrap: 'wrap', | ||
}} | ||
> | ||
{records.map((record) => { | ||
const value = yScale.getText(record[yField]); | ||
return ( | ||
<group | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
padding: [0, '10px', 0, 0], | ||
}} | ||
> | ||
{showItemMarker ? ( | ||
<marker | ||
style={{ | ||
width: itemMarkerStyle.width, | ||
marginRight: '6px', | ||
}} | ||
attrs={{ | ||
...itemMarkerStyle, | ||
fill: record.color, | ||
}} | ||
/> | ||
) : null} | ||
<text | ||
attrs={{ | ||
...defaultStyle.nameStyle, | ||
...nameStyle, | ||
text: value | ||
? `${record[xField]}${joinString}` | ||
: record[xField], | ||
}} | ||
/> | ||
<text | ||
attrs={{ | ||
...defaultStyle.valueStyle, | ||
...valueStyle, | ||
text: value, | ||
}} | ||
/> | ||
</group> | ||
); | ||
})} | ||
</group> | ||
</group> | ||
{showCrosshairs ? ( | ||
<group> | ||
{directionEnabled(crosshairsType, 'x') ? ( | ||
<line | ||
attrs={{ | ||
fill: record.color, | ||
r: '6px', | ||
lineWidth: '2px', | ||
stroke: '#fff', | ||
x1: coordLeft, | ||
y1: y, | ||
x2: coordRight, | ||
y2: y, | ||
...defaultStyle.crosshairsStyle, | ||
crosshairsStyle, | ||
}} | ||
/> | ||
<text | ||
style={{ | ||
marginLeft: '16px', | ||
}} | ||
) : null} | ||
{directionEnabled(crosshairsType, 'y') ? ( | ||
<line | ||
attrs={{ | ||
fill: 'black', | ||
text: record['genre'], | ||
x1: x, | ||
y1: coordTop, | ||
x2: x, | ||
y2: coordBottom, | ||
...defaultStyle.crosshairsStyle, | ||
crosshairsStyle, | ||
}} | ||
/> | ||
</group> | ||
); | ||
})} | ||
) : null} | ||
</group> | ||
) : null} | ||
{/* <polygon | ||
attrs={{ | ||
...background, | ||
// points | ||
}} | ||
/> */} | ||
</group> | ||
</group> | ||
); | ||
}; | ||
); | ||
} | ||
} |
Oops, something went wrong.