Categories
JavaScript Answers

How to remove x-axis label and text in Chart.js and JavaScript?

Sometimes, we want to remove x-axis label and text in Chart.js and JavaScript.

In this article, we’ll look at how to remove x-axis label and text in Chart.js and JavaScript.

How to remove x-axis label and text in Chart.js and JavaScript?

To remove x-axis label and text in Chart.js and JavaScript, we can set some properties in options.

In Chart.js 3.x, we write

const chart = new Chart(ctx, {
  //...
  options: {
    scales: {
      x: {
        display: false,
      },
    },
  },
});

to remove x-axis labels and grid chart lines.

We write

const chart = new Chart(ctx, {
  //...
  options: {
    scales: {
      x: {
        ticks: {
          display: false,
        },
      },
    },
  },
});

to remove only x-axis labels.

In Chart.js 2.x, we write

const chart = new Chart(ctx, {
  //...
  options: {
    scales: {
      xAxes: [
        {
          display: false,
        },
      ],
    },
  },
});

to remove x-axis labels and grid chart lines.

We write

const chart = new Chart(ctx, {
  //...
  options: {
    scales: {
      xAxes: [
        {
          ticks: {
            display: false,
          },
        },
      ],
    },
  },
});

to remove only x-axis labels.

Conclusion

To remove x-axis label and text in Chart.js and JavaScript, we can set some properties in options.

Categories
JavaScript Answers

How to use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript?

Sometimes, we want to use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript.

In this article, we’ll look at how to use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript.

How to use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript?

To use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript, we can use the getZoom and setZoom methods.

For instance, we write

google.maps.event.addListenerOnce(map, "bounds_changed", function (event) {
  if (this.getZoom() > 15) {
    this.setZoom(15);
  }
});

to listen to the bounds_change event on the map with addListenerOnce.

In the event listener, we call getZoom to check of the zoom level is bigger than 15.

If it is, then we call setZoom to set the zoom level to 15.

Conclusion

To use setZoom() after use fitBounds() with Google Maps API V3 with JavaScript, we can use the getZoom and setZoom methods.

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.