Home Web Front-end JS Tutorial How to use dynamic data in Highcharts to display real-time data

How to use dynamic data in Highcharts to display real-time data

Dec 17, 2023 pm 06:57 PM
highcharts dynamic data real time display

How to use dynamic data in Highcharts to display real-time data

How to use dynamic data in Highcharts to display real-time data

With the advent of the big data era, the display of real-time data has become more and more important. Highcharts, as a popular charting library, provides rich functions and customizability, allowing us to flexibly display real-time data. This article will introduce how to use dynamic data in Highcharts to display real-time data, and give specific code examples.

First, we need to prepare a data source that can provide real-time data. In this article, we use JavaScript's setInterval function to simulate the generation of real-time data. The code example is as follows:

1

2

3

4

5

6

7

8

// 模拟实时数据生成

setInterval(function() {

  // 生成随机数据

  var randomData = Math.random() * 100;

   

  // 将数据传递给Highcharts

  updateChart(randomData);

}, 1000); // 每隔1秒生成一组数据

Copy after login

Next, we need to create a Highcharts chart object and specify the type of chart and initial data. The code example is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// 创建Highcharts图表对象

var chart = Highcharts.chart('container', {

  chart: {

    type: 'line'

  },

  title: {

    text: '实时数据展示'

  },

  xAxis: {

    type: 'datetime',

    tickPixelInterval: 150

  },

  yAxis: {

    title: {

      text: '数据'

    }

  },

  series: [{

    name: '数据',

    data: [] // 初始数据为空

  }]

});

Copy after login

In the above code, we created a line chart of type line and specified that the initial data is empty. Next, we need to write a function to update the chart's data. The code example is as follows:

1

2

3

4

5

6

7

8

// 更新图表数据

function updateChart(data) {

  var series = chart.series[0], // 获取图表中的第一条序列数据

      shift = series.data.length > 10; // 如果数据长度超过10个,就移除第一个数据

   

  // 添加数据

  chart.series[0].addPoint([new Date().getTime(), data], true, shift);

}

Copy after login

In the above code, we define an updateChart function to update the data of the chart. In this function, we first obtain the first sequence data in the chart, and determine if the data length exceeds 10, remove the first data from the starting position of the chart. Then, we call the addPoint method of Highcharts to add new data points, and use the true parameter to achieve dynamic update effects.

Finally, we need to add a div container to the HTML file to display the Highcharts chart and call the code we wrote in JavaScript. The code example is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<!DOCTYPE html>

<html>

<head>

  <meta charset="utf-8">

  <title>实时数据展示</title>

  <script src="https://code.highcharts.com/highcharts.js"></script>

</head>

<body>

  <div id="container" style="width: 600px; height: 400px;"></div>

</body>

<script>

  // 在这里添加前面编写的JavaScript代码

</script>

</html>

Copy after login

In this way, we can use dynamic data in Highcharts to display real-time data. Generate a set of random data at regular intervals, and then update the chart data by calling the addPoint method of Highcharts to achieve real-time display effects.

Summary: This article introduces how to use dynamic data in Highcharts to display real-time data, and gives specific code examples. Through this method, we can flexibly display real-time data and dynamically change the display effect of the chart as the data is updated. I hope this article will be helpful to developers who use Highcharts to display real-time data.

The above is the detailed content of How to use dynamic data in Highcharts to display real-time data. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Sankey chart to display data in Highcharts How to use Sankey chart to display data in Highcharts Dec 17, 2023 pm 04:41 PM

How to use Sankey diagram to display data in Highcharts Sankey diagram (SankeyDiagram) is a chart type used to visualize complex processes such as flow, energy, and funds. It can clearly display the relationship and flow between various nodes, and can help us better understand and analyze data. In this article, we will introduce how to use Highcharts to create and customize a Sankey chart, with specific code examples. First, we need to load the Highcharts library and Sank

How to use dynamic data in Highcharts to display real-time data How to use dynamic data in Highcharts to display real-time data Dec 17, 2023 pm 06:57 PM

How to use dynamic data in Highcharts to display real-time data. With the advent of the big data era, the display of real-time data has become more and more important. Highcharts, as a popular charting library, provides rich functions and customizability, allowing us to flexibly display real-time data. This article will introduce how to use dynamic data in Highcharts to display real-time data, and give specific code examples. First, we need to prepare a data source that can provide real-time data. In this article, I

How to create a Gantt chart using Highcharts How to create a Gantt chart using Highcharts Dec 17, 2023 pm 07:23 PM

How to use Highcharts to create a Gantt chart requires specific code examples. Introduction: The Gantt chart is a chart form commonly used to display project progress and time management. It can visually display the start time, end time and progress of the task. Highcharts is a powerful JavaScript chart library that provides rich chart types and flexible configuration options. This article will introduce how to use Highcharts to create a Gantt chart and give specific code examples. 1. Highchart

How to use stacked charts to display data in Highcharts How to use stacked charts to display data in Highcharts Dec 18, 2023 pm 05:56 PM

How to use stacked charts to display data in Highcharts Stacked charts are a common way of visualizing data, which can display the sum of multiple data series at the same time and display the contribution of each data series in the form of a bar chart. Highcharts is a powerful JavaScript library that provides a rich variety of charts and flexible configuration options to meet various data visualization needs. In this article, we will introduce how to use Highcharts to create a stacked chart and provide

How to create a map heat map using Highcharts How to create a map heat map using Highcharts Dec 17, 2023 pm 04:06 PM

How to use Highcharts to create a map heat map requires specific code examples. A heat map is a visual data display method that can represent the data distribution in each area through different color shades. In the field of data visualization, Highcharts is a very popular JavaScript library that provides rich chart types and interactive functions. This article will introduce how to use Highcharts to create a map heat map and provide specific code examples. First, we need to prepare some data

How to use scatter plots to display data in Highcharts How to use scatter plots to display data in Highcharts Dec 17, 2023 pm 10:30 PM

How to use scatter plots to display data in Highcharts Preface Highcharts is an open source JavaScript chart library that provides a variety of chart types and powerful customization functions. Among them, scatter plot is a commonly used data visualization method that can show the relationship between two variables and the distribution of variables. This article will introduce how to use scatter plots to display data in Highcharts and provide specific code examples. Step 1: Introduce the Highcharts library

How to use maps to display data in Highcharts How to use maps to display data in Highcharts Dec 18, 2023 pm 04:06 PM

How to use maps to display data in Highcharts Introduction: In the field of data visualization, using maps to display data is a common and intuitive way. Highcharts is a powerful JavaScript charting library that provides rich functionality and flexible configuration options. This article will introduce how to use maps to display data in Highcharts and provide specific code examples. Introducing map data: When using a map, you first need to prepare map data. High

How to create custom charts with Highcharts How to create custom charts with Highcharts Dec 17, 2023 pm 10:39 PM

How to create custom charts with Highcharts Highcharts is a powerful and easy-to-use JavaScript chart library that helps developers create various types of interactive and customizable charts. In order to create custom charts using Highcharts, we need to master some basic concepts and techniques. This article walks through some important steps and provides specific code examples. Step 1: Introduce the Highcharts library First, we need to

See all articles