JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="min-width: 600px; height: 400px; margin: 0 auto" align="right"></div>
<div id="container2" style="min-width: 600px; height: 400px; margin: 0 auto" align="right"></div>
TypeScript
const chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'spline'
},
series: [{
data: [29.9, 55.5, 106.4, 1129.2, 344.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
type: 'spline',
data: []
}]
});
const getLinearInterpolatedValue = (t: number, p1: number, p2: number) => (1 - t) * p1 + t * p2;
const getLinearInterpolatedSegment = (...args: number[]): number[] => {
const [t, p1, p2, ...rest] = args;
return rest.length > 0
? [getLinearInterpolatedValue(t, p1, p2), ...getLinearInterpolatedSegment(t, p2, ...rest)]
: [getLinearInterpolatedValue(t, p1, p2)];
};
// https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
const findPointOnBezierCurve = (t: number, ps: number[]): number =>
ps.length > 1 ? findPointOnBezierCurve(t, getLinearInterpolatedSegment(t, ...ps)) : ps[0];
const subdividePoints = (points: number[], years: number) => {
if (points.length === 0 || years === 0) {
return points;
}
const step = 1 / years / 12;
const newPoints = [];
for (let i = 0; i <= years * 12; i++) {
newPoints.push(findPointOnBezierCurve(step * i, points));
}
return newPoints;
};
const subdivided = new Highcharts.Chart({
chart: {
renderTo: 'container2',
type: 'spline'
},
series: [{
data: subdividePoints([29.9, 55.5, 106.4, 1129.2, 344.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4], 10)
}, {
type: 'spline',
data: []
}]
});
console.log(subdividePoints([29.9, 55.5, 106.4, 1129.2, 344.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4], 10))