`options for exporting images in Highcharts`

I have implemented the code below to export a png:

    var added_chart_options = {
    credits:{
        enabled: true
    },
    title:{
        style:{
            display: 'block'
        }
    },
    subtitle:{
        style:{
            display: 'block'
        }
    }
};

var options = Highcharts.merge(false, Highcharts['charts'][0]['options'], added_chart_options);

var data = {
    options: JSON.stringify(options),
     resources: {
         css: ".highcharts-background { fill: #fff; stroke: #0ff; stroke-width: 2px}"
    },
    filename: 'test.png',
    type: 'image/png',
    async: true
};

var exportUrl = 'https://export.highcharts.com/';
$.post(exportUrl, data, function(data) {
    var imageUrl = exportUrl + data;
    var urlCreator = window.URL || window.webkitURL;
    fetch(imageUrl).then(response => response.blob()).then(data => {console.log(data, imageUrl)});
})

I am encountering several issues:

  1. How can I define fonts for the exported image? Currently, I added font styling using custom CSS for the entire project.

  2. The legend is displaying differently than expected. It's wrapping text unnecessarily.

  3. The formatter is not functioning as expected. The xAxis dataLabels are not being formatted correctly in the export.

Update 1

My current legend code is:

 legend:{
        useHTML: true,
        borderWidth: 1,
        layout: "horizontal",
        align: "center",
        verticalAlign: "bottom",
        alignColumns: true,
        rtl: true,
        itemWidth: null,
        x: 20
    }, 

In the browser, the legend displays perfectly, but after exporting, it appears distorted. It seems that there is a width limitation causing even short legend lines to collapse.

Answer №1

Interactive example showcasing all 3 solutions: http://jsfiddle.net/UniqueUsername/j6w87dza/

  1. How can I customize fonts for exported images? Currently, I've applied a custom font using CSS to the entire project.

The Highcharts property style.fontFamily is used to specify a font (applied to dataLabels in the demo). The default Highcharts exporting server supports many Google fonts - it's recommended to test and see if your desired font is supported.

dataLabels: {
  enabled: true,
  style: {
    fontFamily: 'Cinzel' // Google font
  }
},
  1. I'm experiencing discrepancies in the legend display. Texts are being collapsed unnecessarily.

I wasn't able to replicate that issue. Feel free to fork and modify the demo above to try and recreate the problem.

  1. The formatter function isn't working as expected. When using a formatter in xAxis dataLabels, it doesn't work during export.

Formatters won't work during exporting if defined normally (as callback function) or as a string in the dataLabels.formatter property. They will only work if you use the update method on the chart within the options.callback string like this:

callback: `function(chart) {
    chart.series[0].update({
      dataLabels: {
        formatter: function() {
          return 'Testing font and formatter';
        }
      }
    });
  }`

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 are some strategies for ensuring accurate data retrieval from the process.env file?

I am facing an issue where I need to retrieve data from a .env.local file, but instead of fetching the data from the file in the current folder, it is pulling the information from another project that I previously created. How can I instruct node.js to ide ...

Utilizing the native cursor feature in Adobe AIR JavaScript using MouseCursorData

I have been exploring the content of this article: which details how to create a native cursor in AIR without resorting to using a sprite to mimic the functionality. However, my project is based on HTML/JavaScript rather than ActionScript. Here is the c ...

Is there a way to extract the primary color of Windows 10 specifically for use in Electron applications?

Recently, I had the opportunity to use an Electron app called Knote which had a unique feature that allowed users to match the main color of the application with Windows 10. However, it seems that this feature has been removed. This led me to wonder if the ...

Automatically bookmark webpages using JavaScript in your browser

I am hoping to trigger the add to bookmark action upon page load using JavaScript or jQuery. ...

Moshi: Efficiently Decode a Single Object or Multiple Objects (Kotlin)

