-
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.
* fix: stock股票图重写changeData方法 * fix: 股票图处理数据方法抽离为公共方法 * fix: 添加股票图获取数据方法的单测 * fix: 完善股票图changeData单测 * fix: 完善股票图data的单测 Co-authored-by: 沈建斌 <wb-sjb709429@antfin.com>
- Loading branch information
1 parent
79dc60b
commit ed08107
Showing
5 changed files
with
163 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { pick } from '@antv/util'; | ||
import { Stock } from '../../../../src'; | ||
import { kdata, SH000001 } from '../../../data/stock'; | ||
import { createDiv } from '../../../utils/dom'; | ||
|
||
describe('stock', () => { | ||
it('change data', () => { | ||
const stock = new Stock(createDiv(), { | ||
width: 400, | ||
height: 300, | ||
data: kdata, | ||
xField: 'date', | ||
yField: ['start', 'end', 'max', 'min'], | ||
meta: { | ||
date: { | ||
mask: 'YYYY', | ||
}, | ||
}, | ||
}); | ||
|
||
stock.render(); | ||
|
||
expect(stock.chart.geometries[0].elements.length).toEqual(kdata.length); | ||
|
||
const newData = [ | ||
...kdata, | ||
{ date: '2015-10-25', start: 8, max: 8.54, min: 4.99, end: 4.99, volumn: 2769.31, money: 19337.44 }, | ||
]; | ||
stock.changeData(newData); | ||
expect(stock.chart.geometries[0].elements.length).toEqual(kdata.length + 1); | ||
// 添加的数据 | ||
const addData = stock.chart.geometries[0].elements[kdata.length].getData(); | ||
expect(addData.date).toEqual('2015-10-25'); | ||
expect(stock.options.data).toEqual(newData); | ||
stock.destroy(); | ||
}); | ||
}); |
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,93 @@ | ||
import { kdata } from '../../../data/stock'; | ||
import { getStockData } from '../../../../src/plots/stock/utils'; | ||
import { TREND_FIELD, TREND_UP, TREND_DOWN, Y_FIELD } from '../../../../src/plots/stock/constant'; | ||
|
||
describe('stock data', () => { | ||
it('stock data', () => { | ||
const yField = ['start', 'end', 'max', 'min']; | ||
const originalData = [ | ||
{ date: '2015-11-19', start: 8.18, max: 8.33, min: 7.98, end: 8.32, volumn: 1810, money: 14723.56 }, | ||
{ date: '2015-11-18', start: 8.37, max: 8.6, min: 8.03, end: 8.09, volumn: 2790.37, money: 23309.19 }, | ||
{ date: '2015-11-17', start: 8.7, max: 8.78, min: 8.32, end: 8.37, volumn: 3729.04, money: 31709.71 }, | ||
]; | ||
const data = getStockData(originalData, yField); | ||
expect(data.length).toEqual(originalData.length); | ||
expect(data).toEqual([ | ||
{ | ||
date: '2015-11-19', | ||
start: 8.18, | ||
max: 8.33, | ||
min: 7.98, | ||
end: 8.32, | ||
volumn: 1810, | ||
money: 14723.56, | ||
[TREND_FIELD]: TREND_UP, | ||
[Y_FIELD]: [8.18, 8.32, 8.33, 7.98], | ||
}, | ||
{ | ||
date: '2015-11-18', | ||
start: 8.37, | ||
max: 8.6, | ||
min: 8.03, | ||
end: 8.09, | ||
volumn: 2790.37, | ||
money: 23309.19, | ||
[TREND_FIELD]: TREND_DOWN, | ||
[Y_FIELD]: [8.37, 8.09, 8.6, 8.03], | ||
}, | ||
{ | ||
date: '2015-11-17', | ||
start: 8.7, | ||
max: 8.78, | ||
min: 8.32, | ||
end: 8.37, | ||
volumn: 3729.04, | ||
money: 31709.71, | ||
[TREND_FIELD]: TREND_DOWN, | ||
[Y_FIELD]: [8.7, 8.37, 8.78, 8.32], | ||
}, | ||
]); | ||
}); | ||
|
||
it('start = end', () => { | ||
const yField = ['start', 'end', 'max', 'min']; | ||
const originalData = [ | ||
{ date: '2015-11-19', start: 8.18, max: 8.33, min: 7.98, end: 8.18, volumn: 1810, money: 14723.56 }, | ||
]; | ||
const data = getStockData(originalData, yField); | ||
expect(data).toEqual([ | ||
{ | ||
date: '2015-11-19', | ||
start: 8.18, | ||
max: 8.33, | ||
min: 7.98, | ||
end: 8.18, | ||
volumn: 1810, | ||
money: 14723.56, | ||
[TREND_FIELD]: TREND_UP, | ||
[Y_FIELD]: [8.18, 8.18, 8.33, 7.98], | ||
}, | ||
]); | ||
}); | ||
|
||
it('contain invalid value', () => { | ||
const yField = ['start', 'end', 'max', 'min']; | ||
const originalData = [ | ||
{ date: '2015-11-19', start: undefined, max: 8.33, min: 7.98, end: null, volumn: 1810, money: 14723.56 }, | ||
]; | ||
const data = getStockData(originalData, yField); | ||
expect(data).toEqual([ | ||
{ | ||
date: '2015-11-19', | ||
start: undefined, | ||
max: 8.33, | ||
min: 7.98, | ||
end: null, | ||
volumn: 1810, | ||
money: 14723.56, | ||
[TREND_FIELD]: TREND_DOWN, | ||
[Y_FIELD]: [undefined, null, 8.33, 7.98], | ||
}, | ||
]); | ||
}); | ||
}); |
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,18 @@ | ||
import { isArray, map } from '@antv/util'; | ||
import { TREND_FIELD, TREND_DOWN, TREND_UP, Y_FIELD } from './constant'; | ||
|
||
/** | ||
* @desc 股票图数据处理 | ||
* @param data | ||
* @param yField | ||
*/ | ||
export function getStockData(data, yField) { | ||
return map(data, (obj) => { | ||
if (isArray(yField)) { | ||
const [open, close, high, low] = yField; | ||
obj[TREND_FIELD] = obj[open] <= obj[close] ? TREND_UP : TREND_DOWN; | ||
obj[Y_FIELD] = [obj[open], obj[close], obj[high], obj[low]]; | ||
} | ||
return obj; | ||
}); | ||
} |