echarts GL操作实践

<!DOCTYPE html>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
    <link href="../static/echarts/layer/theme/default/layer.css" th:href="@{echarts/layer/theme/default/layer.css}" rel="stylesheet"/>
    <link href="../static/echarts/nano.min.css" th:href="@{/echarts/nano.min.css}" rel="stylesheet"/>
</head>
<style>
    .changeColor {
        position: relative;
        bottom: 80px;
        left: 5px;
        display: flex;
        width: 100px;
        justify-content: space-between;
    }
    .chart {
        height: 100%;
    }
    body {
        margin: 0px;
        height: 100%;
    }
    html {
        height: 100%;
    }
    .beautiful {
        width: 40px;
        height: 28.8px;
        border-radius: 3px;
        border: 2px solid #337ab7;
        display: none;
        position: absolute;
        left: 100%;
        bottom: 100%;
    }
    .btns{
        height: 28.8px;
        position: absolute;
        left: 160%;
        bottom: 120%;
    }
</style>
<script th:src="@{/js/jquery.min.js}" src="../static/js/jquery.min.js"></script>
<script th:src="@{/echarts/echarts.min.js}" src="../static/echarts/echarts.min.js"></script>
<script th:src="@{/echarts/echarts.GL.min.js}" src="../static/echarts/echarts.GL.min.js"></script>
<script th:src="@{/echarts/layer/layer.min.js}"></script>
<script th:src="@{/echarts/axios.min.js}" src="../static/echarts/axios.min.js"></script>
<script th:src="@{/echarts/pickr.min.js}" src="../static/echarts/pickr.min.js"></script>
<script th:src="@{/ruoyi/js/ry-ui.js}"></script>
<div id="chart" class="chart">
</div>
<body>
    <div class="changeColor">
        <div id="color1"></div>
        <div id="color2"></div>
        <div id="color3"></div>
        <input contenteditable="true" type="text" class="beautiful"/>
        <button class="btn btn-warning btns" onclick="setFlashTime()" id="btn">刷新时间</button>
        <input contenteditable="true" type="text"onchange="updateArea(this)"/>
    </div>
