找回密码
 立即注册
首页 业界区 安全 echart图表折线图

echart图表折线图

钨哄魁 2025-9-26 11:48:44
  1. var cdata=[
  2.     2000.56,
  3.     1800,
  4.     2239.56,
  5.     3239.56,
  6.     2239.56
  7. ]
  8. let lineColor=['#C5E879', '#2DF0DB']
  9. let cxdata=[2021,2022,2023,2024,2025]
  10. // 增长率数据,正数为增长(↑),负数为下降(↓),实际根据计算得到
  11.         // var growthRates = [12.35, -12.29, 12.35, 12.35, 12.35];
  12.         var growthRates =  [12.35, -12.29, 12.35, 12.35, 12.35];
  13.         // 处理数据,用于标注显示(拼接数值和增长率)
  14.         var labelData = cdata.map((val, index) => {
  15.           let growthSymbol = growthRates[index] >= 0 ? '↑' : '↓';
  16.           let str = "";
  17.           if (growthRates[index] >= 0) { // 增长
  18.             str = `{value1|${val}人/km²}\n{value2|${Math.abs(growthRates[index])}% ${growthSymbol}}`
  19.           } else {
  20.             str = `{value1|${val}人/km²}\n{value3|${Math.abs(growthRates[index])}% ${growthSymbol}}`
  21.           }
  22.           return str;
  23.         });
  24.         // 颜色渐变
  25.         const createGradient = (color1, color2) => {
  26.           return new echarts.graphic.LinearGradient(0, 0, 1, 0, [
  27.             { offset: 0, color: color1 },
  28.             { offset: 1, color: color2 }
  29.           ]);
  30.         };
  31.         // 处理最后一个数据显示圆圈
  32.         const processData = (data, markerImage, length) => {
  33.           return data.map((value, index) => {
  34.             // 检查是否为最后一个数据点
  35.             if (index === length - 1) {
  36.               return {
  37.                 value: value,
  38.                 symbol: `image://${markerImage}`,
  39.                 symbolSize: [47, 38],
  40.                 symbolOffset: [0, 0]
  41.               };
  42.             }
  43.             return value;
  44.           });
  45.         };
  46.         const processDataFormat = processData(cdata, '', cdata.length)
  47.         option = {
  48.            backgroundColor: '#0c2d55',
  49.           title: {
  50.             // text: '', // 可根据需求设置标题
  51.             // left: 'center'
  52.           },
  53.           tooltip: {
  54.             trigger: 'axis',
  55.             axisPointer: {
  56.               type: 'shadow',
  57.               shadowStyle: {
  58.                 color: 'rgba(255, 255, 255, 0.1)'
  59.               }
  60.             },
  61.             backgroundColor: 'rgba(18, 28, 40, 0.9)',
  62.             borderColor: 'rgba(255, 255, 255, 0.1)',
  63.             borderWidth: 1,
  64.             textStyle: {
  65.               color: '#E4F3FF'
  66.             },
  67.             // formatter: function (params) {
  68.             //     let item = params[0];
  69.             //     let growthSymbol = growthRates[item.dataIndex] >= 0 ? '↑' : '↓';
  70.             //     return `${this.cxdata[item.dataIndex]}<br/>${item.seriesName}: ${item.value}人/㎡<br/>增长率: ${Math.abs(growthRates[item.dataIndex])}% ${growthSymbol}`;
  71.             // }
  72.           },
  73.           xAxis: {
  74.             type: 'category',
  75.             data: cxdata,
  76.             axisLabel: {
  77.               fontSize: 18,
  78.               fontFamily: "Source Han Sans",
  79.               color: '#E4F3FF' // 可选:设置字体颜色
  80.             }
  81.           },
  82.           yAxis: {
  83.             type: 'value',
  84.             axisTick: { show: false },
  85.             axisLine: { show: false },
  86.             axisLabel: {
  87.               fontSize: 16,
  88.               fontFamily: "Source Han Sans",
  89.               color: '#BCC8D4' // 可选:设置字体颜色
  90.             }
  91.           },
  92.           grid: {
  93.             left: '2%',
  94.             right: '2%',
  95.             bottom: '3%',
  96.             containLabel: true
  97.           },
  98.           series: [
  99.             {
  100.               name: '青年人口密度',
  101.               type: 'line',
  102.               data: processDataFormat,
  103.               lineStyle: {
  104.                 color: createGradient(lineColor[0], lineColor[1]), // 折线颜色,类似示例中的红色
  105.               },
  106.               symbol: 'circle',  // 隐藏拐点圆
  107.               smooth: false,
  108.               // symbolSize: 6, // 必须设置大小,确保标签有定位基准
  109.               itemStyle: {
  110.                 color: 'rgba(76, 175, 80, 0)', // 拐点颜色
  111.                 borderWidth: 0
  112.               },
  113.               label: {
  114.                 show: true,
  115.                 ignore: true,
  116.                 position: 'top', // 标注位置,可根据需求调整为 'bottom' 等
  117.                 formatter: function (params) {
  118.                   return labelData[params.dataIndex];
  119.                 },
  120.                 rich: { // 定义文本颜色
  121.                   value1: {
  122.                     color: '#fff',
  123.                     fontSize: 16,
  124.                     // marginBottom: 5
  125.                   },
  126.                   value2: {
  127.                     color: '#43CF7C',
  128.                     fontSize: 16
  129.                   },
  130.                   value3: {
  131.                     color: '#FF5733',
  132.                     fontSize: 16
  133.                   },
  134.                 },
  135.                 lineHeight: 30
  136.               },
  137.             }
  138.           ],
  139.         };
复制代码
效果图:
1.png

 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册