Issue What is the best way to extract a single Warning object or a group of Warning objects (List<Warning>) from an API using Moshi? The response for a single warning: { "warnings": {...} } The response for multiple warnings: { " ...

Issue: The jQuery function with the ID jQuery1830649454693285679_1359620502896 did not receive a JSON

After going through numerous Q&As on the same problem, I couldn't find a solution specific to my issue. The situation involves a PHP script that outputs a JSON string: header('Content-Type: application/json'); echo $result; Here is th ...

Angular 6 component experiencing issues with animation functionality

I've implemented a Notification feature using a Notification component that displays notifications at the top of the screen. The goal is to make these notifications fade in and out smoothly. In my NotificationService, there's an array that holds ...

Customizing the appearance of a JavaScript countdown timer's output on an individual basis

I am currently working on customizing the appearance of a JavaScript date counter. My goal is to style the days, hours, minutes, and seconds individually. Ideally, I want each unit to be right-aligned within its own div, with specific left and top absolute ...

Add a CSS class to the text that is selected within a Content Editable div

Hey there, I'm having an issue where the class is being applied to the button when pressed instead of the selected text. Here's my current code: The button needs to be a div, but it might be causing the problem. I just want the highlighted text ...

Consecutive pair of JavaScript date picker functions

My issue involves setting up a java script calendar date picker. Here are my input fields and related java scripts: <input type="text" class="text date" maxlength="12" name="customerServiceAccountForm:fromDateInput" id="customerServiceAccountForm:from ...

Prevent user input when an alert window is open

Users keep pressing the enter key multiple times when an alert box pops up, causing them to accidentally dismiss the message by hitting 'ok'. Is there a simple way to prevent key presses on alert windows and only allow mouse input? ...

How to use Javascript 'if statement' to check for a numerical value in a form field?

I am currently using some JavaScript within a Mongoose Schema to automatically increment a value if no number is manually entered in the form field. // before validation starts, the number of Items is counted..afterwards, the position is set ItemSche ...

What is the reason that the 400 status code consistently causes the enter catch block to execute when using axios?

In the frontend of my website, there is a contact form with three fields -> name, email, message. These fields are then sent to the backend using Axios. If a user fails to fill out any of the required fields, they should see a message saying "please f ...

Storing HTML input data into an external JSON file

I am facing an issue with my input form that includes fields for name, age, email, and password. I am attempting to transfer this data from the HTML form to JavaScript, where it will be converted into a JSON string and stored in an external JSON file for f ...

Toggle the collapse of the navigation bar

I'm having an issue with my navbar collapse in HTML. Currently, I am using the code provided at http://pastebin.com/s92VvJr6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <div ...

Place a cookie on the alternate language domain

Within my website, we utilize 2 distinct domains, each dedicated to a different language. www.mysite.com en.mysite.com I am interested in implementing JavaScript to establish a cookie on en.mysite.com when clicking a link on www.mysite.com. Here is my ...

What could be the reason for typescript not issuing a warning regarding the return type in this specific function?

For instance, there is an onClick event handler attached to a <div> element. The handler function is supposed to return a value of type React.MouseEventHandler<HTMLDivElement> | undefined. Surprisingly, even if I return a boolean value of fal ...

Execute the laravel {{action(Controller@method}} by sending a parameter from a vue.js array

Within my view created using Blade templates, I have a link with an href attribute that directly calls a Controller method. Here is an example: <a id="@{{user.id}}" href="{{action('Controller@method')}}">>update</a> Everything wo ...

What are the steps involved in converting a MERN application into a desktop application?

As a beginner in front-end application development, I recently created a MERN application in React with separate frontend and backend parts. Everything is working smoothly, but now I want to convert it into a desktop application that can run independently ...

What is the reason behind the shifting behavior of e.currentTarget when using event delegation in jQuery?

Click here for the code snippet <div id="container"> <button id="foo">Foo button</button> <button id="bar">Bar button</button> </div> $('#container').on('click', function(e) { var targ ...