javascript - Highcharts 标签重绘

标签 javascript jquery css highcharts

我正在工作this JSFIDDLE它显示了如何将标签添加到图表中的点。在图表没有响应的情况下,这可以很好地固定标签的位置。更改 fiddle 的宽度,标签位于原始位置。我希望标签保留在最后一列的顶部。

我已经尝试使用 loadredraw 事件并且我很接近,但是我需要在创建新标签之前销毁标签。

events: {
    load: function () {
        var point = this.series[0].points[2];
        this.renderer.label(
                '<div class="text-center">GOAL TO REACH</div>', 270, 50, 'callout', point.plotX + this.plotLeft, point.plotY + this.plotTop
        )
        .css({
            color: '#FFFFFF'
        })
        .attr({
            fill: 'rgba(0, 0, 0, 0.75)',
            padding: 8,
            r: 5,
            zIndex: 6
        })
        .add();
    },
    redraw: function() {
        var point = this.series[0].points[2];
        this.renderer.label(
                '<div class="text-center">GOAL TO REACH</div>', 270, 50, 'callout', point.plotX + this.plotLeft, point.plotY + this.plotTop
        )
        .css({
            color: '#FFFFFF'
        })
        .attr({
            fill: 'rgba(0, 0, 0, 0.75)',
            padding: 8,
            r: 5,
            zIndex: 6
        })
        .add();
    }
 }

当图表的宽度改变时,我希望标签跟随该点。

我尝试使用工具提示,但在前两列禁用它时遇到问题。我可以重新绘制工具提示,但无法从所有其他列中禁用它

最佳答案

查看这个更新的 JSFiddle

我在全局范围内添加了一个变量var theLabel;

然后在图表初始绘制上,设置 theLabel = chart.renderer.label(...)

然后

redraw: function() {
    theLabel.destroy();
        point = this.series[0].points[2];
        theLabel = this.renderer.label('<div class="text-center">Reach 10 active subscriptions<br/>and enjoy a one year FREE membership!</div>', this.plotWidth - 200, 50, 'callout', point.plotX + this.plotLeft, point.plotY + this.plotTop)
    .css({
      color: '#FFFFFF'
    })
    .attr({fill: 'rgba(0, 0, 0, 0.75)',
      padding: 8,
      r: 5,
      zIndex: 6
    })
    .add();
}

注意在redraw:中:

  1. 我调用 theLabel.destroy() 删除原始标签
  2. 我将 chart 更改为 this
  3. 我没有将其绘制在 x = 270 处,而是将其绘制在 this.plotWidth - 200 处,以使其或多或少位于图的右侧V 形始终指向红色条。

希望这对你有用

关于javascript - Highcharts 标签重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37456853/

相关文章:

javascript - 如何获取传单中cql_filtered wms图层的bbox?

jquery - 通过ajax调用刷新部分 View 挂起应用程序

html - 如何在div中垂直居中链接?

javascript - 堆叠图像/图标链接

html - 无法将 Css 应用到我的 ASP :Label

javascript - 灰尘模板包含多次

javascript - 将 div onload 滚动到底部,以及使用新数据更新时 - angularjs

javascript - 有没有办法通过javascript中的appium获取按钮的绝对位置

javascript - 在模糊事件期间选择此输入

javascript - 从点击触发的新页面获取被点击的html元素的访问权限