highcharts - 将设计添加到plotLabel Highcharts

标签 highcharts

我正在制作 highchart 上的动态图表。我的问题是,有没有办法在我的绘图标签上添加设计? 目前是这样的

enter image description here

我希望它看起来像这样

enter image description here

这是我的代码

$(function() {

 Highcharts.setOptions({
    global: {
     useUTC: false
   }
});

 // Create the chart
  $('#container').highcharts('StockChart', {
  chart: {
  events: {
    load: function() {

      // set up the updating of the chart each second
      var series = this.series[0],
        hasPlotLine = false,
        $button = $('#button'),
        chart = $('#container').highcharts(),
        yAxis = chart.yAxis[0],
        plotLine,
        d,
        newY;

      yAxis.addPlotLine({
        value: 66,
        color: 'red',
        width: 2,
        id: 'plot-line-1',
                    label: {
                text: 66,
                align: 'right',
                y: newY,
                x: 0
            }
      });

      setInterval(function() {

        var x = (new Date()).getTime(), // current time
          y = Math.round(Math.random() * 100);
        series.addPoint([x, y], true, true);

        plotLine = yAxis.plotLinesAndBands[0].svgElem;

        d = plotLine.d.split(' ');

        newY = yAxis.toPixels(y) - d[2];

        plotlabel = yAxis.plotLinesAndBands[0].label;
        plotlabel.animate({
            translateY: newY,
            text: Highcharts.numberFormat(y, 2)
          }, {
            duration: 400,
            step: function() {
              $(this.element).html(Highcharts.numberFormat(this.textStr,2));
            },
            complete: function() { }
          }),


          plotLine.animate({
            translateY: newY
          }, 400);


      }, 1000);
    }
  }
},

rangeSelector: {
  buttons: [{
    count: 1,
    type: 'minute',
    text: '1M'
  }, {
    count: 5,
    type: 'minute',
    text: '5M'
  }, {
    type: 'all',
    text: 'All'
  }],
  inputEnabled: false,
  selected: 0
},

title: {
  text: 'Live random data'
},
yAxis: [{
  opposite: false,
  title: {
       enabled: false
  }
}],

exporting: {
  enabled: false
},

series: [{
  name: 'Random data',
  data: (function() {
    // generate an array of random data
    var data = [],
      time = (new Date()).getTime(),
      i;

    for (i = -999; i <= 0; i += 1) {
      data.push([
        time + i * 1000,
        Math.round(Math.random() * 100)
      ]);
    }
    return data;
  }())
}]

 });

 });

这是我的工作代码供您引用http://jsfiddle.net/t7x2jehn/

最佳答案

您不能像这样设置情节线标签的样式,但您可以使用更多样式选项创建自己的标签并为其设置动画 - renderer.label .

在间隔之前创建标签:

  const labelOffset = 15
      const plotbandLabel = this.renderer.label((66).toFixed(2), chart.plotLeft + chart.plotWidth - 8, yAxis.toPixels(66) - labelOffset, 'rect').css({
          color: '#FFFFFF'
        }).attr({
          align: 'right',
          zIndex: 99,
          fill: 'rgba(0, 0, 0, 0.75)',
          padding: 8
        })
        .add()

在每个间隔上,为其设置动画:

 plotbandLabel.animate({
            y: yAxis.toPixels(y) - labelOffset
          }, {
            duration: 400,
            step: function() {
              this.attr({
                text: yAxis.toValue(this.y + labelOffset).toFixed(2)
              })
            },
            complete: function() {
              this.attr({
                text: y.toFixed(2)
              })
            }
          }),

示例:http://jsfiddle.net/x8vhp0gr/

关于highcharts - 将设计添加到plotLabel Highcharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45210835/

相关文章:

c# - 使用 MVC3 根据 Highchart 条形图中的值更改条形颜色

highcharts - Highstock:从 rangeSelector 中删除单词 "Zoom"

javascript - 为什么我的 addPoint() 在我的 highcharts 代码中没有被识别为函数?

javascript - Highstock 系列与前一天相比

javascript - 仅在系列内堆叠 Highcharts 列

javascript - 使 highchart 成为动态数据驱动的

javascript - 如何防止在图形区域或浏览器窗口调整大小时隐藏 highcharts 工具提示弹出窗口?

php - 使用数据库中条目的 Highcharts 制作条形图

javascript - 基本的 jquery fiddle 到 Angular 指令

javascript - 图表底部的 Highcharts 信息气泡 - 这可能吗?