mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
`karma.conf.ci.js` has been merged into `karma.conf.js` for local testing consistency: `gulp unittestWatch` has been replaced by `gulp unittest --watch` and thus use exactly the same config file. Upgrade to latest jasmine and karma packages and remove deprecated `gulp-karma` dependency (directly use `karma.Server` in gulp). Split `test/mockContext.js` into smaller `test/jasmine.*` modules to make easier unit tests maintenance and finally, move all `*.test.js` files under the `test/specs` folder.
77 lines
2.3 KiB
JavaScript
77 lines
2.3 KiB
JavaScript
/**
|
|
* Injects a new canvas (and div wrapper) and creates teh associated Chart instance
|
|
* using the given config. Additional options allow tweaking elements generation.
|
|
* @param {object} config - Chart config.
|
|
* @param {object} options - Chart acquisition options.
|
|
* @param {object} options.canvas - Canvas attributes.
|
|
* @param {object} options.wrapper - Canvas wrapper attributes.
|
|
* @param {boolean} options.persistent - If true, the chart will not be released after the spec.
|
|
*/
|
|
function acquireChart(config, options) {
|
|
var wrapper = document.createElement('div');
|
|
var canvas = document.createElement('canvas');
|
|
var chart, key;
|
|
|
|
config = config || {};
|
|
options = options || {};
|
|
options.canvas = options.canvas || {height: 512, width: 512};
|
|
options.wrapper = options.wrapper || {class: 'chartjs-wrapper'};
|
|
|
|
for (key in options.canvas) {
|
|
if (options.canvas.hasOwnProperty(key)) {
|
|
canvas.setAttribute(key, options.canvas[key]);
|
|
}
|
|
}
|
|
|
|
for (key in options.wrapper) {
|
|
if (options.wrapper.hasOwnProperty(key)) {
|
|
wrapper.setAttribute(key, options.wrapper[key]);
|
|
}
|
|
}
|
|
|
|
// by default, remove chart animation and auto resize
|
|
config.options = config.options || {};
|
|
config.options.animation = config.options.animation === undefined? false : config.options.animation;
|
|
config.options.responsive = config.options.responsive === undefined? false : config.options.responsive;
|
|
config.options.defaultFontFamily = config.options.defaultFontFamily || 'Arial';
|
|
|
|
wrapper.appendChild(canvas);
|
|
window.document.body.appendChild(wrapper);
|
|
|
|
chart = new Chart(canvas.getContext('2d'), config);
|
|
chart.$test = {
|
|
persistent: options.persistent,
|
|
wrapper: wrapper
|
|
};
|
|
|
|
return chart;
|
|
}
|
|
|
|
function releaseChart(chart) {
|
|
chart.destroy();
|
|
|
|
var wrapper = (chart.$test || {}).wrapper;
|
|
if (wrapper && wrapper.parentNode) {
|
|
wrapper.parentNode.removeChild(wrapper);
|
|
}
|
|
}
|
|
|
|
function injectCSS(css) {
|
|
// http://stackoverflow.com/q/3922139
|
|
var head = document.getElementsByTagName('head')[0];
|
|
var style = document.createElement('style');
|
|
style.setAttribute('type', 'text/css');
|
|
if (style.styleSheet) { // IE
|
|
style.styleSheet.cssText = css;
|
|
} else {
|
|
style.appendChild(document.createTextNode(css));
|
|
}
|
|
head.appendChild(style);
|
|
}
|
|
|
|
module.exports = {
|
|
injectCSS: injectCSS,
|
|
acquireChart: acquireChart,
|
|
releaseChart: releaseChart
|
|
};
|