How to print a graphic with onload="window.print ()?

0

I have the following code

    <?php

    if(isset($_GET['mod']))
    {
        require_once 'PSController.php';

        $fecha_actual   = date("d-m-Y");
        $fecha          = date("d-m-Y",strtotime($fecha_actual."- 1 days")); 

        $ps = new PSController();

        $PDP_ContextImprimir            = $ps->PDP_ContextImprimir();
    }
?>

<h2 class="text-center">Reportes <?php echo $fecha; ?></h2>

    <!-- GRAFICOS !-->
    <div class="col-md-5">
        <div id="grafico1">

            <script src="https://code.highcharts.com/highcharts.js"></script>
            <script src="https://code.highcharts.com/modules/series-label.js"></script>
            <script src="https://code.highcharts.com/modules/exporting.js"></script>
            <script src="https://code.highcharts.com/modules/export-data.js"></script>

            <script type="text/javascript">

                chartCPU = new Highcharts.chart('grafico1', 
                {
                    chart: {
                        type: 'line'
                    },
                    title: {
                        text: 'PDP Context 3G'
                    },
                    xAxis: {
                        gridLineWidth: 1,
                        categories: 
                        [
                            <?php
                                foreach($PDP_ContextImprimir['LABEL'] as $hora)
                                {
                                    echo "'$hora',";
                                }
                            ?>
                        ]
                    },
                    yAxis: 
                    [
                        {
                            min: 0,
                            title: 
                            {
                                text: '',
                            }

                        }, 

                        {
                            min: 0,
                            max: 100,
                            title: 
                            {
                                text: '',
                            },
                            opposite: true
                        }
                    ],
                    plotOptions: {
                        column: {
                            pointPadding: 0.2,
                            borderWidth: 0
                        }
                    },
                    credits: {
                      enabled: false
                    },
                    tooltip: {
                        split: true
                    },
                    series: 
                    [
                        {
                            name: 'RATE',
                            yAxis: 1,
                            data: 
                            [
                                <?php 
                                    for ($j=0; $j < count($PDP_ContextImprimir['SUCC_RATE']) ; $j++) 
                                    { 
                                        echo $PDP_ContextImprimir['SUCC_RATE'][$j].',';
                                    }
                                ?>
                            ],
                            cursor: 'pointer'
                        },{
                            name: 'REQUEST',
                            yAxis: 0,
                            data: 
                            [
                                <?php 
                                    for ($k=0; $k < count($PDP_ContextImprimir['REQUEST']) ; $k++) 
                                    { 
                                        echo $PDP_ContextImprimir['REQUEST'][$k].','; 
                                    }
                                ?>
                            ],
                            cursor: 'pointer'
                        },{
                            name: 'SUCCESS',
                            yAxis: 0,
                            data: 
                            [
                                <?php 
                                    for ($p=0; $p < count($PDP_ContextImprimir['SUCCESS']) ; $p++) 
                                    { 
                                        echo $PDP_ContextImprimir['SUCCESS'][$p].',';
                                    }
                                ?>
                            ],
                            cursor: 'pointer'
                        }
                    ]
                });

            </script>

        </div>
    </div>
    <!-- END GRAFICOS !-->

I want to do it is to print that page with body onload="window.print ();" I can print it but it does not show me the lines of the graphic and if I keep the onload="window.print ();" the graphic is fine with its perfect lines

    
asked by Juan Perez 08.11.2018 в 14:05
source

1 answer

0

It does not work in the event onload of body because you need to load the data of your chart to be able to print later, therefore you can hear the event onload of chart and there shoot the action of print.

Try to change the start of the declaration of your Chart:

chartCPU = new Highcharts.chart('grafico1', 
{
   events:
   {
      load: function(){
         window.print();
      }
   }
//A partir de aqui si puedes seguir con tu declaración

If you want to print the graph specifically you should check that you have included this script:

Y I would change the lines of code above for the following:

chartCPU = new Highcharts.chart('grafico1', 
{
   events:
   {
      load: function(){
         setTimeout(function(){
            chartCPU.print();
         }, 1000);
      }
   }
//A partir de aqui si puedes seguir con tu declaración

You can check that it works in this Fiddle

    
answered by 08.11.2018 в 14:24