Column_g2.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { onMount } from "solid-js";
  2. import { Chart, registerShape } from '@antv/g2';
  3. // import DataSet from '@antv/data-set';
  4. import "../../../assets/style/CommonBigScreenHome.css"
  5. let cli1 = [
  6. "135,206,250",
  7. "0,191,255",
  8. "127,255,170",
  9. "0,255,127",
  10. "50,205,50",
  11. "240,230,140",
  12. "255,215,0",
  13. "255,127,80",
  14. "255,99,71",
  15. "255,0,0",
  16. ], index = 0, index1 = 0;
  17. // 绘制柱状图顶部
  18. registerShape('interval', 'triangle', {
  19. // 1. 定义关键点
  20. getPoints(cfg) {
  21. const x = cfg.x;
  22. const y = cfg.y;
  23. const width = cfg.size;
  24. return [
  25. { x: x - width / 2, y },
  26. { x, y: y + width*2 },
  27. { x: x + width / 2, y },
  28. { x, y: y - width*2 },
  29. ];
  30. },
  31. // 2. 绘制
  32. draw(cfg, group) {
  33. const points = this.parsePoints(cfg.points); // 将0-1空间的坐标转换为画布坐标
  34. const more = (points[2].x - points[0].x)/4;
  35. const polygon = group.addShape('path', {
  36. attrs: {
  37. path: [
  38. ['M', points[0].x, points[0].y], // 左点
  39. ['L', points[1].x, points[0].y+more], // 上点
  40. ['L', points[2].x, points[2].y], // 右点
  41. ['L', points[3].x, points[0].y-more], // 下点
  42. ],
  43. fillOpacity: 0.95,
  44. fill: `rgb(${cli1[index % cli1.length]})`
  45. },
  46. });
  47. index++
  48. return polygon;
  49. },
  50. })
  51. // 绘制柱状图楞线
  52. registerShape('interval', 'edgeLine', {
  53. // 1. 定义关键点
  54. getPoints(cfg) {
  55. const x = cfg.x;
  56. const y = cfg.y;
  57. const y0 = cfg.y0;
  58. const width = cfg.size;
  59. const p1 = (width / 100).toFixed(4) - 0
  60. return [
  61. { x: x - p1, y: y - width / 2 },
  62. { x: x + p1, y: y - width / 2 },
  63. { x: x + p1, y: y0 },
  64. { x: x - p1, y: y0 },
  65. ];
  66. },
  67. // 2. 绘制
  68. draw(cfg, group) {
  69. const points = this.parsePoints(cfg.points); // 将0-1空间的坐标转换为画布坐标
  70. const polygon = group.addShape('path', {
  71. attrs: {
  72. path: [
  73. ['M', points[0].x, points[0].y],
  74. ['L', points[1].x, points[1].y],
  75. ['L', points[2].x, points[2].y],
  76. ['L', points[3].x, points[3].y],
  77. ],
  78. fillOpacity: 1,
  79. fill: `l(90) 0:rgba(${cli1[index1 % cli1.length]}, 1) 1:rgba(${cli1[index1 % cli1.length]}, 0.4)`
  80. },
  81. });
  82. index1++
  83. return polygon;
  84. },
  85. })
  86. function Column(prop) {
  87. let $canvas = undefined;
  88. function TrendChart(width, height, $canvas) {
  89. const data = prop.list.map(v => {
  90. return {
  91. type: v.scheduleName,
  92. value: v.watchRate
  93. }
  94. });
  95. const chart = new Chart({
  96. container: $canvas,
  97. autoFit: true,
  98. height,
  99. width,
  100. padding: [
  101. 20,
  102. 20,
  103. 50,
  104. 20
  105. ]
  106. });
  107. chart.data(data);
  108. chart.legend(false);
  109. chart.axis('type', {
  110. tickLine: {
  111. alignTick: false,
  112. },
  113. label: {
  114. autoHide: false,
  115. formatter: n => {
  116. const nl = n.split(""), len = nl.length > 6 ? 5 : nl.length, out = [];
  117. for (let i = 0; i < len; i++) {
  118. if (i % 2 !== 1 || i === len - 1) {
  119. out.push(nl[i]);
  120. continue
  121. }
  122. out.push(nl[i] + '\n');
  123. }
  124. if (nl.length <= 6) return out.join("")
  125. else return out.join("") + "..."
  126. },
  127. style: {
  128. fill: "#fff"
  129. }
  130. }
  131. });
  132. chart.axis('value', false);
  133. chart.tooltip(false);
  134. chart.interval().position('type*value').color('type', cli1.map(v => `l(90) 0:rgba(${v},0.8) 1:rgba(${v},0.2)`));
  135. index = 0;
  136. chart.interval().position('type*value').shape('triangle');
  137. index = 0;
  138. chart.interval().position('type*value').shape('edgeLine');
  139. // 添加文本标注
  140. data.forEach((item) => {
  141. chart
  142. .annotation()
  143. .text({
  144. position: [item.type, item.value],
  145. content: item.value + '%',
  146. style: {
  147. textAlign: 'center',
  148. fill: "#fff"
  149. },
  150. offsetY: -10,
  151. });
  152. });
  153. chart.render();
  154. }
  155. onMount(() => {
  156. TrendChart(prop.width, prop.height, $canvas)
  157. })
  158. return (
  159. <div class="livRang" style={{
  160. width:`${prop.width}px`
  161. }}>
  162. <div class="head">{prop.title}</div>
  163. <div ref={$canvas}></div>
  164. </div>
  165. );
  166. }
  167. export default Column;