PlotlyCharts.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <div id="myDiv"></div>
  3. </template>
  4. <script>
  5. import Plotly from "plotly.js-dist";
  6. export default {
  7. name: "PlotlyChart",
  8. mounted() {
  9. this.initializeChart();
  10. },
  11. data() {
  12. return {
  13. imageDataUri: "",
  14. };
  15. },
  16. methods: {
  17. initializeChart() {
  18. const layout = {
  19. title: "Power Curve",
  20. xaxis: { title: "Wind Speed (m/s)" },
  21. yaxis: { title: "Power (kW)" },
  22. legend: {
  23. orientation: "h",
  24. y: -0.2,
  25. x: 0.5,
  26. xanchor: "center",
  27. },
  28. };
  29. const data = {
  30. analysisTypeCode: "power_curve",
  31. graphType: "scatter",
  32. data: [
  33. {
  34. engineName: "#1",
  35. windSpeed: [
  36. 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  37. 27, 30,
  38. ],
  39. power: [
  40. 6.06, 76.54, 184.1, 345.94, 574.4, 879.9, 1274.73, 1664.94,
  41. 1942.96, 2004.78, 2004.78, 2004.78, 2004.78, 2004.78, 2004.78,
  42. 2004.78, 2004.78, 2004.78, 2230,
  43. ],
  44. },
  45. {
  46. engineName: "#2",
  47. windSpeed: [
  48. 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5,
  49. 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16,
  50. 16.5, 33,
  51. ],
  52. power: [
  53. 2, 9.23, 100.12, 155.51, 230.09, 400.15, 500.95, 689.55, 961.09,
  54. 1257.53, 1346.02, 1479.78, 1528.01, 1618.52, 1789.09, 1819.35,
  55. 1920.29, 2053.52, 2053.52, 2053.52, 2053.52, 2053.52, 2053.52,
  56. 2053.52, 2053.52, 2053.52, 2053.52, 2053.52, 2053.52, 2053.52,
  57. 2153.52, 2159,
  58. ],
  59. },
  60. {
  61. engineName: "#3",
  62. windSpeed: [
  63. 2.7, 3.7, 4.7, 5.7, 6.7, 7.7, 8.7, 9.7, 10.7, 11.5, 12, 13.5, 14,
  64. 15.5, 16, 17.5, 18, 19.5, 21, 21.5, 22, 22.5, 23, 23.5, 24, 27.5,
  65. 28, 29.5, 30, 36.5,
  66. ],
  67. power: [
  68. 2, 9.23, 100.12, 155.51, 230.09, 400.15, 500.95, 689.55, 961.09,
  69. 1257.53, 1346.02, 1479.78, 1528.01, 1618.52, 1789.09, 1819.35,
  70. 1920.29, 2053.52, 2053.52, 2053.52, 2053.52, 2053.52, 2053.52,
  71. 2053.52, 2053.52, 2053.52, 2053.52, 2053.52, 2053.52, 2053.52,
  72. 2053.52,
  73. ],
  74. },
  75. ],
  76. };
  77. const plotlyData = data.data.map((engine) => ({
  78. x: engine.windSpeed,
  79. y: engine.power,
  80. mode: "lines+markers",
  81. name: engine.engineName,
  82. }));
  83. const modeBarButtons = [
  84. {
  85. name: "套索选择",
  86. icon: Plotly.Icons.lasso,
  87. click: function (gd) {
  88. // 处理套索选择功能
  89. Plotly.relayout(gd, { dragmode: "lasso" });
  90. },
  91. },
  92. {
  93. name: "恢复",
  94. icon: Plotly.Icons.home,
  95. click: function (gd) {
  96. Plotly.relayout(gd, {
  97. "xaxis.autorange": true,
  98. "yaxis.autorange": true,
  99. });
  100. },
  101. },
  102. {
  103. name: "拍照",
  104. icon: Plotly.Icons.camera,
  105. click: function (gd) {
  106. Plotly.downloadImage(gd);
  107. },
  108. },
  109. {
  110. name: "放大",
  111. icon: Plotly.Icons.zoom_plus,
  112. click: function (gd) {
  113. Plotly.relayout(gd, {
  114. "xaxis.range": [
  115. gd._fullLayout.xaxis.range[0],
  116. gd._fullLayout.xaxis.range[1] * 0.5,
  117. ],
  118. });
  119. },
  120. },
  121. {
  122. name: "缩小",
  123. icon: Plotly.Icons.zoom_minus,
  124. click: function (gd) {
  125. Plotly.relayout(gd, {
  126. "xaxis.range": [
  127. gd._fullLayout.xaxis.range[0],
  128. gd._fullLayout.xaxis.range[1] * 2,
  129. ],
  130. });
  131. },
  132. },
  133. {
  134. name: "平移",
  135. icon: Plotly.Icons.pan,
  136. click: function (gd) {
  137. Plotly.relayout(gd, { dragmode: "pan" });
  138. },
  139. },
  140. {
  141. name: "自动缩放",
  142. icon: Plotly.Icons.autoscale,
  143. click: function (gd) {
  144. Plotly.relayout(gd, {
  145. "xaxis.autorange": true,
  146. "yaxis.autorange": true,
  147. });
  148. },
  149. },
  150. // 添加更多的自定义按钮
  151. ];
  152. const config = {
  153. modeBarButtonsToAdd: modeBarButtons,
  154. modeBarButtonsToRemove: [
  155. "toImage",
  156. "zoom2d",
  157. "pan2d",
  158. "select2d",
  159. "lasso2d",
  160. "zoomIn2d",
  161. "zoomOut2d",
  162. "autoScale2d",
  163. "resetScale2d",
  164. "hoverClosestCartesian",
  165. "hoverCompareCartesian",
  166. ],
  167. displaylogo: false, // 移除plotly logo
  168. scrollZoom: true,
  169. // editable: true,//编辑图表标题
  170. };
  171. Plotly.newPlot("myDiv", plotlyData, layout, config).then((gd) => {
  172. //生成图片方法
  173. Plotly.toImage(gd, { format: "png" }).then((dataUrl) => {
  174. // console.log(dataUrl, "dataUrl");
  175. this.imageDataUri = dataUrl;
  176. });
  177. // 处理选择事件
  178. gd.on("plotly_selected", (eventData) => {
  179. if (eventData) {
  180. const selectedPoints = eventData.points.map((point) => ({
  181. x: point.x,
  182. y: point.y,
  183. }));
  184. console.log("选中的点:", selectedPoints);
  185. // 这里可以保存选中的数据进行后续处理
  186. }
  187. });
  188. // 处理套索结束事件
  189. gd.on("plotly_relayout", (eventData) => {
  190. if (eventData["dragmode"] === "lasso") {
  191. console.log("套索选择模式激活");
  192. // 可以处理用户开始新的套索选择操作
  193. }
  194. });
  195. });
  196. },
  197. },
  198. };
  199. </script>
  200. <style scoped>
  201. /* 样式可选 */
  202. </style>