【问题标题】:How can i use dynamic data on a Highcharts chart?如何在 Highcharts 图表上使用动态数据?
【发布时间】:2023-03-15 22:12:01
【问题描述】:

我正在从我的页面上的 websocket 连接接收数据,我想在 Highcharts 深度图表上绘制该数据。

深度图:https://www.highcharts.com/docs/stock/depth-chart

这是我的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script language="javascript">
    mySocket.onmessage = function(event) {
        var data = JSON.parse(event.data);
        rawAsks = data['asks']
        rawBids = data['bids']

        const asks = rawasks.map(x => x.map(y => parseFloat(y))) //DATA TO USE ON THE CHART
        const bids = rawbids.map(x => x.map(y => parseFloat(y))) //DATA TO USE ON THE CHART

    };

    Highcharts.chart('container', {
      chart: {
        type: 'area',
        zoomType: 'xy'
      },
      title: {
        text: 'ETH-BTC Market Depth'
      },
      xAxis: {
        minPadding: 0,
        maxPadding: 0,

      },
      yAxis: [{
        lineWidth: 1,
        gridLineWidth: 1,
        title: null,
        tickWidth: 1,
        tickLength: 5,
        tickPosition: 'inside',
        labels: {
          align: 'left',
          x: 8
        }
      }, {
        opposite: true,
        linkedTo: 0,
        lineWidth: 1,
        gridLineWidth: 0,
        title: null,
        tickWidth: 1,
        tickLength: 5,
        tickPosition: 'inside',
        labels: {
          align: 'right',
          x: -8
        }
      }],
      legend: {
        enabled: false
      },
      plotOptions: {
        area: {
          fillOpacity: 0.2,
          lineWidth: 1,
          step: 'center'
        }
      },
      tooltip: {
        headerFormat: '<span style="font-size=10px;">Price: {point.key}</span><br/>',
        valueDecimals: 2
      },
      series: [{
        name: 'Bids',
        data: Bids, //HERE GOES THE DATA
        color: '#03a7a8'
      }, {
        name: 'Asks',
        data: Asks, //HERE GOES THE DATA
        color: '#fc5857'
      }]
    });
</script>

以下是我的图表的问题:1) 要绘制的数据是 asksbids。问题是这两个变量每秒都会从 websocket 连接更新,所以它们不是静态值。 2) 如何将asksbids 传递给Highcharts 图表,如何在每次刷新数据时刷新图表?提前致谢!

这是我每秒收到的数据样本,需要绘制:

var sampleData = [
[7062.24, 0.402181],
[7062.56, 2.472812],
[7062.59, 0.006595],
[7063.01, 1.2001],
[7063.27, 0.4],
[7063.28, 0.100615],
[7063.48, 0.4],
[7063.76, 0.086983],
[7063.83, 0.005],
[7064.19, 0.399752],
[7064.2, 1.70819],
[7064.41, 0.25],
[7064.43, 0.015026],
]

【问题讨论】:

标签: javascript jquery highcharts


【解决方案1】:

我准备了一个例子,用空数据渲染图表,每1秒更新一次数据(模拟从WebSocket获取数据)。

演示:https://jsfiddle.net/BlackLabel/zuhg390j/

events: {
  load() {
    let chart = this;
    setTimeout(function() {
      chart.series[0].update({
        data: dataBids
      }, false)

      chart.series[1].update({
        data: dataAsks
      })
    }, 1000)
  }
}

API:https://api.highcharts.com/class-reference/Highcharts.Series#update

【讨论】:

  • 感谢您的回答!我正在查看您的示例,这很清楚;我完全不明白的是:我怎样才能将它集成到我自己的代码中?你能给我一点帮助吗?
  • 好吧,我想我做到了!有一件事很难:我认为当新数据到达时它不会更新,为了做到这一点,我需要添加什么?
  • 我不确定您是如何从服务器请求数据的 - 我的意思是您如何刷新数据,能否在线重现?
  • 当然!让我解释一下:你看到我的问题上的 sampleData 数组了吗?我每秒都会收到一个这样的数组,但当然有不同的数据。所以每一秒我都会收到两个数组:一个用于出价,另一个用于询价!
  • 在这里尝试类似的操作:jsfiddle.net/BlackLabel/795te8vn 当新阵列准备就绪时 - 使用新数据更新 chart.series
【解决方案2】:

您可以使用map 函数获取每个series 数据,如下所示

var data = [
        [7062.24, 0.402181],
        [7062.56, 2.472812],
        [7062.59, 0.006595],
        [7063.01, 1.2001],
        [7063.27, 0.4],
        [7063.28, 0.100615],
        [7063.48, 0.4],
        [7063.76, 0.086983],
        [7063.83, 0.005],
        [7064.19, 0.399752],
        [7064.2, 1.70819],
        [7064.41, 0.25],
        [7064.43, 0.015026],
    ];


const asks = data.map(x => x[0]); //DATA TO USE ON THE CHART
const bids = data.map(x => x[1]); //DATA TO USE ON THE CHART

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
<script src = "https://code.highcharts.com/highcharts.js"></script>

    <div id = "container"> </div> 
    <script language = "javascript">

    var data = [
        [7062.24, 0.402181],
        [7062.56, 2.472812],
        [7062.59, 0.006595],
        [7063.01, 1.2001],
        [7063.27, 0.4],
        [7063.28, 0.100615],
        [7063.48, 0.4],
        [7063.76, 0.086983],
        [7063.83, 0.005],
        [7064.19, 0.399752],
        [7064.2, 1.70819],
        [7064.41, 0.25],
        [7064.43, 0.015026],
    ];


const asks = data.map(x => x[0]); //DATA TO USE ON THE CHART
const bids = data.map(x => x[1]); //DATA TO USE ON THE CHART



Highcharts.chart('container', {
    chart: {
        type: 'area',
        zoomType: 'xy'
    },
    title: {
        text: 'ETH-BTC Market Depth'
    },
    xAxis: {
        minPadding: 0,
        maxPadding: 0,

    },
    yAxis: [{
        lineWidth: 1,
        gridLineWidth: 1,
        title: null,
        tickWidth: 1,
        tickLength: 5,
        tickPosition: 'inside',
        labels: {
            align: 'left',
            x: 8
        }
    }, {
        opposite: true,
        linkedTo: 0,
        lineWidth: 1,
        gridLineWidth: 0,
        title: null,
        tickWidth: 1,
        tickLength: 5,
        tickPosition: 'inside',
        labels: {
            align: 'right',
            x: -8
        }
    }],
    legend: {
        enabled: false
    },
    plotOptions: {
        area: {
            fillOpacity: 0.2,
            lineWidth: 1,
            step: 'center'
        }
    },
    tooltip: {
        headerFormat: '<span style="font-size=10px;">Price: {point.key}</span><br/>',
        valueDecimals: 2
    },
    series: [{
        name: 'Bids',
        data: bids, //HERE GOES THE DATA
        color: '#03a7a8'
    }, {
        name: 'Asks',
        data: asks, //HERE GOES THE DATA
        color: '#fc5857'
    }]
}); 
</script>

【讨论】:

  • 感谢您的回答!我不明白一件事:websocket 连接的代码在哪里?由于数据是从那里检索的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 2017-08-14
  • 1970-01-01
相关资源
最近更新 更多