javascript - 使用带有多个堆栈的 x-range 的带有 highcharts 的时间线图表

标签 javascript highcharts

我正在使用 Highcharts 创建一个时间线图表,显示不同“状态”随时间的流动。当前的实现位于 http://jsfiddle.net/hq1kdpmo/8/。看起来像 state chart .

当前代码如下:

Highcharts.chart('container', {
  "chart": {
    "type": "xrange"
  },
  "title": {
    "text": "State Periods"
  },
  "xAxis": {
    "type": "datetime"
  },
  "yAxis": [{
    "title": {
      "text": "Factions"
    },
    "categories": ["A", "B", "C", "D"],
    "reversed": true
  }],
  "plotOptions": {
    "xrange": {
      "borderRadius": 0,
      "borderWidth": 0,
      "grouping": false,
      "dataLabels": {
        "align": "center",
        "enabled": true,
        "format": "{point.name}"
      },
      "colorByPoint": false
    }
  },
  "tooltip": {
    "headerFormat": "<span style=\"font-size: 0.85em\">{point.x} - {point.x2}</span><br/>",
    "pointFormat": "<span style=\"color:{series.color}\">●</span> {series.name}: <b>{point.yCategory}</b><br/>"
  },
  "series": [{
    "name": "State A",
    "pointWidth": 20,
    "data": [{
      "x": 1540430613000,
      "x2": 1540633768100,
      "y": 0
    }, {
      "x": 1540191009000,
      "x2": 1540633768100,
      "y": 1
    }, {
      "x": 1540191009000,
      "x2": 1540530613000,
      "y": 2
    }, {
      "x": 1540530613000,
      "x2": 1540633768100,
      "y": 3
    }]
  }, {
    "name": "State B",
    "pointWidth": 20,
    "data": [{
      "x": 1540191009000,
      "x2": 1540430613000,
      "y": 0
    }, {
      "x": 1540530613000,
      "x2": 1540633768100,
      "y": 2
    }, {
      "x": 1540191009000,
      "x2": 1540330613000,
      "y": 3
    }]
  }, {
    "name": "State C",
    "pointWidth": 20,
    "data": [{
      "x": 1540330613000,
      "x2": 1540530613000,
      "y": 3
    }]
  }],
  "exporting": {
    "enabled": true,
    "sourceWidth": 1200
  }
});

现在我期待的是创造这样的东西(原谅我的绘画技巧)chart to be .

这里类别 A 到 D 是 y 上唯一的轴。但我想对可变数量的平行范围进行分组。用例是在任何时间点都可以有多个状态,并且任何时间点的状态数都是可变的。我该如何继续这样做?

最佳答案

要创建这样的图表,您必须添加 12 yAxis (0-11) 并设置适当的刻度和标签,以便仅绘制 A-D 类别。此外,调整 plotOptions.pointPaddingplotOptions.groupPadding自动设置点宽度的属性(series.pointWidth 应该是未定义的)。

y轴选项:

  yAxis: [{
    title: {
      text: "Factions"
    },
    categories: ["A", "B", "C", "D"],
    tickPositions: [-1, 2, 5, 8, 11],
    lineWidth: 0,
    labels: {
      y: -20,
      formatter: function() {
        var chart = this.chart,
          axis = this.axis,
          label;

        if (!chart.yaxisLabelIndex) {
          chart.yaxisLabelIndex = 0;
        }

        if (this.value !== -1) {

          label = axis.categories[chart.yaxisLabelIndex];
          chart.yaxisLabelIndex++;

          if (chart.yaxisLabelIndex === 4) {
            chart.yaxisLabelIndex = 0;
          }

          return label;
        }
      },
    },
    reversed: true
  }]

演示: https://jsfiddle.net/wchmiel/s9qefg7t/1/

关于javascript - 使用带有多个堆栈的 x-range 的带有 highcharts 的时间线图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53023280/

相关文章:

javascript - Highcharts - 区域 "appearance"但不是从 0 开始

jquery - 使 dataLabel 可拖动

node.js - 使用(moSTLy)一个代码库构建动态 HighCharts 和图像 Highcharts?

javascript - 如何在不使用mousemove动画万花筒的情况下随机化JS中的backgroundPosition

javascript - Javascript 中的异步类是个好主意吗?

javascript - 使用 Highcharts 创建一维图表

javascript - X 轴不显示正确的时间

JavaScript Express、 Node 和 CSVtoJSON

javascript - 如何将变量从 javascript 移动到 javascript?

javascript - 如果文件不存在,则创建一个文件