Skip to content

Commit

Permalink
Merge pull request #217 from willsoto/method-visibility
Browse files Browse the repository at this point in the history
fix(ng-chartist): corrected method visibility in chartist component.
  • Loading branch information
willsoto authored Jan 15, 2019
2 parents 15258d3 + 0771314 commit e5a3221
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 64 deletions.
94 changes: 56 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ import { ChartistModule } from 'ng-chartist';
@NgModule({
imports: [
ChartistModule // add ChartistModule to your imports
],
]
})
export class AppModule {}
```

```typescript
// bar-chart.component.ts
import {IBarChartOptions, IChartistAnimationOptions, IChartistData} from 'chartist';
import {ChartEvent, ChartType} from 'ng-chartist';
import {
IBarChartOptions,
IChartistAnimationOptions,
IChartistData
} from 'chartist';
import { ChartEvent, ChartType } from 'ng-chartist';

@Component({
selector: 'app-bar-chart',
Expand All @@ -86,44 +90,58 @@ import {ChartEvent, ChartType} from 'ng-chartist';
[events]="events"
></x-chartist>
`,
styles: [`
x-chartist {
display: block;
height: 300px;
}
`]
styles: [
`
x-chartist {
display: block;
height: 300px;
}
`
]
})
export class BarChartComponent {
type: ChartType = 'Bar';
data: IChartistData = {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
series: [
[5, 4, 3, 7, 5, 10, 3, 4, 8, 10, 6, 8],
[3, 2, 9, 5, 4, 6, 4, 6, 7, 8, 7, 4]
]
};

options: IBarChartOptions = {
axisX: {
showGrid: false
}
};

events: ChartEvent = {
draw: (data) => {
if (data.type === 'bar') {
data.element.animate({
y2: <IChartistAnimationOptions>{
dur: '0.5s',
from: data.y1,
to: data.y2,
easing: 'easeOutQuad'
}
});
}
type: ChartType = 'Bar';
data: IChartistData = {
labels: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
series: [
[5, 4, 3, 7, 5, 10, 3, 4, 8, 10, 6, 8],
[3, 2, 9, 5, 4, 6, 4, 6, 7, 8, 7, 4]
]
};

options: IBarChartOptions = {
axisX: {
showGrid: false
}
};

events: ChartEvent = {
draw: (data) => {
if (data.type === 'bar') {
data.element.animate({
y2: <IChartistAnimationOptions>{
dur: '0.5s',
from: data.y1,
to: data.y2,
easing: 'easeOutQuad'
}
});
}
};
}
};
}
```

Expand Down
30 changes: 15 additions & 15 deletions projects/ng-chartist/src/lib/chartist.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('chartist component', function(): void {
instance.data = data[chartType];
instance.type = chartType;

await instance.renderChart();
await instance['renderChart']();

expect(Chartist.Bar).toHaveBeenCalledTimes(1);
});
Expand All @@ -45,15 +45,15 @@ describe('chartist component', function(): void {
instance.data = data[chartType];
instance.type = chartType;

const chart = await instance.renderChart();
const chart = await instance['renderChart']();

expect(chart instanceof Chartist.Bar).toBe(true);
});

it('should bind events if there are events', async function() {
const chartType: ChartType = 'Bar';

spyOn(instance, 'bindEvents').and.callThrough();
spyOn(<any>instance, 'bindEvents').and.callThrough();

instance.data = data[chartType];
instance.type = chartType;
Expand All @@ -65,7 +65,7 @@ describe('chartist component', function(): void {

await instance.ngOnInit();

expect(instance.bindEvents).toHaveBeenCalled();
expect(instance['bindEvents']).toHaveBeenCalled();
});

it('should re-render the chart if the chart type changes', function(): void {
Expand All @@ -75,11 +75,11 @@ describe('chartist component', function(): void {

instance.type = 'Line';

spyOn(instance, 'renderChart').and.callThrough();
spyOn(<any>instance, 'renderChart').and.callThrough();

instance.update(changes);
instance['update'](changes);

expect(instance.renderChart).toHaveBeenCalled();
expect(instance['renderChart']).toHaveBeenCalled();
});

it('should update the chart if the data changes', async function() {
Expand All @@ -95,19 +95,19 @@ describe('chartist component', function(): void {

fixture.detectChanges();

await instance.renderChart();
await instance['renderChart']();

instance.data = data.Line;
instance.type = 'Line';

spyOn(instance.chart, 'update').and.callThrough();
spyOn(instance, 'renderChart').and.callThrough();
spyOn(<any>instance, 'renderChart').and.callThrough();

fixture.detectChanges();

instance.update(changes);
instance['update'](changes);

expect(instance.renderChart).not.toHaveBeenCalled();
expect(instance['renderChart']).not.toHaveBeenCalled();
expect(instance.chart.update).toHaveBeenCalled();
});

Expand All @@ -123,19 +123,19 @@ describe('chartist component', function(): void {

fixture.detectChanges();

await instance.renderChart();
await instance['renderChart']();

instance.data = data.Bar;
instance.type = 'Bar';

spyOn(instance.chart, 'update').and.callThrough();
spyOn(instance, 'renderChart').and.callThrough();
spyOn(<any>instance, 'renderChart').and.callThrough();

fixture.detectChanges();

instance.update(changes);
instance['update'](changes);

expect(instance.renderChart).not.toHaveBeenCalled();
expect(instance['renderChart']).not.toHaveBeenCalled();
expect(instance.chart.update).toHaveBeenCalled();
});

Expand Down
24 changes: 13 additions & 11 deletions projects/ng-chartist/src/lib/chartist.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,25 @@ export interface ChartEvent {
})
export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
@Input()
public data: Promise<Chartist.IChartistData> | Chartist.IChartistData;
data: Promise<Chartist.IChartistData> | Chartist.IChartistData;

@Input() public type: Promise<ChartType> | ChartType;
@Input()
type: Promise<ChartType> | ChartType;

@Input()
public options: Promise<Chartist.IChartOptions> | Chartist.IChartOptions;
options: Promise<Chartist.IChartOptions> | Chartist.IChartOptions;

@Input()
public responsiveOptions: Promise<ResponsiveOptions> | ResponsiveOptions;
responsiveOptions: Promise<ResponsiveOptions> | ResponsiveOptions;

@Input() public events: ChartEvent;
@Input()
events: ChartEvent;

public chart: ChartInterfaces;

constructor(private elementRef: ElementRef) {}

public ngOnInit(): Promise<ChartInterfaces> {
ngOnInit(): Promise<ChartInterfaces> {
if (!this.type || !this.data) {
Promise.reject('Expected at least type and data.');
}
Expand All @@ -72,17 +74,17 @@ export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
});
}

public ngOnChanges(changes: SimpleChanges): void {
ngOnChanges(changes: SimpleChanges): void {
this.update(changes);
}

public ngOnDestroy(): void {
ngOnDestroy(): void {
if (this.chart) {
this.chart.detach();
}
}

public renderChart(): Promise<ChartInterfaces> {
private renderChart(): Promise<ChartInterfaces> {
const promises: any[] = [
this.type,
this.elementRef.nativeElement,
Expand All @@ -104,7 +106,7 @@ export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
});
}

public update(changes: SimpleChanges): void {
private update(changes: SimpleChanges): void {
if (!this.chart || 'type' in changes) {
this.renderChart();
} else {
Expand All @@ -120,7 +122,7 @@ export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
}
}

public bindEvents(chart: any): void {
private bindEvents(chart: any): void {
for (const event of Object.keys(this.events)) {
chart.on(event, this.events[event]);
}
Expand Down

0 comments on commit e5a3221

Please sign in to comment.