plot.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating Python for plot blocks.
  22. * @author acbart@vt.edu (Austin Cory Bart)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Python.plot');
  26. goog.require('Blockly.Python');
  27. Blockly.Python['plot_title'] = function(block) {
  28. Blockly.Python.definitions_['import_matplotlib'] = 'import matplotlib.pyplot as plt';
  29. var parameter_plot_title = Blockly.Python.quote_(block.getFieldValue('TEXT'))
  30. var code = 'plt.title('+parameter_plot_title+')\n';
  31. return code;
  32. };
  33. Blockly.Python['plot_xlabel'] = function(block) {
  34. Blockly.Python.definitions_['import_matplotlib'] = 'import matplotlib.pyplot as plt';
  35. var parameter_plot_title = Blockly.Python.quote_(block.getFieldValue('TEXT'))
  36. var code = 'plt.xlabel('+parameter_plot_title+')\n';
  37. return code;
  38. };
  39. Blockly.Python['plot_ylabel'] = function(block) {
  40. Blockly.Python.definitions_['import_matplotlib'] = 'import matplotlib.pyplot as plt';
  41. var parameter_plot_title = Blockly.Python.quote_(block.getFieldValue('TEXT'))
  42. var code = 'plt.ylabel('+parameter_plot_title+')\n';
  43. return code;
  44. };
  45. Blockly.Python['plot_line'] = function(block) {
  46. Blockly.Python.definitions_['import_matplotlib'] = 'import matplotlib.pyplot as plt';
  47. var code = 'plt.plot(';
  48. var argument1 = Blockly.Python.valueToCode(block, 'y_values',
  49. Blockly.Python.ORDER_NONE) || '___';
  50. code += argument1 +')\n';
  51. return code;
  52. };
  53. Blockly.Python['plot_hist'] = function(block) {
  54. Blockly.Python.definitions_['import_matplotlib'] = 'import matplotlib.pyplot as plt';
  55. var code = 'plt.hist(';
  56. var argument1 = Blockly.Python.valueToCode(block, 'values',
  57. Blockly.Python.ORDER_NONE) || '___';
  58. code += argument1 +')\n';
  59. return code;
  60. };
  61. Blockly.Python['plot_lineXY'] = function(block) {
  62. Blockly.Python.definitions_['import_matplotlib'] = 'import matplotlib.pyplot as plt';
  63. var code = 'plt.line(';
  64. var argument0 = Blockly.Python.valueToCode(block, 'x_values',
  65. Blockly.Python.ORDER_NONE) || '___';
  66. var argument1 = Blockly.Python.valueToCode(block, 'y_values',
  67. Blockly.Python.ORDER_NONE) || '___';
  68. code += argument0 + ','+ argument1 + ')\n';
  69. return code;
  70. };
  71. Blockly.Python['plot_scatter'] = function(block) {
  72. Blockly.Python.definitions_['import_matplotlib'] = 'import matplotlib.pyplot as plt';
  73. var code = 'plt.scatter(';
  74. var argument0 = Blockly.Python.valueToCode(block, 'x_values',
  75. Blockly.Python.ORDER_NONE) || '___';
  76. var argument1 = Blockly.Python.valueToCode(block, 'y_values',
  77. Blockly.Python.ORDER_NONE) || '___';
  78. code += argument0 + ','+ argument1 + ')\n';
  79. return code;
  80. };
  81. Blockly.Python['plot_show'] = function(block) {
  82. Blockly.Python.definitions_['import_matplotlib'] = 'import matplotlib.pyplot as plt';
  83. var code = 'plt.show()\n';
  84. return code;
  85. };