Display data in Highchart legend only if it has values

In a unique scenario, I am required to compare various items and display the results in a chart. The user inputs two, three, or four article IDs and receives the corresponding data displayed in a chart. The issue arises when the user enters only two or three article IDs; in the chart legend, there are still names of lines without any data associated with them.

$scope.chartConfig1 = getChartConfigWithYaxisPlotLines('Production');
            $scope.chartConfig1.series.push({
                id: 1,
                name: "Item " + firstId,
                data: $scope.data[0],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 2,
                name: "Item  " + firstId,
                color: "#1ea775",
                data: $scope.data[1],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 3,
                name: "Item  " + firstId,
                data: $scope.data[2],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 4,
                name: "Item " + firstId,
                data: $scope.data[3],
                tooltip: {
                    valueDecimals: 2
                }
            },{
                id: 5,
                name: "Item " + secondId,
                data: $scope.data[4],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 6,
                name: "Item " + secondId,
                data: $scope.data[5],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 7,
                name: "Item " + secondId,
                data: $scope.data[6],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 8,
                name: "Item " + secondId,
                data: $scope.data[7],
                tooltip: {
                    valueDecimals: 2
                }
            },{
                id: 9,
                name: "Item " + thirdId,
                data: $scope.data[8],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 10,
                name: "Item " + thirdId,
                data: $scope.data[9],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 11,
                name: "Item " + thirdId,
                data: $scope.data[10],
                tooltip: {
                    valueDecimals: 2
                }
            }, {
                id: 12,
                name: "Item " + thirdId,
                data: $scope.data[11],
                tooltip: {
                    valueDecimals: 2
                }
            }
            );

The legend displays Item followed by the article ID inputted during submission, so when comparing only two articles, the legend shows "Item 1," "Item 2," "Item undefined," "Item undefined." Everything works fine when all four inputs are provided. https://i.stack.imgur.com/oFugp.jpg

Is it possible to hide the name in the legend on Highcharts if the data is null or undefined? Thank you for any insight.

Answer №1

If you want to analyze series in a loop and hide those without any data, you can utilize the load event:

var chart = Highcharts.chart('container', {

  chart: {
    events: {
      load: function() {
        this.series.forEach(function(s) {
          s.update({
            showInLegend: s.points.length
          });
        });
      }
    }
  },

  series: [{
    data: [1, 2]
  }, {
    data: [3, 4, 2]
  }, {
    data: [] // no data
  }]
});

Check out the live demo here: http://jsfiddle.net/kkulig/355u0kaw/


For more information, refer to the following API documentation:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing search results with data retrieved from a jSON response

