Getting graphic data from DIV element

Viewed 34

I using Chart.js and I want to pull the data from the properties inside the DIV element.

I can get data in data-gpget and data-gpret. It writes to the console but does not see it as "data" data.

data: getATT('#graph3','data-gpval') and data: getATT('#graph3','data-gpret') What could be the reason for this? What am I missing? Thank you.

<div id="graph3" data-gpget="[28,12,14,27,11]" data-gpret="[21,64,32,73,76]"> 
  <canvas id="js-chartjs-earnings"></canvas>
</div>

JS:

function getATT(id,value) {
   return $(id).attr(value);
}



return e = a, t = [{
                key: "initCharts", value: function () {
Chart.defaults.scale.gridLines.color = "transparent", Chart.defaults.scale.gridLines.zeroLineColor = "transparent", Chart.defaults.scale.ticks.beginAtZero = !0, Chart.defaults.global.elements.line.borderWidth = 0, Chart.defaults.global.elements.point.radius = 0, Chart.defaults.global.elements.point.hoverRadius = 0, Chart.defaults.global.tooltips.cornerRadius = 3, Chart.defaults.global.legend.labels.boxWidth = 12;
                    var r, a = jQuery(".js-chartjs-earnings");
                    r = {
                        labels: ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"],
                        datasets: [{
                            label: "GETTING",
                            fill: !0, 
                            data: getATT('#graph3','data-gpget')
                        }, {
                            label: "RETURNS",
                            fill: !0,
                            data: getATT('#graph3','data-gpret')
                        }]
                    }, a.length && new Chart(a, {
                        type: "bar",
                        data: r,
                        options: {
                            tooltips: {
                                intersect: !1, callbacks: {
                                    label: function (r, a) {
                                        return a.datasets[r.datasetIndex].label + ": $" + r.yLabel
                                    }
                                }
                            }
                        }
                    })
                }
            }, {
                key: "init", value: function () {
                    this.initCharts()
                }
            }], null && r(e.prototype, null), t && r(e, t), a
1 Answers

the attr method will always return a string.
try using the data method instead...

return $(id).data(value);

just be sure to remove the data- prefix from the attribute names.

see / run following example...

var testATT = getATT('#graph3','data-gpget');
var testData = getData('#graph3','gpget');
console.log(typeof testATT, testATT);
console.log(typeof testData, testData);

function getData(id,value) {
   return $(id).data(value);
}
function getATT(id,value) {
   return $(id).attr(value);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="graph3" data-gpget="[28,12,14,27,11]" data-gpret="[21,64,32,73,76]"> 
  <canvas id="js-chartjs-earnings"></canvas>
</div>

Related