javascript - 绘图仪动态

标签 javascript plotly plotly.js real-time-data

我有一个无法解决的 Plotly JS Gauge 问题,我已经尝试了一千种方法来解决它。

如何实时、动态地使用 Plotly JS Gauge?

我已经尝试过这个,但它不起作用......;-(:

<script>
    function rand() {
        return Math.floor(Math.random() * (250 - 0) ) + 0;
    }


    var data = [
                {
                    domain: { x: [0, 1], y: [0, 1] },
                    value: 0,
                    title: { text: "Speed" },
                    type: "indicator",
                    mode: "gauge+number"
                }
            ];

    var layout = { width: 600, height: 500, margin: { t: 0, b: 0 } };
    Plotly.newPlot('myDiv', data, layout);

    var cnt = 0;
    var interval = setInterval(function() {
        Plotly.extendTraces('myDiv', {value: [[rand()]]}, [0])
        if(++cnt === 100) clearInterval(interval);
    }, 300);
</script>

最佳答案

尝试使用Plotly.update():

function rand() {
return Math.floor(Math.random() * (250 - 0) ) + 0;
}

var data = [
        {
            domain: { x: [0, 1], y: [0, 1] },
            value: 0,
            title: { text: "Speed" },
            type: "indicator",
            mode: "gauge+number"
        }
    ];

var layout = { width: 600, height: 500, margin: { t: 0, b: 0 } };
Plotly.newPlot('myDiv', data, layout);

var cnt = 0;
var interval = setInterval(function() {
Plotly.update('myDiv', {value: rand()}, {}, [0])
if(++cnt === 100) clearInterval(interval);
}, 800);
<head>
	<!-- Load plotly.js into the DOM -->
	<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
</head>

<body>
	<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
</body>

关于javascript - 绘图仪动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62061915/

相关文章:

javascript - 在 Google Chrome 扩展程序中使用 XMLHttpRequest

python - 如何在 AWS Elastic Beanstalk 中使用 Plotly Python SDK

d3.js - 当两个条的值为 0 时,相对 barmode 标签中的 Plotly.js 会重叠

javascript - 如何编写JavaScript正则表达式来接受8到15之间的整数和小数?

javascript - 灯箱背景颜色应使用 CSS 覆盖整个屏幕

javascript - 上传到nodejs时损坏文件/图像

javascript - Plotly.js 在悬停在图表标题上时显示工具提示

r - ggplotly 和 lubridate : Hoover shows seconds, 不是分钟

r - 如何从 R 绘图图中获取坐标

javascript - 我应该通过 npm 安装 plotly.js 还是 plotly.js-dist?