Adjust the size of a Highcharts chart before printing

I'm facing an issue with a chart that doesn't have a specified height or width.

My goal is to print the graph in a taller and larger size when I click on the print button. I attempted to use setSize() function, but since there are no original dimensions for the graph, I can't resize it properly and end up with an oversized graph. I also tried to duplicate the chart by changing the renderTo, width, and height attributes inside a different div, but unfortunately, I couldn't get it to work. I've spent several hours on this problem and now I'm feeling completely confused.

What should be my next steps to stretch the graph for printing without altering the original version?

Thank you for your assistance.

Answer №1

Pawel's Fiddle is experiencing compatibility issues with IE, as the second setSize function is being executed before printing. To resolve this, a timeout needs to be set for the size adjustment to occur after the printing.

For a working solution, please refer to: http://jsfiddle.net/6hyfk/34/

$('#container').highcharts({
    series: [{
        data: [1, 2, 3]        
    }]
});

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

    chart.setSize(200,500, false);
    chart.print();
    setTimeout(function() {
        chart.setSize(600,500, false);
    }, 1000);

});

Answer №2

Personally, I have had success with this solution. Take a look at the following example: http://jsfiddle.net/Fusher/6hyfk/6/

If you are encountering difficulties, it could be due to setting the size in the wrong place. It would be helpful if you could provide some code (even better if done through jsfiddle).

$('#container').highcharts({
    series: [{
        data: [1, 2, 3]        
    }]
});

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

    chart.setSize(200,500, false);
    chart.print();
    chart.setSize(600,500, false)

});

Answer №3

Aside from the solutions provided by Pawel and Arthur, you can refer to this thread for restoring auto-resizing functionality.

I encountered a similar issue when dealing with printing an entire page that happened to include a highchart, rather than just printing a chart itself.

During my research, I discovered several helpful tips that may benefit future developers:

Retrieving current Chart size: To reset the chart after printing, you can capture and store the current size of the chart using chart.chartHeight or chart.chartWidth (where

var chart = $('#chart-container').highcharts();
)

Restoring auto-resizing after setting size: Once you have called setSize() after printing, the chart will no longer resize automatically if the window changes. You can address this by following the 'hack' outlined in this explanation: Is modifying chart.hasUserSize a bad idea after calling Chart.setSize()? This involves setting chart.hasUserSize = false;

Utilizing before and after print events: Details on handling these events can be found in this discussion: Do modern browsers support onbeforeprint / onafterprint HTML events?, where the answer is (in essence) yes, but necessitates using a combination of onbeforeprint events and the watchMedia eventHandler API.

Hopefully, this information proves beneficial to someone!

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

Flipped the dimensions of an image for better responsiveness

Trying to implement this particular design on a responsive website. Wondering if there's a way to establish an inverse resizing technique using jQuery / Javascript so that as the viewport shrinks, the text size increases. Attempted to use jQuery to a ...

How can I implement a feature to automatically close a drawer in a React application using the Material UI

Currently, I am utilizing a React material design theme However, I am facing an issue where the drawer navigation button on mobile view does not close automatically upon clicking I need something like (onClick={handleClose}) to resolve this problem Does ...

Locate the position of a substring within a Uint8Array

I'm working with a Uint8Array that contains the content of a PDF file. My goal is to locate a specific string within this array in order to insert additional content at that particular position. My current approach involves converting the Uint8Array ...

Best practice for pausing a setInterval during a beforeSend function in an AJAX call

