Skip to content

Commit

Permalink
Merge pull request #168 from chinyihan/virtual_formula_equation
Browse files Browse the repository at this point in the history
Implemented formula equation for virtual plugin
  • Loading branch information
nicolaisi authored Jul 4, 2024
2 parents 5f18d91 + 43b64c3 commit e607953
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion blueprint/designer.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
{% end %}

{% if data['style'][key]['type'] == "virtual" %}
<div id='{{ key }}' class='varbox' data-type="{{ data['style'][key]['type'] }}" data-virtual-id="{{ data['style'][key]['div']['data-virtual-id'] }}" data-decimal-numbers="{{ data['style'][key]['div']['data-decimal-numbers'] }}" data-smaller-than="{{ data['style'][key]['div']['data-smaller-than'] }}" data-larger-than="{{ data['style'][key]['div']['data-larger-than'] }}" data-exp="{{ data['style'][key]['div']['data-exp'] }}" data-link-adei="{{ data['style'][key]['div']['data-link-adei'] }}" data-trend="{{ data['style'][key]['div']['data-trend'] }}" style='position: absolute; top: {{ data['style'][key]['top'] }};
<div id='{{ key }}' class='varbox' data-type="{{ data['style'][key]['type'] }}" data-formula="{{ data['style'][key]['div']['data-formula'] }}" data-virtual-id="{{ data['style'][key]['div']['data-virtual-id'] }}" data-decimal-numbers="{{ data['style'][key]['div']['data-decimal-numbers'] }}" data-smaller-than="{{ data['style'][key]['div']['data-smaller-than'] }}" data-larger-than="{{ data['style'][key]['div']['data-larger-than'] }}" data-exp="{{ data['style'][key]['div']['data-exp'] }}" data-link-adei="{{ data['style'][key]['div']['data-link-adei'] }}" data-trend="{{ data['style'][key]['div']['data-trend'] }}" style='position: absolute; top: {{ data['style'][key]['top'] }};
left:{{ data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px; height:{{ data['style'][key]['height'] }}px; font-size: {{ data['style'][key]['div']['font-size'] }}; font-weight: {{ data['style'][key]['div']['font-weight'] }};'>
<span class='text_name'>{{ data['style'][key]['div']['text-name'] }}</span> <span class='value'>XXX.XX</span> <span class='unit_name'>{{ data['style'][key]['div']['unit-name'] }}</span>
</div>
Expand Down
80 changes: 65 additions & 15 deletions js_plugins/virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,88 @@ function parse_virtual(id, response) {
var data_smaller_than = $("#" + id).attr("data-smaller-than");
var data_larger_than = $("#" + id).attr("data-larger-than");
var data_exp = $("#" + id).attr("data-exp");
var data_formula = $("#" + id).attr("data-formula");


var delta = (currentTimeMillis - timestamp) / 1000.0;
// since delta is converted to seconds, we need to convert invalid to seconds as well
invalid = invalid / 1000.0;


if (data_decimal_numbers != undefined && data_decimal_numbers > 0) {
value = (Math.round(value * 100) / 100).toFixed(data_decimal_numbers);
var myformula = data_formula.trim();
var data = myformula;
var re= /\[(.*?)\]/g;
var buffer = []
for(m = re.exec(data); m; m = re.exec(data)){
buffer.push(m[1]);
}

if (data_smaller_than != undefined && value < data_smaller_than) {
$("#" + id + "> .value").css('color', 'red');
$("#" + id + "> .unit-name").css('color', 'red');
} else {
$("#" + id + "> .value").css('color', 'green');
$("#" + id + "> .unit-name").css('color', 'green');
}
var finalstring = "";
var pattern = /\[(.*?)\]/g;
var teststring = myformula.split(/[\[\]]+/);

if (data_larger_than != undefined && value > data_larger_than) {
$("#" + id + "> .value").css('color', 'red');
$("#" + id + "> .unit-name").css('color', 'red');
} else {
$("#" + id + "> .value").css('color', 'green');
$("#" + id + "> .unit-name").css('color', 'green');
for (var i = 0; i < teststring.length; i++) {
var val = "";
if (teststring[i] in response) {
val = response[teststring[i]]["value"];
} else {
val = teststring[i];
}
finalstring += val;
finalstring += " ";
}

finalstring = finalstring.trim();
console.log("(line code delay) suppress error from math.js");
console.log("(line code delay) suppress error from math.js");

var value = math.round(math.eval(finalstring),parseInt(data_decimal_numbers));


if (data_smaller_than != undefined && data_larger_than != undefined) {

if (value > data_smaller_than && value < data_larger_than) {
$("#" + key + "> .value").css('color', 'green');
$("#" + key + "> .unit-name").css('color', 'green');
} else if (data_smaller_than == '' && data_larger_than == '') {
$("#" + key + "> .value").css('color', 'green');
$("#" + key + "> .unit-name").css('color', 'green');
} else if (value < data_smaller_than && data_larger_than == '') {
$("#" + key + "> .value").css('color', 'red');
$("#" + key + "> .unit-name").css('color', 'red');
} else if (value > data_smaller_than && data_larger_than == '') {
$("#" + key + "> .value").css('color', 'green');
$("#" + key + "> .unit-name").css('color', 'green');
} else if (value > data_larger_than && data_smaller_than == '') {
$("#" + key + "> .value").css('color', 'red');
$("#" + key + "> .unit-name").css('color', 'red');
} else if (value < data_larger_than && data_smaller_than == '') {
$("#" + key + "> .value").css('color', 'green');
$("#" + key + "> .unit-name").css('color', 'green');
} else {
// value < data_smaller_than && value > data_larger_than
$("#" + key + "> .value").css('color', 'red');
$("#" + key + "> .unit-name").css('color', 'red');
}
}


if (data_exp != undefined && data_exp == "true") {
if (data_decimal_numbers != undefined && data_decimal_numbers > 0) {
value = parseFloat(value).toExponential(data_decimal_numbers);
} else {
value = parseFloat(value).toExponential();
}
} else {
// when data_exp == false
if (data_decimal_numbers != undefined && data_decimal_numbers >= 0) {
if (value >= 0) {
value = (Math.round(value * 100) / 100).toFixed(data_decimal_numbers);
} else {
// negative value
value = ((value * 100) / 100).toFixed(data_decimal_numbers);
}
}
}

if (invalid != undefined && invalid < delta) {
Expand Down
1 change: 1 addition & 0 deletions template/virtual.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
tooltip=""
data-virtual-id="{{ data['style'][key]['div']['data-virtual-id'] }}"
data-type="{{ data['style'][key]['type'] }}"
data-formula="{{ data['style'][key]['div']['data-formula'] }}"
data-decimal-numbers="{{ data['style'][key]['div']['data-decimal-numbers'] }}"
data-smaller-than="{{ data['style'][key]['div']['data-smaller-than'] }}"
data-larger-than="{{ data['style'][key]['div']['data-larger-than'] }}"
Expand Down
5 changes: 5 additions & 0 deletions typedef/virtual.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"type": "string"
"optional": "Yes"
"data": "No"
"formula":
"value": ""
"type": "string"
"optional": "Yes"
"data": "Yes"
"virtual_id":
"value": ""
"type": "string"
Expand Down

0 comments on commit e607953

Please sign in to comment.