Skip to content
This repository was archived by the owner on May 9, 2026. It is now read-only.
This repository was archived by the owner on May 9, 2026. It is now read-only.

tooltip 点击后一直显示,formatter 使用 function 返回 html 格式显示失败。 #44

Description

@IvanYue
Image Image
String _generateTooltipTemplate() {
    if (widget.legends.isEmpty) {
      return '''
      function(params) {
        var item = params[0];
        var value = Array.isArray(item.value) ? item.value[1] : item.value;
        return item.name + '<br/>' +
               '<span style="display:inline-block;margin-right:5px;width:8px;height:8px;border-radius:50%;background:' + item.color + '"></span>' +
               item.seriesName + '    ' + (value ? value.toFixed(1) : '-') + ' ${widget.unit}';
      } ''';
    }
// 为多个系列生成模板
    String jsFunction = '''function(params) {var result = params[0].name + '<br/>'; ''';
    for (int i = 0; i < widget.legends.length; i++) {
      jsFunction += '''
      var item${i} = params[${i}];
      var value${i} = Array.isArray(item${i}.value) ? item${i}.value[1] : item${i}.value;
      result += '<span style="display:inline-block;margin-right:5px;width:8px;height:8px;border-radius:50%;background:' + item${i}.color + '"></span>' +
                item${i}.seriesName + '    ' + (value${i} ? value${i}.toFixed(1) : '-') + ' ${widget.unit}' + '<br/>';''';
    }
    jsFunction += '''
      return result;
    }''';
    return jsFunction;
  }
 "tooltip": {
        "trigger": "axis",
        "triggerOn": "mousemove|click",
        "alwaysShowContent": false,
        'formatter': _generateTooltipTemplate(),
        "enterable": false,
        "confine": true,
        "hideDelay": 100,
        "axisPointer": {"type": "line", "animation": false}
      }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions