-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(line): add code example by line (#1255)
- Loading branch information
Showing
10 changed files
with
143 additions
and
39 deletions.
There are no files selected for viewing
File renamed without changes.
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,35 @@ | ||
import { Line } from '../../../../src'; | ||
import { partySupport } from '../../../data/party-support'; | ||
import { createDiv } from '../../../utils/dom'; | ||
|
||
describe('line', () => { | ||
it('x*y', () => { | ||
const line = new Line(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data: partySupport.filter((o) => o.type === 'FF'), | ||
x: 'date', | ||
y: 'value', | ||
}); | ||
|
||
line.render(); | ||
expect(line.chart).toBeDefined(); | ||
setTimeout(() => { | ||
line.render(); | ||
}, 1000); | ||
}); | ||
|
||
it('x*y with color', () => { | ||
const line = new Line(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data: partySupport, | ||
x: 'date', | ||
y: 'value', | ||
color: 'type', | ||
}); | ||
|
||
line.render(); | ||
expect(line.chart).toBeDefined(); | ||
}); | ||
}); |
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,10 +1,23 @@ | ||
export const createDiv = (id?: string): HTMLDivElement => { | ||
const canvasDiv = document.createElement('div'); | ||
canvasDiv.style.width = '600px'; | ||
canvasDiv.style.height = '600px'; | ||
canvasDiv.style.left = '30px'; | ||
canvasDiv.style.top = '30px'; | ||
canvasDiv.id = id; | ||
document.body.appendChild(canvasDiv); | ||
return canvasDiv; | ||
}; | ||
/** | ||
* 创建一个 div 节点,并放到 container,默认放到 body 上 | ||
* @param container | ||
*/ | ||
export function createDiv(container: HTMLElement = document.body): HTMLElement { | ||
const div = document.createElement('div'); | ||
|
||
container.appendChild(div); | ||
|
||
return div; | ||
} | ||
|
||
/** | ||
* 移除 dom 元素 | ||
* @param dom | ||
*/ | ||
export function removeDom(dom: HTMLElement) { | ||
const parent = dom.parentNode; | ||
|
||
if (parent) { | ||
parent.removeChild(dom); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import { Chart } from '@antv/g2'; | ||
import { Options } from '../types'; | ||
|
||
/** | ||
* schema 转 G2 的适配器基类 | ||
*/ | ||
export abstract class Adaptor<O extends Options> { | ||
public convent(options: O) {} | ||
/** | ||
* 将 G2Plot 配置转换成 G2 的逻辑操作 | ||
* @param chart | ||
* @param options | ||
*/ | ||
public abstract convent(chart: Chart, options: O); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,20 @@ | ||
import { Adaptor } from '../../core/adaptor'; | ||
import { LineOptions } from './types'; | ||
import { Chart } from '@antv/g2'; | ||
|
||
export class LineAdaptor extends Adaptor<LineOptions> {} | ||
/** | ||
* 折线图适配器 | ||
*/ | ||
export class LineAdaptor extends Adaptor<LineOptions> { | ||
public convent(chart: Chart, options: LineOptions) { | ||
const { data, x, y, color } = options; | ||
|
||
// TODO 具体的折线图操作逻辑 | ||
chart.data(data); | ||
const geometry = chart.line().position(`${x}*${y}`); | ||
|
||
if (color) { | ||
geometry.color(color); | ||
} | ||
} | ||
} |
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,11 +1,16 @@ | ||
import { Plot } from '../../core/plot'; | ||
import { LineOptions } from './types'; | ||
import { LineAdaptor } from './adaptor'; | ||
import { Adaptor } from '../../core/adaptor'; | ||
|
||
export class Line extends Plot<LineOptions> { | ||
/** 图表类型 */ | ||
public type: string = 'line'; | ||
|
||
protected getSchemaAdaptator(): LineAdaptor { | ||
return; | ||
/** | ||
* 获取 折线图 的适配器 | ||
*/ | ||
protected getSchemaAdaptor(): Adaptor<LineOptions> { | ||
return new LineAdaptor(); | ||
} | ||
} |
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,3 +1,7 @@ | ||
import { Options } from '../../types'; | ||
|
||
export interface LineOptions extends Options {} | ||
export interface LineOptions extends Options { | ||
x: string; | ||
y: string; | ||
color?: string; | ||
} |
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,7 +1,7 @@ | ||
export type Options = { | ||
// TODO | ||
}; | ||
|
||
export type Adaptor = { | ||
// TODO | ||
readonly width: number; | ||
readonly height: number; | ||
readonly autoFit?: boolean; | ||
readonly padding?: number[] | 'auto'; | ||
readonly data: Record<string, any>[]; | ||
}; |