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

Tips for handling daily JavaScript tasks using Angular JS

Over the past few days, I've been diving into AngularJS. While it seemed intuitive in tutorials and videos, when I actually started replacing my current web app code with AngularJS, I encountered numerous issues. For instance, if I wanted to add HTML ...

Dynamic dropdown menu based on the location using JSON and jQuery

I need to create a series of dropdown menus for selecting country, state, and city. The country and state dropdowns are working fine, but I am having trouble populating the city dropdown. What could be missing in my code? Here is what I have so far: <f ...

Connecting different jQuery methods to create a chain

From my understanding of jQuery chaining, the first method in the list must complete before the next method executes. Here is an example: $.fn.reportZebraStriper = function(options) { console.log('reportZebraStriper()'); return true; } ...

What is the best way to show information entered into a textbox on a different webpage?

Looking for advice on how to collect data in a text box and display it on another page? I have included HTML code for both the announcement page (where the data is entered) and the archive page (where the data should be shown). Could someone guide me on ...

Displaying client-side filtered rows in a jqGrid with postData filter upon initial loading

Our website includes a jqGrid that initially retrieves its rows using the built-in ajax fetch feature, returning a json object. We then apply filtering and searching on the client side by creating custom functions to generate postData filters. However, we ...

What is the best way to display a PDF in a web browser using a JavaScript byte array?

I have a controller that sends the response Entity as a byte array in PDF form to an ajax call. However, I am struggling to display it in the browser despite trying various suggestions from old Stack Overflow questions. Here is the response from the Sprin ...

Which is better for parent-child directive communication: using $emit or callbacks?

Im currently exploring the optimal UNIVERSAL method for communication between parent and child directives with isolated scopes (which could potentially be reused components). For example, if a child directive needs to update the parent directive in some w ...

Looking to obtain the coordinates of a draggable element?

After dragging a div around the page and submitting a form, I would like to capture its location on the page so it can render in that same spot when the page reloads. My current question is how can I capture the coordinates or location of the div after it ...

How does the method of including JavaScript libraries in HTML differ from adding them as npm dependencies?

Upon browsing through npm highly regarded packages, I noticed that popular projects such as Grunt, lodash, and underscore are readily available. I have always utilized these in the traditional manner: <script src="js/lib/lodash.min.js"></script& ...

Onload, capture page elements, delete them, and then access the original content

I am encountering two DIV elements on my page that I need to capture upon loading the page and preserve their contents until the page is refreshed. The nested DIV element is contained within the other one. After locating these elements initially, I want t ...

Having trouble getting my ReactJS page to load properly

I am currently linked to my server using the command npm install -g http-server in my terminal, and everything seems to be working smoothly. I just want to confirm if my h1 tag is functional so that I can proceed with creating a practice website. I have a ...

AngularJS functionality is only activated when using the ng-app directive with an empty value

I am facing an issue with my HTML page - it works fine when ng-app="" but not when ng-app="MyApp". How can I modify this page to work with ng-app="MyApp"? This code on the page allows for text input that is then displayed as the user types into the textbo ...

Ways to switch the positions of two characters in a text box

Is there a way to access the text content of a textarea and swap the two characters around the cursor using Javascript? I am interested in creating a Chrome extension that will allow me to quickly correct typos in Gmail. (I am assuming that the main editin ...

Is there a way to streamline this function call that appears to be redundantly repeating the same actions?

I have developed a function to search for blog posts, prioritizing titles over excerpts and excerpts over content when added to the containsQuery array. While the code seems to be working well, I have noticed that there is a lot of redundant code. How can ...

Firestore TimeStamp.fromDate is not based on UTC timing

Does anyone have a solution for persisting UTC Timestamps in Firestore? In my Angular application, when I convert today's date to a Timestamp using the code below, it stores as UTC+2 (due to summer time in Switzerland). import {firebase} from ' ...

Error encountered in Angular CLI: Attempting to access property 'value' of an undefined variable

I am encountering an issue while trying to retrieve the values of radio buttons and store them in a MySql database. The error message I receive is TypeError: Cannot read property 'value' of undefined. This project involves the use of Angular and ...

Troubleshooting Bootstrap select box design discrepancies

Currently revamping a website and encountered an unusual issue with select boxes. There seems to be an overlapping white space where the option values should be. Here's a visual reference: View Image of Select Box Issue Utilizing Bootstrap for the re ...

Using gulp to duplicate files from a specific directory nestled within a larger folder structure

I'm trying to figure out how to address this issue: My goal is to transfer all fonts from bower_components to .tmp/assets/fonts. However, the complication arises with some fonts being .svg files. If I were to use the following code in a typical manne ...

Struggling with incorporating a search feature? Encounter the error message "TypeError: data.filter is not a function"?

UPDATE: here is what console.log(data) shows. The data appears correctly, but the filtering process seems to be malfunctioning.] !https://imgur.com/a/SsEDAKj! UPDATE 2: this.state.items represents an array. I am currently working on integrating a search ...

Exploring and listing the key-value pairs from several arrays within an object

My inquiry pertains to the following function: function loadConfigurations(configs){ console.log(configs); } The 'configs' object received by the loadConfigurations function contains two properties --two arrays named "assigned" and "unassign ...