JavaScript Add Link to X-Label Chart.js link chart label to cell,excel link chart label to

I'm looking to be able to link the x-labels in a Chart.js bar chart. I've searched pretty thoroughly, and ended up trying to come up with my own solution: because the labels correspond to the bars directly above them and Chart.js has a built in getBarsAtEvent(evt) method, I tried creating an event if the user didn't click on a chart - this new event had pageX and pageY that was directly above the initial click such that if the user had clicked on a label, the new event would simulate a click on the bar graph.

However, calling getBarsAtEvent(createdClickEvent) repeatedly gives me a Uncaught TypeError ("Cannot read property 'getBoundingClientRect' of null"), which must mean that the getBarsAtEvent method, when called on my simulated click, isn't actually returning anything.

Any suggestions or alternate approaches would be greatly appreciated, thanks in advance.

Answer:1

An alternative approach would be to determine the point where the user is actually clicked and based on that calculate which label was clicked. For that you will need some information about the chart created and have to do some calculations.

Below is a way of doing that, and here is a Fiddle with this code/approach. Hope it helps.

    $("#canvas").click( 
       function(evt){
            var ctx = document.getElementById("canvas").getContext("2d");
            // from the endPoint we get the end of the bars area
            var base = myBar.scale.endPoint;
            var height = myBar.chart.height;
            var width = myBar.chart.width;
            // only call if event is under the xAxis
            if(evt.pageY > base){
                // how many xLabels we have
                var count = myBar.scale.valuesCount;
                var padding_left = myBar.scale.xScalePaddingLeft;
                var padding_right = myBar.scale.xScalePaddingRight;
                // calculate width for each label
                var xwidth = (width-padding_left-padding_right)/count;
                // determine what label were clicked on AND PUT IT INTO bar_index 
                var bar_index = (evt.offsetX - padding_left) / xwidth;
                // don't call for padding areas
                if(bar_index > 0 & bar_index < count){
                    bar_index = parseInt(bar_index);
                    // either get label from barChartData
                    console.log("barChartData:" + barChartData.labels[bar_index]);
                    // or from current data
                    var ret = [];
                    for (var i = 0; i < myBar.datasets[0].bars.length; i++) {
                        ret.push(myBar.datasets[0].bars[i].label)
                    };
                    console.log("current data:" + ret[bar_index]);
                    // based on the label you can call any function
                }
            }
       }
);   
Answer:2

I am developing intel cordova app... to download files from server , i have included cordova file download plugin but it has data that i want to pass through variable... here is my code: var app = { ...

I am developing intel cordova app... to download files from server , i have included cordova file download plugin but it has data that i want to pass through variable... here is my code: var app = { ...

  1. index.js file cordova
  2. create js file cordova

I have a CSS property that was set in a stylesheet file: .main-outer-div-filter { max-height: 230px; } I am trying to remove it with jQuery like so: $('.main-outer-div-filter').css('max-height',...

I have a CSS property that was set in a stylesheet file: .main-outer-div-filter { max-height: 230px; } I am trying to remove it with jQuery like so: $('.main-outer-div-filter').css('max-height',...

  1. remove properties to pdf

I am interested in switching my entire site from http over to https. My concern is that I have some content that uses absolute http URLs. I will need to edit each page in order to change those ...

I am interested in switching my entire site from http over to https. My concern is that I have some content that uses absolute http URLs. I will need to edit each page in order to change those ...

  1. make http https
  2. make http https apache

I run this code in JavaScript: setTimeout(function(){console.log("1");}, 0); console.log("2"); But output is: 2 1 Why not vice versa?

I run this code in JavaScript: setTimeout(function(){console.log("1");}, 0); console.log("2"); But output is: 2 1 Why not vice versa?