如何在Highchart标签中使用IF值

时间:2016-09-16 06:57:36

标签: javascript jquery highcharts highstock

enter image description here

我使用像this这样的Highstock
想在{点.y}之后显示等级标签,如“KPAX:3.10,B”(请看图片)。
所以我在数据库中有等级值,或者可以使用IF来获得等级值吗?

这是我的代码:

http://jsfiddle.net/3v9wzp90/

$(function() {
  var seriesOptions = [],
    seriesCounter = 0,
    names = ['KPAX'];

  /**
   * Create the chart when all data is loaded
   * @returns {undefined}
   */
  function createChart() {
    $('#container').highcharts('StockChart', {
      legend: {
        enabled: true
      },
      rangeSelector: {
        selected: 4
      },
      yAxis: [{
        labels: {
          formatter: function() {
            return this.value;
          }
        },
        plotLines: [{
          value: 0,
          width: 2,
          color: 'silver'
        }]
      }, {
        opposite: true
      }, {
        opposite: true
      }, {
        opposite: true
      }],

      plotOptions: {
        series: {
          compare: 'normal'
        }
      },
      tooltip: {
        pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
        valueDecimals: 2
      },
      series: seriesOptions
    });
  }

  $.each(names, function(i, name) {
    var url = "asset/data/testdata.php?id=<?=$id?>&callback=?";
    $.getJSON(url, function(data) {
      seriesOptions[i] = {
        name: name,
        data: data,
        dataGrouping: {
          approximation: "average",
          enabled: true,
          forced: true,
          units: [
            ['month', [1]]
          ],
        },
      };

      // As we're loading the data asynchronously, we don't know what order it will arrive. So
      // we keep a counter and create the chart when all the data is loaded.
      seriesCounter += 1;

      if (seriesCounter === names.length) {
        createChart();
      }
    });
  });
});

1 个答案:

答案 0 :(得分:0)

您可以使用pointFormatter函数而不是pointFormat。在此功能中,您可以添加负责计算成绩的代码:

  tooltip: {
    pointFormatter: function() {
      var grades = ['F', 'D', 'C', 'B', 'A'],
        index = Math.floor(this.y),
        grade = grades[index - 1];

      var text = '<span style="color:' + this.series.color + '">' + this.series.name + '</span>: <b>' + this.y + '</b>, ' + grade + '<br/>';
      return text;
    },
  },

在这里,您可以看到简单的示例:http://jsfiddle.net/3v9wzp90/4/