My goal is to add an average line to my barchart.
I downloaded the github repo for this plugin chartjs-plugin-annotation and added to my wordpress theme in path:
/Users/Name/Downloads/public_html/wp-content/themes/Total/assets/js/chartjs-plugin-annotation-master/src/annotation.js
Next, I call it in my html:
<script src="annotation.js"></script>
Lastly, I call it my chartjs script:
<canvas id="Chart"></canvas>
<script>
var charts_plugin = chartjs-plugin-annotation.js;
var canvas_2 = document.getElementById("Chart");
var ctx_2 = canvas_2.getContext('2d');
var Chart = new Chart(ctx_2, {
plugins: [charts_plugin],
type: 'bar',
data: {
labels: <?php echo json_encode($arr_macd_dates_formated); ?>,
datasets: [
{
label: '<?php echo $str_macd, $arr_macd_name[0] ; ?>',
backgroundColor: ["#faae00"],
data: <?php echo json_encode($arr_macd_diff); ?>
}
]
},
options: {
legend: { display: false },
title: {
display: true,
},
annotation: {
annotations: [{
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: 5,
borderColor: 'rgb(75, 192, 192)',
borderWidth: 4,
label: {
enabled: false,
content: 'Test label'
}
}]
}
}
});
</script>
Here is how I call my chart js :
function custom_scripts() {
wp_enqueue_script( 'chart-js', 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.1/chart.js', array(), true );
}
add_action( 'wp_enqueue_scripts', 'custom_scripts');
This does not call the plugin:
var charts_plugin = chart-js-annotation.js;
makes the chart disappear.