javascript - 仅显示图表上的第一个和最后一个 y 轴标签

标签 javascript highcharts label

对于图表,我只想显示属于第一个值和最后一个值的标签。

我尝试通过事件触发器获取最后一个值。不幸的是,这仅适用于最后一个值,不适用于第一个值。我也不知道如何为这两个系列执行此操作。到目前为止,我只管理系列 [0]。

  chart: {
    type: 'line',
    events: {
      load: function() {
        var chart = this,
          points = chart.series[0].points,
          lastValue,
          chosenPointlast;

        points.forEach(function(point, index) {
          if (!lastValue || lastValue < point.x) {
            lastValue = point.x;
            chosenPointlast = point;
          }
        }); 

        chosenPointlast.update({
          marker: {
            enabled: true,
            fillColor: 'orange'
          },
          dataLabels: {
            enabled: true,
            color: 'orange'
          },
        });
      }
    }
  },
  title: {
    text: 'Monthly Average Temperature'
  },
  subtitle: {
    text: 'Source: WorldClimate.com'
  },
  xAxis: {
    categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  },
  yAxis: {
    title: {
      text: 'Temperature (°C)'
    }
  },
  plotOptions: {
    line: {
      dataLabels: {
        enabled: false
      },
      enableMouseTracking: false
    }
  },
  series: [{
    name: 'Tokyo',
    data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
  }, {
    name: 'London',
    data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
  }]
});

所以我希望看到系列 [0] 的标签 7.0 和 9.6,以及系列 [1] 的标签 3.9 和 4.8。希望你能帮我解决这个问题。 JSFiddle

最佳答案

您可以通过以下方式找到一个系列的 4 个边缘点:

const pointsSortedByY = serie.points.sort((p1, p2) => p2.y - p1.y);
const top = pointsSortedByY[0];
const bottom = pointsSortedByY[pointsSortedByY.length - 1];

const pointsSortedByX = serie.points.sort((p1, p2) => p2.x - p1.x);
const first = pointsSortedByX[0];
const last = pointsSortedByX[pointsSortedByX.length - 1];

然后你可以打印它:

[top,bottom, first, last].forEach(function (point) {
    point.update({
        marker: {
            enabled: true,
            fillColor: 'green'
        },
        dataLabels: {
            enabled: true,
            color: 'green'
        }
    });
},this);

最后对每个系列重复相同的操作:

chart1.series.forEach(function (serie) {
    printTopAndBottom(serie);
}, this);

这是结果: Result

https://jsfiddle.net/7ue8ow0b/

关于javascript - 仅显示图表上的第一个和最后一个 y 轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56303897/

相关文章:

javascript - TinyMCE可以用来编辑完整的网页吗?

javascript - 简单的 javascript 更正(日期/时间)

javascript - 如何通过 li data-val 获取输入隐藏字段中的值

javascript - 动画元素 Angular 不消失

php - 如何在不使用列或第 3 方类的情况下使用 TCPDF 制作包含二维条码的 2x6 张标签?

javascript - 同步两个 highcharts 以立即向下钻取

highcharts - 如何在highchart中实现移动平均线

javascript - 在单独的容器中包含丰富信息的 Highcharts

c# - 如何检查标签是否包含字符串的一部分

javascript - c3 图表中的多线标签