[关闭]
@liruiyi962464 2024-05-31T07:07:41.000000Z 字数 4833 阅读 82

动态循环环状图中心有字

前端

  1. var angle = 0 //角度,用来做简单的动画效果的
  2. var currentIndex = 0
  3. var color = ['#00FDFE', '#00D8FF']
  4. var legend = ['档口', '仓储']
  5. var seriesData = [
  6. { name: '档口', value: 5492 },
  7. { name: '仓储', value: 6492 },
  8. ]
  9. option = {
  10. backgroundColor: "#050e31",
  11. color: color,
  12. legend: {
  13. itemHeight: 14,
  14. itemWidth: 14,
  15. icon: "rect",
  16. orient: 'vertical',
  17. top: 'center',
  18. right: 0,
  19. textStyle: {
  20. align: 'left',
  21. verticalAlign: 'middle',
  22. rich: {
  23. name: {
  24. color: '#A0C2BF',
  25. fontSize:14,
  26. },
  27. value: {
  28. color: '#A0C2BF',
  29. fontSize: 14,
  30. },
  31. rate: {
  32. color: '#A0C2BF',
  33. fontSize: 14,
  34. },
  35. },
  36. },
  37. data: legend,
  38. formatter: (name) => {
  39. if (seriesData.length) {
  40. const item = seriesData.filter((item) => item.name === name)[0];
  41. return `{name|${name}:}{rate| ${item.value}%} {value| |} {value| ${item.value}}`;
  42. }
  43. },
  44. },
  45. series: [{
  46. // 内环线 1
  47. name: "ring5",
  48. type: 'custom',
  49. coordinateSystem: "none",
  50. renderItem: function (params, api) {
  51. return {
  52. type: 'arc',
  53. shape: {
  54. cx: api.getWidth() / 2,
  55. cy: api.getHeight() / 2,
  56. r: Math.min(api.getWidth(), api.getHeight()) / 2 * 0.37,// 调整位置
  57. },
  58. style: {
  59. stroke: "#0CD3DB",
  60. fill: "transparent",
  61. lineWidth: 1
  62. },
  63. silent: true
  64. };
  65. },
  66. data: [0]
  67. }, {
  68. name: "ring5",
  69. type: 'custom',
  70. coordinateSystem: "none",
  71. renderItem: function (params, api) {
  72. return {
  73. type: 'arc',
  74. shape: {
  75. cx: api.getWidth() / 2,
  76. cy: api.getHeight() / 2,
  77. r: Math.min(api.getWidth(), api.getHeight()) / 2 * 0.52,
  78. startAngle: (180 + angle) * Math.PI / 180,
  79. endAngle: (270 + angle) * Math.PI / 180
  80. },
  81. style: {
  82. stroke: "#0CD3DB",
  83. fill: "transparent",
  84. lineWidth: 1.5
  85. },
  86. silent: true
  87. };
  88. },
  89. data: [0]
  90. }, {
  91. name: "ring5",
  92. type: 'custom',
  93. coordinateSystem: "none",
  94. renderItem: function (params, api) {
  95. return {
  96. type: 'arc',
  97. shape: {
  98. cx: api.getWidth() / 2,
  99. cy: api.getHeight() / 2,
  100. r: Math.min(api.getWidth(), api.getHeight()) / 2 * 0.65,
  101. startAngle: (360 + -angle) * Math.PI / 180,
  102. endAngle: (80 + -angle) * Math.PI / 180
  103. },
  104. style: {
  105. stroke: "#0CD3DB",
  106. fill: "transparent",
  107. lineWidth: 1
  108. },
  109. silent: true
  110. };
  111. },
  112. data: [0]
  113. }, {
  114. name: "ring5",
  115. type: 'custom',
  116. coordinateSystem: "none",
  117. renderItem: function (params, api) {
  118. return {
  119. type: 'arc',
  120. shape: {
  121. cx: api.getWidth() / 2,
  122. cy: api.getHeight() / 2,
  123. r: Math.min(api.getWidth(), api.getHeight()) / 2 * 0.65,
  124. startAngle: (180 + -angle) * Math.PI / 180,
  125. endAngle: (260 + -angle) * Math.PI / 180
  126. },
  127. style: {
  128. stroke: "#0CD3DB",
  129. fill: "transparent",
  130. lineWidth: 1.5
  131. },
  132. silent: true
  133. };
  134. },
  135. data: [0]
  136. }, {
  137. name: "ring5",
  138. type: 'custom',
  139. coordinateSystem: "none",
  140. renderItem: function (params, api) {
  141. let x0 = api.getWidth() / 2;
  142. let y0 = api.getHeight() / 2;
  143. let r = Math.min(api.getWidth(), api.getHeight()) / 2 * 0.65;
  144. let point = getCirlPoint(x0, y0, r, (180 + -angle))
  145. return {
  146. type: 'circle',
  147. shape: {
  148. cx: point.x,
  149. cy: point.y,
  150. r: 2
  151. },
  152. style: {
  153. stroke: "#0CD3DB",//粉
  154. fill: "#0CD3DB",
  155. lineWidth: 5
  156. },
  157. silent: true
  158. };
  159. },
  160. data: [0]
  161. }, {
  162. name: "ring5", //绿点
  163. type: 'custom',
  164. coordinateSystem: "none",
  165. renderItem: function (params, api) {
  166. let x0 = api.getWidth() / 2;
  167. let y0 = api.getHeight() / 2;
  168. let r = Math.min(api.getWidth(), api.getHeight()) / 2 * 0.65;
  169. let point = getCirlPoint(x0, y0, r, (360 + -angle))
  170. return {
  171. type: 'circle',
  172. shape: {
  173. cx: point.x,
  174. cy: point.y,
  175. r: 2
  176. },
  177. style: {
  178. stroke: "#0CD3DB", //绿
  179. fill: "#0CD3DB",
  180. lineWidth: 5
  181. },
  182. silent: true
  183. };
  184. },
  185. data: [0]
  186. }, {
  187. name: '占比',
  188. type: 'pie',
  189. center: ['50%', '50%'],
  190. radius: ['40%', '50%'],
  191. label: {
  192. normal: {
  193. show: false,
  194. position: 'center',
  195. formatter: '{value|{c}}\n{label|{b}}',
  196. },
  197. },
  198. labelLine: {
  199. show: false,
  200. length: 0,
  201. length2: 0,
  202. },
  203. itemStyle: {//间隔
  204. normal: {
  205. borderWidth: 3,
  206. borderColor: '#050e31',
  207. },
  208. },
  209. data: seriesData,
  210. },
  211. ],
  212. title: {
  213. text: '', // 初始标题为空
  214. subtext: '', // 初始子标题为空
  215. left: 'center',
  216. top: 'center',
  217. textStyle: {
  218. fontSize: '25',
  219. // 其他样式设置...
  220. }
  221. }
  222. };
  223. myChart.setOption(option);
  224. myChart.on('mouseover', (params) => {
  225. currentIndex = params.dataIndex;
  226. highlightPie(currentIndex);
  227. updateCenterTitle(params); // 传递params以获取当前高亮部分的详细信息
  228. });
  229. function highlightPie(currentIndex) {
  230. myChart.dispatchAction({
  231. type: 'highlight',
  232. seriesIndex: 0,
  233. dataIndex: currentIndex
  234. });
  235. // 不需要downplay,因为只有一个高亮部分
  236. }
  237. function updateCenterTitle(params) {
  238. var currentItem = seriesData[params.dataIndex]; // 获取当前高亮部分的数据项
  239. option.title.text = currentItem.name ;
  240. option.title.subtext = currentItem.value;
  241. myChart.setOption({
  242. title: {
  243. text: option.title.text, // 设置新的标题文本
  244. subtext: option.title.subtext // 设置新的子标题文本
  245. }
  246. });
  247. }
  248. updateCenterTitle({ dataIndex: 0 }); // 默认加载第一条数据
  249. //获取圆上面某点的坐标(x0,y0表示坐标,r半径,angle角度)
  250. function getCirlPoint(x0, y0, r, angle) {
  251. let x1 = x0 + r * Math.cos(angle * Math.PI / 180)
  252. let y1 = y0 + r * Math.sin(angle * Math.PI / 180)
  253. return {
  254. x: x1,
  255. y: y1
  256. }
  257. }
  258. function draw() {
  259. angle = angle + 3
  260. myChart.setOption(option, true)
  261. }
  262. setInterval(function () {
  263. //用setInterval做动画感觉有问题
  264. draw()
  265. }, 100);

