Categories
JavaScript Answers

How to customize the colors of individual series with HighCharts and JavaScript?

Sometimes, we want to customize the colors of individual series with HighCharts and JavaScript.

In this article, we’ll look at how to customize the colors of individual series with HighCharts and JavaScript.

How to customize the colors of individual series with HighCharts and JavaScript?

To customize the colors of individual series with HighCharts and JavaScript, we set the color property of each object in the series array property.

For instance, we write

const chart = new Highcharts.Chart({
  chart: {
    renderTo: "container",
  },
  xAxis: {
    type: "datetime",
  },

  series: [
    {
      name: "John",
      color: "#0066FF",
      dashStyle: "ShortDash",
      data: [
        [Date.UTC(2022, 0, 1), 29.9],
        [Date.UTC(2022, 2, 1), 71.5],
        [Date.UTC(2022, 3, 1), 106.4],
      ],
    },
    {
      name: "Jane",
      color: "#FF0000",
      data: [
        [Date.UTC(2022, 0, 1), 60.9],
        [Date.UTC(2022, 1, 1), 40.5],
        [Date.UTC(2022, 2, 1), 90.0],
        [Date.UTC(2022, 3, 1), 80.4],
      ],
    },
  ],
});

to create a chart with the Highcharts.Chart constructor.

We call it with an object that has the series property set to an array with the series data.

We set the series color by setting the color property.

Conclusion

To customize the colors of individual series with HighCharts and JavaScript, we set the color property of each object in the series array property.

Categories
JavaScript Answers

How to reset textbox value in JavaScript?

To reset textbox value in JavaScript, we set the value property to an empty string.

For instance, we write

document.getElementById("searchField").value = "";

to select the input with getElementById.

Then we set its value property to an empty string.

Conclusion

To reset textbox value in JavaScript, we set the value property to an empty string.

Categories
JavaScript Answers

How to create array of unique objects by property with JavaScript?

To create array of unique objects by property with JavaScript, we use the array filter method.

For instance, we write

const getUniqueBy = (arr, prop) => {
  const set = new Set();
  return arr.filter((o) => !set.has(o[prop]) && set.add(o[prop]));
};

to define the getUniqueBy function.

In it, we create a set with the Set constructor.

And then we return the array returned by filter that adds the o[prop] to the set if it doesn’t exist and if the property value doesn’t exist.

Categories
JavaScript Answers

How to force the soft keyboard to hide with JavaScript?

Sometimes, we want to force the soft keyboard to hide with JavaScript.

In this article, we’ll look at how to force the soft keyboard to hide with JavaScript.

How to force the soft keyboard to hide with JavaScript?

To force the soft keyboard to hide with JavaScript, we call blur when we focus on the input.

For instance, we write

<input type="text" id="phone-number" onfocus="blur();" />;

to call blur when we focus on the input by setting the onfocus attribute to blur().

Conclusion

To force the soft keyboard to hide with JavaScript, we call blur when we focus on the input.

Categories
JavaScript Answers

How to open new page in same window with JavaScript?

Sometimes, we want to open new page in same window with JavaScript.

In this article, we’ll look at how to open new page in same window with JavaScript.

How to open new page in same window with JavaScript?

To open new page in same window with JavaScript, we call the window.open method.

For instance, we write

window.open("http://example.com", "_self", false);

to call window.open with the URL we want to open and '_self' to open the new page in the same window.

Conclusion

To open new page in same window with JavaScript, we call the window.open method.