【问题标题】:RGraph Pie Chart Doesn't Refresh ValueRGraph饼图不刷新值
【发布时间】:2021-05-01 00:29:09
【问题描述】:

我尝试从 API 获取数据并刷新 Electron 应用程序页面上的饼图,但我无法刷新图表的值。图表上的值永远不会改变。我之前用 RGraph Gauge 尝试过这种方法并且它有效,但是使用 Electron 不会刷新值。我究竟做错了什么?谢谢您的帮助。 Screenshot of my electron application

<script>
    const ipcRenderer = require("electron").ipcRenderer;
    const  {session}  = require('electron').remote;
    document.getElementById("backBtn").addEventListener("click",()=>{
        ipcRenderer.send("btnBack","101");
    });

    temp = new RGraph.HorseshoeMeter({
        id: 'temp',
        min: 0,
        max: 50,
        value: 15,
        options: {
            colors: ["#3678c1", '#BED1E3'],
            textColor: "#3678c1",
            animationOptions: {frames: 60}                    // Doesn't need to be a string
        }
    }).draw();

    hum = new RGraph.HorseshoeMeter({
        id: 'hum',
        min: 0,
        max: 100,
        value: 45,
        options: {
            colors: ["#3678c1", '#BED1E3'],
            textColor: "#3678c1",
            animationOptions: {frames: 60}                    // Doesn't need to be a string
        }
    }).draw();

    iaq = new RGraph.HorseshoeMeter({
        id: 'iaq',
        min: 0,
        max: 3000,
        value: 1232,
        options: {
            colors: ["#3678c1", '#BED1E3'],
            textColor: "#3678c1",
            animationOptions: {frames: 60}                    // Doesn't need to be a string
         }
    }).draw();

async function getSessionInfo(){
    let myPromise = new Promise(function(myResolve, myReject) {
        session.defaultSession.cookies.get({name: "human_presence"},   (error,cookies)=>{
            if(error){ myReject(error)}
            if(cookies.length>0){
                let arr = cookies[0];
                if(arr.name === "human_presence" && ( (Date.now()-arr.expirationDate) < 600000)){
                    let obj = JSON.parse(arr.value);
                    myResolve(obj.accessToken);
                }
                else{ myResolve("Token bulunamadı")}
            }
        });
    });
    return await myPromise;
}

function httpCall(){
    getSessionInfo().then(function (val){
        let method = "GET";
        let url = "http://localhost:4000/classroom/101";
        let xmlHttpRequest = new XMLHttpRequest();
        xmlHttpRequest.addEventListener("readystatechange", function() {
            if(this.readyState === 4) {
                console.log(this.responseText);
                let obj = JSON.parse(this.responseText);
                console.log(obj);
                document.getElementById("dateTime-101").innerHTML = "Son Kayıt Zamanı : "+obj.created;
                document.getElementById("NoS-101").innerHTML = "Öğrenci Sayısı : "+obj.NoS;
                temp.value = parseInt(obj.Temp);
                hum.value = parseInt(obj.Hum);
                iaq.value = parseInt(obj.IAQ);

                RGraph.redrawCanvas(temp.canvas);
                RGraph.redrawCanvas(hum.canvas);
                RGraph.redrawCanvas(iaq.canvas);
            }
        });
        xmlHttpRequest.open(method, url);
        xmlHttpRequest.setRequestHeader('Authorization', 'Bearer ' + val);
        xmlHttpRequest.send();
    })
}

window.onload = httpCall();
window.setInterval(function(){
    httpCall();
}, 20000);

【问题讨论】:

    标签: javascript html electron rgraph


    【解决方案1】:

    这是我在 RGraph 论坛上发布的答案:

    这是因为 HorseShoe 仪表并不是真正的“真实”RGraph 图表对象 - 而是饼图的改编版。因此,我认为在更新时重绘整个图表会更容易。

    这里有一些代码:

    <canvas id="cvs" width="400" height="400">[No canvas support]</canvas>
    
    <script>
        function draw (value)
        {
            RGraph.reset(document.getElementById('cvs'));
    
            new RGraph.HorseshoeMeter({
                id: 'cvs',
                min: 0,
                max: 10000,
                value: value,
                options: {
                }
            }).roundRobin();
        }
        
        delay = 2000;
        
        function update ()
        {
            var value = Math.random(0, 1000);
    
            draw(value * 10000);
            
            // Call ourselves again
            setTimeout(update, delay);
        }
        
        setTimeout(update, delay);
    </script>
    

    这是代码的 CodePen:

    https://codepen.io/rgraph/pen/gOLYOej

    【讨论】:

    • 对于下一个版本,我想我将把基于饼图的仪表(马蹄形仪表、分段仪表、活动仪表、径向进度仪表)推广为“真实”图表对象,而不是基于饼形图。这将使动画变得更好 - 在您的情况下,这意味着图表在更改值时不会总是从零开始动画。完成后看起来好多了。
    猜你喜欢
    • 2017-07-13
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多