修改外围环图

  1. //获取圆上面某点的坐标(x0,y0表示坐标,r半径,angle角度)
  2. function getCirlPoint(x0, y0, r, angle) {
  3. let x1 = x0 + r * Math.cos(angle * Math.PI / 180)
  4. let y1 = y0 + r * Math.sin(angle * Math.PI / 180)
  5. return {
  6. x: x1,
  7. y: y1
  8. }
  9. }
  10. function draw() {
  11. angle = (angle + 3) % 360; // 更新角度,并确保它在0到360之间
  12. startAngle = angle; // 更新旋转扇区的起始角度
  13. // 更新旋转扇区的结束角度
  14. option.series[0].data[option.series[0].data.length - 1].endAngle = startAngle + 3;
  15. // 设置图表的新配置
  16. myChart.setOption(option, true); // 第二个参数为true表示不合并之前的数据和配置项
  17. }
  18. setInterval(function () {
  19. //用setInterval做动画感觉有问题
  20. draw()
  21. }, 100);
  1. //updateCenterTitle({ dataIndex: 0 }); // 默认加载第一条数据
  2. // 示例:假设您有一个初始数据项,想要在图表加载时设置为其标题和子标题
  3. var initialDataItem = {
  4. name: '初始标题',
  5. value: '初始子标题值'
  6. };
  7. // 使用初始数据设置标题和子标题
  8. option.title.text = initialDataItem.name;
  9. option.title.subtext = initialDataItem.value;
  10. // 更新图表以反映初始数据的标题和子标题
  11. myChart.setOption({
  12. title: {
  13. text: option.title.text,
  14. subtext: option.title.subtext
  15. }
  16. });
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注