This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
property-grapher.js
137 lines (123 loc) · 3.73 KB
/
property-grapher.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
(function () {
var interactive,
$interactiveIframe = $('#interactive-iframe'),
graph;
graphData = [],
dataIndex = 0,
graphSamplePeriod = 0.05,
graphOptions = {
title: "Area versus Applied Pressure",
xlabel: "Model Time (ps)",
ylabel: "Pressure Times Area (pN⋅nm)",
xmax: 1000,
xmin: 0,
ymax: 2500,
ymin: 0,
xTickCount: 4,
yTickCount: 5,
xFormatter: ".3r",
yFormatter: ".2r",
sample: graphSamplePeriod,
realTime: true,
fontScaleRelativeToParent: true
};
// private functions
// this shouldn't be called until the model has loaded
function addEventHook(name, func, props) {
var privateName = name + '.graph';
interactive.post('listenForDispatchEvent', {eventName: privateName, properties: props});
interactive.addListener(privateName, func);
}
function removeEventHook(name) {
var privateName = name + '.graph';
interactive.post('removeListenerForDispatchEvent', privateName);
}
function addInteractiveEventListeners() {
addEventHook("tick", function(props) {
updateGraph(props);
}, ['pressure', 'pressureTimesArea', 'area']);
addEventHook('play', function(props) {
if (graph.numberOfPoints() && props.tickCounter < graph.numberOfPoints()) {
resetGraphData(props.tickCounter);
graph.reset('#graph', graphOptions);
}
}, ['tickCounter']);
addEventHook('reset', function(props) {
renderGraph();
}, ['displayTimePerTick']);
addEventHook('stepForward', function(props) {
if (props.newStep) {
updateGraph();
} else {
graph.updateOrRescale(props.tickCounter);
}
}, ['tickCounter', 'newStep']);
addEventHook('stepBack', function(props) {
graph.updateOrRescale(props.tickCounter);
}, ['tickCounter']);
}
function removeEventListeners() {
// remove listeners
removeEventHook("tick");
removeEventHook('play');
removeEventHook('reset');
removeEventHook('stepForward');
removeEventHook('stepBack');
}
function updateGraph(props) {
graph.addPoint(updateGraphData(props));
}
function renderGraph() {
resetGraphData();
graphOptions.dataset = graphData;
removeEventListeners();
if (graph) {
graph.reset('#graph', graphOptions);
} else {
graph = LabGrapher('#graph', graphOptions);
}
addInteractiveEventListeners();
}
// Add another sample of data to the graphData array of arrays.
function updateGraphData(props) {
var p = props.pressure;
f = props.pressureTimesArea,
a = props.area,
point = [dataIndex++, f];
graphData[0].push(point);
return point;
}
// Reset the graphData arrays to a specific length by passing in an index value,
// or empty the graphData arrays and initialize the first sample.
function resetGraphData(index) {
var i,
len;
if (index && graphData[0].length > index) {
for (i = 0, len = graphData.length; i < len; i++) {
graphData[i].length = index;
}
dataIndex = index;
return index;
} else {
graphData = [[0]];
dataIndex = 0;
return 0;
}
}
function setupGrapher() {
graphSamplePeriod = 1;
interactive.addListener('propertyValue', function(response) {
if(response.name == 'displayTimePerTick') {
graphSamplePeriod = response.value;
graphOptions.sample = graphSamplePeriod;
renderGraph();
}
});
interactive.post('get', 'displayTimePerTick');
}
// Intitialization
interactive = new iframePhone.ParentEndpoint($interactiveIframe[0]);
interactive.addListener('modelLoaded', function(){
setupGrapher();
});
})();