Categories
JavaScript Answers

How to convert integer array to string array in JavaScript?

Sometimes, we want to convert integer array to string array in JavaScript.

In this article, we’ll look at how to convert integer array to string array in JavaScript.

How to convert integer array to string array in JavaScript?

To convert integer array to string array in JavaScript, we call the array map method with String.

For instance, we write

const strs = sphValues.map(String);

to call the array map method with String to convert each value in the sphValues array to strings.

The returned array is assigned to strs.

Conclusion

To convert integer array to string array in JavaScript, we call the array map method with String.

Categories
JavaScript Answers

How to set different color for each bar in a bar chart with Chart.js and JavaScript?

Sometimes, we want to set different color for each bar in a bar chart with Chart.js and JavaScript.

In this article, we’ll look at how to set different color for each bar in a bar chart with Chart.js and JavaScript.

How to set different color for each bar in a bar chart with Chart.js and JavaScript?

To set different color for each bar in a bar chart with Chart.js and JavaScript, we can set the fillColor of each entry in the datasets array.

For instance, we write

const barChartData = {
  labels: ["January", "February", "March"],
  datasets: [
    {
      label: "My First dataset",
      fillColor: "rgba(220,220,220,0.5)",
      strokeColor: "rgba(220,220,220,0.8)",
      highlightFill: "rgba(220,220,220,0.75)",
      highlightStroke: "rgba(220,220,220,1)",
      data: [20, 59, 80],
    },
  ],
};

window.onload = () => {
  const ctx = document.getElementById("mycanvas").getContext("2d");
  window.myObjBar = new Chart(ctx).Bar(barChartData, {
    responsive: true,
  });

  myObjBar.datasets[0].bars[0].fillColor = "green";
  myObjBar.datasets[0].bars[1].fillColor = "orange";
  myObjBar.datasets[0].bars[2].fillColor = "red";
  myObjBar.update();
};

To add the datasets object property into barChartData.

Then we use that to create the Bar chart.

Next, we set the bar colors with

myObjBar.datasets[0].bars[0].fillColor = "green";
myObjBar.datasets[0].bars[1].fillColor = "orange";
myObjBar.datasets[0].bars[2].fillColor = "red";

Finally, we call update to update the chart.

Conclusion

To set different color for each bar in a bar chart with Chart.js and JavaScript, we can set the fillColor of each entry in the datasets array.

Categories
JavaScript Answers

How to scroll to the middle of the element with JavaScript scrollIntoView()?

Sometimes, we want to scroll to the middle of the element with JavaScript scrollIntoView().

In this article, we’ll look at how to scroll to the middle of the element with JavaScript scrollIntoView().

How to scroll to the middle of the element with JavaScript scrollIntoView()?

To scroll to the middle of the element with JavaScript scrollIntoView(), we can set the block and inline properties to 'center'.

For instance, we write

document.getElementById("myID").scrollIntoView({
  behavior: "auto",
  block: "center",
  inline: "center",
});

to select the element we want to scroll to with getElementById.

Then we call scrollIntoView with an object with block and inline properties set to 'center' to scroll to the middle of the element we selected.

Conclusion

To scroll to the middle of the element with JavaScript scrollIntoView(), we can set the block and inline properties to 'center'.

Categories
JavaScript Answers

How to embed OpenStreetMap map in webpage with JavaScript?

Sometimes, we want to embed OpenStreetMap map in webpage with JavaScript.

In this article, we’ll look at how to embed OpenStreetMap map in webpage with JavaScript.

How to embed OpenStreetMap map in webpage with JavaScript?

To embed OpenStreetMap map in webpage with JavaScript, we can use Leaflet.

For instance, we write

<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
<link
  href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
  rel="stylesheet"
/>
<div id="osm-map"></div>

to add the JavaScript and CSS files for Leaflet and the map container div.

Then we write

const element = document.getElementById("osm-map");
element.style = "height: 300px;";

const map = L.map(element);
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
  attribution:
    '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);

const target = L.latLng("47.50737", "19.04611");
map.setView(target, 14);
L.marker(target).addTo(map);

to select the div with getElementById.

Then we create the map with L.map.

Next, we add a layer with L.tileLayer.

We call addTo to add the layer to the map.

And then we set the location of the map with setView.

And we call L.marker with target and addTo to add the marker to the map.

Conclusion

To embed OpenStreetMap map in webpage with JavaScript, we can use Leaflet.

Categories
JavaScript Answers

How to force landscape orientation mode with JavaScript?

Sometimes, we want to force landscape orientation mode with JavaScript.

In this article, we’ll look at how to force landscape orientation mode with JavaScript.

How to force landscape orientation mode with JavaScript?

To force landscape orientation mode with JavaScript, we can use the screen.orientation.lock method.

For instance, we write

screen.orientation.lock("landscape");

to call screen.orientation.lock to lock the screen to landscape mode.

Conclusion

To force landscape orientation mode with JavaScript, we can use the screen.orientation.lock method.