Plotly:创建水平时间轴

时间:2017-05-23 11:34:13

标签: javascript html plotly timeline

那是我到目前为止所拥有的......



Plotly.plot('graph', [{
  type: 'bar',
  y: [1,0,0],
  x: [1,2,1],
  orientation: 'h',
  base: [0,2,5]
}, {
  type: 'bar',
  y: [1,1,1],
  x: [2,1,2],
  orientation: 'h',
  base: [0,3,5]
}])

<head>
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
  <div id="graph"></div>
</body>
&#13;
&#13;
&#13;

如何在横轴上使用日期而不是数值?

2 个答案:

答案 0 :(得分:1)

您需要使用x轴类型属性。您可以将x轴指定为日期类型: 像

x:{type:"date"}

我更新了你的例子。请看下面的例子

&#13;
&#13;
Plotly.plot('graph', [{
  type: 'bar',
  y: [1,0,0],
  x: ['2000-01-01', '2000-01-02', '2000-01-03'],
  orientation: 'v',
  base: [0,2,5]
}, {
  type: 'bar',
  y: [1,1,1],
  x: ['2000-01-01', '2000-01-02', '2000-01-03'],
  orientation: 'v',
  base: [0,3,5]
}],{xaxis:{type:"date"}})
&#13;
<head>
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
  <div id="graph"></div>
</body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

最后创建了时间轴! Codepen.io链接:https://codepen.io/elv1s42/pen/bKVEJb

与所有人分享以防您需要:

var layout = {
  height: 300,
  yaxis: {
    showgrid: false,
    zeroline: false,
    showline: false,
    showticklabels: false
  }
};

Plotly.plot('graph', [{
    x: ['2017-11-04 22:23:00', '2017-11-04 22:24:00'],
    y: [1, 1],
    type: 'scatter', 
    opacity: 0.5,
    line: { color: 'red', width: 20 },
    mode: 'lines',
    name: 'First event'
  },{
    x: ['2017-11-04 22:24:00', '2017-11-04 22:24:30'],
    y: [1, 1],
    type: 'scatter', 
    opacity: 0.5,
    line: { color: 'green', width: 20 },
    mode: 'lines',
    name: 'Second event'
  },{
    x: ['2017-11-04 22:25:00', '2017-11-04 22:26:00'],
    y: [1, 1],
    type: 'scatter', 
    opacity: 0.5,
    line: { color: 'yellow', width: 20 },
    mode: 'lines',
    name: 'Third event'
  }], layout)
    <head>
      <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    </head>
    <body>
      <div id="graph"></div>
    </body>