How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?

immanual

I have created a bar graph using chartJS. I want to display the data present in my JSON array into the graph tooltip.

JSON

{
    "meth": [ {
        "tech": "CSS", "avg": 3, "Count": 80, "sum": 53
    }
    ,
    {
        "tech": "CCS", "avg": 9, "Count": 70, "sum": 25
    }
    ,
    {
        "tech": "CSC", "avg": 7, "Count": 50, "sum": 66
    }
    ]
}

The above json data is used to plot the graph by using chartJS in javascript but i want the other data to be displayed in the graph as tooltip. the data avg & sum should be displayed as part of tooltip along with label as tech and data as count.

Graph Code in Javascript:

<script> 
var ctx1=document.getElementById('bar').getContext('2d');
var myChart1=new Chart(ctx1, {
    type: 'bar', legend: {
        display: true
    }
    , options: {
        tooltips: {
            callbacks: {
                label: function(tooltipItem, data) {
                    var dataset=data.datasets[tooltipItem.datasetIndex];
                    return data.datasets[tooltipItem.datasetIndex].label+ ' : ' +dataset.data[tooltipItem.index]+"%";
                }
            }
        }
        , scales: {
            yAxes: [ {
                ticks: {
                    beginAtZero: true, steps: 10, stepValue: 5, max: 100
                }
            }
            ]
        }
    }
    , data: {
        labels: techDATA, datasets: [ {
            backgroundColor: colorCode, label: 'Method Covered', data: countDATA
        }
        ]
    }
}

);
</script>

I want to display the data like avg and sum in the tooltip which already has the default tooltip with label and data. Help me with code.....!

immanual

Customized Multiline Tooltip in ChartJS:

<!DOCTYPE html>
                <!--
                homt.html
                -->
                <html>
                    <head>
                        <title>Customised tooltip</title>
                        <!--ChartJs-->
                        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
                        <!--ChartJs-->
                        <style>
                div {
                  height: 600px;
                  width: 700px;
                }
                </style>
                    </head>
                    <body>
                        <div id="myCan" >
                            <canvas id="bar"></canvas>
                        </div>
                    </body>
                    <script type="text/javascript">
                        var dataJason = {
                    "meth": [{
                        "tech": "CSS", "avg": 3, "Count": 80, "sum": 53
                    },
                    {
                        "tech": "CCS", "avg": 9, "Count": 70, "sum": 25
                    },
                    {
                        "tech": "CSC", "avg": 7, "Count": 50, "sum": 66
                    }]
                };

                var techDATA = [];
                var countDATA = [];
                var avgDATA = [];
                var sumDATA = [];
                function techData(){
                    var jdata = dataJason.meth;
                    var jl = jdata.length;
                    for(var i = 0; i < jl; i++){
                        techDATA.push(dataJason.meth[i].tech);
                        countDATA.push(dataJason.meth[i].Count);
                        avgDATA.push(dataJason.meth[i].avg);
                        sumDATA.push(dataJason.meth[i].sum);
                    }
                }

                var colorCode = ['#5DADE2','#F1C40F','#00FFFF','#A569BD','#F5B041','#566573'];

                function init(){
                    techData();
                    var ctx1 = document.getElementById("bar").getContext('2d');
                    var myChart1 = new Chart(ctx1, {
                                type: 'bar',  
                            options: {
                                legend: {
                                display: false
                            },
                                tooltips: {
                                    callbacks: {
                                        beforeLabel: function(tooltipItem, data){
                                            var dataset = data.datasets[tooltipItem.datasetIndex];
                                            return data.datasets[tooltipItem.datasetIndex].label+ ' : '+dataset.data[tooltipItem.index]+"%";
                                        },
                                        label: function(tooltipItem, data) {
                                            var avg = "Avg: "+avgDATA[tooltipItem.index];
                                            return avg;
                                        },
                                        afterLabel: function(tooltipItem, data){
                                            var sum = "Sum: "+sumDATA[tooltipItem.index];
                                            return sum;
                                        }
                                    }
                                }, 
                                scales: {
                                    yAxes: [{
                                        ticks: {
                                            beginAtZero: true, steps: 10, stepValue: 5, max: 100
                                        }
                                    }]
                                }
                            }, 
                            data: {
                                labels: techDATA, datasets: [{
                                    backgroundColor: colorCode, label: 'Method Covered', data: countDATA
                                }]
                            }
                       }
                   );
                }

                window.onload = function(){
                  init();  
                };

                    </script>
                </html>

******Output******

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how display custom data in chartjs tooltip

ChartJS - Custom tooltip with icon

ChartJS, Disable Tooltip for one plot on a multi graph data

How to create a chart with chartjs.org with data from an array?

How to create a stacked graph using ChartJS

How to create line graph use chartjs?

Json data visualization in Javascript with chartjs

create custom objects array from json data

chartjs - How to access chart instance in a custom tooltip callback

How can I create custom tooltips for each data point in a graph?

How to create Custom spinner adapter with JSON Data?

how to build a json array with custom elements with javascript

How to create a JSON Array with the div data

How to create JSON Array with Loop Data in Android

How to use JSON data in creating a chart with chartjs?

ChartJS - How to change color of some data points in graph

How to set the chartjs bar graph scale to the highest value in the result data

How to create JSON data using javascript and jquery

how to create list from data json on javascript?

In javascript, how do I create a nested array or object from json data?

Create array for Data JSON

JavaScript: Build custom data tree from JSON array

create json array in javascript

Chartjs - Insert additional data into chart tooltip

ChartJS - Display a single line data in tooltip

Missing Tooltip for some data points using chartjs

How to append more data in tooltip of graph in chart.js

How to create a Json Array from class fields with custom values?

How to pass Json data into JavaScript Array?