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

Add CHART.JS v4 compatibility #165

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ module.exports = {
}],
],
chainWebpack: (config) => {
config.module
.rule('chart.js')
.include.add(path.resolve('node_modules/chart.js')).end()
.use('babel-loader')
.loader('babel-loader')
.options({
presets: ['@babel/preset-env']
})
.end();
config.merge({
resolve: {
alias: {
Expand All @@ -27,7 +36,7 @@ module.exports = {
}
}
});
},
},
themeConfig: {
repo: 'kurkle/chartjs-chart-treemap',
logo: '/favicon.ico',
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = function(karma) {
files: [
{pattern: './test/fixtures/**/*.js', included: false},
{pattern: './test/fixtures/**/*.png', included: false},
'node_modules/chart.js/dist/chart.js',
'node_modules/chart.js/dist/chart.umd.js',
'node_modules/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.js',
{pattern: 'test/index.js', watched: false},
{pattern: 'src/index.js', watched: false},
Expand Down
829 changes: 361 additions & 468 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@
"url": "https://github.com/kurkle/chartjs-chart-treemap/issues"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^23.0.0",
"@rollup/plugin-json": "^5.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-commonjs": "^23.0.2",
"@rollup/plugin-json": "^5.0.1",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.1.0",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"chart.js": "^3.8.0",
"chartjs-adapter-date-fns": "^2.0.0",
"chart.js": "^4.0.1",
"chartjs-plugin-datalabels": "^2.0.0",
"chartjs-plugin-zoom": "^1.2.0",
"chartjs-test-utils": "^0.5.0",
"concurrently": "^7.4.0",
"cross-env": "^7.0.3",
"date-fns": "^2.20.2",
"eslint": "^8.3.0",
"eslint-config-chartjs": "^0.3.0",
"eslint-plugin-es": "^4.1.0",
Expand All @@ -71,17 +70,16 @@
"karma-summary-reporter": "^3.0.0",
"ng-hammerjs": "^2.0.8",
"pixelmatch": "^5.2.1",
"rollup": "^2.79.1",
"rollup": "^3.3.0",
"rollup-plugin-istanbul": "^4.0.0",
"rollup-plugin-analyzer": "^4.0.0",
"rollup-plugin-istanbul": "^3.0.0",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^4.3.5",
"vuepress": "^1.8.2",
"typescript": "^4.7.4",
"vuepress": "^1.9.7",
"vuepress-plugin-flexsearch": "^0.3.0",
"vuepress-plugin-redirect": "^1.2.5",
"vuepress-theme-chartjs": "^0.2.0"
},
"peerDependencies": {
"chart.js": "^3.0.0"
"chart.js": ">=3.0.0"
}
}
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const resolve = require('@rollup/plugin-node-resolve').default;
const terser = require('rollup-plugin-terser').terser;
const terser = require('@rollup/plugin-terser').default;
const json = require('@rollup/plugin-json');
const pkg = require('./package.json');

Expand Down
2 changes: 1 addition & 1 deletion src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ TreemapController.overrides = {
};

TreemapController.beforeRegister = function() {
requireVersion('3.8', Chart.version);
requireVersion('chart.js', '3.8', Chart.version);
};

TreemapController.afterRegister = function() {
Expand Down
27 changes: 24 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {isObject} from 'chart.js/helpers';

const isOlderPart = (act, req) => req > act || (act.length > req.length && act.slice(0, req.length) === req);

export const getGroupKey = (lvl) => '' + lvl;

function scanTreeObject(key, treeLeafKey, obj, tree = [], lvl = 0, result = []) {
Expand Down Expand Up @@ -168,9 +170,28 @@ export function sum(values, key) {
return s;
}

export function requireVersion(min, ver) {
/**
* @param {string} pkg
* @param {string} min
* @param {string} ver
* @param {boolean} [strict=true]
* @returns {boolean}
*/
export function requireVersion(pkg, min, ver, strict = true) {
const parts = ver.split('.');
if (!min.split('.').reduce((a, c, i) => a && c <= parts[i], true)) {
throw new Error(`Chart.js v${ver} is not supported. v${min} or newer is required.`);
let i = 0;
for (const req of min.split('.')) {
const act = parts[i++];
if (parseInt(req, 10) < parseInt(act, 10)) {
break;
}
if (isOlderPart(act, req)) {
if (strict) {
throw new Error(`${pkg} v${ver} is not supported. v${min} or newer is required.`);
} else {
return false;
}
}
}
return true;
}
2 changes: 1 addition & 1 deletion test/fixtures/basic/labelsMultilineFonts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
tolerance: 0.0370,
tolerance: 0.0400,
config: {
type: 'treemap',
data: {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/events/hoverCaptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const data = [
];

export default {
tolerance: 0.0040,
tolerance: 0.0045,
config: {
type: 'treemap',
data: {
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ window.triggerMouseEvent = triggerMouseEvent;

jasmine.fixtures = specsFromFixtures;

beforeAll(() => {
// Disable colors plugin for tests.
window.Chart.defaults.plugins.colors.enabled = false;
});

beforeEach(function() {
addMatchers();
});

afterEach(function() {
releaseCharts();
});

console.warn('Testing with chart.js v' + Chart.version);
4 changes: 3 additions & 1 deletion types/index.esm.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
Chart,
ChartComponent,
CoreChartOptions,
DatasetController,
Element, VisualElement,
ScriptableContext, Color, Scriptable, FontSpec
} from 'chart.js';
import { AnyObject } from 'chart.js/types/basic';

type AnyObject = Record<string, unknown>;

type TreemapScriptableContext = ScriptableContext<'treemap'> & {
raw: TreemapDataPoint
Expand Down
3 changes: 2 additions & 1 deletion types/tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"target": "ES6",
"moduleResolution": "Node",
"alwaysStrict": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true
},
"include": [
"../index.esm.d.ts",
"./**/*.ts",
"./**/*.ts"
]
}