At the moment, I am using a logic to generate a list of titles. Now, I would like to include data from the response along with the code for each title in the list. const title = responseData.map(item => { return { label: item.title, val ...

What sets response.setHeader apart from response.writeHead?

When it comes to sending a JSON response from my Nodejs server in my application, I have discovered two different methods. However, I am unsure about the distinctions between them. The first method involves var json = JSON.stringify(result.rows); respons ...

I am currently working on creating a system for displaying rating stars for products. The rating value, which ranges from 0 to 5, has already been provided

class PostBodyRatingStars extends React.Component { render() { const { classes } = this.props var rating = this.props.rating function createMappingArray(rating) { var arr = [] for (i = 0; i < 5; i++) { if (rating >= ...

Having trouble accessing the downloaded file from S3 using Angular and NodeJS

Currently, I am facing an issue while attempting to download a jpg image from amazon S3 using the NodeJs SDK. Although I can successfully retrieve the file object on the backend, encountering difficulties arises when trying to create a blob object and down ...

Maintain form activity post submission using jQuery's addClass and removeClass methods

There is a button on my website that, when clicked, reveals a form. The code I am using for this functionality is: $('#theform').addClass('hidden') $('#theform').removeClass('hidden'); <div id="theform" c ...

Is there a way to verify if the JSON Object array includes the specified value in an array?

I am working with JSON data that contains categories and an array of main categories. categories = [ {catValue:1, catName: 'Arts, crafts, and collectibles'}, {catValue:2, catName: 'Baby'}, {catValue:3, catName: 'Beauty ...

What is the best way to activate a function from a modal component without having the function embedded in Angular 14?

I've recently been putting together an e-commerce application using Angular 14. Currently, I'm tackling a form that must only be submitted once the user accepts the "Terms & Conditions" shown in a modal popup. The FormComponent and ModalCompone ...

I am interested in creating a ranking system in JavaScript using JSON data based on points

I have a desire to create the following: var users = {jhon: {name: 'jhon', points: 30}, markus:{name: 'Markus', points: 20}}; // I want it to return like this 1. Jhon with number of points: 30 // 2. Markus with number of points: 20 ...

Verify MVC Controller Authorization and respond with 401 Unauthorized to the AngularJS application

Currently, I have an AngularJS application that interacts with an MVC Controller to retrieve specific data. To ensure that users have the necessary permissions to execute certain actions on the controller, I've created a custom class called RBACAutho ...

The modal remains visible

Has anyone encountered a problem with hiding a Bootstrap modal based on form validation in JavaScript? I'm facing an issue where the submit function is not executed when I click the button. I've attempted using onsubmit=validateForm(), but it doe ...

JavaScript - Deleting the last element of an array

I have currently integrated a third-party API to visualize data using Highcharts. This external API provides data specific to the current month, such as March, April, May, and so on. graphArrOne contains an array with 251 elements. graphArrTwo contains ...

Ways to hear a variable using Google Translate

We're utilizing HTML5 and Javascript. Imagine we have a list containing the names of all players from Barcelona (for example, name = 'Lionel Messi'). I want to make the player's name audible. To achieve this, I would use: var narrator ...

What could be causing my handle button to slide off the timeline towards the right?

I'm facing an issue with my volume bar component where the slider button is rendering outside of the timeline instead of on top of the progress bar. I need assistance in adjusting its position. // Here is the code for my volume bar component: import ...

Creating a reverse proxy using next.js

My goal is to set up a reverse proxy for the GeForce NOW website using Next.js, but I'm struggling to make it work. module.exports = { async rewrites() { return [ { source: '/:slug', destination: 'https://pla ...

Tips for managing multiple projects in Angular 7 simultaneously

Our team is currently working on an application with two separate workspaces. One workspace serves as the main project while the other is exported as a module to our private npm repository. To access this module, we retrieve it through our package.json f ...

In what way can I fill out my search fields using the criteria included in the URL?

In the process of developing an asp.net web application, I have implemented a code snippet to construct a section for search fields. This code builds URL parameters based on user input: <script type="text/javascript> $(document).ready(function(){ ...

Incorporating a for loop, ExpressJS and Mongoose repeatedly utilize the create method to generate

When users input tags separated by commas on my website, ExpressJS is supposed to search for those tags and create objects if they don't already exist. Currently, I am using a for loop to iterate through an array of tags, but only one object is being ...

The functionality of two-way binding in a distinct controller is not functioning properly when integrated with angular-wizard

I've been working hard to integrate this amazing wizard controller into my project: However, I've hit a roadblock with two-way binding not functioning as expected outside of the <section> attribute: http://plnkr.co/edit/N2lFrBRmRqPkHhUBfn ...

JavaScript: AWS Amplify: Retrieving the User ID (Sub ID) Following User Registration. Post Registration, Not to Be Confused with Sign In

Currently, I am utilizing the authentication module from AWS Amplify. One question that has been on my mind is how to obtain the userID once a user signs up. While it is possible to retrieve the ID after a user signs in, I am specifically interested in re ...

Dynamic Formatting with Vue JS: Enhancing Phone Number Input

I am working on a form that includes a phone number input field, and I want the formatting of the number to change to a standard telephone number format as the user types. Can someone provide guidance on how this can be achieved using JavaScript and Vue 3? ...