【问题标题】:how to call ajax function to update the Highcharts graph dynamically?如何调用ajax函数来动态更新Highcharts图?
【发布时间】:2017-04-21 10:56:04
【问题描述】:

我正在使用 Highcharts 绘制温度与时间的关系图。我有一个 JSON 文件,其中来自后端的数据不断更新 JSON 文件。我想调用 ajax 函数,以便图形根据时间自动生成。怎么做?我是高图表新手,请帮助我。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 目前,我使用 JSON 文件中的静态数据来静态生成图形
  • 我用过这个 -$.getJSON('data.json', function(data) {})
  • 用一些相关代码更新你的问题

标签: json ajax highcharts


【解决方案1】:

您可以使用 Series.addPoint 方法。 http://api.highcharts.com/highcharts/Series.addPoint

这是一个使用 Highcharts 和 GET HTTP 请求的实时数据示例。

const options = {
  chart: {
    type: 'spline'
  },
  title: {
    text: 'Live Bitcoin Price'
  },
  xAxis: {
    type: 'datetime'
  },
  yAxis: {
    title: {
      text: 'Price (USD)'
    }
  },
  legend: {
    enabled: false
  },
  exporting: {
    enabled: false
  },
  series: [{
    name: 'Live Bitcoint Price [USD]',
    data: []
  }]
}
const chart = Highcharts.chart('container', options)

// Data
const getData = () => {
  setInterval(() => {
    window.fetch('https://api.cryptonator.com/api/ticker/btc-usd').then((response) => {
      return response.json()
    }).then((data) => {
      chart.series[0].addPoint({ x: data.timestamp * 1000, y: Number(data.ticker.price) })
    })
  }, 3000)
}
getData()
@import 'https://code.highcharts.com/css/highcharts.css';
.highcharts-background {
  fill: #222;
}

.highcharts-title,
.highcharts-axis-title {
  fill: #DDD;
}

.highcharts-credits,
.highcharts-credits:hover {
  fill: #222;
}
body {
  background-color: #222;
  margin 0 !important;
}
#container {
  margin: 0;
  padding: 0;
  border: 0;
  background-color: #222;
  min-height: 400px;
  height:95%;
  width:95%;
  position:absolute;
}
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container"></div>

现场示例: https://jsfiddle.net/xpfkx91w/

【讨论】:

  • 谢谢。有没有机会对当前时间进行绘图?
  • 关于时间的绘图是什么意思?
  • 我可以在 x 轴上相对于当前时间改变 y 轴
  • 是的,您可以使用 Axis.update() api.highcharts.com/highcharts/Axis.update 更新 yAxis
猜你喜欢
  • 1970-01-01
  • 2021-06-17
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多