Realtime Updating Chart Using Apache ECharts

Realtime Updating Chart Using Apache Echarts
Code Snippet:Dynamic Data + Time Axis - Apache ECharts Demo
Author: lry
Published: March 3, 2024
Last Updated: March 4, 2024
Downloads: 460
License: MIT
Edit Code online: View on CodePen
Read More

This code snippet helps you to create a dynamic chart showing realtime updating data. It uses the Apache ECharts library. The chart displays voltage changes over time. The JavaScript code fetches and updates the chart with simulated real-time voltage values. It helps visualize and track live voltage fluctuations.

You can use this code to create real-time voltage charts for monitoring systems. It’s beneficial for tracking live voltage changes in IoT devices.

How to Create Realtime Updating Chart Using Apache Echarts

1. First of all, add the ECharts library by including the CDN link in the <head> section of your HTML file:

 <script src="https://cdn.staticfile.org/echarts/4.2.1/echarts.min.js"></script>

2. Define a container in your HTML file where the chart will be displayed. For instance:

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

3. You can style the chart container or other elements as desired. (The following code doesn’t include specific CSS styles for the chart.)

* {
  margin: 0;
  padding: 0;
}
#chart-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

4. Finally, use JavaScript to initialize the ECharts instance, configure the chart, and simulate real-time data updates. Initialize the ECharts instance and set the chart’s initial configuration. Simulate real-time data updates by defining an interval function.

Inside the interval function, generate random voltage data and update the chart’s x-axis with time data and y-axis with voltage values.

        // 初始化 ECharts 实例
        var voltageChart = echarts.init(document.getElementById('voltageChart'));

        // 设置图表配置项和数据
        var option = {
            title: {
                text: '电压实时动态显示'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    animation: false
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {
                type: 'value',
                boundaryGap: [0, '100%']
            },
            series: [{
                name: '电压值',
                type: 'line',
                showSymbol: false,
                hoverAnimation: false,
                data: []
            }]
        };

        // 使用刚指定的配置项和数据显示图表
        voltageChart.setOption(option);

        // 模拟实时数据更新
        var timeData = [];
        var voltageData = [];

        setInterval(function () {
            var now = new Date();
            var value = Math.random() * 100;  // 模拟电压值
            timeData.push(now.toLocaleTimeString());
            voltageData.push(value.toFixed(2));

            if (timeData.length > 20) {
                timeData.shift();
                voltageData.shift();
            }

            voltageChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: voltageData
                }]
            });
        }, 1000);

That’s all! hopefully, you have successfully created a Realtime Updating Chart Using Apache ECharts library. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X