ArticleZip > Hiding A Highcharts Series Without Using The Legend

Hiding A Highcharts Series Without Using The Legend

If you're using Highcharts in your web development projects, you may have encountered situations where you need to hide a series without relying on the default legend functionality. This can be especially useful when you want to control the visibility of data series programmatically or through user interactions. In this article, we'll explore how you can hide a Highcharts series without utilizing the legend feature.

To hide a series in Highcharts, you can directly manipulate the chart object using JavaScript. Highcharts provides a rich API that allows you to access and modify various elements of the chart dynamically. One approach to hiding a series is by setting the series' `visible` property to false. This will effectively hide the series from the chart while retaining the data for potential future display.

Here's a step-by-step guide on how to hide a Highcharts series without using the legend:

1. First, you need to identify the series you want to hide. Each series in a Highcharts chart is represented by an object with specific properties.

2. Access the Highcharts chart object where the series is rendered. You can do this by assigning the chart object to a variable when initializing the chart or by using `Highcharts.charts[index]` to get the instance of the chart.

3. Once you have access to the chart object, you can iterate through the series and set the `visible` property to false for the series you want to hide. Here's an example code snippet to achieve this:

Javascript

const chart = Highcharts.charts[0]; // Get the chart object
const seriesToHide = 'series-id'; // Identify the series to hide by its ID

chart.series.forEach(series => {
    if (series.options.id === seriesToHide) {
        series.setVisible(false);
    }
});

In the above code snippet, replace `'series-id'` with the actual ID of the series you want to hide. The `setVisible(false)` method call is what hides the series on the chart.

4. After executing the code to hide the series, the chart will update automatically, reflecting the changes. The specified series should now be hidden from view while still being present in the chart's data.

By following these steps, you can effectively hide a Highcharts series without relying on the default legend functionality. This approach gives you more control over the visibility of data series in your charts and allows for dynamic manipulation based on your application's requirements.

In conclusion, understanding how to manipulate Highcharts series visibility through direct API calls opens up possibilities for creating more interactive and customized charting experiences. Experiment with different options provided by the Highcharts API to tailor your charts to specific user interactions and data visualization needs.