Monday, 24 August 2020

node ejs function a script on click

I have a simple function using a graph api and I want to run it when the user pushes a button. The file I am running from is a ejs file with a header and footer as a prefix and suffix to the code.

My code is below:

<%- include("./partials/header")%>
<script type="text/javascript" src ="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.min.js"></script>

<button onclick="<%Functions.common1()%>">Click me</button>

<canvas id="myChart" width="400" height="300"></canvas>

<button onclick="<%Functions.common1()%>">Graph me</button>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});
</script>
<%- include("./partials/footer")%>

I have a function called common1 which is an exported function in another file called functions which simply console.logs a string. I wish to run the entire script to run the graph on the click of a button, how do I do this?



from node ejs function a script on click

No comments:

Post a Comment