Debian Codegraph @

blockly/demos/graph/index.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Blockly Demo: Graph</title>
6 <script src="https://www.google.com/jsapi"></script>
7 <script src="../../blockly_compressed.js"></script>
8 <script src="../../blocks_compressed.js"></script>
9 <script src="../../javascript_compressed.js"></script>
10 <script src="../../msg/js/en.js"></script>
11 <style>
12 body {
13 background-color: #fff;
14 font-family: sans-serif;
15 }
16 h1 {
17 font-weight: normal;
18 font-size: 140%;
19 }
20 #funcText {
21 margin-top: 1em;
22 margin-left: 1.5em;
23 font-family: sans-serif;
24 }
25 #funcText>img {
26 height: 3px;
27 width: 15px;
28 vertical-align: middle;
29 margin-right: .5em;
30 }
31 #y1 {
32 background-color: #36c;
33 }
34 </style>
35 </head>
36 <body>
37 <h1><a href="https://developers.google.com/blockly/">Blockly</a> &gt;
38 <a href="../index.html">Demos</a> &gt; Graph</h1>
39
40 <p>This is a demo of giving instant feedback as blocks are changed.</p>
41
42 <p>&rarr; More info on <a href="https://developers.google.com/blockly/installation/code-generators#realtime_generation">Realtime generation</a>...</p>
43
44 <table>
45 <tr>
46 <td>
47 <div id="visualization" style="width: 400px"></div>
48 </td>
49 <td>
50 <div id="blocklyDiv" style="height: 400px"></div>
51 </td>
52 </tr>
53 </table>
54
55 <div id="funcText">
56 <img id="y1" src="../../media/1x1.gif">
57 ...
58 </div>
59
60 <xml id="toolbox" style="display: none">
61 <category name="Math">
62 <block type="math_number"></block>
63 <block type="math_arithmetic"></block>
64 <block type="math_single"></block>
65 <block type="math_trig"></block>
66 <block type="math_constant"></block>
67 <block type="math_number_property"></block>
68 <block type="math_round"></block>
69 <block type="math_modulo"></block>
70 <block type="math_constrain">
71 <value name="LOW">
72 <block type="math_number">
73 <field name="NUM">1</field>
74 </block>
75 </value>
76 <value name="HIGH">
77 <block type="math_number">
78 <field name="NUM">100</field>
79 </block>
80 </value>
81 </block>
82 <block type="math_random_int">
83 <value name="FROM">
84 <block type="math_number">
85 <field name="NUM">1</field>
86 </block>
87 </value>
88 <value name="TO">
89 <block type="math_number">
90 <field name="NUM">100</field>
91 </block>
92 </value>
93 </block>
94 <block type="math_random_float"></block>
95 </category>
96 <category name="Variables">
97 <block type="graph_get_x"></block>
98 </category>
99 <category name="Logic">
100 <block type="logic_compare"></block>
101 <block type="logic_operation"></block>
102 <block type="logic_negate"></block>
103 <block type="logic_boolean"></block>
104 <block type="logic_ternary"></block>
105 </category>
106 </xml>
107
108 <xml id="startBlocks" style="display: none">
109 <block type="graph_set_y" deletable="false" x="100" y="100">
110 <value name="VALUE">
111 <block type="math_arithmetic">
112 <field name="OP">POWER</field>
113 <value name="A">
114 <block type="graph_get_x"></block>
115 </value>
116 <value name="B">
117 <block type="math_number">
118 <field name="NUM">2</field>
119 </block>
120 </value>
121 </block>
122 </value>
123 </block>
124 </xml>
125
126 <script>
127 // Load the Google Chart Tools Visualization API and the chart package.
128 if (typeof google == 'object') {
129 google.load('visualization', '1', {packages: ['corechart']});
130 } else {
131 alert('Unable to load Google\'s chart API.\n' +
132 'Are you connected to the Internet?');
133 }
134
135 // Define the custom blocks and their JS generators.
136 Blockly.Blocks['graph_get_x'] = {
137 // x variable getter.
138 init: function() {
139 this.setHelpUrl(Blockly.Msg.VARIABLES_GET_HELPURL);
140 this.setColour(330);
141 this.appendDummyInput()
142 .appendField('x');
143 this.setOutput(true, 'Number');
144 this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP);
145 }
146 };
147
148 Blockly.JavaScript['graph_get_x'] = function(block) {
149 // x variable getter.
150 return ['x', Blockly.JavaScript.ORDER_ATOMIC];
151 };
152
153 Blockly.Blocks['graph_set_y'] = {
154 // y variable setter.
155 init: function() {
156 this.setHelpUrl(Blockly.Msg.VARIABLES_SET_HELPURL);
157 this.setColour(330);
158 this.appendValueInput('VALUE')
159 .appendField('y =')
160 .setCheck('Number');
161 this.setTooltip(Blockly.Msg.VARIABLES_SET_TOOLTIP);
162 }
163 };
164
165 Blockly.JavaScript['graph_set_y'] = function(block) {
166 // y variable setter.
167 var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
168 Blockly.JavaScript.ORDER_ASSIGNMENT) || '';
169 return 'y = ' + argument0 + ';';
170 };
171
172 /**
173 * Create a namespace for the application.
174 */
175 var Graph = {};
176
177 /**
178 * Main Blockly workspace.
179 * @type {Blockly.WorkspaceSvg}
180 */
181 Graph.workspace = null;
182
183 /**
184 * Cached copy of the function string.
185 * @type {?string}
186 * @private
187 */
188 Graph.oldFormula_ = null;
189
190 /**
191 * Drawing options for the Chart API.
192 * @type {!Object}
193 * @private
194 */
195 Graph.options_ = {
196 //curveType: 'function',
197 width: 400, height: 400,
198 chartArea: {left: '10%', width: '85%', height: '85%'}
199 };
200
201 /**
202 * Visualize the graph of y = f(x) using Google Chart Tools.
203 * For more documentation on Google Chart Tools, see this linechart example:
204 * https://developers.google.com/chart/interactive/docs/gallery/linechart
205 */
206 Graph.drawVisualization = function() {
207 var formula = Blockly.JavaScript.workspaceToCode(Graph.workspace);
208 if (formula === Graph.oldFormula_) {
209 // No change in the formula, don't recompute.
210 return;
211 }
212 Graph.oldFormula_ = formula;
213
214 // Create and populate the data table.
215 var data = google.visualization.arrayToDataTable(Graph.plot(formula));
216 // Create and draw the visualization, passing in the data and options.
217 new google.visualization.LineChart(document.getElementById('visualization')).
218 draw(data, Graph.options_);
219
220 // Create the "y = ..." label. Find the relevant part of the code.
221 formula = formula.substring(formula.indexOf('y = '));
222 formula = formula.substring(0, formula.indexOf(';'));
223 var funcText = document.getElementById('funcText');
224 funcText.replaceChild(document.createTextNode(formula), funcText.lastChild);
225 };
226
227 /**
228 * Plot points on the function y = f(x).
229 * @param {string} code JavaScript code.
230 * @return {!Array.<!Array>} 2D Array of points on the graph.
231 */
232 Graph.plot = function(code) {
233 // Initialize a table with two column headings.
234 var table = [];
235 var y;
236 // TODO: Improve range and scale of graph.
237 for (var x = -10; x <= 10; x = Math.round((x + 0.1) * 10) / 10) {
238 try {
239 eval(code);
240 } catch (e) {
241 y = NaN;
242 }
243 if (!isNaN(y)) {
244 // Prevent y from being displayed inconsistently, some in decimals, some
245 // in scientific notation, often when y has accumulated rounding errors.
246 y = Math.round(y * Math.pow(10, 14)) / Math.pow(10, 14);
247 table.push([x, y]);
248 }
249 }
250 // Add column heading to table.
251 if (table.length) {
252 table.unshift(['x', 'y']);
253 } else {
254 // If the table is empty, add a [0, 0] row to prevent graph error.
255 table.unshift(['x', 'y'], [0, 0]);
256 }
257 return table;
258 };
259
260 /**
261 * Force Blockly to resize into the available width.
262 */
263 Graph.resize = function() {
264 var width = Math.max(window.innerWidth - 440, 250);
265 document.getElementById('blocklyDiv').style.width = width + 'px';
266 };
267
268 /**
269 * Initialize Blockly and the graph. Called on page load.
270 */
271 Graph.init = function() {
272 Graph.workspace = Blockly.inject('blocklyDiv',
273 {media: '../../media/',
274 toolbox: document.getElementById('toolbox')});
275 Blockly.Xml.domToWorkspace(Graph.workspace,
276 document.getElementById('startBlocks'));
277
278 // When Blockly changes, update the graph.
279 Graph.workspace.addChangeListener(Graph.drawVisualization);
280 };
281
282 window.addEventListener('load', Graph.init);
283 window.addEventListener('resize', Graph.resize);
284 </script>
285
286 </body>
287 </html>