Echarts自定义tooltip悬浮提示框

238 阅读1分钟
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ECharts Line Chart Demo</title>
  <!-- ECharts script -->
  <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
  <style>
    #main {
      width: 600px;
      height: 400px;
    }
  </style>
</head>

<body>
  <div id="main"></div>

  <script>
    // Initialize the chart
    var myChart = echarts.init(document.getElementById('main'))

    // Specify the chart options
    var option = {
      title: {
        text: 'Smooth Line Chart with Colored Segments'
      },
      tooltip: {
        trigger: 'axis',
        backgroundColor: "rgba(0,0,0,0.3)",
        textStyle: {
          show: false,
          color: "#FFFFFF", // 文字的颜色
        },
        borderColor: "rgba(0,0,0,0.3)",
        borderWidth: 1,
        formatter: function (params) {
          console.log('params', params)
          return (
            params[0].marker +
            "心率: " + params[0].data +
            "<br/><span style='margin-left:0.25rem'></span>" +
            params[0].axisValue
          )
        },
      },
      xAxis: {
        type: 'category',
        data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        axisTick: {
          show: false  // Disable x-axis tick lines
        },
        axisLine: {
          show: true,  // Enable x-axis line
          lineStyle: {
            color: '#CCCCCC',  // Set y-axis line color
          },
        },
        axisLabel: {
          color: '#000000'  // Set x-axis label color
        },
        splitLine: {
          show: true,  // Show x-axis grid lines
          lineStyle: {
            type: 'dashed'  // Set grid lines to dashed
          }
        }
      },
      yAxis: {
        type: 'value',
        axisTick: {
          show: false  // Disable y-axis tick lines
        },
        axisLabel: {
          color: '#000000'  // Set x-axis label color
        },
        axisLine: {
          show: true,  // Enable y-axis line
          lineStyle: {
            color: '#CCCCCC',  // Set y-axis line color
          },
        },
        splitLine: {
          show: true,  // Show y-axis grid lines
          lineStyle: {
            type: 'dashed'  // Set grid lines to dashed
          }
        }
      },
      series: [
        {
          name: 'Values',
          type: 'line',
          smooth: true,
          data: [12, 18, 23, 28, 34, 26, 19, 14, 22, 30, 32, 27],
          showSymbol: false  // Disable the points at each data mark
        }
      ],
      visualMap: {
        show: false,
        dimension: 1,
        pieces: [
          {
            gt: 10,
            lte: 20,
            color: 'red'
          },
          {
            gt: 20,
            lte: 30,
            color: 'blue'
          },
          {
            gt: 30,
            lte: 40,
            color: 'green'
          }
        ]
      }
    }

    // Set the chart options
    myChart.setOption(option)
  </script>
</body>

</html>