Home > Web Front-end > H5 Tutorial > body text

Graphic and text code analysis of Web SCADA reports based on HTML5

黄舟
Release: 2017-03-27 15:45:50
Original
1478 people have browsed it

Background

Recently encountered the need to display equipment reports in a Web page in a SCADA project. A complete report generally includes a filtering operation area, table, Chart, display board, etc. elements, and the data table is the most commonly used control. In previous industrial projects, all tables looked the same, displaying relevant information through numbers and simple background color changes. Through the influence of various mobile apps and web applications, people's aesthetics and requirements are constantly improving, especially in web projects, it is indeed a bit outdated to still use old-fashioned digital tables

How to choose a suitable one. HTML front-end table control? You can omit 10,000 words here. Haha. There are many mature cases in the control libraries in jQuery, React, but these are based on DOM controls also have shortcomings. One is efficiency: if you use custom cell controls in a table with a large amount of data, the burden on the browser is too heavy, especially on the mobile side. Another problem is development efficiency, as mentioned above. The control libraries have different encapsulation levels and interface forms, but overall they still require developers to have a deep understanding of CSS and JS, as well as the reuse, embedding, and publishing of controls. , transplantation, are also problems. Based on the above considerations, we finally adopted HT based on

Canvas

. Through the custom rendering interface of HT table control and the multi-threaded data of Web Worker Simulation, the effect of the implemented table control is as follows:


Start

The first thing we have to do is combine the business logic to control the different columns in the table The data is rendered differently. For example, the running time, downtime, etc. in the historical information of the device are more suitable for summary display. The user can intuitively compare the historical status of the device from the list. Let's take a look. How to implement it in one step.

HT has its own DataModel data

model

, which omits our development work on data status management, time dispatch, and UI refresh of child elements in the DataModel container. , which is the most basic data structure in HT and can be mapped to different UI controls. On the canvas, Data can be displayed as vectors, pictures or text, etc. On the tree control, Data is displayed as a node of the tree. Each Data in the table corresponds to a row in the table. That is, the table control itself contains a DataModel. When drawing, each Data in this Model is drawn into a different column, and the data in the Data is displayed. Different properties. For example, we can save the device's downtime to the stopping property of Data. When configuring the Column information of the table, we can specify the header description of the column "Downtime", its data cells correspond to the Stopping property of Data, and the custom drawing format:

Custom Rendering

In the basic display format of cells, text,

array

, color and other types have been provided by default. The data can be automatically formatted and displayed as text or background color, etc., but It has not yet met our individual needs, so we need to overload drawCell in Column as a custom rendering function. Parameters of drawCell: function (g, data, selected, column, x, y, w, h, view), where g is the environment information of Canvas, and data is the data body of the row. Based on this information, we use HTML5 The native Canvas API can draw the desired effect. Too lazy to use the native interface of HTML5 directly? HT provides an encapsulation interface for the Canvas API, including various vector types and some simple Charts. Using this function, you can easily combine complex effects. For detailed introduction, please refer to our vector manual.

First create an object, the image vector object is responsible for containing the description information of the combined vector, and then pass the image object and the context information of drawCell as parameters into the ht.Default.drawStretchImage function to realize automatic Define draw.

//drawCellfunction (g, data, selected, column, x, y, w, h, tableView) {    
var value = data.a(attr);    
var image = {
        width: 60,
        height: 30,
        comps: [
            {
                type: 'rect',
                rect: [11,11,8,8],
                borderWidth: 1,
                borderColor: '#34495E',
                background: legendColor,
                depth: 3
            },
            {
                type: 'text',
                text: value,
                rect:[30, 0, 30, 30],
                align: 'left',
                color: '#eee',
                font: 'bold 12px Arial'
            }
        ]};
    ht.Default.drawStretchImage(g, image, 'centerUniform', x, y, w, h);
}
Copy after login

Because there are multiple columns displayed by the Legend legend, we can simply wrap it up and use a getDrawLegend function. The parameters are the color of the legend of the column and the Data attribute name, and the return value is the drawCell function.

getDrawLegend: function(attr,legendColor){return drawCell}
Copy after login

At this point, we have completed the custom drawing of the start and stop time columns:

“统计”列的饼图,实际上更简单。还是利用 HT 的矢量接口,把上述几项时间数据传入饼图矢量结构即可。

var values = [
    data.a('running'), 
    data.a('stopping'), 
    data.a('overhauling')
];var image = {
    width: 200,
    height: 200,
    comps: [
        {
            type: 'pieChart',
            rect: [20,20, 150, 150],
            hollow: false,
            label: false,
            labelColor: 'white',
            shadow: true,
            shadowColor: 'rgba(0, 0, 0, 0.8)',
            values: values,
            startAngle: Math.PI,
            colors: pieColors
        }
    ]
};
Copy after login

其他列的渲染过程大同小异。在“风速”列中,我们可以根据风速大小计算一个颜色透明值,来实现同一色系的映射变换,比原来那种非红即绿的报警表,看起来更舒服一些。在“可用率”列,用 Rect 的不同长度变化,来模拟进度条的效果。在功率曲线中稍微有点不同,因为想实现曲线覆盖区域的颜色渐变,在 HT 的 lineChart 中没有找到相关接口,所以直接采用了 Canvas 绘制。

为了运行效率考虑,在表格的单元格中绘制 Chart,应该追求简洁大方,一目了然。这几个 Legend 图例小矩形,其实是应该画在表头的。我为了偷懒,就画在了单元格,导致画面显得有点乱。

Web Worker

众所周知,浏览器的 JS 环境是基于单进程的,在页面元素较多,而且有很大运算需求的情况下,会导致无法兼顾渲染任务和计算任务,造成页面卡顿或失去响应。在这种情况,可以考虑使用 Web Worker 的多线程,来分担一些计算任务。

