| 1 |
<?php |
| 2 |
$height = m_chart()->get_post_meta( $post_id, 'height' ); |
| 3 |
|
| 4 |
$width = ''; |
| 5 |
|
| 6 |
if ( '' != $args['width'] && 'responsive' != $args['width'] ) { |
| 7 |
$width = ' width="' . absint( $args['width'] ) . '"'; |
| 8 |
} |
| 9 |
?> |
| 10 |
<div id="m-chart-container-<?php echo absint( $post_id ); ?>-<?php echo absint( $this->instance ); ?>" class="m-chart-container chartjs"> |
| 11 |
<canvas id="m-chart-<?php echo absint( $post_id ); ?>-<?php echo absint( $this->instance ); ?>" class="m-chart" height="<?php echo absint( $height ); ?>"<?php echo $width; ?>></canvas> |
| 12 |
</div> |
| 13 |
<script type="text/javascript"> |
| 14 |
var m_chart_container_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>_canvas = document.getElementById( 'm-chart-<?php echo absint( $post_id ); ?>-<?php echo absint( $this->instance ); ?>' ).getContext('2d'); |
| 15 |
|
| 16 |
var m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?> = { |
| 17 |
chart_args: <?php echo $this->unicode_aware_stripslashes( json_encode( $this->library( 'chartjs' )->get_chart_args( $post_id, $args ), JSON_HEX_QUOT ) ); ?>, |
| 18 |
post_id: <?php echo absint( $post_id ); ?>, |
| 19 |
instance: <?php echo absint( $this->instance ); ?>, |
| 20 |
render_1: true |
| 21 |
}; |
| 22 |
|
| 23 |
<?php do_action( 'm_chart_after_chart_args', $post_id, $args, $this->instance ); ?> |
| 24 |
|
| 25 |
(function( $ ) { |
| 26 |
m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.render_chart = function() { |
| 27 |
$( '.m-chart' ).trigger({ |
| 28 |
type: 'render_start', |
| 29 |
post_id: this.post_id, |
| 30 |
instance: this.instance |
| 31 |
}); |
| 32 |
|
| 33 |
var target = this.chart_args.options.animation; |
| 34 |
|
| 35 |
var source = { |
| 36 |
onComplete: function() { |
| 37 |
// This deals with an issue in Chart.js 3.1.0 where onComplete can run too many times |
| 38 |
// We only want to trigger on the first render anyway so we'll just check |
| 39 |
if ( false === m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.render_1 ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.render_1 = false; |
| 44 |
|
| 45 |
$( '.m-chart' ).trigger({ |
| 46 |
type: 'render_done', |
| 47 |
post_id: m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.post_id, |
| 48 |
instance: m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.instance |
| 49 |
}); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! target ) { |
| 54 |
source = {animation: source}; |
| 55 |
target = this.chart_args.options; |
| 56 |
} |
| 57 |
|
| 58 |
Object.assign( target, source ); |
| 59 |
|
| 60 |
Chart.register( ChartDataLabels ); |
| 61 |
|
| 62 |
this.chart = new Chart( |
| 63 |
m_chart_container_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>_canvas, |
| 64 |
this.chart_args |
| 65 |
); |
| 66 |
}; |
| 67 |
|
| 68 |
$( function() { |
| 69 |
$.when( m_chart_chartjs_helpers.init() ).done(function() { |
| 70 |
m_chart_chartjs_<?php echo absint( $post_id ); ?>_<?php echo absint( $this->instance ); ?>.render_chart(); |
| 71 |
}); |
| 72 |
} ); |
| 73 |
})( jQuery ); |
| 74 |
</script> |
| 75 |
|