angular-chart.js icon indicating copy to clipboard operation
angular-chart.js copied to clipboard

How to pass options?

Open lucasengel opened this issue 10 years ago • 11 comments

I'm freaking out here with what seems to be a very noob-ish question but... how can I pass options to the chart on the 1.x?

I've created a chart using:

<canvas chart-line
chart-labels="chart.labels"
chart-series="chart.series"
chart-data="chart.data"
chart-colors="chart.colors"
chart-options="chart.options">

then in my controller I have:

$scope.chart = {
labels: ['I', 'II', 'II', 'IV'],
series: ['A', 'B'],
data: [[1,5,3,4], [4,3,5,1]],
colors: ['#AA0000', '#00AA00'],
options: { fill: false, lineTension : 0, pointRadius: 0 }
}

And the rendered line-chart simply looks as stock. What am I missing?

lucasengel avatar May 20 '16 21:05 lucasengel

Are you sure that these are the correct options for Chart.js 1.x? Here are some examples:

  • https://github.com/jtblin/angular-chart.js/blob/master/examples/app.js#L163
  • https://github.com/jtblin/angular-chart.js/blob/master/test/fixtures/configure-line-chart.js#L14

jtblin avatar May 22 '16 21:05 jtblin

I really don't get it @jtblin. I've tried both { fill: false } and { datasetFill: false } and neither seem to work.

lucasengel avatar May 22 '16 21:05 lucasengel

Please post a complete repro case using the jsbin template.

jtblin avatar May 23 '16 00:05 jtblin

Here it is: https://plnkr.co/edit/eVf5DE2y4iUuMrEa7T69

lucasengel avatar May 23 '16 12:05 lucasengel

This issue was already answered on #370. Thanks for the support @jtblin

lucasengel avatar May 31 '16 15:05 lucasengel

Hi, Please I have a REST api that returns the JSON below, below how I can use this as datasource for this chart. I am currently getting error : Cannot read property length of undefined. Here is my JSON : [ { "label": "Year One", "data": 1 }, { "label": "test", "data": 0 }, { "label": "JS 11", "data": 0 }, { "label": "JSS 1", "data": 0 }, { "label": "JSS III", "data": 0 } ]

Thanks

babaphemy avatar Oct 04 '16 22:10 babaphemy

Hi babaphemy,

It'll be easier if you put your Json structure in the format that the chart takes the data. Update your Rest API to return the json as follows:

{ "labels": ["Year One", "test", "JS 11"], "data": [ [1, 0, 0] ] }

Then in your controller, just set the data in the chart as follows:

            $scope.data = response.data.data;
            $scope.labels = response.data.labels;

where the http request from your controller handles an object called response and your chart defined in your html markup has called the chart-data option set to "data" and the chart-labels option set to "labels" as follows:

    <canvas id="line" class="chart chart-line" chart-data="data"
        chart-labels="labels" chart-series="series" chart-options="options"
        chart-dataset-override="datasetOverride" chart-click="onClick"></canvas>

Hope this helps! Adrian.

hettothecool avatar Oct 06 '16 08:10 hettothecool

Hi Adrian, Thanks. I have done it as shown below, though its working but I will still try your solution as well :

$scope.data = []; $scope.labels = []; $scope.getTrnxReport = function(){ Classes.classTranx().then(function(data){ $scope.rept = data; angular.forEach($scope.rept, function(value){ $scope.data.push(value.data); $scope.labels.push(value.label); }); }) }

Regards

babaphemy avatar Oct 06 '16 08:10 babaphemy

Hi Babaphemy,

No problem. As long as you got it sorted. The main advantage to the approach I took was you don't have to do any client side processing of the data itself. You just plug it straight into the data array for the chart.

Good Luck with it.

hettothecool avatar Oct 06 '16 10:10 hettothecool

Please help me, there is no output and no error, I want to fill the chart from json data this is the json data: [{ "date": "2000", "followersNo": "100", "revenue": "2000", "channel": "1" }, { "date": "2009", "followersNo": "1200", "revenue": "14000", "channel": "1" }] and this is a controller code: (function () { var channelController = function ($scope, $http) { $scope.getData = function () { $http.get("app/data/revData.json").then(function (res) { $scope.jsonData = res.data; }) } $scope.getData();

    $scope.labels = [];
    $scope.series = [];
    $scope.data = [[]];

    for (var i = 0, l = $scope.jsonData.length; i < l; i++) {
        $scope.labels.push($scope.jsonData[i].date);
        $scope.series.push($scope.jsonData[i].channel);
        $scope.data[0].push($scope.jsonData[i].followersNo);  
    }

}
channelController.$inject = ["$scope", "$http"];

angular.module("ChannelsApp")
    .controller("channelController", channelController);

}())

Marwa3hmed avatar Aug 03 '17 21:08 Marwa3hmed

we can find a lot of properties for all types of charts here in chart.js documentation : http://www.chartjs.org/docs/latest/

abdallahokasha avatar Aug 28 '17 11:08 abdallahokasha