说明

有时候项目组要求做的一些折线图比较奇葩一点,比如本年每个月的数据和去年的数据进行比较,而且标注的文字要根据数据的大小变化,大的值标注的文字在上面,小的值标注的文字要在下面,这个案例在官方或者论坛上都没有案例,后面经过自己的一些想法和研究,搞了一个案例出来了。
废话不多说,直接上干货。

示例图

QQ截图20190318110859.png

HTML代码

  1. <div id="main" style="width: 50%;height:400px;margin: 0 auto;"></div>

JS代码

  1. <script src="js/jquery-1.6.2.min.js"></script>
  2. <script src="js/echarts.min.js"></script>
  3. <script>
  4. $(function () {
  5. var data={
  6. title:"xxx标题",
  7. color:["red","blue"],
  8. x:['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
  9. y:[{min:null,max:null,name:"(单位:亿元)"}],
  10. data:[
  11. {
  12. name:"本年累计",
  13. data:["21","3","38","9","8.5","5","35","25","14","11","9","25"]
  14. },
  15. {
  16. name:"去年累计",
  17. data:["13","9.3","9","17","8.5","11.5","9.3","9","17","8.5","27.5","23.666"]
  18. }
  19. ]
  20. };
  21. //前端细节区分
  22. var tip={
  23. tofiexd:2,//y轴刻度保留2位数小数
  24. unit:"%",//y轴刻度单位
  25. }
  26. drawLine("main","line_auto_up_down",data,tip);
  27. });
  28. /**
  29. * @func
  30. * @desc 折线函数
  31. * @param {string} id - string |目标id
  32. * @param {string} type - string | line_auto_up_down(双折线,并且排列标记字的位置
  33. * @param {object} data - 后端数据 @returns {...}
  34. * @param {object} tip - 前端重定义echar画图细节和区别 @returns {...}
  35. *
  36. */
  37. function drawLine(id,type,data,tip){
  38. var myChart = echarts.init(document.getElementById(id));
  39. var option = {
  40. backgroundColor:"rgba(10,10,10,0.1)",
  41. title: {
  42. text: "默认标题",
  43. x: 'center',
  44. top: '5'
  45. },
  46. tooltip: {
  47. trigger:'axis',
  48. confine: true,
  49. axisPointer:{
  50. type:"line"
  51. }
  52. },
  53. legend:{
  54. show:true,
  55. bottom: 15,
  56. },
  57. xAxis: {
  58. type: 'category',
  59. data: ['周一','周二','周三','周四','周五','周六','周日']
  60. },
  61. yAxis: [
  62. {
  63. type: 'value',
  64. show: true,
  65. min: null,
  66. max: null,
  67. name:"(单位:xx)",
  68. splitLine:{show:false}
  69. },
  70. {
  71. type: 'value',
  72. show: false,
  73. min: null,
  74. max: null,
  75. name:"(单位:xx)",
  76. splitLine:{show:false}
  77. },
  78. ],
  79. grid: {
  80. left: '4%',
  81. right: '4%',
  82. bottom: '15%',
  83. top:"20%",
  84. containLabel: true
  85. },
  86. series: []
  87. }
  88. //后端传数据源进行处理
  89. if(data.title){
  90. option.title.text = data.title;
  91. }
  92. if(data.color){
  93. var arrColor = data.color;
  94. option.color = arrColor;
  95. }
  96. if(data.x){
  97. option.xAxis.data = data.x;
  98. }
  99. //全局处理设置Y轴刻度单位(默认无单位),显示多少位小数(默认0位)
  100. var sunit = "",stofixed = 0;
  101. //前端控制设置
  102. if(tip){
  103. if(tip.tofiexd){
  104. stofixed = tip.tofiexd
  105. }
  106. if(tip.unit){
  107. sunit = tip.unit
  108. }
  109. }
  110. for(var mm=0;mm<data.y.length;mm++){
  111. if( data.y[mm].name){
  112. option.yAxis[mm].name = data.y[mm].name;
  113. }
  114. if( data.y[mm].min){
  115. option.yAxis[mm].min = data.y[mm].min;
  116. }
  117. if( data.y[mm].max){
  118. option.yAxis[mm].max = data.y[mm].max;
  119. }
  120. option.yAxis[mm].axisLabel={
  121. formatter: function(val){
  122. if(stofixed == 0){
  123. return val + sunit;
  124. }else{
  125. return val.toFixed(stofixed) + sunit;
  126. }
  127. }
  128. }
  129. }
  130. if(data.y.length > 1){
  131. //双Y轴处理
  132. option.yAxis[1].show = true;
  133. }
  134. //line_auto_up_down标注文字上下自动排序显示类型处理
  135. if(type=="line_auto_up_down"){
  136. var arrVal1=[],arrVal2=[];
  137. for(var i=0;i<data.data.length;i++){
  138. var val=data.data[i];
  139. var tmp={
  140. name:val.name,
  141. yAxisIndex:0,
  142. type:"line",
  143. data:[]
  144. }
  145. for(var j=0;j<val.data.length;j++){
  146. var vval={
  147. value:val.data[j],
  148. label:{show:true},
  149. };
  150. tmp.data.push(vval)
  151. }
  152. option.series.push(tmp)
  153. }
  154. for(var j=0;j<data.data[0].data.length;j++){
  155. arrVal1.push(data.data[0].data[j])
  156. arrVal2.push(data.data[1].data[j])
  157. }
  158. //判断数组大小并进行上下自动显示
  159. if(arrVal1.length == arrVal2.length){
  160. for(var j=0;j<arrVal1.length;j++){
  161. var val0 = option.series[0].data[j].value;
  162. var val1 = option.series[1].data[j].value;
  163. if(Number(val0)>Number(val1) || Number(val0)==Number(val1)){
  164. option.series[0].data[j].label.position="top";
  165. option.series[1].data[j].label.position="bottom";
  166. }else{
  167. option.series[0].data[j].label.position="bottom";
  168. option.series[1].data[j].label.position="top";
  169. }
  170. }
  171. }
  172. }
  173. // 画图
  174. myChart.setOption(option,true);
  175. myChart.resize();
  176. }
  177. </script>

本文为原创文章,转载请附上原文链接!