Skip to content

Commit fc7f0f3

Browse files
committed
feat: introduce new charts
- FacetChart - ViolinChart closes: #299
1 parent 24041b1 commit fc7f0f3

File tree

7 files changed

+115
-3
lines changed

7 files changed

+115
-3
lines changed

__tests__/plots/facet.spec.tsx

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { mount } from '@vue/test-utils'
2+
import FacetChart, { FacetChartProps } from '../../src/plots/facet'
3+
4+
const config: FacetChartProps = {
5+
padding: [0, 10, 10],
6+
appendPadding: [0, 0, 30, 20],
7+
type: 'rect',
8+
fields: ['cut'],
9+
cols: 3, // 超过3个换行
10+
data: [],
11+
axes: {},
12+
meta: {
13+
carat: {
14+
sync: true,
15+
},
16+
price: {
17+
sync: true,
18+
},
19+
clarity: {
20+
sync: true,
21+
},
22+
},
23+
eachView: (view, f) => {
24+
return {
25+
type: 'scatter',
26+
options: {
27+
data: f.data,
28+
xField: 'carat',
29+
yField: 'price',
30+
colorField: 'clarity',
31+
shape: 'circle',
32+
pointStyle: { fillOpacity: 0.3, stroke: null },
33+
},
34+
}
35+
},
36+
}
37+
38+
describe('FacetChart', () => {
39+
test('should render without crashed', () => {
40+
mount(() => <FacetChart {...config} />)
41+
})
42+
})

__tests__/plots/violin.spec.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { mount } from '@vue/test-utils'
2+
import ViolinChart, { ViolinChartProps } from '../../src/plots/violin'
3+
4+
const config: ViolinChartProps = {
5+
data: [],
6+
xField: 'a',
7+
yField: 'b',
8+
}
9+
10+
describe('ViolinChart', () => {
11+
test('should render without crashed', () => {
12+
mount(() => <ViolinChart {...config} />)
13+
})
14+
})

scripts/sync.ts

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { App, defineComponent } from 'vue-demi'
6969
import { ${chart}, ${chart}Options } from '@antv/g2plot'
7070
import BaseChart, { BaseChartProps } from '../../components/base'
7171
import { Writeable } from '../../types'
72+
import { mergeAttrs } from '../../utils'
7273
7374
export type ${chart}ChartProps = Writeable<Omit<BaseChartProps<${chart}Options>, 'chart'> &
7475
${chart}Options>

src/components/base.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ const BaseChart = defineComponent<
6262
mounted() {
6363
const chartRef = this.$attrs.chartRef as Ref<BasePlot<any> | null>
6464
const { chart: Chart } = this.attrConfig
65-
this.plot = new Chart(this.$el as HTMLElement, {
65+
const plot = new Chart(this.$el as HTMLElement, {
6666
data: this.chartData,
6767
...this.chartConfig,
6868
})
69-
this.plot.render()
69+
plot.render()
7070
if (chartRef) {
71-
chartRef.value = this.plot
71+
chartRef.value = plot
7272
}
73+
this.plot = plot
7374
},
7475
beforeUnmount() {
7576
/* istanbul ignore else */

src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ import { MultiViewChartProps as _MultiViewChartProps } from './plots/multi-view'
4545

4646
import { MixChartProps as _MixChartProps } from './plots/mix'
4747

48+
import { ViolinChartProps as _ViolinChartProps } from './plots/violin'
49+
50+
import { FacetChartProps as _FacetChartProps } from './plots/facet'
51+
4852
export { default as AreaChart } from './plots/area'
4953
export type AreaChartProps = _AreaChartProps
5054

@@ -129,3 +133,7 @@ export { default as MultiViewChart } from './plots/multi-view'
129133
export type MultiViewChartProps = _MultiViewChartProps
130134
export { default as MixChart } from './plots/mix'
131135
export type MixChartProps = _MixChartProps
136+
export { default as ViolinChart } from './plots/violin'
137+
export type ViolinChartProps = _ViolinChartProps
138+
export { default as FacetChart } from './plots/facet'
139+
export type FacetChartProps = _FacetChartProps

src/plots/facet/index.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { App, defineComponent } from 'vue-demi'
2+
import { Facet, FacetOptions } from '@antv/g2plot'
3+
import BaseChart, { BaseChartProps } from '../../components/base'
4+
import { Writeable } from '../../types'
5+
import { mergeAttrs } from '../../utils'
6+
7+
export type FacetChartProps = Writeable<
8+
Omit<BaseChartProps<FacetOptions>, 'chart'> & FacetOptions
9+
>
10+
11+
const FacetChart = defineComponent<FacetChartProps>({
12+
name: 'FacetChart',
13+
setup(props, ctx) {
14+
return () => <BaseChart chart={Facet} {...mergeAttrs(props, ctx.attrs)} />
15+
},
16+
})
17+
18+
/* istanbul ignore next */
19+
FacetChart.install = (app: App) => {
20+
app.component(FacetChart.name, FacetChart)
21+
}
22+
23+
export default FacetChart

src/plots/violin/index.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { App, defineComponent } from 'vue-demi'
2+
import { Violin, ViolinOptions } from '@antv/g2plot'
3+
import BaseChart, { BaseChartProps } from '../../components/base'
4+
import { Writeable } from '../../types'
5+
import { mergeAttrs } from '../../utils'
6+
7+
export type ViolinChartProps = Writeable<
8+
Omit<BaseChartProps<ViolinOptions>, 'chart'> & ViolinOptions
9+
>
10+
11+
const ViolinChart = defineComponent<ViolinChartProps>({
12+
name: 'ViolinChart',
13+
setup(props, ctx) {
14+
return () => <BaseChart chart={Violin} {...mergeAttrs(props, ctx.attrs)} />
15+
},
16+
})
17+
18+
/* istanbul ignore next */
19+
ViolinChart.install = (app: App) => {
20+
app.component(ViolinChart.name, ViolinChart)
21+
}
22+
23+
export default ViolinChart

0 commit comments

Comments
 (0)