Web Worker 是 HTML5 的多线程 API,和我们原来传统概念中的多线程开发有所不同。Web Worker 的线程之间,没有内存共享的概念,所有信息交互都采用 Message 的异步传递。这样多线程之间无法访问对方的上下文,也无法访问对方的成员变量及函数,也不存在互斥锁等概念。在消息中传递的数据,也是通过值传递,而不是地址传递。

在 Demo 中,我们利用 Web Worker 作为模拟后端,产生虚拟数据。并采用前端分页的方式,从 worker 获取当前页显示条目的相关数据。 在主线程中,创建 Web Worker注册消息监听函数。

worker = new Worker("worker.js");    
worker.addEventListener('message', function(e) {  
    //收到worker的消息后,刷新表格    
    pageTable.update(e.data);
});

pageTable.request = function(){    
//向worker发送分页数据请求    
worker.postMessage({
        pageIndex: pageTable.getPageIndex(),
        pageRowSize: pageTable.getPageRowSize()        
    });                 
}; 
pageTable.request();
Copy after login

本处的new Worker创建,对于主线程来说是异步的,等加载完 worker.js,并完成初始化后,该 worker 才是真正可用状态。我们不需要考虑 worker 的可用状态,可以在创建语句后直接发送消息。在完成初始化之前向其发送的请求,都会自动保存在主线程的临时消息队列中,等 worker 创建完成,这些信息会转移到 worker 的正式消息队列。

在 worker 中,创造虚拟随机数据,监听主线程消息,并返回其指定的数据。

self.addEventListener('message', function(e) {    var pageInfo = getPageInfo(e.data.pageIndex, e.data.pageRowSize);   
    self.postMessage(pageInfo);
}, false);
Copy after login

由于前面提到的无法内存共享,Web Worker 无法操作 Dom,也不适用于与主线程进行大数据量频繁的交互。那么在生产环境中,Web Worker 能发挥什么作用?在我们这种应用场景,Web Worker 适合在后台进行数据清洗,可以对从后端取到的设备历史数据进行插值计算、格式转换等操作,再配合上 HT 的前端分页,就能实现大量数据的无压力展示。

分页

传统上有后端分页和前端分页,我们可以根据实际项目的数据量、网速、数据库等因素综合考虑。

采用后端分页的话,可以简化前端架构。缺点是换页时会有延迟,用户体验不好。而且在高并发的情况下,频繁的历史数据查询会对后端数据库造成很大压力。

采用前端分页,需要担心的是数据量。整表的数据量太大,会造成第一次获取时的加载太慢,前端资源占用过多。

在本项目中,得益于给力的 GOLDEN 实时数据库,我们可以放心的采用前端分页。历史数据插值、统计等操作可以在数据库层完成,传递到前端的是初步精简后的数据。在数千台设备的历史查询中,得到的数据量完全可以一次发送,再由前端分页展示。

在某些应用场景,我们会在表格中显示一些实时数据,这些数据是必须是动态获取的。类似在 Demo 中的趋势刷新效果,我们可以在创建表格时批量获取所有历史数据,然后再动态向数据库获取当前页所需的实时数据。如果网速实在不理想,也可以先只获取第一页的历史数据,随后在后台线程慢慢接收完整数据。

这样的架构实现了海量数据的快速加载,换页操作毫无延迟,当前页面元素实时动态刷新的最终效果。

还有一些传统客户,喜欢在一张完整的大表上进行数据筛选、排序等操作。

我们可以把 Demo 中的数据总量改成一万条,单页数量也是一万条,进行测试:

出乎意料的是,HT 面对上万数据量的复杂表格,轻松经受住了考验。页面的滚动、点击等交互毫无影响,动态刷新没有延迟,表格加载、排序等操作时,会有小的卡顿,在可接受的程度之内。当然也跟客户端的机器配置有关。可以想象,几万个 Chart的展示以及动态刷新,对于基于dom的控件几乎是件无法完成的任务。关于 HT 的其他矢量和控件,同样有高性能特性:http://www.hightopo.com/demo/fan/index.html

后记

如前文所述,我们基于 HT 的表格实现了海量数据的可定制展现,并取得了令人满意的效果。以下是一些还可以改进的地方。

在 Demo 中,通过对 HT 表格控件的 drawCell 进行重载,实现了自定义渲染,然后把这些 drawCell 放到了 PageTable 的原型函数中,以供 Column 调用。实际上,更好的办法应该是把这些常见的 Chart、图例封装到 Column 的基本类型中,那样在配置表格 Column 列时,可以指定 type 为 pieChart 或 lineChart 即可,不需再自行绘制相关矢量。

对于这些表格中的 Chart,也可以增加一些交互接口,例如可以增加单元格 Tooltip 的自定义渲染功能,在鼠标停留时浮出一个信息量更大的 Chart,可以对指定设备进行更深入的了解。
界面美观优化。对 HT 的控件进行颜色定制,可以通过相关接口进行配置:


var tableHeader = pageTable.getTablePane().getTableHeader();    
tableHeader.getView().style.backgroundColor = 'rgba(51,51,51,1)';     
tableHeader.setColumnLineColor('#777');var tableView = pageTable.getTablePane().getTableView();                 
tableView.setSelectBackground('#3D5D73');
tableView.setRowLineColor('#222941');
tableView.setColumnLineVisible(false);                
tableView.setRowHeight(30);
Copy after login

今后也可以对htconfig进行全局配置,在单独文件中进行样式的整体管理,实现外观样式与功能的分离,有助于工程管理。

The above is the detailed content of Graphic and text code analysis of Web SCADA reports based on HTML5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template