javascript - Highcharts 刻度位置未在多个 Y 轴上对齐

标签 javascript highcharts

我在同一页面上有多个图表,目标是所有图表上的每个指标都具有相同的最大值,以便更轻松地比较图表 ( https://www.screencast.com/t/vtF6h5ZRWS )

我尝试过最大值和最小值,但它不起作用,可能是因为多个 y 轴。

现在我尝试使用刻度位置(我在后端计算)并将其传递到图表。但这里存在相对轴上刻度对齐的问题,如图 https://www.screencast.com/t/iwnGOhJFb 所示。

下面是我如何设置刻度位置的一小部分代码以及我拥有的更简单版本的图表(我有更多 y 轴)

        yAxis: [{ // Primary yAxis
        labels: {
            format: '{value}',
            style: {
                color: Highcharts.getOptions().colors[2]
            }
        },
        title: {
            text: 'Impressions',
            style: {
                color: Highcharts.getOptions().colors[2]
            }
        },
        tickPositions: [0, 5500000, 11000000, 15000000]

    }

http://jsfiddle.net/j82oen9c/8/

如何在所有 y 轴上实现刻度对齐?

最佳答案

为了对齐两个轴上的刻度,您可以使用两种不同的方法。 第一个是定义您自己的 Axis.tickPositioner 函数,该函数返回根据您的需要调整的计算出的刻度数组。

继续,您需要在两个轴上获得相同数量的刻度,并且它们应该位于轴上的相同位置,因此定位器函数应该接收两个参数 - maxValueOfCurrentAxistickAmount。我这样写了这个函数:

var positioner = function(tAmount, axisMax) {
  var positions = []
  for (var i = 0; i < tAmount; i++) {
    positions.push(Math.round(((axisMax / tAmount) * i) * 100) / 100)
  }
  positions.push(axisMax)
  return function() {
    return positions
  }
}

现在,我们需要为该函数分配特定参数作为Axis.tickPositioner,并定义两个轴的刻度数和最大值:

 var tickAmount = 3
 var firstAxisMax = 15000000
 var secondAxisMax = 2

 yAxis: [{ // Primary yAxis
            labels: {
              format: '{value}',
              style: {
                color: Highcharts.getOptions().colors[2]
              }
            },
            title: {
              text: 'Impressions',
              style: {
                color: Highcharts.getOptions().colors[2]
              }
            },
            tickPositioner: positioner.apply(this, [tickAmount, firstAxisMax]),
          }, { // Secondary yAxis
            gridLineWidth: 0,
            tickPositioner: positioner.apply(this, [tickAmount, secondAxisMax]),
            title: {
              text: 'eCPM',
              style: {
                color: Highcharts.getOptions().colors[0]
              }
            },
            labels: {
              format: '{value} $',
              style: {
                color: Highcharts.getOptions().colors[0]
              }
            },
            opposite: true

          }],

实例:http://jsfiddle.net/vL324nqx/

解决该问题的第二种方法是在两个轴上使用 Axis.tickPixelInterval 属性,但没有根据您的需求进行太多调整。不用解释得更详细,因为API里已经有明确的信息了。

实例:http://jsfiddle.net/mqget4hb/

API引用:

https://api.highcharts.com/highcharts/yAxis.tickPositioner

https://api.highcharts.com/highcharts/yAxis.tickPixelInterval

关于javascript - Highcharts 刻度位置未在多个 Y 轴上对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51737720/

相关文章:

javascript - 自定义系列图标

javascript - 如何将 highcharts 包含在 bower 中?

javascript - knockout 绑定(bind)自定义组件,使其不与中心 View 模型冲突

javascript - 将一行中的 4 列居中对齐并响应

javascript - 数组有项目,但找不到长度

javascript - HighCharts 多点选择 - 在点选择后立即访问/更新 getSelectedPoints 数组

javascript - Highcharts 柱形图中的最大条形宽度

javascript - 如何让div一次显示一个,设置为一个时间

javascript - 如何将行索引值附加到 td 的 ID

colors - Highcharts 面积图,在 X 轴上方/下方使用 2 种填充颜色