Skip to content

Commit

Permalink
helpers chore
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jan 16, 2022
1 parent cc63518 commit 280c591
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
40 changes: 39 additions & 1 deletion src/helpers/helpers.chart.js
Expand Up @@ -2,11 +2,30 @@ import {isFinite} from 'chart.js/helpers';
import {getRectCenterPoint} from './helpers.geometric';
import {isBoundToPoint} from './helpers.options';

/**
* @typedef { import("chart.js").Chart } Chart
* @typedef { import("chart.js").Scale } Scale
* @typedef { import("chart.js").Point } Point
* @typedef { import('../../types/options').CoreAnnotationOptions } CoreAnnotationOptions
* @typedef { import('../../types/options').PointAnnotationOptions } PointAnnotationOptions
*/

/**
* @param {Scale} scale
* @param {number|string} value
* @param {number} fallback
* @returns {number}
*/
export function scaleValue(scale, value, fallback) {
value = typeof value === 'number' ? value : scale.parse(value);
return isFinite(value) ? scale.getPixelForValue(value) : fallback;
}

/**
* @param {Scale} scale
* @param {{start: number, end: number}} options
* @returns {{start: number, end: number}}
*/
function getChartDimensionByScale(scale, options) {
if (scale) {
const min = scaleValue(scale, options.min, options.start);
Expand All @@ -22,6 +41,11 @@ function getChartDimensionByScale(scale, options) {
};
}

/**
* @param {Chart} chart
* @param {CoreAnnotationOptions} options
* @returns {Point}
*/
export function getChartPoint(chart, options) {
const {chartArea, scales} = chart;
const xScale = scales[options.xScaleID];
Expand All @@ -39,13 +63,18 @@ export function getChartPoint(chart, options) {
return {x, y};
}

/**
* @param {Chart} chart
* @param {CoreAnnotationOptions} options
* @returns {{x?:number, y?: number, x2?: number, y2?: number, width?: number, height?: number}}
*/
export function getChartRect(chart, options) {
const xScale = chart.scales[options.xScaleID];
const yScale = chart.scales[options.yScaleID];
let {top: y, left: x, bottom: y2, right: x2} = chart.chartArea;

if (!xScale && !yScale) {
return {options: {}};
return {};
}

const xDim = getChartDimensionByScale(xScale, {min: options.xMin, max: options.xMax, start: x, end: x2});
Expand All @@ -65,6 +94,10 @@ export function getChartRect(chart, options) {
};
}

/**
* @param {Chart} chart
* @param {PointAnnotationOptions} options
*/
export function getChartCircle(chart, options) {
const point = getChartPoint(chart, options);
return {
Expand All @@ -75,6 +108,11 @@ export function getChartCircle(chart, options) {
};
}

/**
* @param {Chart} chart
* @param {PointAnnotationOptions} options
* @returns
*/
export function resolvePointPosition(chart, options) {
if (!isBoundToPoint(options)) {
const box = getChartRect(chart, options);
Expand Down
14 changes: 11 additions & 3 deletions src/helpers/helpers.geometric.js
@@ -1,3 +1,11 @@
/**
* @typedef {import('chart.js').Point} Point
*/

/**
* @param {{x: number, y: number, width: number, height: number}} rect
* @returns {Point}
*/
export function getRectCenterPoint(rect) {
const {x, y, width, height} = rect;
return {
Expand All @@ -8,10 +16,10 @@ export function getRectCenterPoint(rect) {

/**
* Rotate a `point` relative to `center` point by `angle`
* @param {{x: number, y: number}} point - the point to rotate
* @param {{x: number, y: number}} center - center point for rotation
* @param {Point} point - the point to rotate
* @param {Point} center - center point for rotation
* @param {number} angle - angle for rotation, in radians
* @returns {{x: number, y: number}} rotated point
* @returns {Point} rotated point
*/
export function rotated(point, center, angle) {
var cos = Math.cos(angle);
Expand Down

0 comments on commit 280c591

Please sign in to comment.