JavaScript - Modificar un parámetro de una función con dat.gui y que se modifique su grafica

 
Vista:
sin imagen de perfil
Val: 7
Ha aumentado su posición en 23 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

Modificar un parámetro de una función con dat.gui y que se modifique su grafica

Publicado por Ayelen (1 intervención) el 02/04/2021 00:12:30
¡Bunas tardes!

El objetivo del trabajo es obtener la gráfica de una función en 3D, la cual se pueda ir modificando con un controlador que cambie uno de sus parámetros. Hasta ahora eh podido graficar la curva y poner el controlador que modifica uno de los parámetros, pero a medida que cambio lo parámetros me va graficando las nuevas curvas sobre las anteriores, por lo que me gustaría saber cómo hacer para que borre la curva vieja y aparezca solo la nueva.

Espero puedan ayudarme!!!

Muchas gracias.

Adjunto el código:

1
2
3
4
5
6
7
8
9
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<head>
 
	<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
    <script src="dat.gui.js"></script>
 
</head>
 
<body>
	<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
<script >
 
 
	var button_layer_1_height = 1.2
	var button_layer_2_height = 1.1
	var annotation_offset = 0.04
	var zPts = [];
	var xPts = [];
	var yPts = [];
 
	//Defino parámetro a controlar con dat.gui
    var params = {
				a: 0.1,
 
			};
 
 
 
    var updateGraphFunc = function(){
 
    for(x=-3; x<3; x+=0.01) {
		let zTemp = [];
		let yTemp = [];
		let xTemp = [];
    for (y=0; y<0.5; y+=0.01) {
    zTemp.push((params.a+Math.exp(-(x*x))*Math.sqrt(2/(Math.PI*y)))*[Math.sin(x/y*y)+ Math.sin(x*x/y*y)]);
    yTemp.push(y);
    xTemp.push(x);
  }
 
	zPts.push(zTemp);
	yPts.push(yTemp);
	xPts.push(xTemp);
  }
 
	var data = [{
		z: zPts,
		x: xPts,
		y: yPts,
		type: 'surface',
		colorscale:'Jet',
		contours: {
		z: {
		show:true,
		usecolormap: true,
		highlightcolor:"#42f462",
		project:{z: true}
    }
  }
 
  }];
  Plotly.newPlot('myDiv', data, layout);
    }
    var updateGraph = function() { updateGraphFunc(); }
 
  var updatemenus=[
 
    {
        buttons: [
            {
                args: ['colorscale', 'Viridis'],
                label: 'Viridis',
                method: 'restyle'
            },
            {
                args: ['colorscale', 'Electric'],
                label:'Electric',
                method:'restyle'
            },
            {
                args: ['colorscale', 'Earth'],
                label:'Earth',
                method:'restyle'
            },
            {
                args: ['colorscale', 'Hot'],
                label:'Hot',
                method:'restyle'
            },
            {
                args: ['colorscale', 'Portland'],
                label:'Portland',
                method:'restyle'
            },
            {
                args: ['colorscale', 'Blackbody'],
                label:'Blackbody',
                method:'restyle'
            },
 
            {
                args: ['colorscale', 'Cividis'],
                label:'Cividis',
                method:'restyle'
            }
        ],
				direction: 'left',
				pad: {'r': 10, 't': 10},
				showactive: true,
				type: 'buttons',
				x: 0.15,
				xanchor: 'left',
				y: button_layer_1_height,
				yanchor: 'top'
    },
   ]
    var annotations = [
 
    {
			text: 'Colorscale:',
			x: 0,
			y: button_layer_1_height - annotation_offset,
			yref: 'paper',
			align: 'left',
			showarrow: false
    },
    ]
 
	var layout = {
    autosize: false,
    width: 1300,
    height: 600,
    margin: {t: 130, b: 0, l: 0, r: 0},
    updatemenus: updatemenus,
    annotations: annotations,
    scene: {
        xaxis:{
            gridcolor: 'rgb(255, 255, 255)',
            zerolinecolor: 'rgb(255, 255, 255)',
            showbackground: true,
            backgroundcolor:'rgb(230, 230,230)'
        },
        yaxis: {
            gridcolor: 'rgb(255, 255, 255)',
            zerolinecolor: 'rgb(255, 255, 255)',
            showbackground: true,
            backgroundcolor: 'rgb(230, 230, 230)'
        },
        zaxis: {
            gridcolor: 'rgb(255, 255, 255)',
            zerolinecolor: 'rgb(255, 255, 255)',
            showbackground: true,
            backgroundcolor: 'rgb(230, 230,230)'
        },
        aspectratio: {x: 1, y: 1, z: 0.7},
        aspectmode: 'manual'
  }
  };
 
 
 
    //Control GUI
 
     var gui = new dat.GUI();
 
        var folder0 = gui.addFolder('Parameters');
        var aGUI= folder0.add(params, 'a').onChange(updateGraph);
            folder0.open();
 
 
        // onChange or onFinishChange
 
 
        gui.open();
 
	// do this last so all structures are set up, all parameters defined
	updateGraphFunc();
 
 
 
</script>
</body>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder