Plotly uirevision 在 plotly.js-basic-dist-min 上不起作用。

huangapple go评论60阅读模式
英文:

Plotly uirevision doesn't work on plotly.js-basic-dist-min?

问题

我正在尝试构建 Plotly 图表,在动态添加数据时不重置图表。

我正在使用 buddy.works 作为部署引擎,而且我有内存限制,所以必须使用 "plotly.js-basic-dist-min" 版本。

这是我想要实现的效果:https://codepen.io/plotly/pen/ebMJEW。

但在使用 dist-min 版本时遇到了问题,因为 uirevision 在以下情况下可以正常工作:

import Plot from 'react-plotly.js';

但在以下情况下无法工作:

import createPlotlyComponent from "react-plotly.js/factory";
var Plotly = require('plotly.js-basic-dist-min');

是否有其他方法?我有遗漏什么吗?谢谢!

以下是您的完整代码:

import React, { useEffect, useState } from 'react';
import './App.css';
import createPlotlyComponent from "react-plotly.js/factory";
var Plotly = require('plotly.js-basic-dist-min')

function App() {
  const Plot = createPlotlyComponent(Plotly);
  const [data, setData] = useState<any>({
    x: [],
    y: [],
    type: 'scatter'
  })
  useEffect(() => {
    let x: any = []
    let y: any = []
    let i = 1;
    let interval = setInterval(() => {
      x = [...x, i]
      y = [...y, Math.floor(Math.random() * 10) + 1]
      setData({
        x: x,
        y: y,
        type: 'scatter'
      })
      i++
    }, 5000)
    return () => {
      clearInterval(interval);
    }
  }, [])
  return (
    <div className="App">
      <Plot
        data={[data]}
        config={{ scrollZoom: false, responsive: true, displaylogo: false }}
        useResizeHandler={true}
        style={{ width: "100%", height: "100%" }}
        layout={{
          title: 'User Zoom Persists<br>When uirevision Unchanged',
          uirevision: 'true',
          xaxis: { autorange: true },
          yaxis: { autorange: true }
        }}
      />
    </div>
  );
}

export default App;

请注意,我只提供了代码部分的翻译,没有包括问题的回答。如果您有其他问题或需要更多帮助,请随时提出。

英文:

I'm trying to build Plotly chart where it shouldn't reset chart when dynamically adding data.
I'm using buddy.works as deploy engine and i have limited ram so had to use " plotly.js-basic-dist-min"
this is what I'm trying to achieve https://codepen.io/plotly/pen/ebMJEW .

but I'm facing problem while using dist-min version. as uirevision works fine on

import Plot from &#39;react-plotly.js&#39;;

but it doesn't work for

import createPlotlyComponent from &quot;react-plotly.js/factory&quot;;
var Plotly = require(&#39;plotly.js-basic-dist-min&#39;)

Is there any other way? did i miss something? Thanks!

here's my full code

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

import React, { useEffect, useState } from &#39;react&#39;;
import &#39;./App.css&#39;;
import createPlotlyComponent from &quot;react-plotly.js/factory&quot;;
var Plotly = require(&#39;plotly.js-basic-dist-min&#39;)
function App() {
const Plot = createPlotlyComponent(Plotly);
const [data, setData] = useState&lt;any&gt;({
x:[],
y:[],
type:&#39;scatter&#39;
})
useEffect(()=&gt;{
let x:any=[]
let y:any=[]
let i=1;
let inval = setInterval(()=&gt;{
x= [...x, i]
y= [...y, Math.floor(Math.random() * 10) + 1]
setData({
x:x,
y:y,
type:&#39;scatter&#39;
})
i++
}, 5000)
return ()=&gt;{
clearInterval(inval);
}
},[])
return (
&lt;div className=&quot;App&quot;&gt;
&lt;Plot
data={[data]}
config={{ scrollZoom: false, responsive: true, displaylogo: false}}
useResizeHandler={true}
style={{ width: &quot;100%&quot;, height: &quot;100%&quot; }} 
layout={{
title: &#39;User Zoom Persists&lt;br&gt;When uirevision Unchanged&#39;,
uirevision:&#39;true&#39;,
xaxis: {autorange: true},
yaxis: {autorange: true}
}}
/&gt;
&lt;/div&gt;
);
}
export default App;

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 0

moving const Plot = createPlotlyComponent(Plotly); outside function fix it.

英文:

moving const Plot = createPlotlyComponent(Plotly); outside function fix it.

huangapple
  • 本文由 发表于 2023年2月6日 16:09:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定