我在我的代码中使用HighChart.目前,当用户单击图例项(例如,"ABC")时,标记为"ABC"的对应饼图将变为灰色.再次单击时,灰色将被移除,饼图将恢复为其原始 colored颜色 .现在,我希望通过在饼变为灰色时向"ABC"图例项添加删除线效果,并在饼恢复到其原始 colored颜色 时删除删除线来增强此功能.

Highcharts.chart('container', {
  series: [{
    type: 'pie',
    data: [1, 2, 3, 4],
    showInLegend: true,
    point: {
      events: {
        legendItemClick: function() {
          var originalColor = this.options.originalColor; // Retrieve original color from options
          var newColor = (this.color === 'gray') ? originalColor : 'gray'; // Toggle between gray and original color
          
          // Toggle visibility of data labels
          var showDataLabels = (newColor !== 'gray');

          this.update({
            color: newColor,
            dataLabels: {
              enabled: showDataLabels
            }
          });
          
          return false;
        }
      }
    },
    events: {
      afterInit: function() {
        // Store original color for each point
        this.points.forEach(function(point) {
          point.options.originalColor = point.color;
        });
      }
    }
  }],
  accessibility: {
    enabled: false,
  },
});
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container"></div>

推荐答案

您可以使用图例labelFormatter在应用gray colored颜色 时注入样式化的HTML标签.

Highcharts.chart('container', {
  legend: {
    labelFormatter: function () {
      return this.color === 'gray'
        ? `<span style="text-decoration:line-through">${this.name}</span>`
        : this.name;
    },
  },
  series: [{
    type: 'pie',
    data: [1, 2, 3, 4],
    showInLegend: true,
    point: {
      events: {
        legendItemClick: function() {
          var originalColor = this.options.originalColor; // Retrieve original color from options
          var newColor = (this.color === 'gray') ? originalColor : 'gray'; // Toggle between gray and original color
          
          // Toggle visibility of data labels
          var showDataLabels = (newColor !== 'gray');

          this.update({
            color: newColor,
            dataLabels: {
              enabled: showDataLabels
            }
          });
          
          return false;
        }
      }
    },
    events: {
      afterInit: function() {
        // Store original color for each point
        this.points.forEach(function(point) {
          point.options.originalColor = point.color;
        });
      }
    }
  }],
  accessibility: {
    enabled: false,
  },
});
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container"></div>

Javascript相关问答推荐

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

我不知道为什么setwritten包装promise 不能像我预期的那样工作

配置WebAssembly/Emscripten本地生成问题

从Node JS将对象数组中的数据插入Postgres表

使用Java脚本根据按下的按钮更改S文本

如何解决useState错误—setSelect Image不是函数''

还原器未正确更新状态

禁用.js文件扩展名并从目录导入隐式根index.js时,找不到NodeJS导入模块

以Angular 实现ng-Circle-Progress时出错:模块没有导出的成员

使用getBorbingClientRect()更改绝对元素位置

NG/Express API路由处理程序停止工作

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

使用插件构建包含chart.js提供程序的Angular 库?

警告框不显示包含HTML输入字段的总和

为什么延迟在我的laravel项目中不起作用?

基于产品ID更新条带产品图像的JavaScript命中错误

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

Jexl to LowerCase()和Replace()

递归地将JSON对象的内容上移一级

单击子按钮时将活动切换类添加到父分区