-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtradebox.user.js
153 lines (150 loc) · 5.8 KB
/
tradebox.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// ==UserScript==
// @name LSViewer
// @namespace https://yiyunzhi.github.io/
// @version 0.1.2
// @description Extend the page chart and infomation on the page of ls-x.de
// @author GF Zhang
// @include *ls-x.de/de/tradebox*
// @supportURL https://github.com/yiyunzhi/lsviewer
// @updateURL https://github.com/yiyunzhi/lsviewer/raw/master/lsviewer/tradebox.user.js
// @downloadURL https://raw.githubusercontent.com/yiyunzhi/lsviewer/master/tradebox.user.js
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @require https://code.highcharts.com/highcharts.js
// @require https://code.highcharts.com/modules/heatmap.js
// @require https://code.highcharts.com/modules/treemap.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_setClipboard
// @grant GM_addStyle
// @grant GM_getResourceText
// @run-at document-end
// @noframes
// ==/UserScript==
(function() {
'use strict';
var $ = jQuery.noConflict();
'use strict';
// template used to insert
var _template='<div class="row">'+
'<div class="col-md-12">'+
'<div id="treeMapContainer" style="width:100%; height:400px;"></div>'+
'<h4 id="updateDate" style="text-align: center"></h4></div></div>';
var _treemapParentElm=$('div#page_content> .mpe_container> .mpe_bootstrapgrid');
var _tradeTable=$('table[data-type="trades"]');
_treemapParentElm.prepend(_template);
// only 10 items be plotted
const _topMaxLength = 10;
var _topCollection = [];
var _lastMinVlaue = 0;
// initial highchart
var myChart = Highcharts.chart('treeMapContainer', {
colorAxis: {
minColor: '#FFFFDF',
maxColor: Highcharts.getOptions().colors[0]
},
chart: {
animation: false
},
series: [{
type: 'treemap',
animation: false,
layoutAlgorithm: 'squarified',
data: []
}],
plotOptions: {
treemap: {
animation: false,
tooltip: {
headerFormat: '<b>{point.name}</b><br>',
pointFormat: '<b>{point.name}</b><br>{point.volume} Stk.,{point.price} Euro, Ingst: {point.value} Euro'
},
dataLabels: {
crop: true,
formatter: function () {
return '<b style="font-size: 16px">' + this.point.name + '</b> ' +
'<br/>' +
'<b>Preise: ' + this.point.price + ' €</b> ' +
'<br/>' +
'<b>Kurse: ' + this.point.volume + ' Stk.</b> ' +
'<br/>' +
'<b>Beitrag: ' + this.point.value + ' €</b>';
},
color: 'red'
}
}
},
title: {
text: 'Trade Treemap'
}
});
// function used to sort
function dsc_compare(property) {
return function (obj1, obj2) {
var value1 = obj1[property];
var value2 = obj2[property];
return value1 - value2;
}
}
// function used to part sort of collection
function part_sort(array, part_idx) {
return Array.prototype.concat(
array.slice(0, part_idx).sort(dsc_compare('value', true)),
array.slice(part_idx))
}
//function to update the dataCollection of myChart
var updateTopCollection = function (obj) {
var _idx = _topCollection.findIndex(function (o) {
return o.name === obj.name;
});
if (_idx != -1) {
_topCollection[_idx].value += obj.value;
_topCollection[_idx].volume += obj.volume;
_topCollection[_idx].price = obj.price;
_topCollection = part_sort(_topCollection, _idx);
} else {
_topCollection.push(obj);
if (obj.value > _lastMinVlaue) {
_topCollection.sort(dsc_compare('value', true));
}
}
_lastMinVlaue = _topCollection[0].value;
};
//function to update the DataSeries of myChart
var funcUpdateDataSeries = function () {
var _series = myChart.series[0];
var _iteration_max = _topCollection.length >= _topMaxLength ? _topMaxLength : _topCollection.length;
var _tops = _topCollection.slice(-1 * _iteration_max);
for (var i = 0; i < _iteration_max; i++) {
var _p = {
name: _tops[i].name,
value: _tops[i].value,
price: _tops[i].price,
volume: _tops[i].volume,
colorValue: _iteration_max - i
};
if (_series.data[i]) {
_series.data[i].update(_p);
} else {
_series.addPoint(_p);
}
}
};
// function to parse the price string that with currency symbol in it.
var funcParsePriceString = function (s) {
return parseFloat(s.replace('.', '').replace(',', '.').replace('€', ''));
};
// bind the DOMNodeInserted and handle the update of the topCollection and the chart
_tradeTable.on('DOMNodeInserted', function (e) {
var array = $(this).find("tbody tr").eq(1).children().toArray();
var _obj = {
date: array.pop().innerText,
volume: parseInt(array.pop().innerText),
price: funcParsePriceString(array.pop().innerText),
name: array.pop().innerText
};
_obj.value = parseFloat(_obj.volume * _obj.price.toFixed(3));
updateTopCollection(_obj);
funcUpdateDataSeries();
$('#updateDate').text(_obj.date);
});
})();