Skip to content

Commit a4b9f66

Browse files
Mathieu2301MakiBMjppeltier
committed
Stable GraphicParser
Co-Authored-By: Bartek Makoś <[email protected]> Co-Authored-By: Jean-Pascal Peltier <[email protected]>
1 parent f71edcb commit a4b9f66

File tree

2 files changed

+209
-20
lines changed

2 files changed

+209
-20
lines changed

examples/GraphicIndicator.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ chart.setMarket('BINANCE:BTCEUR', {
1313
timeframe: 'W',
1414
});
1515

16-
// TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
17-
TradingView.getIndicator('USER;8bbd8017fd3e4881bf91f4fea5e3d538').then((indic) => {
16+
// TradingView.getIndicator('USER;01efac32df544348810bc843a7515f36').then((indic) => {
17+
TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
1818
const STD = new chart.Study(indic);
1919

2020
STD.onError((...err) => {
@@ -25,19 +25,10 @@ TradingView.getIndicator('USER;8bbd8017fd3e4881bf91f4fea5e3d538').then((indic) =
2525
console.log('STD Loaded !');
2626
});
2727

28-
STD.onUpdate((changes) => {
29-
// STD.graphic;
30-
console.log('Update:', changes);
28+
STD.onUpdate(() => {
29+
console.log(STD.graphic);
30+
// console.log('Tables:', changes, STD.graphic.tables);
31+
// console.log('Cells', STD.graphic.tables[0].cells());
32+
client.end();
3133
});
3234
});
33-
34-
setInterval(() => {
35-
chart.fetchMore(100);
36-
}, 2000);
37-
38-
setTimeout(() => {
39-
console.log('Setting timeframe to: \'D\'');
40-
chart.setSeries('D');
41-
}, 5000);
42-
43-
chart.onUpdate(() => console.log(chart.periods.length));

src/chart/graphicParser.js

Lines changed: 202 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,159 @@
1+
const TRANSLATOR = {
2+
/** @typedef {'right' | 'left' | 'both' | 'none'} ExtendValue */
3+
extend: {
4+
r: 'right',
5+
l: 'left',
6+
b: 'both',
7+
n: 'none',
8+
},
9+
10+
/** @typedef {'price' | 'abovebar' | 'belowbar'} yLocValue */
11+
yLoc: {
12+
pr: 'price',
13+
ab: 'abovebar',
14+
bl: 'belowbar',
15+
},
16+
17+
/**
18+
* @typedef {'none' | 'xcross' | 'cross' | 'triangleup'
19+
* | 'triangledown' | 'flag' | 'circle' | 'arrowup'
20+
* | 'arrowdown' | 'label_up' | 'label_down' | 'label_left'
21+
* | 'label_right' | 'label_lower_left' | 'label_lower_right'
22+
* | 'label_upper_left' | 'label_upper_right' | 'label_center'
23+
* | 'square' | 'diamond'
24+
* } LabelStyleValue
25+
* */
26+
labelStyle: {
27+
n: 'none',
28+
xcr: 'xcross',
29+
cr: 'cross',
30+
tup: 'triangleup',
31+
tdn: 'triangledown',
32+
flg: 'flag',
33+
cir: 'circle',
34+
aup: 'arrowup',
35+
adn: 'arrowdown',
36+
lup: 'label_up',
37+
ldn: 'label_down',
38+
llf: 'label_left',
39+
lrg: 'label_right',
40+
llwlf: 'label_lower_left',
41+
llwrg: 'label_lower_right',
42+
luplf: 'label_upper_left',
43+
luprg: 'label_upper_right',
44+
lcn: 'label_center',
45+
sq: 'square',
46+
dia: 'diamond',
47+
},
48+
49+
/**
50+
* @typedef {'solid' | 'dotted' | 'dashed'| 'arrow_left'
51+
* | 'arrow_right' | 'arrow_both'} LineStyleValue
52+
*/
53+
lineStyle: {
54+
sol: 'solid',
55+
dot: 'dotted',
56+
dsh: 'dashed',
57+
al: 'arrow_left',
58+
ar: 'arrow_right',
59+
ab: 'arrow_both',
60+
},
61+
62+
/** @typedef {'solid' | 'dotted' | 'dashed'} BoxStyleValue */
63+
boxStyle: {
64+
sol: 'solid',
65+
dot: 'dotted',
66+
dsh: 'dashed',
67+
},
68+
};
69+
70+
/**
71+
* @typedef {'auto' | 'huge' | 'large'
72+
* | 'normal' | 'small' | 'tiny'} SizeValue
73+
*/
74+
/** @typedef {'top' | 'center' | 'bottom'} VAlignValue */
75+
/** @typedef {'left' | 'center' | 'right'} HAlignValue */
76+
/** @typedef {'none' | 'auto'} TextWrapValue */
77+
/**
78+
* @typedef {'top_left' | 'top_center' | 'top_right'
79+
* | 'middle_left' | 'middle_center' | 'middle_right'
80+
* | 'bottom_left' | 'bottom_center' | 'bottom_right'
81+
* } TablePositionValue
82+
*/
83+
184
/**
285
* @typedef {Object} GraphicLabel
386
* @prop {number} id Drawing ID
87+
* @prop {number} x Label x position
88+
* @prop {number} y Label y position
89+
* @prop {yLocValue} yLoc yLoc mode
90+
* @prop {string} text Label text
91+
* @prop {LabelStyleValue} style Label style
92+
* @prop {number} color
93+
* @prop {number} textColor
94+
* @prop {SizeValue} size Label size
95+
* @prop {HAlignValue} textAlign Text horizontal align
96+
* @prop {string} toolTip Tooltip text
497
*/
598

699
/**
7100
* @typedef {Object} GraphicLine
8101
* @prop {number} id Drawing ID
102+
* @prop {number} x1 First x position
103+
* @prop {number} y1 First y position
104+
* @prop {number} x2 Second x position
105+
* @prop {number} y2 Second y position
106+
* @prop {ExtendValue} extend Horizontal extend
107+
* @prop {LineStyleValue} style Line style
108+
* @prop {number} color Line color
109+
* @prop {number} width Line width
9110
*/
10111

11112
/**
12113
* @typedef {Object} GraphicBox
13114
* @prop {number} id Drawing ID
115+
* @prop {number} x1 First x position
116+
* @prop {number} y1 First y position
117+
* @prop {number} x2 Second x position
118+
* @prop {number} y2 Second y position
119+
* @prop {number} color Box color
120+
* @prop {number} bgColor Background color
121+
* @prop {ExtendValue} extend Horizontal extend
122+
* @prop {BoxStyleValue} style Box style
123+
* @prop {number} width Box width
124+
* @prop {string} text Text
125+
* @prop {SizeValue} textSize Text size
126+
* @prop {number} textColor Text color
127+
* @prop {VAlignValue} textVAlign Text vertical align
128+
* @prop {HAlignValue} textHAlign Text horizontal align
129+
* @prop {TextWrapValue} textWrap Text wrap
130+
*/
131+
132+
/**
133+
* @typedef {Object} TableCell
134+
* @prop {number} id Drawing ID
135+
* @prop {string} text Cell text
136+
* @prop {number} width Cell width
137+
* @prop {number} height Cell height
138+
* @prop {number} textColor Text color
139+
* @prop {HAlignValue} textHAlign Text horizontal align
140+
* @prop {VAlignValue} textVAlign Text Vertical align
141+
* @prop {SizeValue} textSize Text size
142+
* @prop {number} bgColor Background color
14143
*/
15144

16145
/**
17146
* @typedef {Object} GraphicTable
18147
* @prop {number} id Drawing ID
148+
* @prop {TablePositionValue} position Table position
149+
* @prop {number} rows Number of rows
150+
* @prop {number} columns Number of columns
151+
* @prop {number} bgColor Background color
152+
* @prop {number} frameColor Frame color
153+
* @prop {number} frameWidth Frame width
154+
* @prop {number} borderColor Border color
155+
* @prop {number} borderWidth Border width
156+
* @prop {() => TableCell[][]} cells Table cells matrix
19157
*/
20158

21159
/**
@@ -70,19 +208,79 @@ module.exports = function graphicParse(rawGraphic = {}, indexes = []) {
70208
// console.log('indexes', indexes);
71209
return {
72210
labels: Object.values(rawGraphic.dwglabels ?? {}).map((l) => ({
73-
...l,
211+
id: l.id,
212+
x: indexes[l.x],
213+
y: l.y,
214+
yLoc: TRANSLATOR.yLoc[l.yl] ?? l.yl,
215+
text: l.t,
216+
style: TRANSLATOR.labelStyle[l.st] ?? l.st,
217+
color: l.ci,
218+
textColor: l.tci,
219+
size: l.sz,
220+
textAlign: l.ta,
221+
toolTip: l.tt,
74222
})),
75223

76224
lines: Object.values(rawGraphic.dwglines ?? {}).map((l) => ({
77-
...l,
225+
id: l.id,
226+
x1: indexes[l.x1],
227+
y1: l.y1,
228+
x2: indexes[l.x2],
229+
y2: l.y2,
230+
extend: TRANSLATOR.extend[l.ex] ?? l.ex,
231+
style: TRANSLATOR.lineStyle[l.st] ?? l.st,
232+
color: l.ci,
233+
width: l.w,
78234
})),
79235

80236
boxes: Object.values(rawGraphic.dwgboxes ?? {}).map((b) => ({
81-
...b,
237+
id: b.id,
238+
x1: indexes[b.x1],
239+
y1: b.y1,
240+
x2: indexes[b.x2],
241+
y2: b.y2,
242+
color: b.c,
243+
bgColor: b.bc,
244+
extend: TRANSLATOR.extend[b.ex] ?? b.ex,
245+
style: TRANSLATOR.boxStyle[b.st] ?? b.st,
246+
width: b.w,
247+
text: b.t,
248+
textSize: b.ts,
249+
textColor: b.tc,
250+
textVAlign: b.tva,
251+
textHAlign: b.tha,
252+
textWrap: b.tw,
82253
})),
83254

84255
tables: Object.values(rawGraphic.dwgtables ?? {}).map((t) => ({
85-
...t,
256+
id: t.id,
257+
position: t.pos,
258+
rows: t.rows,
259+
columns: t.cols,
260+
bgColor: t.bgc,
261+
frameColor: t.frmc,
262+
frameWidth: t.frmw,
263+
borderColor: t.brdc,
264+
borderWidth: t.brdw,
265+
cells: () => {
266+
const matrix = [];
267+
Object.values(rawGraphic.dwgtablecells ?? {}).forEach((cell) => {
268+
if (cell.tid !== t.id) return;
269+
if (!matrix[cell.row]) matrix[cell.row] = [];
270+
matrix[cell.row][cell.col] = {
271+
id: cell.id,
272+
text: cell.t,
273+
width: cell.w,
274+
height: cell.h,
275+
textColor: cell.tc,
276+
textHAlign: cell.tha,
277+
textVAlign: cell.tva,
278+
textSize: cell.ts,
279+
bgColor: cell.bgc,
280+
};
281+
});
282+
return matrix;
283+
},
86284
})),
87285

88286
horizLines: Object.values(rawGraphic.horizlines ?? {}).map((h) => ({

0 commit comments

Comments
 (0)