带有KendoUI图表的KnockoutJS组件只适用于最后一个图表

KnockoutJS components with KendoUI chart only works for the last chart

本文关键字:适用于 最后一个 组件 KendoUI 带有 KnockoutJS      更新时间:2023-09-26

我有一个可观察数组,它创建了一个带有参数的组件。

在每个组件中,我使用该参数查询数据(通过AJAX)。我将这些数据返回到一个可观察数组中,并使用该数组作为KendoUI图表的数据源。

带有参数的组件列表示例:

<csglist params="account:'08167526'"></csglist>
<hr>
<csglist params="account:'0873458'"></csglist>
<hr>
<csglist params="account:'0828337'"></csglist>
<hr>
<csglist params="account:'086778'"></csglist>
获取数据 的

调用

        getCategory = function () {
            self.categoryChart([]);
            xhr_get(cont.publish + 'api/dbmain/getCategory', { 'id': params.account }).done(function (allData) {
                var mappedLogs = $.map(allData, function (item) { return new categoryData(item) });
                self.categoryChart(mappedLogs);
                buildChart();
            })
        }
function buildchart(){
    $(document).ready(createChart);
    $(document).bind("kendo:skinChange", createChart);
}

创建图表

 function createChart() {    
      $("#" + self.act() + "chart").kendoChart({
        dataSource: {
          data: ko.toJS(self.categoryChart)
            //, sort: { field: "category", dir: "asc" }
        },
        title: {
          text: self.type()
        },
        legend: {
          visible: true,
          position: "bottom"
        },
        seriesDefaults: {
          type: "bar",
          stack: true
        },
        series: [{
          field: "sales",
          name: "Current Sales",
          color: "#66110F"
        }, {
          field: "opp",
          name: "Opportunity",
          color: "#E65F5B"
        }],
        valueAxis: {
          //   max: 180,
          line: {
            visible: false
          },
          minorGridLines: {
            visible: true
          },
          visible: false
        },
        categoryAxis: {
          field: "category",
          majorGridLines: {
            visible: false
          }
        },
        tooltip: {
          visible: false,
          template: "#= series.name #: #= value #"
        }
      });
    }

但是当所有组件都创建后,只有最后一个组件有数据。

谁能告诉我为什么会这样?

当我将buildChart =函数更改为self时。buildChart = function,它工作了!