Skip to main content

Unable to add a slider in Plotly.js

I tried to add a slider to change the value of scaleFactor which has been added in the code. But even after writing down the steps and arguments I'm unable to add the slider in the existing 'var layout' and thus getting no results.

Here is the code that I've written to implement the slider

var scaleFactor = 120; // adjust this to change the scale of the cones
var velocity_trace_cones = [];

// code for creating velocity cones goes here

var data = [pressure_trace_edges, velocity_trace_nodes, velocity_trace_edges, ...velocity_trace_cones];

var steps = [];
for (var i = 0; i <= 10; i++) {
    var newScaleFactor = i * 10;
    var newData = [];
    for (var j = 0; j < velocity_trace_cones.length; j++) {
        var cone = velocity_trace_cones[j];
        var newCone = Object.assign({}, cone); // create a deep copy of the cone object
        newCone.u = [cone.u[0] * newScaleFactor];
        newCone.v = [cone.v[0] * newScaleFactor];
        newCone.w = [cone.w[0] * newScaleFactor];
        newData.push(newCone);
    }
    var step = {
        label: newScaleFactor.toString(),
        method: 'update',
        args: [{visible: false}, {visible: false}, {visible: false}, ...newData] // replace the velocity cones with new ones based on the new scale factor
    };
    steps.push(step);
}

And here is the part where I tried to implement the slider into an existing layout:

updatemenus: [{
            buttons: [{
                method: 'update',
                args: [{visible: false}, {visible: true}, {visible: true}, ...velocity_trace_cones], // show only the nodes and edges by default
                label: 'Reset'
            }],
            direction: 'left',
            pad: {t: 50, r: 10},
            showactive: false,
            type: 'buttons',
            x: 0.1,
            y: 1.1
        }],
    sliders: [{
        steps: steps,
        active: 0,
        x: 0.1,
        len: 0.9,
        pad: {t: 50, b: 10},
        currentvalue: {
            xanchor: 'right',
            prefix: 'Scale Factor: ',
            font: {size: 20, color: '#888'}
        },
        transition: {duration: 500, easing: 'cubic-in-out'} // add transition to slider
    }],
    xaxis: {title: 'X Axis'},
    yaxis: {title: 'Y Axis'},
    zaxis: {title: 'Z Axis'},

    //for changing the 2D view
    xaxis: {
        visible: false,
        showgrid: false,
    },
    yaxis: {
        visible: false,
        showgrid: false,
    },
    zaxis: {
        visible: false,
        showgrid: false,
    }
};
Plotly.newPlot('network', data, layout);
  
                })

As you can see I tried the same Plotly.newPlot which I guess is returning no results

If you can make any changes it would be a great help.

My full code:

