-
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.
- Loading branch information
Showing
15 changed files
with
261 additions
and
18 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,45 @@ | ||
import { csv } from 'd3-fetch'; | ||
import { autoType } from 'd3-dsv'; | ||
import { rollup } from 'd3-array'; | ||
import { G2Spec } from '../../../src'; | ||
|
||
export async function barleyLineTrail(): Promise<G2Spec> { | ||
const data = await csv('data/barley.csv', autoType); | ||
const key = (d) => `${d.site},${d.variety}`; | ||
const keyDelta = rollup( | ||
data, | ||
([a, b]) => { | ||
if (b.year < a.year) [a, b] = [b, a]; | ||
return b.yield - a.yield; | ||
}, | ||
key, | ||
); | ||
return { | ||
type: 'facetRect', | ||
data, | ||
paddingLeft: 120, | ||
paddingBottom: 100, | ||
encode: { x: 'site' }, | ||
children: [ | ||
{ | ||
type: 'line', | ||
frame: false, | ||
encode: { | ||
x: (d) => `${d.year}`, | ||
y: 'variety', | ||
series: 'variety', | ||
color: (d) => keyDelta.get(key(d)), | ||
size: 'yield', | ||
}, | ||
tooltip: { title: '', items: [{ field: 'year' }, { field: 'yield' }] }, | ||
legend: { size: false, color: { title: 'yield delta' } }, | ||
scale: { | ||
size: { range: [0, 12] }, | ||
color: { palette: 'rdBu' }, | ||
}, | ||
style: { shape: 'trail' }, | ||
interaction: { tooltip: { series: false } }, | ||
}, | ||
], | ||
}; | ||
} |
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,19 @@ | ||
import { G2Spec } from '../../../src'; | ||
|
||
export function stocksLineVarSize(): G2Spec { | ||
return { | ||
type: 'line', | ||
data: { | ||
type: 'fetch', | ||
value: 'data/stocks.csv', | ||
}, | ||
encode: { | ||
x: (d) => new Date(d.date), | ||
y: 'price', | ||
color: 'symbol', | ||
size: 'price', | ||
}, | ||
legend: { size: false }, | ||
style: { shape: 'trail' }, | ||
}; | ||
} |
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,45 @@ | ||
/** | ||
* A recreation of this demo: https://vega.github.io/vega-lite/examples/trail_comet.html | ||
*/ | ||
import { Chart } from '@antv/g2'; | ||
import { rollup } from 'd3-array'; | ||
|
||
fetch('https://assets.antv.antgroup.com/g2/barley.json') | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
const key = (d) => `${d.site},${d.variety}`; | ||
const keyDelta = rollup( | ||
data, | ||
([a, b]) => { | ||
if (b.year < a.year) [a, b] = [b, a]; | ||
return b.yield - a.yield; | ||
}, | ||
key, | ||
); | ||
|
||
const chart = new Chart({ | ||
container: 'container', | ||
theme: 'classic', | ||
paddingLeft: 120, | ||
paddingBottom: 100, | ||
}); | ||
|
||
const facet = chart.facetRect().data(data).encode('x', 'site'); | ||
|
||
facet | ||
.line() | ||
.encode('x', (d) => `${d.year}`) | ||
.encode('y', 'variety') | ||
.encode('series', 'variety') | ||
.encode('color', (d) => keyDelta.get(key(d))) | ||
.encode('size', 'yield') | ||
.tooltip({ title: '', items: [{ field: 'year' }, { field: 'yield' }] }) | ||
.scale('size', { range: [0, 12] }) | ||
.scale('color', { palette: 'rdBu' }) | ||
.style('shape', 'trail') | ||
.legend('color', { title: 'yield delta' }) | ||
.attr('frame', false) | ||
.interaction('tooltip', { series: false }); | ||
|
||
chart.render(); | ||
}); |
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,26 @@ | ||
/** | ||
* A recreation of this demo: https://vega.github.io/vega-lite/examples/trail_color.html | ||
*/ | ||
import { Chart } from '@antv/g2'; | ||
|
||
const chart = new Chart({ | ||
container: 'container', | ||
theme: 'classic', | ||
autoFit: true, | ||
}); | ||
|
||
chart | ||
.line() | ||
.data({ | ||
type: 'fetch', | ||
value: | ||
'https://gw.alipayobjects.com/os/bmw-prod/cb99c4ab-e0a3-4c76-9586-fe7fa2ff1a8c.csv', | ||
}) | ||
.encode('x', (d) => new Date(d.date)) | ||
.encode('y', 'price') | ||
.encode('color', 'symbol') | ||
.encode('size', 'price') | ||
.legend('size', false) | ||
.style('shape', 'trail'); | ||
|
||
chart.render(); |
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,80 @@ | ||
import { path as d3path } from 'd3-path'; | ||
import { Path } from '@antv/g'; | ||
import { ShapeComponent as SC } from '../../runtime'; | ||
import { select } from '../../utils/selection'; | ||
import { applyStyle, getShapeTheme } from '../utils'; | ||
import { angle, dist, sub, add, Vector2 } from '../../utils/vector'; | ||
import { Curve } from './curve'; | ||
|
||
/** | ||
* | ||
* x9-x0---------x1-x2 | ||
* / |r1 |r2 \ | ||
*x8---p0---------p1---x3 | ||
* \ r4| | r3 / | ||
* x7-x6--------x5-x4 | ||
*/ | ||
function stroke(path, p0, p1, s0, s1) { | ||
const v = sub(p1, p0); | ||
const a = angle(v); | ||
const a1 = a + Math.PI / 2; | ||
const r1: Vector2 = [(s0 / 2) * Math.cos(a1), (s0 / 2) * Math.sin(a1)]; | ||
const r2: Vector2 = [(s1 / 2) * Math.cos(a1), (s1 / 2) * Math.sin(a1)]; | ||
const r3: Vector2 = [(s1 / 2) * Math.cos(a), (s1 / 2) * Math.sin(a)]; | ||
const r4: Vector2 = [(s0 / 2) * Math.cos(a), (s0 / 2) * Math.sin(a)]; | ||
const x0 = add(p0, r1); | ||
const x1 = add(p1, r2); | ||
const x2 = add(x1, r3); | ||
const x3 = add(p1, r3); | ||
const x4 = sub(x3, r2); | ||
const x5 = sub(p1, r2); | ||
const x6 = sub(p0, r1); | ||
const x7 = sub(x6, r4); | ||
const x8 = sub(p0, r4); | ||
const x9 = sub(x0, r4); | ||
|
||
path.moveTo(...x0); | ||
path.lineTo(...x1); | ||
path.arcTo(...x2, ...x3, s1 / 2); | ||
path.arcTo(...x4, ...x5, s1 / 2); | ||
path.lineTo(...x6); | ||
path.arcTo(...x7, ...x8, s0 / 2); | ||
path.arcTo(...x9, ...x0, s0 / 2); | ||
path.closePath(); | ||
} | ||
|
||
export type TrailOptions = Record<string, any>; | ||
|
||
export const Trail: SC<TrailOptions> = (options) => { | ||
return (P, value, coordinate, theme) => { | ||
const { mark, shape, defaultShape, seriesSize, color } = value; | ||
const { defaultColor, ...defaults } = getShapeTheme( | ||
theme, | ||
mark, | ||
shape, | ||
defaultShape, | ||
); | ||
|
||
const path = d3path(); | ||
|
||
for (let i = 0; i < P.length - 1; i++) { | ||
const p0 = P[i]; | ||
const p1 = P[i + 1]; | ||
const s0 = seriesSize[i]; | ||
const s1 = seriesSize[i + 1]; | ||
stroke(path, p0, p1, s0, s1); | ||
} | ||
|
||
return select(new Path({})) | ||
.call(applyStyle, defaults) | ||
.style('fill', color || defaultColor) | ||
.style('d', path.toString()) | ||
.call(applyStyle, options) | ||
.node(); | ||
}; | ||
}; | ||
|
||
Trail.props = { | ||
...Curve.props, | ||
defaultMarker: 'line', | ||
}; |
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