Graphing Plugin

Library for Calculator (Graphing)

目前为 2024-08-10 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/503150/1425281/Graphing%20Plugin.js

  1. // ==UserScript==
  2. // @name Graphing Plugin
  3. // @namespace https://gf.qytechs.cn/en/users/1291009
  4. // @author BadOrBest
  5. // @license MIT
  6. // @version 1.2
  7. // @match *://*/*
  8. // @grant GM_addStyle
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant GM_deleteValue
  12. // @grant GM_registerMenuCommand
  13. // @grant GM.registerMenuCommand
  14. // @require https://cdnjs.cloudflare.com/ajax/libs/mathjs/13.0.3/math.min.js
  15. // @require https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.min.js
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20.  
  21. let chartInstance = null;
  22.  
  23. // Function to initialize or update the graph
  24. function updateGraph() {
  25. const graphData = GM_getValue('graphData', []);
  26. const ctx = document.getElementById('graphCanvas').getContext('2d');
  27.  
  28. if (chartInstance) {
  29. chartInstance.destroy();
  30. }
  31.  
  32. chartInstance = new Chart(ctx, {
  33. type: 'line',
  34. data: {
  35. datasets: graphData.map(data => ({
  36. label: data.label,
  37. data: data.points,
  38. borderColor: data.color || 'blue',
  39. borderWidth: 1,
  40. fill: false
  41. }))
  42. },
  43. options: {
  44. scales: {
  45. x: {
  46. type: 'linear',
  47. position: 'bottom'
  48. }
  49. }
  50. }
  51. });
  52. }
  53.  
  54. // Function to toggle the graph display
  55. function toggleGraph() {
  56. let canvas = document.getElementById('graphCanvas');
  57. if (!canvas) {
  58. canvas = document.createElement('canvas');
  59. canvas.id = 'graphCanvas';
  60. canvas.style.position = 'fixed';
  61. canvas.style.bottom = '0';
  62. canvas.style.right = '0';
  63. canvas.style.zIndex = '9999';
  64. document.body.appendChild(canvas);
  65. } else {
  66. canvas.style.display = canvas.style.display === 'none' ? 'block' : 'none';
  67. if (canvas.style.display === 'block') {
  68. updateGraph();
  69. }
  70. }
  71. }
  72.  
  73. // Function to clear the graph data
  74. function clearGraph() {
  75. GM_setValue('graphData', []);
  76. const canvas = document.getElementById('graphCanvas');
  77. if (canvas) {
  78. canvas.style.display = 'none';
  79. }
  80. if (chartInstance) {
  81. chartInstance.destroy();
  82. chartInstance = null;
  83. }
  84. }
  85.  
  86. // Function to process user input and update graph data
  87. function addGraphData() {
  88. const expression = prompt('Enter a mathematical expression (e.g., y=x^2):');
  89. if (expression) {
  90. const xValues = Array.from({ length: 100 }, (_, i) => i - 50); // X values from -50 to 50
  91. const yValues = xValues.map(x => math.evaluate(expression.replace(/x/g, x))); // Evaluate expression
  92.  
  93. const newGraphData = GM_getValue('graphData', []);
  94. newGraphData.push({
  95. label: `Graph ${newGraphData.length + 1}`,
  96. points: xValues.map((x, i) => ({ x, y: yValues[i] }))
  97. });
  98.  
  99. GM_setValue('graphData', newGraphData);
  100. updateGraph();
  101. }
  102. }
  103.  
  104. // Register menu commands for graphing actions
  105. GM_registerMenuCommand('Clear Graph', clearGraph);
  106. GM_registerMenuCommand('Add Graph Data', addGraphData);
  107.  
  108. // Listen for custom events to control the graph
  109. window.addEventListener('graphControlEvent', (event) => {
  110. const { action } = event.detail;
  111. if (action === 'toggle') {
  112. toggleGraph();
  113. }
  114. });
  115.  
  116. // Initialize or update the graph display if needed
  117. if (GM_getValue('graphData', []).length > 0) {
  118. updateGraph();
  119. }
  120. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址