<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>
            Visualization
        </title>
        <link rel="stylesheet" href="style.css">
        <script src="https://cdn.plot.ly/plotly-1.58.5.min.js"></script>
        <style>
            .graph-container {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
            }
            .main-panel {
                width: 100%;
                height: 800px;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
            }
            .side-panel {
            position: fixed;
            top: 0;
            bottom: 0;
            right: -300px;
            width: 300px;
            background-color: #f2f2f2;
            transition: right 0.5s;
            overflow-y: auto;
            padding: 20px;
        }
           
            .side-panel.open {
            right: 0;
            }

            .arrow {
            position: absolute;
            top: 20px;
            left: 20px;
            width: 0;
            height: 0;
            border-top: 20px solid transparent;
            border-bottom: 20px solid transparent;
            border-right: 20px solid #2196F3;
            cursor: pointer;
        }

            .arrow.open {
                transform: rotate(180deg);
            }
        </style>

    </head>
    <body>
        
        <div class="graph-container">
            
            <div id="network" class="main-panel"></div>
            <div id="graph" class="side-panel">
                <div class="arrow"></div>
            </div>
        </div>

        <div id="main">
            <!-- <button class="openbtn" onclick="openNav()">☰ Open Sidebar</button>   -->
          </div>

        <script>

            function openNav() {
                document.getElementById("graph").style.width = "500px";
                document.getElementById("main").style.marginLeft = "500px";
            }
            function closeNav() {
                document.getElementById("graph").style.width = "0";
                document.getElementById("main").style.marginLeft= "0";
            }

            const sidepanel = document.querySelector('.side-panel');
            const arrow = document.querySelector('.arrow');

            arrow.addEventListener('click', function() {
            sidepanel.classList.toggle('open');
            arrow.classList.toggle('open');
        });

            //read the data from the json file
            fetch("results.json")
                .then(response => response.json())
                .then( data => {
                    //This part of the code is used to used initialize the variables which will be used to create a network graph

                    //Initializing array for storing the variables
                    var nnodes = data.nnodes;
                    var nedges = data.nedges;
                    var ncones = data.ncones;
                    var x = data.x;
                    var y = data.y;
                    var z = data.z;
                    var edge_x = data.edge_x;
                    var edge_y = data.edge_y;
                    var edge_z = data.edge_z;
                    var pressure_edge_gradient = data.pressure_gradient;

                    console.log(data)
                    // Velocity and the max and min values of the velocity
                    var velocity = data.velocity;
                    var max_velocity = data.max_velocity;
                    var min_velocity = data.min_velocity;

                    //This part of code is used to initialize the variables which will be used to create cones
                    var x_cone = data.x_cone;
                    var y_cone = data.y_cone;
                    var z_cone = data.z_cone;
                    var u_cone = data.u_cone;
                    var v_cone = data.v_cone;
                    var w_cone = data.w_cone;
                    console.log(x_cone[0])

                    //Keeping a list of true and false values for the concentration and the cones which are used while displaying through layout
                    var TrueList_cones = [];
                    var FalseList_cones = [];
                    for (var i = 0; i < ncones; i++) {
                        TrueList_cones.push(true);
                        FalseList_cones.push(false);
                    };

                    //This part of the code is used to create a pressure graph
                    //Creating Edges for pressure graph
                    var pressure_trace_edges = {
                        x: edge_x,
                        y: edge_y,
                        z: edge_z,
                        mode: 'lines',
                        line: {
                            colorscale: "YlOrRd",
                            color: pressure_edge_gradient,
                            width: 5,
                            showscale: true,
                            //decrease the size of the colorbar
                            colorbar: {
                                thickness: 20,
                                len: 0.5,
                                x: 0.9,
                                y: 0.5,
                                title: {
                                    text: "Pressure",
                                    side: "right"
                                }
                            }
                        },
                        type: 'scatter3d',
                        name: "Pressure Edges",
                        hoverinfo: 'none',
                        visible: true,
                        showlegend: true,
                        
                        //set the position of the legend
                    };

                    //This part of the code is used to create a velocity graph
                    //Creating nodes for velocity graph
                    
                    var velocity_trace_nodes = {
                        x: x,
                        y: y,
                        z: z,
                        mode: 'markers',
                        marker: {
                            size: Array(nnodes).fill(6),
                            sizeref: 0.5, 
                            
                            color: "rgb(200,200,200))",
                            showscale: false,
                        },
                        type: 'scatter3d',
                        name: "Velocity Nodes",
                        hoverinfo: 'none',
                        visible: false,
                        showlegend: false,
                    };

                    //Creating edges for velocity graph
                    var velocity_trace_edges = {
                        x: edge_x,
                        y: edge_y,
                        z: edge_z,
                        mode: 'lines',
                        line: {
                            color: "rgb(200,200,200)",
                            width: 5,
                            showscale: true,
                        },
                        type: 'scatter3d',
                        name: "Velocity Edges",
                        hoverinfo: 'none',
                        showlegend: false,
                        visible: false,
                        showscale: false,
                        
                    };

                    //Creating cones for velocity graph
                    var scaleFactor = 120; // adjust this to change the scale of the cones
var velocity_trace_cones = [];

// code for creating velocity cones goes here

var data = [pressure_trace_edges, velocity_trace_nodes, velocity_trace_edges, ...velocity_trace_cones];

var steps = [];
for (var i = 0; i <= 10; i++) {
    var newScaleFactor = i * 10;
    var newData = [];
    for (var j = 0; j < velocity_trace_cones.length; j++) {
        var cone = velocity_trace_cones[j];
        var newCone = Object.assign({}, cone); // create a deep copy of the cone object
        newCone.u = [cone.u[0] * newScaleFactor];
        newCone.v = [cone.v[0] * newScaleFactor];
        newCone.w = [cone.w[0] * newScaleFactor];
        newData.push(newCone);
    }
    var step = {
        label: newScaleFactor.toString(),
        method: 'update',
        args: [{visible: false}, {visible: false}, {visible: false}, ...newData] // replace the velocity cones with new ones based on the new scale factor
    };
    steps.push(step);
}

//creating a list for dummy nodes for concentration nodes
var layout = {
    updatemenus: [
        {
            x: 0,
            y: 15.5,
            yanchor: "top",
            xanchor: "left",
            showactive: false,
            direction: "Right",
            height: 8000,
            width: 8000,
            buttons: [
                {
                    method: "restyle",
                    args: ["visible", [true,false,false,...FalseList_cones]],
                    label: "Pressure"
                },
                {
                    method: "restyle",
                    args: ["visible", [false,true,true,...TrueList_cones]],
                    label: "Velocity"
                },
            ],
        },
    ],

    updatemenus: [{
            buttons: [{
                method: 'update',
                args: [{visible: false}, {visible: true}, {visible: true}, ...velocity_trace_cones], // show only the nodes and edges by default
                label: 'Reset'
            }],
            direction: 'left',
            pad: {t: 50, r: 10},
            showactive: false,
            type: 'buttons',
            x: 0.1,
            y: 1.1
        }],
    sliders: [{
        steps: steps,
        active: 0,
        x: 0.1,
        len: 0.9,
        pad: {t: 50, b: 10},
        currentvalue: {
            xanchor: 'right',
            prefix: 'Scale Factor: ',
            font: {size: 20, color: '#888'}
        },
        transition: {duration: 500, easing: 'cubic-in-out'} // add transition to slider
    }],
    xaxis: {title: 'X Axis'},
    yaxis: {title: 'Y Axis'},
    zaxis: {title: 'Z Axis'},

    //for changing the 2D view
    xaxis: {
        visible: false,
        showgrid: false,
    },
    yaxis: {
        visible: false,
        showgrid: false,
    },
    zaxis: {
        visible: false,
        showgrid: false,
    }
};
Plotly.newPlot('network', data, layout);
  
                })
        </script>

    </body>
</html>
Via Active questions tagged javascript - Stack Overflow https://ift.tt/gBhXkJi

Comments

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data