</body>
    <script th:inline="javascript">
         var ctx = [[@{/}]];
        var LastOne={};
        var ModbusAddrSum="";
        var PressureType="";
         const prefix = ctx + "workOrder/order";
        //获取当前支架的数量和类型
        $(function () {
            $.ajax({
                type:"get",
                url:prefix+"/getConfig",
                dataType:"json",
                success:function (data) {
                    ModbusAddrSum=data.ModbusAddrSum;
                    PressureType=data.PressureType;
                }
            })
        });


       const txtstyle={
          'color':'white',
          'fontFamily':'"microsoft yahei",Helvetica,Tahoma,Arial,"Microsoft jhengHei",sans-serif',
           'fontSize':22
           };

        const myChart = echarts.init(document.getElementById("chart"), "shine");
        window.onresize = function(){
            myChart.resize();
        };
        let option = {

        legend:{
             textStyle:{
             color:"white"
        }
        },
            grid3D: {
                viewControl: {
                    center: [20, -25, 0],
                    //projection:'orthographic'  正交投影
                    alpha:60,  //视角绕x轴的角度
                    beta:50, //视角绕y轴的角度
                    distance:300,

                },
                splitLine:false,//是否显示轴线
                boxWidth: 200,
                boxDepth: 150,//y轴的高度
                boxHeight:50,//z轴的高度
                show:true, //是否显示坐标轴

            },
            title:{
                text:"支架压力", //大标题
                left:20, //距离容器的距离
                top:20,
                textStyle:txtstyle

            },
            backgroundColor: '#173265',
            tooltip: {
                formatter: function(e) {
                const data = e.data;
                if (PressureType==1) {
                   return `
                        支架数量: ${data[2]}<br />
                        时间: ${data[3]}<br />
                        压力: ${data[1]}<br/>
                        最大值:${data[1]}
                    `
                }else{
                    return `
                        支架数量: ${data[2]}<br />
                        时间: ${data[3]}<br />
                        压力: ${data[1]}<br/>
                        最后值:${data[1]}<br/>

                    `
                }
                },
            },
            yAxis3D: {
                type: "category",
                name: "时间",
                axisLabel: {
                    interval:0,
                    color:"white"
                },
                //data:["00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"],
                   data:["00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16",
                       "17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33",
                       "34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50",
                       "51","52","53","54","55","56","57","58","59"],
                nameTextStyle:{
                    backgroundColor:"white",
                    fontSize:20,
                    padding:5
                },
                axisTick:{
                    lineStyle:{
                        color:"white"
                    }

                },

            },
            xAxis3D: {
                type: "category",
                name: "支架数量",
                axisLabel: {
                    interval:12,
                    color:"white"
                },
                axisTick:{
                    lineStyle:{
                        color:"white"
                    }

                },
                // minInterval: 1, //好看
                max:function () {
                    return ModbusAddrSum;
                },
                nameTextStyle:{
                    backgroundColor:"white",
                   fontSize:20,
                    padding:5
            }
            },
            zAxis3D: {
                nameTextStyle:{
                    backgroundColor:"white",
                    fontSize:20,
                    padding:5
                },
                name:"支架压力",
                axisTick:{
                    lineStyle:{
                        color:"white"
                    }

                },
                axisLabel:{
                    color:"white"
                }
            },
            visualMap: {
                type: "piecewise",
                dimension:1,
                //splitNumber: 3,
                // inRange: {
                //     color: ["#264653", "#2a9d8f", "#e9c46a"],
                // },
                pieces: [
                    { gte: 80, color: 'red' },
                    { gte: 64, lt: 80, color: 'blue' },
                    { gte: 40, lt: 64, color: 'yellow' },
                    { gte: 0, lt: 40, color: 'green' },
                    { value: 0, color: 'white' },
                ],
                textStyle:{
                    color:function(index){
                        var colors = ["red","blue","yellow","green","white"];
                        return colors[index];
                    },
                    fontSize:18
                }
            },
            dataset: {
                source: [],
            },
            series: [
                {
                    type: "bar3D",
                    //barGap:'100',/*多个并排柱子设置柱子之间的间距*/
                   // barCategoryGap:'100',//*多个并排柱子设置柱子之间的间距*
                    bevelSizeL:5,
                    shading: "lambert",
                    encode: {
                        x: "maxshelfval",
                        y: "FixTime",
                        z: "maxpressureval",
                    },
                    label: {
                        formatter: function(e){
                            return e.data[1];
                        },
                    },
                },
            ],
        };

        function getMax(data) {
            let _data_max=0;
            for (let i = 1; i < data.length; i++) {
                let v = parseInt(data[i][1]);
                if (v > _data_max) _data_max = v;

                LastOne[data[i][0]]=data[i][2];

            }
            return _data_max;
        };

        // function pickr() {
        //     var pickr1 = Pickr.create({
        //         el: "#color1",
        //         theme: "nano",
        //         default: "#0935bc",
        //         swatches: ["#0935bc"],
        //         components: {
        //             preview: true,
        //             opacity: true,
        //             hue: true,
        //             interaction: {
        //                 input: true,
        //                 clear: true,
        //                 save: true
        //             }
        //         },
        //         i18n: {
        //             "btn:save": "保存",
        //             "btn:clear": "取消"
        //         }
        //     });
        //     var pickr2 = Pickr.create({
        //         el: "#color2",
        //         theme: "nano",
        //         default: "#14ab27",
        //         swatches: ["#14ab27"],
        //         components: {
        //             preview: true,
        //             opacity: true,
        //             hue: true,
        //             interaction: {
        //                 input: true,
        //                 clear: true,
        //                 save: true
        //             }
        //         },
        //         i18n: {
        //             "btn:save": "保存",
        //             "btn:clear": "取消"
        //         }
        //     });
        //     var pickr3 = Pickr.create({
        //         el: "#color3",
        //         theme: "nano",
        //         default: "#b7a50c",
        //         swatches: ["#b7a50c"],
        //         components: {
        //             preview: true,
        //             opacity: true,
        //             hue: true,
        //             interaction: {
        //                 input: true,
        //                 clear: true,
        //                 save: true
        //             }
        //         },
        //         i18n: {
        //             "btn:save": "保存",
        //             "btn:clear": "取消"
        //         }
        //     });
        //     pickr1.on("save", color => {
        //         option.visualMap.inRange.color[0] = color.toHEXA().toString();
        //         myChart.setOption(option);
        //     });
        //     pickr2.on("save", color => {
        //         option.visualMap.inRange.color[1] = color.toHEXA().toString();
        //         myChart.setOption(option);
        //     });
        //     pickr3.on("save", color => {
        //         option.visualMap.inRange.color[2] = color.toHEXA().toString();
        //         myChart.setOption(option);
        //     });
        // }

        const url = prefix+"/test1";
        function getData(HZ) {
            axios.get(url).then((e) => {
                option.dataset.source = e.data; //这里把请求到的数据传入echarts
                option.visualMap.max=getMax(e.data);
                myChart.showLoading();     //数据加载完之前先显示一段简单的loading动画
                myChart.setOption(option);
                myChart.hideLoading();
                window.setTimeout(() => {
                    getData(HZ);
                }, (HZ?HZ:30) * 1000); //30s后 重新自己调用一次
            });
        }

        // pickr();
        var HZ=30;
        getData(HZ);
           function setFlashTime () {
           var btnVal= $("#btn").text();
           if (btnVal=="刷新时间"){
               $("#btn").text("确定").removeClass("btn-warning").addClass("btn-success");
               $(".beautiful").show();
           }
           if (btnVal=="确定"){
               var inputVal=$(".beautiful").val();
               if (inputVal=="") {
                 $.modal.alertWarning("请输入刷新时间!")
               }
               else {
               $.modal.confirm("确定修改刷新时间吗?",function () {
                   getData(inputVal);
                   layer.msg("修改成功");
                   $("#btn").text("刷新时间");
                   $(".beautiful").hide();
               })
           }
           }
           }



 //输入数据修改数据范围
        function updateArea(e) {
        var pieces=[];
        var data=$(e).val();
        if (data!=""){
            pieces.push({ gte: 80, color: 'red' });
            pieces.push({ gte: 64, lt: 80, color: 'blue' });
            pieces.push({ gte: 40, lt: 64, color: 'yellow' });
            pieces.push({gt:0,lt:data,colo:'green'});
            pieces.push({value:0,color:'white'});
            option.visualMap.pieces=pieces;
            myChart.setOption(option);
        }

        }

    </script>
</html>

图片采用了在这里插入图片描述 图片采用了echarts的3d效果,个人觉得echarts的属性比较灵活,每个属性基本都自带回调函数,便于我们灵活处理数据,重点要说一下option这个对象,所有的参数都在option对象中,我觉得最重要的方法是setOption方法,这个方法 可以方便我们处理一些需求,比如,我们要自定义映射范围,就可以通过重新设置option,然后在setoption方法,这俩方法,是最重要也是最好用的方法。 贴出来 记录一下,也方便大家 可以参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值