吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1888|回复: 12
收起左侧

[其他原创] js解析示波器csv并使用Highcharts绘图

  [复制链接]
苏紫方璇 发表于 2023-10-29 15:06
前段时间淘了个玩具级的正点原子的mini示波器,示波器有个导出数据到csv的功能,但是官方却没给解析的工具,然后就百度被,找了好几页,不是要环境,就是得装各种软件的,感觉都挺麻烦。
最后打算干脆自己造吧,反正需求不复杂,就想画个图表。还好咱有点底子,自学js+面向搜索引擎/csdn/ai编程,总算抄出来了点成品。水个帖子,抛砖引玉。
Highcharts在官网上下载就行,或者直接用官方提供的链接


QQ截图20231029150347.jpg
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html>
        <head>
                <meta charset="utf-8" />
                <title>正点原子示波器csv解析</title>
        </head>
        <body>
                <div>
                        读取文件:
                        <input type="file" id="file1" accept="*.csv" />
                </div>
                <div>
                        显示进度:
                        <progress id="pro" value="0"></progress>
                </div>
                <div>
                        <button type="button">测试</button>
                </div>

                <!-- 图表容器 DOM -->
                <div id="container" style="width: 600px; height: 400px"></div>
                <!-- 引入 highcharts.js -->
                <script src="./Highcharts-10.0.0/code/highcharts.js"></script>
                <script src="./Highcharts-10.0.0/code/modules/exporting.js"></script>
                <script src="./Highcharts-10.0.0/code/modules/boost.js"></script>
                <!--  <script src="./Highcharts-10.0.0/code/themes/dark-unica.js"></script>  -->
                <script>
                        var strDw = ["nS", "uS", "mS", "S"];
                        let container = document.getElementById("container"),
                                highlightPoint = function (e) {
                                        var chart, point, i, event;
                                        for (i = 0; i < Highcharts.charts.length; i = i + 1) {
                                                chart = Highcharts.charts[i];
                                                // Find coordinates within the chart
                                                event = chart.pointer.normalize(e);
                                                // Get the hovered point
                                                point = chart.series[0].searchPoint(event, true);
                                                if (point) {
                                                        point.highlight(e);
                                                }
                                        }
                                };
                        ["mousemove", "touchmove", "touchstart"].forEach(function (eventType) {
                                container.addEventListener(eventType, function (e) {
                                        highlightPoint(e);
                                });
                        });
                        /**
                         * 重写内部的方法, 这里是将提示框即十字准星的隐藏函数关闭
                         */
                        Highcharts.Pointer.prototype.reset = function () {
                                return undefined;
                        };
                        /**
                         * 高亮当前的数据点,并设置鼠标滑动状态及绘制十字准星线
                         */
                        Highcharts.Point.prototype.highlight = function (event) {
                                this.onMouseOver(); // 显示鼠标激活标识
                                this.series.chart.tooltip.refresh(this); // 显示提示框
                                this.series.chart.xAxis[0].drawCrosshair(event, this); // 显示十字准星线
                        };
                        /**
                         * 同步缩放效果,即当一个图表进行了缩放效果,其他图表也进行缩放
                         */
                        function syncExtremes(e) {
                                var thisChart = this.chart;
                                if (e.trigger !== "syncExtremes") {
                                        Highcharts.each(Highcharts.charts, function (chart) {
                                                if (chart !== thisChart) {
                                                        if (chart.xAxis[0].setExtremes) {
                                                                chart.xAxis[0].setExtremes(e.min, e.max, undefined, false, {
                                                                        trigger: "syncExtremes",
                                                                });
                                                        }
                                                }
                                        });
                                }
                        }

                        function Addchart(obj) {
                                var chartDiv = document.createElement("div");
                                chartDiv.className = "chart";
                                container.appendChild(chartDiv);

                                Highcharts.chart(chartDiv, {
                                        boost: {
                                                useGPUTranslations: true,
                                        },
                                        chart: {
                                                type: "line", //指定图表的类型,默认是折线图(line)
                                                marginLeft: 40, // Keep all charts left aligned
                                                spacingTop: 20,
                                                zoomType: "x", //x轴缩放
                                                spacingBottom: 20,
                                                panning: true, //开启平移
                                                panKey: "shift", //平移按键
                                        },
                                        title: {
                                                text: obj.name, // 标题
                                                align: "left",
                                                margin: 0,
                                                x: 30,
                                        },
                                        subtitle: {
                                                text: "时间单位 " + obj.dw + " 按shift平移",
                                                align: "right",
                                        },
                                        credits: {
                                                enabled: false,
                                        },
                                        legend: {
                                                enabled: false,
                                        },
                                        xAxis: {
                                                gridLineWidth: 1, //网格线宽度
                                                crosshair: true,
                                                events: {
                                                        setExtremes: syncExtremes,
                                                },

                                                //labels: {
                                                //        format: "{value} km",
                                                //},
                                        },
                                        yAxis: {
                                                title: {
                                                        text: "电压",
                                                },
                                        },
                                        /*
                                        tooltip: {
                                                positioner: function () {
                                                        return {
                                                                // right aligned
                                                                x: this.chart.chartWidth - this.label.width,
                                                                y: 10, // align to title
                                                        };
                                                },
                                                borderWidth: 0,
                                                backgroundColor: "none",
                                                pointFormat: "{point.y}",
                                                headerFormat: "",
                                                shadow: false,
                                                style: {
                                                        fontSize: "18px",
                                                },
                                                valueDecimals: dataset.valueDecimals,
                                        },
                                        */
                                        series: [
                                                {
                                                        data: obj.data,
                                                        name: obj.name,
                                                        step: obj.step,
                                                        //type: dataset.type,
                                                        //color: Highcharts.getOptions().colors[i],
                                                        //fillOpacity: 0.3,
                                                        //tooltip: {
                                                        //        valueSuffix: " " + dataset.unit,
                                                        //},
                                                },
                                        ],
                                });
                        }

                        //解析csv
                        var file1 = document.getElementById("file1");
                        file1.onchange = function () {
                                var file = file1.files[0];
                                //读取为二进制
                                var reader = new FileReader();
                                reader.readAsText(file, "utf-8");
                                //显示进度
                                var pro = document.getElementById("pro");
                                pro.max = file.size;
                                pro.value = 0;
                                reader.onprogress = function (e) {
                                        pro.value = e.loaded;
                                };
                                reader.onload = function () {
                                        var str = reader.result;
                                        var rows = str.split("\n");
                                        var clients = {
                                                data: [],
                                        };
                                        var clients1 = {
                                                data: [],
                                        };
                                        var multi = false;
                                        for (var i = 0; i < rows.length; i++) {
                                                var row = rows[i].split(",");
                                                //判断是否首行
                                                if (i == 0) {
                                                        clients.name = row[0];
                                                        clients.step = "left"; //转折位置
                                                        if (row.length == 3) {
                                                                multi = true;
                                                                clients1.name = row[1];
                                                                clients1.step = "left"; //转折位置
                                                        }
                                                        //处理采样时间
                                                        var s = row[row.length - 1];
                                                        var number = s.match(/\d+/)[0];
                                                        //1秒
                                                        var num1s = 1 * 1000 * 1000 * 1000;
                                                        number = num1s / number;
                                                        num1s = 0;
                                                        while (number / 1000 >= 1) {
                                                                number = number / 1000;
                                                                num1s++;
                                                        }
                                                        clients.dw = number + strDw[num1s];
                                                } else {
                                                        clients.data.push(parseFloat(row[0]));
                                                        if (multi) clients1.data.push(parseFloat(row[1]));
                                                }
                                        }
                                        //options.series.push(clients);
                                        //options.series.push(clients1);
                                        // 图表初始化函数
                                        Addchart(clients);
                                        Addchart(clients1);
                                        //var chart = Highcharts.chart("container", options);
                                };
                        };

                        function btnCeshi() {
                                var txtFile;

                                txtFile = new XMLHttpRequest();

                                txtFile.open("GET", "./test.CSV", false);

                                txtFile.send();

                                var txtDoc = txtFile.responseText;
                                var rows = txtDoc.split("\n");
                                var clients = {
                                        data: [],
                                };
                                var clients1 = {
                                        data: [],
                                };
                                var multi = false;
                                for (var i = 0; i < rows.length; i++) {
                                        var row = rows[i].split(",");
                                        //判断是否首行
                                        if (i == 0) {
                                                clients.name = row[0];
                                                clients.step = "left"; //转折位置
                                                if (row.length == 3) {
                                                        multi = true;
                                                        clients1.name = row[1];
                                                        clients1.step = "left"; //转折位置
                                                }
                                                //处理采样时间
                                                var s = row[row.length - 1];
                                                var number = s.match(/\d+/)[0];
                                                //1秒
                                                var num1s = 1 * 1000 * 1000 * 1000;
                                                number = num1s / number;
                                                num1s = 0;
                                                while (number / 1000 >= 1) {
                                                        number = number / 1000;
                                                        num1s++;
                                                }
                                                clients.dw = number + strDw[num1s];
                                        } else {
                                                clients.data.push(parseFloat(row[0]));
                                                if (multi) clients1.data.push(parseFloat(row[1]));
                                        }
                                }
                                //options.series.push(clients);
                                //options.series.push(clients1);
                                // 图表初始化函数
                                Addchart(clients);
                                Addchart(clients1);
                        }
                </script>
        </body>
</html>

免费评分

参与人数 6吾爱币 +13 热心值 +5 收起 理由
cccfind911 + 1 + 1 我很赞同!
SVIP008 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
wushaominkk + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
xieyulei + 1 + 1 我很赞同!
hrh123 + 1 + 1 用心讨论,共获提升!
善良的果仁 + 2 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zbr878458173 发表于 2023-10-29 15:09
我只能说,大佬就是大佬,膜拜
smile1110 发表于 2023-10-29 15:58
@苏紫方璇  老哥,我害怕ai模型难哭了你,你竟然会给自己加难度了,你是人形ai。
简减 发表于 2023-10-29 22:56
tomliu 发表于 2023-10-30 08:50
大佬就是大佬, 这也太厉害了
paypayone 发表于 2023-10-30 14:24
这东西简单
15518705709 发表于 2023-10-30 21:36
支持楼主
nxcr 发表于 2024-4-11 20:23
高技术,学习学习
吾爱蛋蛋 发表于 2024-6-24 12:10
csv是什么文件啊
sois 发表于 2024-6-24 12:39
大佬这是怎么做的   有没有教程
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 14:38

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表