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

Create a submit button using Vue.js for text input

Can anyone help with a beginner question? I have a form that includes a text field. When I type something in and press enter, no result shows up. However, when I type something in and click the button, I get the desired result. Could someone guide me on ...

Issue with child prop not being updated despite changes in parent component

I'm facing a strange issue where altering a child component's prop doesn't trigger a re-render. Here's the scenario: In the parent component, I have: <child :problemProp="proplemPropValue"></child> In the child component, ...

Tips for managing unexpected TCP disconnects?

Using Node.js, I set up both a TCP server and an HTTP server. The TCP server was intended to connect with hardware devices via TCP connection. I have 100 TCP clients that are maintaining their connections with the server. Normally, when a TCP client disc ...

Ways to automatically check a checkbox using jQuery

Is there a way to use jQuery to programmatically click a checkbox? I am trying to create a hidden checkbox that will be checked when a user clicks on a specific element, but my current code is not functioning as expected and I am unsure why. You can view ...

Prevent postback in case of validation error

In my current setup, I have a textbox control with the following specifications: <asp:TextBox ID="txtAmount" runat="server" AutoPostBack="true" Width="85px" class="validate[ERExpenseTypeDaterequired,custom[float]]" On ...

Next.js is throwing an error: "Module cannot be found: Unable to resolve 'canvg'"

I am facing an issue in my next.js project where I keep encountering the following error message: error - ./node_modules/jspdf/dist/jspdf.es.min.js:458:25 Module not found: Can't resolve 'canvg' I'm confused because I have not included ...

Having trouble with jQuery functionality when using Laravel 7 mix

Hey there! I'm currently using Laravel Mix to compile all my JS files, but I'm running into an issue with jQuery not working. I've tried adding the defer attribute, but no luck. Can anyone help me troubleshoot this? Thanks! Here's a sn ...

Is storing HTML tags in a database considered beneficial or disadvantageous?

At times, I find myself needing to modify specific data or a portion of it that originates from the database. For instance: If there is a description (stored in the DB) like the following: HTML 4 has undergone adjustments, expansions, and enhancements b ...

Retrieving an Array in CakePHP Through an Ajax Post Request

Despite the countless other 'duplicate' questions on this topic, I still find myself stuck. My goal is simply to log an array passed from PHP via ajax. In CakePHP 2.X: In View: <button class="quarter q1" value="1" value>Quarter 1</but ...

Prevent scrolling in AngularJS model popups

When loading data for the first time in a model popup, the scroll bar is not displayed inside the popup. However, after performing a search function and filtering the data, the scroll bar appears inside the model popup. How can this issue be fixed? this ...

Is there a faster alternative to using jQuery's indexOf method?

Looking to create a search box for a large set of options (around 2000 options) The indexOf method is very slow...any alternative solutions? Jquery Code : $('#textfortitlesearch').on('keyup', function() { var query = this.value ...

Implementing personalized callback methods for AJAX requests in Prototype

After refactoring my code to use proper objects, I am facing an issue with getting Prototype's AJAX.Request to work correctly. The code snippet below is functioning within the context of YUI's DataTable: SearchTable.prototype.setTableColumns = f ...

Show or hide the legend on a highchart dynamically

I am currently utilizing highchart in my application and I am interested in learning how to toggle the visibility of the legend within the highchart. Here is what I have attempted: var options = { chart: { type: 'column&a ...

The process of manipulating the content of an HTML textarea using Selenium with Python

http://neomx.iwedding.co.kr/roundcube I attempted to change the content of a textarea, specifically the tracking number. My goal was to replace "9400111206213835724810" with "487289527385922090". However, I encountered an error message stating "ElementNot ...

React not functioning properly when packaged with Webpack

I tried implementing React in the simplest way possible, but I am facing some issues. After bundling React and opening the index.html page, it shows up completely blank with no console errors to indicate any mistakes. Below is my webpack.config.js: const ...

Building Nested Divs with JavaScript

I've encountered an issue with this code snippet. While it works perfectly fine in jsfiddle, I faced a problem when testing it on my local file. Only the first three lines created by html are displayed: test h1 test h2 test p (I tested the code ...

Encase children within a view component in React Native

I've been working on the following code: renderActionButtons(actionButtons){ let actionButtonsContent = actionButtons.map((button, i) => { return <TouchableHighlight key={i} onPress={() => {this.updateConversation(button);}} style= ...

Is it possible to utilize dynamic imports in conjunction with a for loop in Next.js?

I often use dynamic import to bring in multiple components efficiently. Is it feasible to use a 'for' loop for this purpose? import dynamic from "next/dynamic"; let Dynamic = []; for (let i = 1; i < 80; i++) { const DynamicComponent = d ...

What is the difference between using 'classes' and 'className' in Material UI?

I find myself a bit perplexed about these two properties. Let's say I have, const useStyles = makeStyles(() => ({ style: { width: 600, height: 400, }, })); With this, I can use, const classes = useStyles(); <SomeComponent classNa ...

What is the most efficient way to apply multiple values to the css property outline?

Currently, I am working with IE11 to display a dialog box. The default style for the button in focus includes a dotted border. Is there a way to add an additional value for the outline attribute, like shown below: input[type=button]:focus{ outline: 1p ...