【问题标题】:How to set the background of clicked or selected label on y axis gantt chart Highcharts?如何在y轴甘特图Highcharts上设置点击或选择标签的背景?
【发布时间】:2021-11-17 10:37:36
【问题描述】:

我想在甘特图中突出显示选定的 Y 轴标签。还有什么方法可以给 y 轴标题添加一些背景颜色?

y轴:{ className: "highcharts-color-0", 唯一名称:真, 标题: { 文本:“数据” }, 标签: { 事件:{ 点击:函数(){ 警报(“你好www”); var图表=这个, 系列=图表系列, plotLeft = chart.plotLeft, plotTop = chart.plotTop, plotWidth = chart.plotWidth; 如果(chart.myBackground){ chart.myBackground.destroy(); }

      chart.myBackground = chart.renderer
        .rect(10, plotTop, 500, 30, 1)
        .attr({
          "stroke-width": 2,
          stroke: "red",
          fill: "yellow",
          opacity: 0.5,
          zIndex: -1
        })
        .add();
      }
    }
  }
}

enter image description here

链接到代码笔https://codepen.io/mehrotrarohit07/pen/PoKxvQp?editors=1010

【问题讨论】:

    标签: highcharts gantt-chart


    【解决方案1】:

    尝试使用这种方法:

      yAxis: {
        labels: {
          events: {
            click: function() {
              const chart = this.chart;
              const axis = this.axis;
              const labelPos = this.pos;
              const tick = axis.ticks[labelPos]
              const x = chart.marginRight;
              const width = tick.slotWidth;
              const height = axis.height / (axis.tickPositions.length);
              const y = axis.top + labelPos * height;
    
              chart.renderer
                .rect(x, y, tick.slotWidth, height)
                .attr({
                  fill: 'yellow',
                  zIndex: 0
                })
                .add();
            }
          }
        }
      },
    

    我希望代码中的一切都清楚 - 如有任何疑问,请随时提问。

    演示:https://jsfiddle.net/BlackLabel/qyj327Lx/

    API:https://api.highcharts.com/class-reference/Highcharts.SVGRenderer#rect

    【讨论】:

    • 嗨,@Sebastian Wędzel 感谢您提供的代码,它就像魅力一样。只是一个疑问,我们可以在标签突出显示时更改标签的颜色吗?提前致谢。
    • @RohitMehrotra 我认为您可以将 HTML 事件附加到 yAxis 标签元素来实现它,查看此演示:jsfiddle.net/BlackLabel/3ygb9rwe
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多