chart.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import * as echarts from "echarts";
  2. export function chartBar(data) {
  3. let countryColors = [
  4. 'rgba(224,96,93)',
  5. 'rgba(231,168,79)',
  6. 'rgba(163,201,90)',
  7. 'rgba(90,201,148)',
  8. 'rgba(69,239,209)',
  9. 'rgba(69,160,239)',
  10. 'rgba(69,73,239)',
  11. 'rgba(231,79,176)',
  12. 'rgba(224,93,142)',
  13. 'rgba(155,90,201)',
  14. ]
  15. // let yLabel = []
  16. let seriesData = []
  17. let areaStyleColor = []
  18. let axisLabelRich = {
  19. text: {
  20. width: 40,
  21. fontSize : 16,
  22. marginLeft: '40px'
  23. }
  24. };
  25. data.forEach((item, index) => {
  26. // yLabel.push(item.student.student_name)
  27. seriesData.push(Math.abs(item.score_total))
  28. axisLabelRich[index] = {
  29. height: 40, // 图片高度
  30. width: 40, // 图片宽度
  31. backgroundColor: {
  32. image: item.student.student_cartoon_photo, // 图片路径
  33. }
  34. }
  35. if (item.score_total < 0) {
  36. areaStyleColor.push('rgba(231,79,176,0.2)')
  37. } else {
  38. areaStyleColor.push('rgba(0,0,0,0)')
  39. }
  40. })
  41. return {
  42. grid: {
  43. top: 0,
  44. bottom: 0,
  45. left: 140,
  46. right: '10%'
  47. },
  48. xAxis: {
  49. max: 'dataMax'
  50. },
  51. yAxis: {
  52. type: 'category',
  53. boundaryGap: true,
  54. splitArea: {
  55. show: true,
  56. interval: 0,
  57. areaStyle: {
  58. color: areaStyleColor
  59. },
  60. },
  61. inverse: true,
  62. animationDuration: 1000,
  63. animationDurationUpdate: 1000,
  64. axisLabel: {
  65. show: true,
  66. formatter: function(value, index) {
  67. // 根据你的需求调整 formatter 函数
  68. // 这里只是一个简单的示例,假设你想在所有 Y 轴 label 前都添加一个图片
  69. return `{${index}| } {text|${data[index].student.student_name} } `;
  70. },
  71. rich : axisLabelRich
  72. // rich: {
  73. // // img1: {
  74. // // height: 30, // 图片高度
  75. // // width: 30, // 图片宽度
  76. // // backgroundColor: {
  77. // // image: '', // 图片路径
  78. // // // 如果你使用的是模块化的方式(如 Vue),你可能需要使用 require 或 import 来引入图片
  79. // // // 例如:image: require('@/assets/image.png')
  80. // // }
  81. // // },
  82. // text: {
  83. // width: 40,
  84. // fontSize : 16,
  85. // marginLeft: '40px'
  86. // }
  87. // }
  88. }
  89. },
  90. series: [{
  91. itemStyle: {
  92. color: function(param) {
  93. return countryColors[(countryColors.length + param.dataIndex) % countryColors
  94. .length] ||
  95. '#5470c6';
  96. },
  97. height: 40
  98. },
  99. type: 'bar',
  100. data: seriesData,
  101. label: {
  102. show: true,
  103. position: 'right',
  104. valueAnimation: true,
  105. }
  106. }],
  107. legend: {},
  108. animationDuration: 1000,
  109. animationDurationUpdate: 1000,
  110. animationEasing: 'linear',
  111. animationEasingUpdate: 'linear',
  112. };
  113. }