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

Upgrade Chart.js to version 3.9.1 #2548

Merged
merged 8 commits into from
Oct 26, 2022
2 changes: 1 addition & 1 deletion libs/designsystem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"@fontsource/roboto": "4.2.1",
"@ionic/angular": "6.2.1",
"@kirbydesign/core": "0.0.35",
"chart.js": "3.3.2",
"chart.js": "3.9.1",
"chartjs-adapter-date-fns": "^2.0.0",
"chartjs-plugin-annotation": "^1.0.2",
"chartjs-plugin-datalabels": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ElementRef } from '@angular/core';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator';
import { Chart, FontSpec } from 'chart.js';

import { chartConfigHasType } from '../../../../helpers';
import { ChartConfigService } from '../';
import { ColorHelper } from '../../../../helpers';

Expand Down Expand Up @@ -39,7 +40,9 @@ describe('ChartJSService with ChartConfigService', () => {
/* Our 'column' chart is a chart.js 'bar' type chart
with it's indexAxis set to y; therefore testing if
bar is being used. */
expect(chart.config.type).toBe('bar');
if (chartConfigHasType(chart.config)) {
expect(chart.config.type).toBe('bar');
}
});

describe('and no custom options are passed', () => {
Expand Down Expand Up @@ -118,7 +121,9 @@ describe('ChartJSService with ChartConfigService', () => {
});
const chart = chartJSService['chart'];

expect(chart.config.type).toBe('line');
if (chartConfigHasType(chart.config)) {
expect(chart.config.type).toBe('line');
}
});

describe('and no custom options are passed', () => {
Expand Down Expand Up @@ -197,7 +202,9 @@ describe('ChartJSService with ChartConfigService', () => {
});
const chart = chartJSService['chart'];

expect(chart.config.type).toBe('bar');
if (chartConfigHasType(chart.config)) {
expect(chart.config.type).toBe('bar');
}
});

describe('and no custom options are passed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Chart, ChartType as ChartJSType, ChartOptions } from 'chart.js';
import { AnnotationOptions } from 'chartjs-plugin-annotation';
import { MockProvider } from 'ng-mocks';

import { chartConfigHasType } from '../../../../helpers';
import { ChartConfigService } from '../';
import { ChartDataset, ChartHighlightedElements, ChartType } from '../../';
import { deepCopy } from '../../../../helpers/deep-copy';
Expand Down Expand Up @@ -515,14 +516,16 @@ describe('ChartJSService', () => {
});

it('should set a new type', () => {
const oldType = chart.config.type;
const newType = 'line';
expect(oldType).not.toBe(newType);
if (chartConfigHasType(chart.config)) {
const oldType = chart.config.type;
const newType = 'line';
expect(oldType).not.toBe(newType);

chartJSService['nonDestructivelyUpdateType']('line');
chartJSService['nonDestructivelyUpdateType']('line');

expect(chart.config.type).not.toBe(oldType);
expect(newType).toBe('line');
expect(chart.config.type).not.toBe(oldType);
expect(newType).toBe('line');
}
});

it('should apply config from new type', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChartConfiguration, ChartOptions } from 'chart.js';
import { AnnotationOptions } from 'chartjs-plugin-annotation';

import { mergeDeepAll } from '../../../../helpers/merge-deep';
import { chartConfigHasType } from '../../../../helpers';
import { ChartConfigService } from '../chart-config-service';
import {
ChartDataset,
Expand Down Expand Up @@ -116,7 +117,7 @@ export class ChartJSService {
? this.annotationsDelegate.createAnnotationPluginOptionsObject(annotations)
: {};

let mergedOptions: ChartOptions = mergeDeepAll(
const mergedOptions: ChartOptions = mergeDeepAll(
typeConfigOptions,
customOptions,
annotationPluginOptions
Expand Down Expand Up @@ -157,7 +158,7 @@ export class ChartJSService {
}

protected createDatasets(data: ChartDataset[] | number[]): ChartDataset[] {
let datasets = isNumberArray(data) ? [{ data }] : data;
const datasets = isNumberArray(data) ? [{ data }] : data;

if (this.highlightedElements)
this.addHighlightedElementsToDatasets(this.highlightedElements, datasets);
Expand Down Expand Up @@ -188,7 +189,10 @@ export class ChartJSService {
});

this.chart.options = options;
this.chart.config.type = this.chartConfigService.chartTypeToChartJSType(chartType);

if (chartConfigHasType(this.chart.config)) {
this.chart.config.type = this.chartConfigService.chartTypeToChartJSType(chartType);
}
}

private initializeNewChart(canvasElement: HTMLCanvasElement, config: ChartConfiguration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ElementRef } from '@angular/core';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator';
import { Chart } from 'chart.js';

import { chartConfigHasType } from '../../../helpers';
import { ChartConfigService } from '../shared';

import { StockChartJSService } from './stock-chart-js.service';
Expand Down Expand Up @@ -49,7 +50,9 @@ describe('StockChartJSService with ChartConfigService', () => {
});

it('should use correct ChartJS type', () => {
expect(chart.config.type).toBe('line');
if (chartConfigHasType(chart.config)) {
expect(chart.config.type).toBe('line');
}
});

it('should have correct tension', () => {
Expand Down
14 changes: 14 additions & 0 deletions libs/designsystem/src/lib/helpers/chart-config-has-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ChartConfiguration, ChartConfigurationCustomTypesPerDataset } from 'chart.js';

/*
Type guard is needed as of chart.js@3.8.1. The config type has been updated to a union type,
and the newly added type 'ChartConfigurationCustomTypesPerDataset' does not contain the 'type' property.
Typescript will throw an error, when trying to access a property that doesn't exist on all union types,
unless a type guard is used.
*/

export const chartConfigHasType = (
config: ChartConfiguration | ChartConfigurationCustomTypesPerDataset
): config is ChartConfiguration => {
return 'type' in config;
};
1 change: 1 addition & 0 deletions libs/designsystem/src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './design-token-helper';
export * from './string-helper';
export * from './theme-color.type';
export * from './platform.service';
export * from './chart-config-has-type';
11 changes: 7 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"@ionic/angular": "6.2.1",
"@nrwl/angular": "14.1.7",
"@types/prismjs": "1.9.0",
"chart.js": "3.3.2",
"chart.js": "3.9.1",
"chartjs-adapter-date-fns": "^2.0.0",
"chartjs-plugin-annotation": "^1.0.2",
"chartjs-plugin-datalabels": "^2.0.0",
Expand Down
1 change: 0 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"experimentalDecorators": true,
"target": "es2017",
"typeRoots": ["node_modules/@types"],
"skipLibCheck": true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

"lib": ["es2017", "dom"],
"baseUrl": ".",
"resolveJsonModule": true
Expand Down