【问题标题】:How to get ChartJs object from dynamically created chart如何从动态创建的图表中获取 ChartJs 对象
【发布时间】:2021-04-22 23:32:35
【问题描述】:

我有一个使用 chartjs 动态加载图表的 LandingPage。

我需要获取点击的图表并获取它的属性值。

定义图表的代码位于检索到的动态构建列的循环内(引导程序):

var canvasId = columnID.replace("col", "");
    canvasId = parseInt(canvasId);
    var canvasChart = $("#canvas" + canvasId).get(0).getContext("2d");

    chart = new Chart(canvasChart,
        {
            type: type,
            data: { etc etc

onClick 事件调用模式弹出窗口并设置它的元素,但我需要获取正确的图表(每个图表都有不同的画布 id)

function chart_click(colId)
{
    var parentId = $("#" + colId).closest('.colm').prop("id");
    document.getElementById('ChartEditId').value = parentId;

    // Get the chart's properties. The chart (ID) could be 'canvas01,..canvas09'
    var ChartObject =....HELP
    var type= ??;
    var legendPosition= ??;
    var data = ??.data.datasets...:
    etc..

   // Set the modal's elements from the chart properties
   ...code to set modal's elements

    // Show the ChartEdit modalPopup with it's matched elements' property values
    chartEditpopup.style.display = 'block';
}

谢谢

【问题讨论】:

    标签: javascript object chart.js


    【解决方案1】:

    在使图表将对象存储在具有 2 个值的数组中时,图表对象和该图表的 id。这样,当您单击图表时,您就可以从数组中选择正确的对象。

    您也可以不使用对象跟踪 id,而只将 chart.js 对象存储在数组中,并确保对象的索引在 id 中,然后解析索引并从数组中获取该元素

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2019-03-13
      • 2011-05-25
      • 2020-09-02
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 2020-07-07
      相关资源
      最近更新 更多