Here is some code I am working with: var centerTimer = 0; function startCenterRefresh(refreshTime, position, params) { centerTimer = setInterval(function() { jQuery.ajax({ type: "POST", url: host + "/forms/refresh-cente ...

In NextJS, where is the best place to run sensitive code to ensure it is only executed server-side?

I'm currently exploring the world of NextJS and I am in the process of figuring out how to structure my project with a solid architecture that prioritizes security. However, I find myself at a crossroads when it comes to determining the best place to ...

What's the best method for uploading a file to AWS S3: POST or PUT requests?

Could you please provide insights on the advantages and disadvantages of utilizing POST versus PUT requests for uploading a file to Amazon Web Services S3? Although I have come across some relevant discussions on platforms like StackOverflow, such as this ...

Implement image effects in a single div by leveraging the jquery functions .fadeIn/Out and .delay(300)

Is it possible to use jQuery to achieve the following task? I currently have a <div> with an id="divid", and inside is an <image source="images/myimage01.jpg">. I am looking for a script that can sequentially load/loop images from <img src ...

Finding the location of an "Apply" button on an Angular HTML page

There is an issue with the positioning of the apply button in the filter bar. Sometimes it displays in the next row instead of alongside the other filters. How can I determine the exact position of this apply button within the HTML page? <div class=&quo ...

Unable to execute commitlint in husky along with a different custom command

Is it possible to set up two precommit hooks with husky? Specifically, I want to integrate commitlint along with a custom script specified in my package.json. After installing husky and creating a pre-commit script in the .husky folder, here is what I have ...

Linking to a Different Tab without Tab Mutation with Bootstrap 3.3.5

I am facing a similar issue to the mentioned questions. I am trying to use a link to change tabs, but the problem is that the link only changes the tab content and not the active tab. The most similar question can be found at: Bootstrap linking to a tab w ...

Switching from React version 15.6.2 to 16 results in disruptions to the functionality of the web

Currently, I am encountering an issue where none of my index.js files are rendering. After using the react-scripts to build my web application in version 16.2.0, I receive an error stating that r.PropTypes is undefined when trying to access the localhost a ...

jQuery alert: A TypeError was caught stating that the object being referred to does not have the 'dialog' method available

For a few days now, I have been tirelessly searching for a solution to this issue and it has caused me quite a bit of stress. My problem lies in echoing a JQuery popup script within php: echo '<link rel="stylesheet" href="http://code.jquery.c ...

Double Tap Required to Update AngularJS Scope

Controller: // Modal Controller app.controller("ModalCtrl", ["$scope", "$filter", function($scope, $filter) { var date = new Date(); var currentDate = $filter('date')(new Date(), 'MM/dd/yyyy'); // Form Submit Actions $scope.s ...

Steps for linking a keypress to trigger a specific action

I need help creating a script that will redirect to a URL when a button is clicked. Below is the code I have developed. HTML : <a id="#next" href="example.com">↵</a> <a id="#previous" href="example.com">↳</a> JS : $(document ...

Experiencing complications with an Angular 2 router

When a user logs into the system, they are greeted with a navigation bar featuring options like Dashboard, Customers, and Product. Below is an excerpt from my routes file: app.routing.ts export const router: Routes = [ { path: '', redir ...

Can you please let me know if it's possible to store an ajax function/call for future reuse?

While developing a web app using JavaScript and PHP, I've noticed that I keep rewriting the same ajax calls repeatedly. Is there a way to create a reusable function or variable for these calls, with or without parameters? I'm fairly new to JavaS ...

How can I create a table that is 100% width, with one column that has a flexible size and truncates text

I am currently facing an issue with a table nested within a div that is absolutely positioned on my webpage. I want to set specific widths for the second, third, etc columns while allowing the first column to automatically resize, even if it results in tru ...

Unable to display items in controller with AngularJS

I am facing an issue with displaying a slider using ng-repeat in my AngularJS code. The pictures and other elements defined in the controller are not showing up on the page. Here is the JavaScript code snippet: angular.module('starter', []) .co ...

Encountering an empty array in a PHP script when transmitting form data via AJAX using the POST method

I have been using the serialize() method to create a URL encoded string, but despite trying various solutions, nothing seems to work. Below is the code I am working with: <form id="idEditTaskForm"> <div class="form-group"&g ...

Inquiry regarding modules in Javascript/Typescript: export/import and declarations of functions/objects

I'm fresh to the realm of TypeScript and modules. I have here a file/module that has got me puzzled, and I could really use some guidance on deciphering it: export default function MyAdapter (client: Pool): AdapterType { return { async foo ( ...