PlotNearField.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <div>
  3. <ReactiveChart :chart="nearFieldPlot" @plotCreated="manageID($event)" />
  4. </div>
  5. </template>
  6. <script lang="ts">
  7. import ReactiveChart from 'components/ReactiveChart.vue';
  8. import { useStore } from 'src/store';
  9. import {
  10. defineComponent,
  11. // ref,
  12. reactive,
  13. computed,
  14. watch,
  15. } from 'vue';
  16. import {
  17. toUnits,
  18. getMaxFromHeatmap,
  19. getMinFromHeatmap,
  20. arrayMin,
  21. arrayMax,
  22. limitMap,
  23. fromUnits,
  24. } from 'components/utils';
  25. import { plotlyChart } from 'src/store/plot-runtime/state';
  26. import { PlotData, DataTitle, PlotlyHTMLElement } from 'plotly.js-dist-min';
  27. import { nearFieldPlane } from 'src/store/simulation-setup/state';
  28. export default defineComponent({
  29. name: 'PlotNearField',
  30. components: {
  31. ReactiveChart,
  32. },
  33. setup() {
  34. const $store = useStore();
  35. const nearFieldPlotInit: plotlyChart = {
  36. data: [],
  37. layout: {
  38. shapes: [],
  39. margin: {
  40. l: 0,
  41. r: 40,
  42. b: 50,
  43. t: 30,
  44. },
  45. xaxis: {
  46. title: '',
  47. },
  48. yaxis: {
  49. scaleanchor: 'x',
  50. title: '',
  51. },
  52. showlegend: false,
  53. },
  54. config: {
  55. responsive: true,
  56. // showEditInChartStudio: true,
  57. displaylogo: false,
  58. },
  59. };
  60. const nearFieldPlot = reactive(nearFieldPlotInit);
  61. const crossSection = computed(
  62. () => $store.state.simulationSetup.current.nearFieldSetup.crossSection
  63. );
  64. const relativePlotSize = computed(
  65. () => $store.state.simulationSetup.current.nearFieldSetup.relativePlotSize
  66. );
  67. const plotYSideResolution = computed(
  68. () =>
  69. $store.state.simulationSetup.current.nearFieldSetup.plotYSideResolution
  70. );
  71. const plotXSideResolution = computed(
  72. () =>
  73. $store.state.simulationSetup.current.nearFieldSetup.plotXSideResolution
  74. );
  75. const layerWidths = computed(() =>
  76. $store.state.simulationSetup.current.layers.map((x) => x.layerWidth)
  77. );
  78. const totalR = computed(() => layerWidths.value.reduce((a, b) => a + b, 0));
  79. const x0 = computed(() => totalR.value * relativePlotSize.value);
  80. const dx = computed(
  81. () => (x0.value * 2.0) / (plotXSideResolution.value - 1)
  82. );
  83. const units = computed(() => $store.state.guiRuntime.units);
  84. const at_x = computed(() => {
  85. if (
  86. crossSection.value == nearFieldPlane.Ek ||
  87. crossSection.value == nearFieldPlane.Hk
  88. ) {
  89. return $store.state.simulationSetup.current.nearFieldSetup.atRelativeZ0;
  90. }
  91. return $store.state.simulationSetup.current.nearFieldSetup.atRelativeY0;
  92. });
  93. const at_y = computed(() => {
  94. if (crossSection.value == nearFieldPlane.Hk) {
  95. return $store.state.simulationSetup.current.nearFieldSetup.atRelativeY0;
  96. }
  97. return $store.state.simulationSetup.current.nearFieldSetup.atRelativeX0;
  98. });
  99. const xy = computed(() => {
  100. let x: number[] = [];
  101. let y: number[] = [];
  102. const xi =
  103. at_x.value * totalR.value -
  104. (dx.value * (plotXSideResolution.value - 1)) / 2;
  105. const yi =
  106. at_y.value * totalR.value -
  107. (dx.value * (plotYSideResolution.value - 1)) / 2;
  108. for (let j = 0; j < plotYSideResolution.value; ++j) {
  109. for (let i = 0; i < plotXSideResolution.value; ++i) {
  110. x.push(toUnits(xi + i * dx.value, units.value));
  111. y.push(toUnits(yi + j * dx.value, units.value));
  112. }
  113. }
  114. $store.commit('plotRuntime/setNearFieldCoords', { x: x, y: y });
  115. return { x: x, y: y };
  116. });
  117. const limitFrom = computed(
  118. () => $store.state.plotRuntime.nearFieldLimitFrom
  119. );
  120. const limitTo = computed(() => $store.state.plotRuntime.nearFieldLimitTo);
  121. const nearFieldStore = computed(() => {
  122. let nearFieldStoreLocal = $store.state.plotRuntime.nearFieldEabs;
  123. $store.commit(
  124. 'plotRuntime/setNearFieldDataTo',
  125. getMaxFromHeatmap(nearFieldStoreLocal)
  126. );
  127. $store.commit(
  128. 'plotRuntime/setNearFieldDataFrom',
  129. getMinFromHeatmap(nearFieldStoreLocal)
  130. );
  131. $store.commit(
  132. 'plotRuntime/setNearFieldLimitTo',
  133. $store.state.plotRuntime.nearFieldDataTo
  134. );
  135. $store.commit(
  136. 'plotRuntime/setNearFieldLimitFrom',
  137. $store.state.plotRuntime.nearFieldDataFrom
  138. );
  139. return nearFieldStoreLocal;
  140. });
  141. const nearFieldProc = computed(() => {
  142. if (!nearFieldStore.value) return nearFieldStore.value;
  143. return limitMap(nearFieldStore.value, limitFrom.value, limitTo.value);
  144. // return nearFieldStore.map(x=>x>limitTo.value?limitTo.value:x)
  145. });
  146. watch([nearFieldProc, xy], () => {
  147. nearFieldPlot.data.length = 0;
  148. const heatMapSettings: Partial<PlotData> = {
  149. type: 'heatmap',
  150. colorscale: 'Jet',
  151. colorbar: { title: '|𝐸|∕|𝐸𝜊|' },
  152. z: nearFieldProc.value,
  153. };
  154. nearFieldPlot.data.push({ ...xy.value, ...heatMapSettings });
  155. const isPlotShapes =
  156. arrayMin(xy.value.x) < -toUnits(totalR.value, units.value) &&
  157. arrayMax(xy.value.x) > toUnits(totalR.value, units.value) &&
  158. arrayMin(xy.value.y) < -toUnits(totalR.value, units.value) &&
  159. arrayMax(xy.value.y) > toUnits(totalR.value, units.value);
  160. if (nearFieldPlot.layout.shapes) nearFieldPlot.layout.shapes.length = 0;
  161. if (nearFieldPlot.layout.shapes && isPlotShapes) {
  162. let r = 0;
  163. for (let widthLayer of layerWidths.value) {
  164. r += widthLayer;
  165. nearFieldPlot.layout.shapes.push({
  166. type: 'circle',
  167. xref: 'x',
  168. yref: 'y',
  169. x0: toUnits(-r, units.value),
  170. y0: toUnits(-r, units.value),
  171. x1: toUnits(r, units.value),
  172. y1: toUnits(r, units.value),
  173. line: {
  174. color: 'rgba(235, 235, 235, 0.8)',
  175. width: 3,
  176. },
  177. });
  178. }
  179. }
  180. });
  181. const xaxisTitle = computed(() => {
  182. let title: string | Partial<DataTitle> = 'x [' + units.value + ']';
  183. return title;
  184. });
  185. if (nearFieldPlot.layout.xaxis)
  186. nearFieldPlot.layout.xaxis.title = xaxisTitle.value;
  187. watch(xaxisTitle, () => {
  188. if (nearFieldPlot.layout.xaxis)
  189. nearFieldPlot.layout.xaxis.title = xaxisTitle.value;
  190. });
  191. function manageID(chartID: string) {
  192. const myPlot = document.getElementById(chartID) as PlotlyHTMLElement;
  193. myPlot.on('plotly_relayout', function (data) {
  194. if (
  195. data['xaxis.range[0]'] &&
  196. data['xaxis.range[1]'] &&
  197. data['yaxis.range[0]'] &&
  198. data['yaxis.range[1]']
  199. ) {
  200. $store.commit('guiRuntime/setNearFieldZoom', {
  201. fromX: fromUnits(units.value, data['xaxis.range[0]']),
  202. toX: fromUnits(units.value, data['xaxis.range[1]']),
  203. fromY: fromUnits(units.value, data['yaxis.range[0]']),
  204. toY: fromUnits(units.value, data['yaxis.range[1]']),
  205. });
  206. } else {
  207. $store.commit('guiRuntime/setNearFieldZoom', {
  208. fromX: 0,
  209. toX: 0,
  210. fromY: 0,
  211. toY: 0,
  212. });
  213. }
  214. });
  215. }
  216. return {
  217. nearFieldPlot,
  218. totalR,
  219. manageID,
  220. };
  221. },
  222. });
  223. </script>