Kendo UI Chart won't update using Angular

天涯浪子 提交于 2019-12-11 06:26:05

问题


I am using k-data-source on a pie chart using Kendo DataViz and the Angular directives. When I change the datasource object on the $scope, it doesn't update and throws errors. Were the angular directives intended to be used this way?

Here's a JSBin showing the problem: http://jsbin.com/OSudIzEH/9/edit

Angular Code

var app = angular.module('chart-example', ['kendo.directives']);

function ChartController($scope) {
  $scope.pie = {
    title: {
        position: "bottom",
        text: "Share of Internet Population Growth, 2007 - 2012"
    },
    legend: {
        visible: false
    },
    chartArea: {
        background: ""
    },
    seriesDefaults: {
        labels: {
            visible: true,
            background: "transparent",
            template: "#= category #: #= value#%"
        }
    },
    series: [{
        type: "pie",
        field: "value",
        categoryField: "category"
    }],
    tooltip: {
        visible: true,
        format: "{0}%"
    }
  };

  $scope.updatePie = function() {
    $scope.countries = {
    data: [
        {
            category: "Asia2",
            value: 53.8,
            color: "#9de219"
        }, {
            category: "Europe2",
            value: 16.1,
            color: "#90cc38"
        }, {
            category: "Latin America2",
            value: 11.3,
            color: "#068c35"
        }, {
            category: "Africa2",
            value: 9.6,
            color: "#006634"
        }, {
            category: "Middle East2",
            value: 5.2,
            color: "#004d38"
        }, {
            category: "North America2",
            value: 3.6,
            color: "#033939"
        }
    ]
  };
  };

  $scope.countries = {
    data: [
        {
            category: "Asia",
            value: 53.8,
            color: "#9de219"
        }, {
            category: "Europe",
            value: 16.1,
            color: "#90cc38"
        }, {
            category: "Latin America",
            value: 11.3,
            color: "#068c35"
        }, {
            category: "Africa",
            value: 9.6,
            color: "#006634"
        }, {
            category: "Middle East",
            value: 5.2,
            color: "#004d38"
        }, {
            category: "North America",
            value: 3.6,
            color: "#033939"
        }
    ]
  };
}

HTML

<div ng-controller="ChartController">    
    <button ng-click="updatePie()">update</button>
    <div kendo-chart k-options="pie" k-data-source="countries" />

  </div>

回答1:


You need to create a kendo datasource and then update the data object for it to work. JSBin has been updated with working code.

var app = angular.module('chart-example', ['kendo.directives']);

function ChartController($scope) {
  $scope.pie = {
    title: {
        position: "bottom",
        text: "Share of Internet Population Growth, 2007 - 2012"
    },
    legend: {
        visible: false
    },
    chartArea: {
        background: ""
    },
    seriesDefaults: {
        labels: {
            visible: true,
            background: "transparent",
            template: "#= category #: #= value#%"
        }
    },
    series: [{
        type: "pie",
        field: "value",
        categoryField: "category"
    }],
    tooltip: {
        visible: true,
        format: "{0}%"
    }
  };

  $scope.updatePie = function() {
    $scope.countries.data([
        {
            category: "Asia2",
            value: 53.8,
            color: "#9de219"
        }, {
            category: "Europe2",
            value: 16.1,
            color: "#90cc38"
        }, {
            category: "Latin America2",
            value: 11.3,
            color: "#068c35"
        }, {
            category: "Africa2",
            value: 9.6,
            color: "#006634"
        }, {
            category: "Middle East2",
            value: 5.2,
            color: "#004d38"
        }, {
            category: "North America2",
            value: 3.6,
            color: "#033939"
        }
    ]
  );
  };

  $scope.countries = new kendo.data.DataSource({
    data: [
        {
            category: "Asia",
            value: 53.8,
            color: "#9de219"
        }, {
            category: "Europe",
            value: 16.1,
            color: "#90cc38"
        }, {
            category: "Latin America",
            value: 11.3,
            color: "#068c35"
        }, {
            category: "Africa",
            value: 9.6,
            color: "#006634"
        }, {
            category: "Middle East",
            value: 5.2,
            color: "#004d38"
        }, {
            category: "North America",
            value: 3.6,
            color: "#033939"
        }
    ]
  });
}



回答2:


That looks like a bug in angular-kendo to me. One problem is that the params for toDataSource are in the wrong order. However, even after fixing that, the widget is not updated with the new datasource. I made a few changes to the kendo-angular script to make this work (however I have no idea whether this fits in with the rest of its features):

// Keep the element's data up-to-date with changes.
// this will have to be $watchCollection in >= 1.1.4
scope.$watch(attrs.kDataSource, function (mew, old) {
    if (mew !== old) {
        element.data('$kendoDataSource',
            toDataSource(mew, type));

        var role = element.data("role");
        var widgetType = role.charAt(0).toUpperCase() + role.slice(1);
        var w = element.data("kendo" + widgetType);

        if (!w) {
            w = kendo.widgetInstance(element, kendo.ui);
        }

        if (w && typeof w.setDataSource === "function") {
            w.setDataSource(element.data('$kendoDataSource'));
        }
    }
}, true);

demo here



来源:https://stackoverflow.com/questions/20982808/kendo-ui-chart-wont-update-using-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!