Chartjs-How can i refere to an object variable dynamically?

0👍

Try this: Get global variable dynamically by name string in JavaScript

Or add your chart to an Object of which you can access the keys:

var charts = {};
charts.populationIncrease = new Chart(...);

function updateChart(chartName, value) {
    charts[chartName].value = value;
}

updateChart('populationIncrease', { ... });

0👍

Issue you have is that you are trying to access the objects property (getting and setting) however you are trying to access properties of the string $(this).attr("name")
where instead you should be using $(this)

see fixed code below

var prodChart1 = document.getElementById('ProdChart1');
var prodChart1 = new Chart( prodChart1, {
  type: "line",
  data: <%=f_A_GetTotalWorkedHours(Dateadd("d",-2,Date), Date, 48, Line, "")%>,
  options: {
        color: 'red',
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});


$(".update").click(function(){  
    UpdateChart($(this), $(this).attr("name"),""); //Pass in the object and the name
});

function UpdateChart(chartObject, chartName, aFunction) {
    $.ajax({
        type: 'POST', //post method
        url: 'AnalyticsAPI.asp?',
        dataType: "text",
        data: {requestParam: 'f_A_GetTotalWorkedHours|'+ getParam()[0] +'|'+ getParam()[1] +'|48' },
        success: function (result, textStatus, jqXHR)
        {
            data3= result; 
            chartObject.config.data = JSON.parse(data3); //you are using the object not the string attribute of name
            chartObject.update();

        },  
        error: function (xhr, ajaxOptions, thrownError) {
       // alert(xhr.status);
        alert(thrownError);
      }
    });
};

Leave a comment