Categories
JavaScript Answers

How to listen for click events on pie charts in Chart.js with JavaScript?

Sometimes, we want to listen for click events on pie charts in Chart.js with JavaScript.

In this article, we’ll look at how to listen for click events on pie charts in Chart.js with JavaScript.

How to listen for click events on pie charts in Chart.js with JavaScript?

To listen for click events on pie charts in Chart.js with JavaScript, we can add a onClick method into the chart object.

For instance, we write

<canvas id="myChart" width="200" height="200"></canvas>

to add a canvas.

Then we write

const ctx = document.getElementById("myChart").getContext("2d");
const 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,
          },
        },
      ],
    },
    onClick(e) {
      const activePoints = myChart.getElementsAtEvent(e);
      const selectedIndex = activePoints[0]._index;
      console.log(this.data.datasets[0].data[selectedIndex]);
    },
  },
});

to create a chart with the Chart constructor.

We call it with the ctx canvas context and the an object with the onClick method.

In it, we get the points with myChart.getElementsAtEvent.

Then we get the index of the clicked point with activePoints[0]._index.

Then we use this.data.datasets[0].data[selectedIndex]) to get the data for the point.

Conclusion

To listen for click events on pie charts in Chart.js with JavaScript, we can add a onClick method into the chart object.

Categories
Vue Answers

How to call parent method with component with Vue.js?

Sometimes, we want to call parent method with component with Vue.js.

In this article, we’ll look at how to call parent method with component with Vue.js.

How to call parent method with component with Vue.js?

To call parent method with component with Vue.js, we can get the parent component’s method from the $parent property.

For instance, we write

<script src="https://unpkg.com/vue"></script>

<template id="child-template">
  <span @click="someMethod">Click me!</span>
</template>

<div id="app">
  <child></child>
</div>

<script>
  Vue.component("child", {
    template: "#child-template",
    methods: {
      someMethod() {
        this.$parent.someMethod();
      },
    },
  });

  const app = new Vue({
    el: "#app",
    methods: {
      someMethod() {
        console.log("parent");
      },
    },
  });
</script>

to define the child component with Vue.component.

In it, we add the someMethod method that calls this.$parent.someMethod.

this.$parent.someMethod is the parent component’s someMethod method.

Therefore, when we click the span, we see 'parent' logged.

Conclusion

To call parent method with component with Vue.js, we can get the parent component’s method from the $parent property.

Categories
JavaScript Answers

How to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase?

Sometimes, we want to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase.

In this article, we’ll look at how to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase.

How to create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase?

To create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase, we can create a regex with all the patterns in it.

For instance, we write

const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/;

to define the regex that matches all the patterns.

(?=.*\d) matches at least one digit.

(?=.*[a-z]) matches at least one lower case.

(?=.*[A-Z]) matches at least one upper case.

[a-zA-Z0-9]{8,} matches at least 8 from the mentioned characters.

Conclusion

To create a JavaScript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase, we can create a regex with all the patterns in it.

Categories
HTML

How to run HTML file on localhost?

Sometimes, we want to run HTML file on localhost.

In this article, we’ll look at how to run HTML file on localhost.

How to run HTML file on localhost?

To run HTML file on localhost, we can use the http-server Node module.

First, we install Node.js in our system.

Then In CMD, run the command npm install http-server -g.

Next, we navigate to the path of your file folder in CMD and run the http-server command

Finally, we go to our browser and type localhost:8080.

Now we should see the page we want to load.

Conclusion

To run HTML file on localhost, we can use the http-server Node module.

Categories
JavaScript Answers

How to convert time interval given in seconds into more human readable form with JavaScript?

Sometimes, we want to convert time interval given in seconds into more human readable form with JavaScript.

In this article, we’ll look at how to convert time interval given in seconds into more human readable form with JavaScript.

How to convert time interval given in seconds into more human readable form with JavaScript?

To convert time interval given in seconds into more human readable form with JavaScript, we can use the moment duration humanize method.

For instance, we write

const duration = moment.duration(31536000);
console.log(duration.humanize());

to create a duration object by calling moment.duration with a timestamp in milliseconds.

Then we call duration.humanize to return a human readable string with the duration.

Conclusion

To convert time interval given in seconds into more human readable form with JavaScript, we can use the moment duration humanize method.