text-to-chart2.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. let inputField = document.getElementById("keyword_data");
  2. let sendButton = document.getElementById("send_data");
  3. function sendButtonClickHandler() {
  4. console.log(inputField.value);
  5. let input_text_value = inputField.value;
  6. get_data(input_text_value);
  7. }
  8. sendButton.addEventListener("click", sendButtonClickHandler);
  9. inputField.addEventListener("keyup", function (event) {
  10. // 判斷是否按下 Enter 鍵 (key code: 13)
  11. if (event.key === "Enter") {
  12. // 呼叫按鈕點擊事件處理函數
  13. sendButtonClickHandler();
  14. }
  15. });
  16. function apiLoading() {
  17. document.getElementById('chartDataLoading').style.display = 'block';
  18. }
  19. function apiHideLoading() {
  20. document.getElementById('chartDataLoading').style.display = 'none';
  21. }
  22. let labelDependent = "";
  23. function get_data(input_text_value) {
  24. apiLoading();
  25. axios.get('https://cmm.ai:8080/answer?question=' + input_text_value)
  26. .then(response => {
  27. apiHideLoading();
  28. console.log(response);
  29. chartType = response.data.chart_info.Chart_type;
  30. switch (chartType) {
  31. case "line":
  32. document.getElementById("inlineRadio4").checked = true; // 折線圖
  33. break;
  34. case "bar":
  35. document.getElementById("inlineRadio5").checked = true; // 柱狀圖
  36. break;
  37. case "pie":
  38. document.getElementById("inlineRadio6").checked = true; // 圓餅圖
  39. break;
  40. case "table":
  41. document.getElementById("inlineRadio7").checked = true; // 表格
  42. break;
  43. default:
  44. // 默認情況
  45. }
  46. let dataArray = response.data.data;
  47. labelDependent = response.data.chart_info.Label_dependent_variable;
  48. generateChart(dataArray);
  49. })
  50. .catch(error => {
  51. apiHideLoading();
  52. console.error('發生錯誤:', error);
  53. });
  54. }
  55. // generateChart();
  56. // 模板2
  57. function generateChart(dataArray) {
  58. console.log('labelDependent', labelDependent);
  59. // Themes begin
  60. am4core.useTheme(am4themes_animated);
  61. // Themes end
  62. // Create chart instance
  63. var chart = am4core.create("chartdiv", am4charts.XYChart3D);
  64. console.log(dataArray);
  65. chart.data = dataArray;
  66. chart.angle = 30; // 圖表角度
  67. chart.depth = 25; // 圖表深度
  68. let categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
  69. categoryAxis.dataFields.category = "stock_name";
  70. categoryAxis.renderer.labels.template.rotation = 0;
  71. categoryAxis.renderer.labels.template.hideOversized = false;
  72. categoryAxis.renderer.minGridDistance = 20; // 間距
  73. // categoryAxis.renderer.labels.template.horizontalCenter = "left";
  74. // categoryAxis.renderer.labels.template.verticalCenter = "middle";
  75. // categoryAxis.tooltip.label.rotation = 90;
  76. // categoryAxis.tooltip.label.horizontalCenter = "right";
  77. // categoryAxis.tooltip.label.verticalCenter = "middle";
  78. // label 換行
  79. let label = categoryAxis.renderer.labels.template;
  80. label.wrap = true;
  81. label.maxWidth = 100;
  82. categoryAxis.renderer.cellEndLocation = 0.5; // 減少值可以增加間距
  83. let valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
  84. valueAxis.title.text = labelDependent; // Y軸
  85. valueAxis.title.fontWeight = "bold";
  86. // Create series
  87. var series = chart.series.push(new am4charts.ColumnSeries3D());
  88. series.dataFields.valueY = "total_volume";
  89. series.dataFields.categoryX = "stock_name";
  90. series.name = "total_volume";
  91. series.tooltipText = "{categoryX}: [bold]{valueY}[/]";
  92. series.columns.template.fillOpacity = .8;
  93. let columnTemplate = series.columns.template;
  94. columnTemplate.strokeWidth = 2;
  95. columnTemplate.strokeOpacity = 1;
  96. columnTemplate.stroke = am4core.color("#FFFFFF");
  97. // columnTemplate.adapter.add("fill", function (fill, target) {
  98. // return chart.colors.getIndex(target.dataItem.index);
  99. // })
  100. // 設置基數和偶數顏色
  101. columnTemplate.adapter.add("fill", function (fill, target) {
  102. return target.dataItem.index % 2 === 0 ? am4core.color("#288D97") : am4core.color("#427D7E");
  103. });
  104. columnTemplate.adapter.add("stroke", function (stroke, target) {
  105. return chart.colors.getIndex(target.dataItem.index);
  106. })
  107. // // 加上白色線條
  108. // series.columns.template.events.on("validated", function (event) {
  109. // let column = event.target;
  110. // let whiteLine = column.createChild(am4core.Line);
  111. // whiteLine.stroke = am4core.color("#ffffff");
  112. // whiteLine.strokeWidth = 1.3; // 線條寬度
  113. // whiteLine.strokeOpacity = 0.5;// 透明度
  114. // whiteLine.x1 = column.pixelWidth / 1;
  115. // whiteLine.y1 = 0;
  116. // whiteLine.x2 = column.pixelWidth / 1;
  117. // whiteLine.y2 = column.pixelHeight;
  118. // });
  119. chart.cursor = new am4charts.XYCursor();
  120. chart.cursor.lineX.strokeOpacity = 0;
  121. chart.cursor.lineY.strokeOpacity = 0;
  122. categoryAxis.renderer.grid.template.strokeOpacity = 0; // 隱藏 X 軸
  123. valueAxis.renderer.grid.template.strokeOpacity = 0; // 隱藏 Y 軸
  124. valueAxis.renderer.line.strokeOpacity = 0;
  125. // LineSeries 底部線條
  126. var lineSeries = chart.series.push(new am4charts.LineSeries());
  127. lineSeries.dataFields.valueY = "bottom";
  128. lineSeries.dataFields.categoryX = "stock_name";
  129. lineSeries.stroke = am4core.color("#000000")
  130. lineSeries.strokeWidth = 2;
  131. lineSeries.strokeOpacity = 1
  132. var bottomData = [];
  133. for (var i = 0; i < dataArray.length; i++) {
  134. bottomData.push({ "stock_name": dataArray[i].stock_name, "bottom": 0 });
  135. }
  136. lineSeries.data = bottomData;
  137. // 底部線條
  138. // let rectElements = document.querySelectorAll("rect");
  139. // // let widthValue = 0;
  140. // let fifthRectElement = rectElements[3];
  141. // // fifthRectElement.style.position = "relative";
  142. // setTimeout(() => {
  143. // let width = fifthRectElement.getAttribute("width");
  144. // // widthValue = parseFloat(width);
  145. // console.log('rectElements', rectElements);
  146. // console.log('fifthRectElement', fifthRectElement);
  147. // console.log('width', width);
  148. // let line = document.createElement("div");
  149. // line.style.position = "absolute";
  150. // line.style.bottom = "162px";
  151. // line.style.right = "40px";
  152. // line.style.width = `${parseFloat(width)}px`;
  153. // line.style.height = "2px";
  154. // line.style.backgroundColor = "#000000";
  155. // document.getElementById("chartdiv").appendChild(line);
  156. // }, 10)
  157. // end am4core.ready()
  158. }