Bar highcharts use the stackLabels formatter function to determine the X-axis value

Is there a way to show a custom label on top of a bar chart?

I'm having trouble accessing the category name of a stack inside the formatter function of stacklabel. How can I achieve this?

stackLabels: {
                enabled: true,
                align: 'right',
                style: {
                  color: 'green',
                  fontWeight: 'bold'
                }


                formatter: function () {
                   return usedInfoArray[category name];

                 },


                align: 'right'
              }
            },

/**********************************************************/


    Highcharts.chart('chartContainer', {
            chart: {
              type: 'bar'
            },
            title: {
              text: ''
            }

            legend: {
              maxHeight: 40,
              itemWidth: 100,
              itemStyle: {
                color: '#FFF',
                fontWeight: 'bold',
                fontSize: 10
              }
            },
            xAxis: {
              categories: categories
            },
            yAxis: {
              min: 0,
              title: {
                text: ''
              },
              stackLabels: {
                enabled: true,
                align: 'right',
                style: {
                  color: 'green',
                  fontWeight: 'bold'
                }


                formatter: function () {
                   return usedInfoArray[xaxis value];

                 },


                align: 'right'
              }
            },

            plotOptions: {
              series: {
                stacking: 'normal',
                pointWidth: 10,
                groupPadding: 0.2


              },
              dataLabels: {
                formatter: function () {
                  var dataLabel = this.x;
                  if (dataLabel.length > 10) {
                    dataLabel = dataLabel.substring(0, 10);
                  }
                  return dataLabel;
                },
                enabled: true
              }
            },
            tooltip: {
              headerFormat: '<b>{point.x}</b><br/>',
              pointFormat: function () {
                return usedInfoArray[point.x];
              }
            },
            series: [{
              name: 'Free',
              'color': '#FFCC66',
              data: freeData
            }, {
              name: 'Used',
              'color': '#663399',
              data: usedData
            }]
          });

Answer №1

If you want to access the x axis category name in a chart, you can do so indirectly through the following method:

yAxis: {
  stackLabels: {
    enabled: true,
    align: 'right',
    formatter: function() {
      var xValue = this.x,
        xAxis = this.axis.chart.xAxis[0];

      return xAxis.categories[xValue];
    }
  }
}

Check out this live demo for further clarification: https://jsfiddle.net/BlackLabel/g68xphmf/

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

What is the best way to compare two JSON structures in depth to determine whether they contain arrays or objects, without using JSON.stringify

I have been working on tracking changes in a mongoDB record that I send through a GET request and then update with a PUT request. When the PUT request is received, I retrieve the existing document before updating it, and then compare it with the updated r ...

Determining data using the AngularJS Slider component

I am currently utilizing the angularJS slider from this source: https://github.com/venturocket/angular-slider The sliders are functioning correctly, but now I need to extract the values from them for basic calculations. The HTML code snippet is as follo ...

Employing jQuery, how can one assign attributes to appended HTML and store them

So, I am currently working on a backend page for managing a blog. This page allows users to create, edit, and delete articles. When the user clicks the "edit" button for a specific article named 'foo', the following actions are performed: The ...

Determine which function triggers the execution of a callback

I am conducting a test to determine in which function this callback function is executed. The expected result should be a boolean value. I trust that you understand my objective. Below is an example of the code: function test(par, callback) { // ... ...

Could the script tag be causing the text to vanish from the HTML file?

Recently, I was experimenting with HTML to make it select a new random image file every time the page reloads. Surprisingly, I managed to achieve that, but now I'm facing an issue where none of the text appears as intended and only the image is visibl ...

The term "Cardlist" has not been defined and is therefore causing an

I created a CardList and attempted to add cards into the list using map, but encountered an error import React from 'react'; import Card from './Card'; const CardsContainer = ({robots}) => { const cardComponents = robots.map((r ...

encountering a problem with npm while trying to execute the project

Encountering a persistent issue when attempting to run my project with npm start, even after downgrading my node version. Despite trying various solutions found on platforms like stackoverflow and stackexchange, such as deleting the node_modules folder, ru ...

Forming a fresh array incorporating every single element from the subarray

I am looking to create a new array that consolidates all the parameters from the subarrays into a single resulting array. Explaining this with an example below: var myArr=[ { "Id":1, "Name":'Ken ...

Determine whether child components in React Native contain additional child elements

I'm facing an issue while trying to determine if the children (AssetExample2) of my parent (AssetExample) component have their own children. The goal is to use this logic for the splash screen of my app to hide it once the children have loaded. Howeve ...

Organize object keys based on the size of the arrays they contain in JavaScript

I'm working on a project where I need to organize a list of products by manufacturer, displaying them from the one with the most products to the least. I'm looking for an algorithm that will help me sort the object keys accordingly. Below is a s ...

Ways to troubleshoot the logic issue in the loop for my simple login interface

I have been able to successfully implement code that detects when a user enters an incorrect username or password, as well as granting access when the correct credentials are provided initially. However, there seems to be a logic error in my loop that prev ...

Transform the text column into JSON format

We currently have a resource bundle/properties formatted as follows: [tag1] server1 server2 [tag2] server3 server4 [tag3] server5 server6 [No Software Installed] server7 [tag2] server8 [tag5] server9 [tag1] server10 server11 [tag3] server12 server13 serve ...

Looking to dynamically modify the preselected rows in an Angular Slickgrid grid option when a button is clicked by calling a function (check)

When the button is clicked, certain specific rows are selected using a preselected grid option in Angular Slickgrid. However, if I do this, the preselected row in the grid options changes but it does not display properly. Please assist me. Typescript File ...

Difficulty maintaining list formatting in AngularJS and Bootstrap due to ng-repeat functionality

I'm currently working on a project where I need to display content from an array using ng-repeat in Angular. The content is originally in JSON format, but it has been stringified before being added to the array. The problem I am facing is that this c ...

Leveraging JavaScript Functions in HTML

I am having an issue with a JavaScript file containing two similar functions that are executed through an HTML form. While the first function runs smoothly, the second function does not display correctly. It seems like I might be calling or executing the ...

Converting a Javascript array to an NSArray in Xcode

As a complete beginner to Xcode and programming in general, I recently built an app using javascript and html and integrated it into Xcode. Currently, my main focus is on extracting multiple arrays from the html/javascript file and exporting them as a csv ...

Finding the initial occurrence of duplicates within an array of objects using JavaScript

I am working with a data structure retrieved from an API, and it looks like this: [ { id: '10000844', text_id: '10000844-01', }, { id: '10000844', text_id: '10000844-02', }, { id: ' ...

performing dual functions within a single click event handler

Is there a way to simultaneously execute 2 functions? In the following code snippet, I am trying to capture the id of the parent Div with the onclick event and then trigger a function using that id. <div id="selDe"> <input type="radio" class="ger ...

Experiencing a problem when attempting to activate the html5 mode feature in AngularJS to eliminate the #

I've been scouring through various sources like stackoverflow and the web, but I haven't found a clear answer to my question. Can someone please help me out? Here's what I'm struggling with: In my AngularJS application, I enabled &apos ...

Just getting started with jQuery and looking for assistance with adding elements during events

My current project involves creating a button that, when clicked, updates a 'feed' div. The goal is to generate three elements: a tweet(<div>), a message(<p>), and a username(<p>). These elements should be added to the tweet div ...