Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kien.luu/always create chart if has no data #724

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions angular-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@
chartColors: '=?',
chartClick: '=?',
chartHover: '=?',
chartDatasetOverride: '=?'
chartDatasetOverride: '=?',
chartForceUpdate: '=?',
chartDisplayWhenNoData: '=?'
},
link: function (scope, elem/*, attrs */) {
if (useExcanvas) window.G_vmlCanvasManager.initElement(elem[0]);
Expand All @@ -118,7 +120,7 @@
scope.$watch('chartData', watchData, true);
scope.$watch('chartSeries', watchOther, true);
scope.$watch('chartLabels', watchOther, true);
scope.$watch('chartOptions', watchOther, true);
scope.$watch('chartOptions', watchOptions, true);
scope.$watch('chartColors', watchOther, true);
scope.$watch('chartDatasetOverride', watchOther, true);
scope.$watch('chartType', watchType, false);
Expand All @@ -132,14 +134,14 @@
});

function watchData (newVal, oldVal) {
if (! newVal || ! newVal.length || (Array.isArray(newVal[0]) && ! newVal[0].length)) {
if (!scope.chartDisplayWhenNoData && (! newVal || ! newVal.length || (Array.isArray(newVal[0]) && ! newVal[0].length))) {
destroyChart(scope);
return;
}
var chartType = type || scope.chartType;
if (! chartType) return;

if (scope.chart && canUpdateChart(newVal, oldVal))
if (scope.chart && (canUpdateChart(newVal, oldVal) || scope.chartForceUpdate))
return updateChart(newVal, scope);

createChart(chartType, scope, elem);
Expand All @@ -156,6 +158,12 @@
createChart(chartType, scope, elem);
}

function watchOptions (newVal, oldVal) {
if (isEmpty(newVal)) return;
if (angular.equals(newVal, oldVal)) return;
updateChartOption(newVal, scope)
}

function watchType (newVal, oldVal) {
if (isEmpty(newVal)) return;
if (angular.equals(newVal, oldVal)) return;
Expand All @@ -167,7 +175,7 @@

function createChart (type, scope, elem) {
var options = getChartOptions(type, scope);
if (! hasData(scope) || ! canDisplay(type, scope, elem, options)) return;
if (!scope.chartDisplayWhenNoData && (! hasData(scope) || ! canDisplay(type, scope, elem, options))) return;

var cvs = elem[0];
var ctx = cvs.getContext('2d');
Expand Down Expand Up @@ -352,6 +360,18 @@
cvs.onmousemove = scope.chartHover ? getEventHandler(scope, 'chartHover', true) : angular.noop;
}

function updateChartOption(values,scope) {
if (values) {
Object.keys(values).forEach((key) => {
if (scope.chart.options[key]) {
scope.chart.options[key] = values[key]
}
})
scope.chart.update('quiet');
scope.$emit('chart-update', scope.chart);
}
}

function updateChart (values, scope) {
if (Array.isArray(scope.chartData[0])) {
scope.chart.data.datasets.forEach(function (dataset, i) {
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-chart.js",
"version": "1.1.1",
"version": "1.1.3",
"main": [
"./dist/angular-chart.js"
],
Expand Down
39 changes: 25 additions & 14 deletions dist/angular-chart.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/*!
* angular-chart.js - An angular.js wrapper for Chart.js
* http://jtblin.github.io/angular-chart.js/
* Version: 1.1.1
*
* Copyright 2016 Jerome Touffe-Blin
* Released under the BSD-2-Clause license
* https://github.com/jtblin/angular-chart.js/blob/master/LICENSE
*/
(function (factory) {
'use strict';
if (typeof exports === 'object') {
Expand Down Expand Up @@ -118,7 +109,9 @@
chartColors: '=?',
chartClick: '=?',
chartHover: '=?',
chartDatasetOverride: '=?'
chartDatasetOverride: '=?',
chartForceUpdate: '=?',
chartDisplayWhenNoData: '=?'
},
link: function (scope, elem/*, attrs */) {
if (useExcanvas) window.G_vmlCanvasManager.initElement(elem[0]);
Expand All @@ -127,7 +120,7 @@
scope.$watch('chartData', watchData, true);
scope.$watch('chartSeries', watchOther, true);
scope.$watch('chartLabels', watchOther, true);
scope.$watch('chartOptions', watchOther, true);
scope.$watch('chartOptions', watchOptions, true);
scope.$watch('chartColors', watchOther, true);
scope.$watch('chartDatasetOverride', watchOther, true);
scope.$watch('chartType', watchType, false);
Expand All @@ -141,14 +134,14 @@
});

function watchData (newVal, oldVal) {
if (! newVal || ! newVal.length || (Array.isArray(newVal[0]) && ! newVal[0].length)) {
if (!scope.chartDisplayWhenNoData && (! newVal || ! newVal.length || (Array.isArray(newVal[0]) && ! newVal[0].length))) {
destroyChart(scope);
return;
}
var chartType = type || scope.chartType;
if (! chartType) return;

if (scope.chart && canUpdateChart(newVal, oldVal))
if (scope.chart && (canUpdateChart(newVal, oldVal) || scope.chartForceUpdate))
return updateChart(newVal, scope);

createChart(chartType, scope, elem);
Expand All @@ -165,6 +158,12 @@
createChart(chartType, scope, elem);
}

function watchOptions (newVal, oldVal) {
if (isEmpty(newVal)) return;
if (angular.equals(newVal, oldVal)) return;
updateChartOption(newVal, scope)
}

function watchType (newVal, oldVal) {
if (isEmpty(newVal)) return;
if (angular.equals(newVal, oldVal)) return;
Expand All @@ -176,7 +175,7 @@

function createChart (type, scope, elem) {
var options = getChartOptions(type, scope);
if (! hasData(scope) || ! canDisplay(type, scope, elem, options)) return;
if (!scope.chartDisplayWhenNoData && (! hasData(scope) || ! canDisplay(type, scope, elem, options))) return;

var cvs = elem[0];
var ctx = cvs.getContext('2d');
Expand Down Expand Up @@ -361,6 +360,18 @@
cvs.onmousemove = scope.chartHover ? getEventHandler(scope, 'chartHover', true) : angular.noop;
}

function updateChartOption(values,scope) {
if (values) {
Object.keys(values).forEach((key) => {
if (scope.chart.options[key]) {
scope.chart.options[key] = values[key]
}
})
scope.chart.update('quiet');
scope.$emit('chart-update', scope.chart);
}
}

function updateChart (values, scope) {
if (Array.isArray(scope.chartData[0])) {
scope.chart.data.datasets.forEach(function (dataset, i) {
Expand Down
12 changes: 1 addition & 11 deletions dist/angular-chart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.