GetMaterials.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div class="field">
  3. <div class="field is-horizontal">
  4. <div class="field-label is-normal">
  5. <label class="label">Materials</label>
  6. </div>
  7. <div class="field-body">
  8. <b-switch v-model="isVisible">
  9. <div v-if="isVisible==true">Hide</div>
  10. <div v-else>Show</div>
  11. </b-switch>
  12. </div>
  13. </div>
  14. <transition name="slide">
  15. <div class="list" v-if="isVisible">
  16. <table class="table is-striped is-narrow is-hoverable">
  17. <thead>
  18. <tr>
  19. <th>Use</th>
  20. <th>Name</th>
  21. <th>Plot</th>
  22. <th>File or URL</th>
  23. </tr>
  24. </thead>
  25. <tfoot>
  26. <tr>
  27. <th>Use</th>
  28. <th>Name</th>
  29. <th>Plot</th>
  30. <th>File or URL</th>
  31. </tr>
  32. </tfoot>
  33. <tr v-for="material in materials" v-bind:key="material.name">
  34. <td><b-switch v-model="material.isUsed"/>
  35. <!-- <font-awesome-icon icon="check-circle" class="rh-input has-text-success" v-if="material.isLoaded">-->
  36. <!-- <span class="tooltiptext">test</span>-->
  37. <!-- </font-awesome-icon>-->
  38. <span class="rh-input" v-if="material.isLoaded">
  39. <font-awesome-icon icon="check-circle" class="has-text-success"/>
  40. <span class="tooltiptext tip-success">Loaded.</span>
  41. </span>
  42. <span class="rh-input" v-else>
  43. <font-awesome-icon icon="ban" class="has-text-danger"/>
  44. <span class="tooltiptext tip-danger">Failed to load.</span>
  45. </span>
  46. </td>
  47. <td>{{material.name}}</td>
  48. <td><b-checkbox v-model="material.isPlot"/></td>
  49. <td>{{material.fname}}</td>
  50. </tr>
  51. </table>
  52. <div class="chart-container">
  53. <reactive-chart :chart="chart"/>
  54. </div>
  55. </div>
  56. </transition>
  57. </div>
  58. </template>
  59. <!--https://refractiveindex.info/database/data/main/Au/Johnson.yml-->
  60. <script>
  61. import ReactiveChart from "./ReactiveChart.vue";
  62. export default {
  63. name: "GetMaterials",
  64. components: {
  65. ReactiveChart,
  66. },
  67. data () {
  68. return {
  69. isVisible: true,
  70. chart: {
  71. uuid: "materials",
  72. traces: [],
  73. layout: {
  74. // title: 'reactive charts',
  75. xaxis: {
  76. // will be set on mount
  77. title: ''
  78. },
  79. yaxis: {
  80. title: 'refractive index'
  81. },
  82. width: this.plot_width,
  83. height: this.plot_height}
  84. },
  85. }
  86. },
  87. mounted() {
  88. let files = ['Au-Johnson-1972.yml','Ag-Johnson-1972.yml'];
  89. let names = ['Au (Gold) Johnson','Ag (Silver) Johnson'];
  90. let old_names=[];
  91. for (const mat of this.materials) old_names.push(mat.name);
  92. for (let i = 0; i < files.length; i++) {
  93. if (old_names.includes(names[i])) continue; //Filter out reloads during development
  94. this.materials.push({
  95. fname: files[i],
  96. name: names[i],
  97. isUsed: true,
  98. isPlot: false,
  99. isLoaded: false
  100. });
  101. }
  102. this.sortMaterials();
  103. for (const material of this.materials) {
  104. this.loadMaterial(material);
  105. }
  106. },
  107. watch: {
  108. materials: {
  109. handler: function () {
  110. this.chart.traces = [];
  111. for (const mat of this.materials) {
  112. if (mat.isPlot) {
  113. this.chart.traces.push({
  114. x: mat.data_nk[0],
  115. y: mat.data_nk[1],
  116. type: 'scatter',
  117. name: mat.name + ' data Re(n)'
  118. });
  119. this.chart.traces.push({
  120. x: mat.data_nk[0],
  121. y: mat.data_nk[2],
  122. type: 'scatter',
  123. name: mat.name + ' data Im(n)'
  124. });
  125. }
  126. }
  127. // console.log('update material');
  128. },
  129. deep:true
  130. },
  131. plot_width: {
  132. handler: function () {
  133. this.chart.layout.width = this.plot_width;
  134. }
  135. },
  136. plot_height: {
  137. handler: function () {
  138. this.chart.layout.heigth = this.plot_height;
  139. }
  140. },
  141. },
  142. methods: {
  143. transpose(array) {
  144. return array[0].map((col, i) => array.map(row => row[i]));
  145. },
  146. sortMaterials() {
  147. this.materials.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
  148. },
  149. async loadMaterial(material) {
  150. const data_nk = await this.loadMaterialData(material.fname);
  151. material.data_nk = data_nk;
  152. },
  153. async loadMaterialData(URL){
  154. const yaml = require('js-yaml');
  155. // Get document, or throw exception on error
  156. let Ag_data;
  157. try {
  158. let response = await fetch(URL);
  159. let Ag_data = await response.text();
  160. const doc = await yaml.safeLoad(Ag_data);
  161. if (doc.DATA[0].type == "tabulated nk") {
  162. let csv = doc.DATA[0].data;
  163. let rows = csv.split("\n");
  164. let data = rows.map(function (row) {
  165. return row.split(" ");
  166. });
  167. data.pop();
  168. let data_num = data.map(function(elem) {
  169. return elem.map(function(elem2) {
  170. return parseFloat(elem2);
  171. });
  172. })
  173. return this.transpose(data_num);
  174. }
  175. } catch (e) {
  176. console.log(e);
  177. }
  178. }
  179. },
  180. props: ['materials', 'plot_width', 'plot_height']
  181. }
  182. </script>
  183. <style scoped>
  184. .list{
  185. transform-origin: top;
  186. transition: transform .4s ease-in-out;
  187. }
  188. .slide-enter, .slide-leave-to{
  189. transform: scaleY(0);
  190. }
  191. .rh-input {
  192. margin-right: 0px;
  193. position: relative;
  194. display: inline-block;
  195. }
  196. /* Tooltip text */
  197. .tip-danger {
  198. background-color: #ff3860;
  199. }
  200. .tip-success {
  201. background-color: #23d160;
  202. }
  203. .rh-input .tooltiptext {
  204. visibility: hidden;
  205. width: 120px;
  206. /*background-color: #7957d5;*/
  207. /*background-color: #23d160;*/
  208. color: white;
  209. text-align: center;
  210. padding: 5px 0;
  211. border-radius: 6px;
  212. /* Position the tooltip text - see examples below! */
  213. position: absolute;
  214. z-index: 1;
  215. bottom: 110%;
  216. left: 0%;
  217. }
  218. /* Show the tooltip text when you mouse over the tooltip container */
  219. .rh-input:hover .tooltiptext {
  220. visibility: visible;
  221. }
  222. </style>