更新xAxis单个标签位置以打印Highcharts

问题描述 投票:0回答:1

当用户想要打印图表时,是否可以更新特定的xAxis标签?

这是初始图表(具有自定义样式的倒置列)。我用格式化程序功能创建了xAxis标签

 xAxis: {
        arrowOnEnd: true,
        labels: {
          x: 40,
          formatter: function() {
            if (this.isLast) { return 'Less Likely' }
            if (this.isFirst) { return 'More Likely' }
          }
        },

并且能够定位它们,因此看起来xAxis的值从极不可能更可能,如下所示

 events: {
          load: function() {
            // https://stackoverflow.com/questions/51656849/xaxis-point-specific-label-positions
            const chart = this;
            Highcharts.objectEach(chart.xAxis[0].ticks, function(tick: any) {
              if (!tick.isNewLabel && tick.isFirst) { tick.label.attr({ y: 25 })}
              if (!tick.isNewLabel && tick.isLast)  { tick.label.attr({ y: 590 })}
            });
          },

Inverted column chart

<当我要打印图表时:xAxis标签返回到其原始位置。print chart

我试图在events: { load: ... }中复制beforePrint逻辑或将'print'事件包装到highcharts对象,但这似乎没有任何效果

beforePrint: function() { const chart = this; Highcharts.objectEach(chart.xAxis[0].ticks, function(tick: any) { if (tick.isFirst) { tick.label.attr({ y: 125 })} if (tick.isLast) { tick.label.attr({ y: 590 })} }); }

Highcharts.wrap(Highcharts.Chart.prototype, 'print', function (proceed) {
      const chart = this;
      Highcharts.objectEach(chart.xAxis[0].ticks, function(tick: any) {
        if (tick.isFirst) { tick.label.attr({ y: 25 })}
        if (tick.isLast)  { tick.label.attr({ y: 590 })}
      });
      proceed.call(this)
  });
我尝试过tickPositionier,但实际上无法弄清楚如何使它起作用。 

值得一提。具有相同逻辑的afterPrint事件会重置图表,因此我不明白为什么一个事件在起作用(afterPrint),而另一个事件却不在(print)

afterPrint: function() { const chart = this; Highcharts.objectEach(chart.xAxis[0].ticks, function(tick: any) { if (tick.isFirst) { tick.label.attr({ y: 25 })} if (tick.isLast) { tick.label.attr({ y: 590 })} }); },

我在这里想念什么吗?为什么我不能更新打印的刻度位置?

Sidenote-jsfiddle给了我502 Bad Gateway,所以我无法在那里演示它

highcharts
1个回答
1
投票
更改加载事件以呈现将解决在打印之前再次初始化图表的问题。

演示:https://jsfiddle.net/BlackLabel/4qbp1w0z/

chart: { type: 'bar', events: { render: function() { // https://stackoverflow.com/questions/51656849/xaxis-point-specific-label-positions const chart = this; Highcharts.objectEach(chart.xAxis[0].ticks, function(tick) { if (!tick.isNewLabel && tick.isFirst) { tick.label.attr({ y: 20 }) } if (!tick.isNewLabel && tick.isLast) { tick.label.attr({ y: 350 }) } }); }, } },

API:https://api.highcharts.com/highcharts/chart.events.render
© www.soinside.com 2019 - 2024. All rights reserved.