I have just been doing JavaScript and HTML programming for a couple of months so I am still learning. However, I dont like like the fact that plotly.js uses curly brackets like this:
trace[1] = { x: xx, y: yy[1], type: 'scatter' };
My understanding so far is that in JavaScript curly brackets are for JavaScript functions and
regular brackets [ ] are for JavaScript Arrays. I therefore think plotly.js should use regular
brackets like this:
trace[1] = [ x: xx, y: yy[1], type: 'scatter' ];
That would make things a lot simpler. I have been wasting the my whole day because of the curly barackets.
My below plotly.js code has an error message:
"plotJS.js:106:5 TypeError: cannot use ‘in’ operator to search for ‘bardir’ in ‘{x:Time-1,Time-2…’ "
The foor loop that is causing the problem is this one:
// JavaScript for loop not working
for (var j = 1; j <= Nc; j++) {
trace[j] = “{” + “x:” + xx + “,” + “y:” + yy[j] + “,” + “type:” + ‘scatter’ + “}”;
}
If I comment out that loop and hard code it like this:
trace[1] = { x: xx, y: yy[1], type: 'scatter' };
trace[2] = { x: xx, y: yy[2], type: 'scatter' };
trace[3] = { x: xx, y: yy[3], type: 'scatter' };
trace[4] = { x: xx, y: yy[4], type: 'scatter' };
trace[5] = { x: xx, y: yy[5], type: 'scatter' };
Then the plotly.js plot is generated sucessfully but I dont want to hard code it like that because
I want to make the code more generic so I can simulate and plot 100 pure unit roots without
having to type in all those trace variables.
My HTML code ( Plot HTML.html ):
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="plotJS.js"></script>
<meta charset="utf-8" />
<style>
table {
border-collapse: collapse;
text-align: center;
}
table,
td,
th {
border: 1px solid black;
}
</style>
</head>
<body>
<div id="div1"> </div>
<div id="div2"> </div>
<script>
var data = RandomWalk(10, 5);
htmlTable(data);
Plot(data);
</script>
</body>
My JavaScript code ( plotJS.js ) :
function GetRandom(min, max) {
var x = Math.random() * (max - min) + min;
return x;
}
function ArraySelect(DataArray, row_start, column) {
var Nr = DataArray.length;
var Nc = DataArray[1].length;
var D = [];
for (var i = row_start; i < Nr; i++) {
D.push(DataArray[i][column]);
}
return D;
}
function RandomWalk(rows, cols) {
var arr = [];
for (var i = 0; i < rows + 1; i++) {
arr.push([]);
arr[i].push(new Array(cols));
if (i == 1) {
for (var j = 1; j <= cols; j++) {
arr[1][j] = 100;
}
}
else if (i > 1) {
for (var j = 1; j <= cols; j++) {
arr[i][j] = arr[i - 1][j] + GetRandom(-1, +1);
}
}
else {
for (var j = 1; j <= cols; j++) {
arr[i][j] = "Random Walk " + j;
}
}
}
for (var i = 1; i <= rows; i++) {
arr[i][0] = "Time-" + i;
}
arr[0][0] = "2D Array";
console.log(arr);
return arr;
}
function htmlTable(d) {
var data = d;
var html = '<table><thead><tr></tr></thead><tbody>';
for (var i = 0; i < data.length; i++) {
html += '<tr>';
for (var j = 0; j < data[i].length; ++j) {
html += '<td>' + data[i][j] + '</td>';
}
html += "</tr>";
}
$(html).appendTo('#div1');
}
function Plot(DD) {
console.log("DD = " + DD);
var xx = ArraySelect(DD, 1, 0);
console.log("xx = " + xx);
var Nc = DD[1].length - 1;
console.log("Nc = " + Nc);
var yy = [];
for (var j = 1; j <= Nc; j++) {
yy[j] = ArraySelect(DD, 1, j);
}
console.log("yy1 =" + yy[1]);
console.log("yy2 =" + yy[2]);
var trace = [];
// JavaScript for loop not working
for (var j = 1; j <= Nc; j++) {
trace[j] = "{" + "x:" + xx + "," + "y:" + yy[j] + "," + "type:" + 'scatter' + "}";
}
/*
trace[1] = { x: xx, y: yy[1], type: 'scatter' };
trace[2] = { x: xx, y: yy[2], type: 'scatter' };
trace[3] = { x: xx, y: yy[3], type: 'scatter' };
trace[4] = { x: xx, y: yy[4], type: 'scatter' };
trace[5] = { x: xx, y: yy[5], type: 'scatter' };
*/
console.log("trace1 = " + trace[1]);
console.log("trace5 = " + trace[5]);
var data = [trace[1], trace[2], trace[3], trace[4], trace[5]];
Plotly.newPlot('div2', data, { showlegend: false, xaxis: { tickmode: "linear", dtick: 1 } }, { displayModeBar: false });
}