-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: rect mark * feat: add inset in rect shape in cartesian coordinate (#4176)
- Loading branch information
1 parent
3055272
commit f006c15
Showing
15 changed files
with
289 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Rect } from '../../../src/mark'; | ||
import { plot } from './helper'; | ||
|
||
describe('Rect', () => { | ||
it('Rect has expected props', () => { | ||
expect(Rect.props).toEqual({ | ||
defaultShape: 'rect', | ||
defaultLabelShape: 'label', | ||
channels: [ | ||
{ name: 'color' }, | ||
{ name: 'shape' }, | ||
{ name: 'enterType' }, | ||
{ name: 'enterDelay', scaleName: 'enter' }, | ||
{ name: 'enterDuration', scaleName: 'enter' }, | ||
{ name: 'enterEasing' }, | ||
{ name: 'key', scale: 'identity' }, | ||
{ name: 'groupKey', scale: 'identity' }, | ||
{ name: 'label', scale: 'identity' }, | ||
{ name: 'title', scale: 'identity' }, | ||
{ name: 'tooltip', scale: 'identity', independent: true }, | ||
{ name: 'x', required: true }, | ||
{ name: 'y', required: true }, | ||
], | ||
preInference: [{ type: 'maybeZeroY1' }], | ||
postInference: [ | ||
{ type: 'maybeKey' }, | ||
{ type: 'maybeTitleX' }, | ||
{ type: 'maybeTooltipY' }, | ||
], | ||
shapes: ['rect', 'hollow'], | ||
}); | ||
}); | ||
}); | ||
|
||
it('Rect should draw basic rect', () => { | ||
const [I, P] = plot({ | ||
mark: Rect({}), | ||
index: [0, 1], | ||
channel: { | ||
x: [0.1, 0.2], | ||
x1: [0.5, 0.8], | ||
y: [0.1, 0.2], | ||
y1: [0.9, 0.5], | ||
}, | ||
}); | ||
|
||
expect(I).toEqual([0, 1]); | ||
expect(P).toEqual([ | ||
[ | ||
[60, 40], | ||
[300, 40], | ||
[300, 360], | ||
[60, 360], | ||
], | ||
[ | ||
[120, 80], | ||
[480, 80], | ||
[480, 200], | ||
[120, 200], | ||
], | ||
]); | ||
}); |
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,48 @@ | ||
import { hierarchy, treemap } from 'd3-hierarchy'; | ||
import { G2Spec, render } from '../../../src'; | ||
|
||
import { createDiv, mount } from '../../utils/dom'; | ||
|
||
describe('rect', () => { | ||
it.skip('render({...}) should render basic rect', () => { | ||
const width = 640; | ||
const height = 480; | ||
const padding = 3; | ||
|
||
const treeMapLayout = (data) => { | ||
const root = hierarchy(data); | ||
root.count(); | ||
// @ts-ignore | ||
treemap().size([width, height]).padding(padding)(root); | ||
return root | ||
.descendants() | ||
.map((d) => | ||
Object.assign(d, { | ||
x: [d.x0, d.x1], | ||
y: [d.y0, d.y1], | ||
}), | ||
) | ||
.filter((d) => d.height === 0); | ||
}; | ||
|
||
const chart = render<G2Spec>({ | ||
width: 640, | ||
height: 480, | ||
padding: 3, | ||
type: 'rect', | ||
data: { | ||
type: 'fetch', | ||
value: | ||
'https://gw.alipayobjects.com/os/bmw-prod/5155ef81-db23-49f3-b72b-d436a219d289.json', | ||
transform: [{ type: 'custom', callback: treeMapLayout }], | ||
}, | ||
encode: { | ||
x: 'x', | ||
y: 'y', | ||
size: 'r', | ||
color: (d) => d.parent.data.name, | ||
}, | ||
}); | ||
mount(createDiv(), chart); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Rect | ||
|
||
常用于直方图、矩阵树图、聚合热力图等。 | ||
|
||
|
||
## 开始 | ||
|
||
|
||
```js | ||
(() => { | ||
const width = 640; | ||
const height = 480; | ||
const padding = 3; | ||
const layout = (data) => { | ||
const root = d3.hierarchy(data); | ||
root.count(); | ||
d3.treemap().size([width, height]).paddingLeft(padding)(root); | ||
return root | ||
.descendants() | ||
.map((d) => | ||
Object.assign(d, { | ||
x: [d.x0, d.x1,], | ||
y: [d.y0, d.y1], | ||
}), | ||
) | ||
.filter((d) => d.height === 0); | ||
}; | ||
const name = (d) => { | ||
const { name } = d.data; | ||
return name.length > 5 ? name.slice(0, 4) + '...' : name; | ||
}; | ||
const chart = new G2.Chart({ | ||
width, | ||
height, | ||
paddingLeft: padding, | ||
paddingBottom: padding, | ||
paddingRight: padding, | ||
}); | ||
|
||
chart.data({ | ||
type: 'fetch', | ||
value: | ||
'https://gw.alipayobjects.com/os/bmw-prod/5155ef81-db23-49f3-b72b-d436a219d289.json', | ||
transform: [{ type: 'custom', callback: layout }], | ||
}); | ||
|
||
chart | ||
.rect() | ||
.encode('x', 'x') | ||
.encode('y', 'y') | ||
.encode('size', 'r') | ||
.encode('color', (d) => d.parent.data.name) | ||
.encode('tooltip', (d) => d.parent.data.name) | ||
.encode('title', '') | ||
.scale('x', { domain: [0, width], guide: null }) | ||
.scale('y', { domain: [0, height], guide: null, range: [0, 1] }) | ||
.scale('color', { | ||
field: '学派', | ||
guide: { size: 72, autoWrap: true, maxRows: 3, cols: 6 }, | ||
}) | ||
.scale('size', { type: 'identity' }) | ||
.scale('tooltip', { field: '流派' }); | ||
|
||
chart | ||
.text() | ||
.data({ | ||
transform: [ | ||
{ type: 'filterBy', fields: ['height'], callback: (d) => d === 0 }, | ||
], | ||
}) | ||
.encode('x', (d) => d.x[0]) | ||
.encode('y', (d) => d.y[0]) | ||
.encode('text', name) | ||
.style('dy', '15px') | ||
.style('dx', '5px') | ||
.style('fill', 'black') | ||
.style('textAnchor', 'start') | ||
.style('fontSize', 12); | ||
|
||
return chart.render().node(); | ||
})(); | ||
``` | ||
|
||
## Dependance | ||
|
||
```js | dom "pin: false" | ||
d3 = (async () => { | ||
const { voronoi } = await genji.require('d3-voronoi'); | ||
const { hierarchy, treemap } = await genji.require('d3-hierarchy'); | ||
return { voronoi, hierarchy, treemap }; | ||
})(); | ||
``` |
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
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,43 @@ | ||
import { MarkComponent as MC, Vector2 } from '../runtime'; | ||
import { RectGeometry } from '../spec'; | ||
import { | ||
baseGeometryChannels, | ||
basePostInference, | ||
basePreInference, | ||
} from './utils'; | ||
|
||
export type RectOptions = Omit<RectGeometry, 'type'>; | ||
|
||
export const Rect: MC<RectOptions> = () => { | ||
return (index, scale, value, coordinate) => { | ||
const { x: X, x1: X1, y: Y, y1: Y1 } = value; | ||
|
||
const P = Array.from(index, (i) => { | ||
const p1 = [+X[i], +Y[i]]; | ||
const p2 = [+X1[i], +Y[i]]; | ||
const p3 = [+X1[i], +Y1[i]]; | ||
const p4 = [+X[i], +Y1[i]]; | ||
|
||
return [p1, p2, p3, p4].map((d) => coordinate.map(d)) as Vector2[]; | ||
}); | ||
|
||
return [index, P]; | ||
}; | ||
}; | ||
|
||
Rect.props = { | ||
defaultShape: 'rect', | ||
defaultLabelShape: 'label', | ||
channels: [ | ||
...baseGeometryChannels(), | ||
{ name: 'x', required: true }, | ||
{ name: 'y', required: true }, | ||
], | ||
preInference: [...basePreInference(), { type: 'maybeZeroY1' }], | ||
postInference: [ | ||
...basePostInference(), | ||
{ type: 'maybeTitleX' }, | ||
{ type: 'maybeTooltipY' }, | ||
], | ||
shapes: ['rect', 'hollow'], | ||
}; |
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.