Fetching Highcharts information through ajax results in the removal of Datalabels

I have a HighCharts bar chart that is updated with data from an ajax call as shown below.

var updateChart = function() {
            $.ajax({
                url: "/theurl/theid",
                type: "Get",
                success: function(data) {
                    chart.xAxis[0].setCategories(data.SomeText);
                    chart.series[0].setData(data.SomeData, true);
                    chart.series[1].setData(data.OtherData, true);
                }
            });
        };

The construction of the chart is outlined in the code block below

var chart;
        $(document).ready(function() {
            chart = new Highcharts.Chart(
                {
                    chart: {
                        type: 'bar',
                        renderTo: 'container',
                        height: 750
                    }, ... (additional options removed for brevity),                
                    plotOptions: {
                        series: {
                            dataLabels: {
                                enabled: true,
                                format: '{y} %',
                                style: {
                                    fontWeight: 'bold',
                                    color: '#3b3b3b',
                                    fontSize: '125%'
                                },
                                useHTML: true
                            }
                        },
                        bar: {
                            dataLabels: {
                                enabled: true
                            },
                        }
                    },
                  ... (additional options removed for brevity)
            );
        });

The intention is to dynamically update the data displayed on the chart every 10 seconds without refreshing the entire page.

The data appears and the chart is rendered only when the updateChart() function is executed in the console. Upon first execution of updateChart(), everything displays correctly. However, upon subsequent executions (without reloading the page), all components are visible except for the dataLabels which should show percentages (100%, 55%, etc.)

If I remove the useHTML = true option from the plotOptions.Series.DataLabels, the percentages are once again visible but it leads to other styling problems that I had resolved using this same option.

Is there a solution to maintain useHTML set to true while preventing the disappearance of dataLabels during updates?

Answer №1

I tested out your code and everything is working smoothly. Check it out here: http://jsfiddle.net/2Gbja/2/

$("#b").click(function() {
    var chart = $('#container').highcharts();

    chart.xAxis[0].setCategories([Math.random(), Math.random()], false);
    chart.series[0].setData([Math.random(), Math.random()], false);
    chart.series[1].setData([Math.random(), Math.random()], true); //redraw chart once
});

$('#container').highcharts({

    chart: {
        type: 'column'
    },

    series: [{
        data: [10, 20]
    },{
        data: [10, 20]
    }]
});

Please note: the chart should only be redrawn once, so make sure to set false for the redraw and only set the last one to true.

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 Creating a Smooth Slide-Down Effect in Your Navbar Menu

Desired Outcome - My aim was to create a smooth slide down effect for the navbar after scrolling. While I managed to achieve this, I am encountering an issue. The Issue - Whenever I scroll back up to the top, the navbar becomes sticky without any transiti ...

Tips on how to dynamically uncheck and check the Nebular checkbox post-rendering

I incorporated a nebular theme checkbox into my Angular 8 App. <nb-checkbox [checked]="enable_checked" (checkedChange)="enable($event)">Enable</nb-checkbox> I am able to update the checkbox using the Boolean variable "enable_checked". Initia ...

What is the process to convert an IEnumerable of Anonymous Type in C# into a JavaScript Object?

Within my ASP.net C# code, I have an IEnumerable container that contains objects of an anonymous type (loosely based on SQL data). If we take a look at a snippet of my code: var uics = entities.getData() .Select(x => new { id = ...

`json_encode does not output a UTF-8 character`

I send an AJAX request to the PHP server, and receive back an array encoded in JSON. This array has only two indexes. When I log it using console.log(), this is what I see: {"1":"\u00d9\u0081\u00db\u008c\u00d9\u0084\u0 ...

Error: The property '_eventListeners' cannot be set on an undefined object

Encountering an error while running tests with React, Jest, and Enzyme - in need of assistance! ● Test suite failed to run TypeError: Cannot set property '_eventListeners' of undefined at Window.close (E:/TEST-UI/node_mo ...

Struggling with jquery ajax and datatables

I am facing an issue with utilizing jQuery ajax to fetch data from a PHP file. The PHP file generates a table based on a database query. My intention was to apply datatables styling to the returned table in the HTML page, but unfortunately, this approach i ...

Scrolling automatically to the first empty mandatory field with the help of AngularJS

After utilizing angular js to create a form with 7 input elements, I encountered an issue. Upon clicking submit, the form should scroll up to the first blank required field. However, it is not accurately identifying the left blank field. Any suggestions on ...

Manipulate objects within an array by updating state with a button in React

So, I have an array of names that I want to cycle through: const data = { names: [ { name: "Jordan" // additional data }, { name: "Holly" // additional data }, { name: "Sean" // additional data ...

Error when sending Angular 4 GET request with multiple Headers results in a 400 bad request code

I've been attempting to perform a POST request with headers in order to receive a response. The Angular code snippet I'm currently using for this request is shown below: const headers = new HttpHeaders({ 'Content-Type': 't ...

The dynamic drop-down feature in Grails does not seem to be functioning properly when utilizing remote

I am facing an issue with creating a dynamic drop down menu in Grails. The second drop down is not populating after selecting an option from the first drop down. Here is my code located in my _form GSP: <g:select name="department" from="${MCM.MGDepart ...

The art of rotating PDF files and various image formats

Looking for a way to rotate PDF and image files displayed on an HTML page using jQuery. I attempted: adding a CSS class - without success. Here is an example code snippet: .rotate90 { webkit-transform: rotate(90deg); moz-transform: rotate(90de ...

Error encountered in NEXT JS: Unable to parse URL from /api/projects or Error message: Failed to connect to 127.0.0.1:3000

Currently utilizing: export const getStaticProps = async () => { export const getStaticPaths = async () => { and accessing my API (pages/api/projects/) created with Next.js on my local host const res = await fetch("http://localhost:3000/api/ ...

Adding elements to a roster

Can anyone assist me with appending data from a csv file to an unordered HTML list? The csv file I have contains both single and grouped data for the list, as seen below: Test Group Name A,Test Link Name 1,Test URL 1 Test Group Name A,Test Link Name 2,Tes ...

What steps should I follow to incorporate this filter into my code?

Currently, I am working with a list of names obtained from an array using the Fetch method. I have implemented a searchHandler method which is triggered by a button click, and it logs the input data in the console: https://codesandbox.io/s/jovial-lovelac ...

What could be causing my insertRow function to inject empty cells into my table?

I am facing an issue with adding the results of a fetch request into a table as a new row. Strangely, when I click the button to trigger this function for the first time, it adds an empty row instead of the desired data. However, on subsequent clicks, the ...

Button's focus event doesn't trigger on iPad

I am facing an issue with adding a bootstrap popover to my website. The popover should appear when the user clicks a button using the focus event. This functionality works fine on desktop browsers, but on the iPad, it seems like Safari on iOS does not trig ...

Tips for sending JSON data from JavaScript to PHP servers

After encoding an array into JSON using JavaScript, I set up my ajaxrequest object like this: var data = JSON.stringify(cVal); function ajaxRequest(url, method, data, asynch, responseHandler){ var request = new XMLHttpRequest(); request.open(meth ...

Utilizing a separator to individually display each item within a string

How can I print out each element of a string separately without including the last element? var animals = "bear, lion, tiger, jaguar"; $.each(animals.split(",").slice(0,-1), function(index, item) { console.log(item); }); p.s. I want to a ...

Tips for positioning a sticky div underneath a stationary header

I'm currently utilizing Bootstrap 4 for my project, and I am encountering an issue with maintaining a div that has the class "sticky-top" just below a fixed navbar. Despite attempting to use JavaScript to replace the CSS while scrolling, it hasn' ...

The error message thrown is: "Unable to assign headers after they have already been sent to the client."

I've been attempting to make a GET request, but it keeps failing at the app.js res.json line. app.js app.use(function(err, req, res, next) { res.locals.message = err.message; res.locals.error = req.app.get("env") === "development" ? err : {}; ...