Skip to content

Commit

Permalink
Run event handlers in zone (#1365)
Browse files Browse the repository at this point in the history
* Run event handlers in zone

* Updated screenshots
  • Loading branch information
santam85 authored Jan 5, 2022
1 parent a3367d4 commit aa3e7f0
Show file tree
Hide file tree
Showing 10 changed files with 3,683 additions and 4,192 deletions.
1 change: 0 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"preserveSymlinks": true,
"outputPath": "dist/ng2-charts-demo",
"index": "src/index.html",
"main": "src/main.ts",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7,783 changes: 3,621 additions & 4,162 deletions package-lock.json

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
},
"private": true,
"dependencies": {
"@angular/animations": "^13.0.3",
"@angular/cdk": "^13.0.3",
"@angular/common": "^13.0.3",
"@angular/compiler": "^13.0.3",
"@angular/core": "^13.0.3",
"@angular/forms": "^13.0.3",
"@angular/material": "^13.0.3",
"@angular/platform-browser": "^13.0.3",
"@angular/platform-browser-dynamic": "^13.0.3",
"@angular/router": "^13.0.3",
"@angular/animations": "^13.1.1",
"@angular/cdk": "^13.1.1",
"@angular/common": "^13.1.1",
"@angular/compiler": "^13.1.1",
"@angular/core": "^13.1.1",
"@angular/forms": "^13.1.1",
"@angular/material": "^13.1.1",
"@angular/platform-browser": "^13.1.1",
"@angular/platform-browser-dynamic": "^13.1.1",
"@angular/router": "^13.1.1",
"chart.js": "^3.6.0",
"chartjs-adapter-date-fns": "^2.0.0",
"chartjs-chart-financial": "^0.1.0",
Expand All @@ -48,18 +48,18 @@
"zone.js": "~0.11.4"
},
"devDependencies": {
"@angular-devkit/build-angular": "^13.0.4",
"@angular-devkit/core": "^13.0.4",
"@angular-devkit/schematics": "^13.0.4",
"@angular-devkit/build-angular": "^13.1.2",
"@angular-devkit/core": "^13.1.2",
"@angular-devkit/schematics": "^13.1.2",
"@angular-eslint/builder": "^13.0.1",
"@angular-eslint/eslint-plugin": "^13.0.1",
"@angular-eslint/eslint-plugin-template": "^13.0.1",
"@angular-eslint/schematics": "^13.0.1",
"@angular-eslint/template-parser": "^13.0.1",
"@angular/cli": "^13.0.4",
"@angular/compiler-cli": "^13.0.3",
"@angular/language-service": "^13.0.3",
"@nrwl/cypress": "^13.2.3",
"@angular/cli": "^13.1.2",
"@angular/compiler-cli": "^13.1.1",
"@angular/language-service": "^13.1.1",
"@nrwl/cypress": "^13.4.3",
"@schematics/angular": "^13.0.3",
"@types/jasmine": "~3.8.0",
"@types/jasminewd2": "^2.0.10",
Expand All @@ -68,6 +68,7 @@
"@types/node": "^14.17.32",
"@typescript-eslint/eslint-plugin": "^5.5.0",
"@typescript-eslint/parser": "^5.5.0",
"chartjs-test-utils": "^0.3.1",
"cypress": "^9.1.0",
"cypress-image-snapshot": "^4.0.1",
"eslint": "^8.2.0",
Expand All @@ -88,4 +89,4 @@
"tslib": "^2.3.1",
"typescript": "~4.4.4"
}
}
}
2 changes: 1 addition & 1 deletion projects/ng2-charts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng2-charts",
"version": "3.0.5",
"version": "3.0.6",
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
Expand Down
31 changes: 28 additions & 3 deletions projects/ng2-charts/src/lib/base-chart.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { BaseChartDirective } from './base-chart.directive';
import { By } from '@angular/platform-browser';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { Component } from '@angular/core';
import { Chart, registerables } from "chart.js";

@Component({
template: '<canvas baseChart' +
' [data]="data"' +
' [datasets]="datasets"' +
' [labels]="labels"></canvas>'
' [labels]="labels"' +
' (chartClick)="click()"></canvas>'
})
class TestComponent {
public data?: any
public datasets?: any[]
public labels?: string[]
public click? = jasmine.createSpy('chartClick')
public hover? = jasmine.createSpy('chartHover')
}

describe('BaseChartDirective', () => {
Expand Down Expand Up @@ -79,7 +82,7 @@ describe('BaseChartDirective', () => {
expect(directive.chart?.data.labels).toEqual(element.data.labels);

element.data = {
labels: [ 'Life', 'Universe', 'Everything'],
labels: [ 'Life', 'Universe', 'Everything' ],
datasets: []
}

Expand All @@ -89,4 +92,26 @@ describe('BaseChartDirective', () => {
expect(directive.chart?.data?.labels && directive.chart?.data?.labels[0]).not.toEqual('Answers');
});

it('should emit when the chart is clicked', fakeAsync(() => {

fixture.detectChanges();

let canvas = fixture.nativeElement.querySelector('canvas');

canvas.dispatchEvent(
new MouseEvent(
"click",
{
clientX: canvas.getBoundingClientRect().left + 50,
clientY: canvas.getBoundingClientRect().top + 50,
bubbles: true
}
)
);

tick(25);

expect(element.click).toHaveBeenCalled();
}));

});
11 changes: 8 additions & 3 deletions projects/ng2-charts/src/lib/base-chart.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ export class BaseChartDirective<TType extends ChartType = ChartType,
private getChartOptions(): ChartConfiguration<TType, TData, TLabel>['options'] {
return merge({
onHover: (event: ChartEvent, active: {}[]) => {
if (active && !active.length) {
if (this.chartHover.observed && active && !active.length) {
return;
}
this.chartHover.emit({ event, active });

this.zone.run(() => this.chartHover.emit({ event, active }));
},
onClick: (event?: ChartEvent, active?: {}[]) => {
this.chartClick.emit({ event, active });
if(!this.chartClick.observed){
return;
}

this.zone.run(() => this.chartClick.emit({ event, active }));
}
},
this.themeOverrides,
Expand Down
4 changes: 3 additions & 1 deletion src/app/bar-chart/bar-chart.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[data]="barChartData"
[options]="barChartOptions"
[plugins]="barChartPlugins"
[type]="barChartType">
[type]="barChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)">
</canvas>
</div>
<button mat-button mat-raised-button color="primary" (click)="randomize()">Update</button>
Expand Down
6 changes: 3 additions & 3 deletions src/app/bar-chart/bar-chart.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { ChartConfiguration, ChartData, ChartType } from 'chart.js';
import { ChartConfiguration, ChartData, ChartEvent, ChartType } from 'chart.js';
import { BaseChartDirective } from 'ng2-charts';

import DataLabelsPlugin from 'chartjs-plugin-datalabels';
Expand Down Expand Up @@ -45,11 +45,11 @@ export class BarChartComponent {
};

// events
public chartClicked({ event, active }: { event: MouseEvent, active: {}[] }): void {
public chartClicked({ event, active }: { event?: ChartEvent, active?: {}[] }): void {
console.log(event, active);
}

public chartHovered({ event, active }: { event: MouseEvent, active: {}[] }): void {
public chartHovered({ event, active }: { event?: ChartEvent, active?: {}[] }): void {
console.log(event, active);
}

Expand Down

0 comments on commit aa3e7f0

Please sign in to comment.