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
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
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.