Added moving average computation
This commit is contained in:
@@ -17,16 +17,17 @@ Lightweight, canvas‑based charting library written in TypeScript, designed for
|
|||||||
The `createChartElement(seriesList, options)` function supports the following options:
|
The `createChartElement(seriesList, options)` function supports the following options:
|
||||||
|
|
||||||
| Option | Type | Description |
|
| Option | Type | Description |
|
||||||
|------------------|-------------------------------------|-----------------------------------------------------------------------------|
|
|-------------------|--------------------------|----------------------------------------------------------------------------|
|
||||||
| `width` | `number` | Fixed chart width in pixels (optional, defaults to container width) |
|
| `width` | `number` | Fixed chart width in pixels (optional, defaults to container width) |
|
||||||
| `height` | `number` | Fixed chart height in pixels (optional, defaults to container height) |
|
| `height` | `number` | Fixed chart height in pixels (optional, defaults to container height) |
|
||||||
| `min` | `number` | Minimum Y-axis value (optional, auto-computed if not set) |
|
| `min` | `number` | Minimum Y-axis value (optional, auto-computed if not set) |
|
||||||
| `max` | `number` | Maximum Y-axis value (optional, auto-computed if not set) |
|
| `max` | `number` | Maximum Y-axis value (optional, auto-computed if not set) |
|
||||||
| `showYAxis` | `boolean` | Whether to render Y-axis ticks and labels |
|
| `showYAxis` | `boolean` | Whether to render Y-axis ticks and labels |
|
||||||
| `yTicks` | `number` | Number of Y-axis ticks (default: `5`) |
|
| `yTicks` | `number` | Number of Y-axis ticks (default: `5`) |
|
||||||
| `backgroundColor`| `string` | Optional background fill color |
|
| `backgroundColor` | `string` | Optional background fill color |
|
||||||
| `maxTimeDelta` | `number \| null` | Gap in ms that breaks line continuity (default: `600`) |
|
| `maxTimeDelta` | `number \| null` | Gap in ms that breaks line continuity (default: `600`) |
|
||||||
| `dateFormat` | `(ts: number) => string` | Custom formatter for the cursor timestamp label |
|
| `dateFormat` | `(ts: number) => string` | Custom formatter for the cursor timestamp label |
|
||||||
|
| `averageData` | `number` | Compute simple moving average of lasy n data to smooth out line (optional) |
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
// Example: format timestamp to full Czech date+time
|
// Example: format timestamp to full Czech date+time
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
"version": "1.0.9",
|
"version": "1.0.10",
|
||||||
"description": "Lightweight charting library for the browser and Node.js",
|
"description": "Lightweight charting library for the browser and Node.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
18
src/index.ts
18
src/index.ts
@@ -3,6 +3,21 @@ export type ChartSeries = {
|
|||||||
strokeColor?: string;
|
strokeColor?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function averageData(data: [number, number][], windowSize: number = 5): [number, number][] {
|
||||||
|
if (data.length < windowSize) return data;
|
||||||
|
const result: [number, number][] = [];
|
||||||
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
let sum = 0;
|
||||||
|
let count = 0;
|
||||||
|
for (let j = Math.max(0, i - Math.floor(windowSize / 2)); j <= Math.min(data.length - 1, i + Math.floor(windowSize / 2)); j++) {
|
||||||
|
sum += data[j][1];
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
result.push([data[i][0], sum / count]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
export function drawChart(
|
export function drawChart(
|
||||||
ctx: CanvasRenderingContext2D,
|
ctx: CanvasRenderingContext2D,
|
||||||
seriesList: ChartSeries[],
|
seriesList: ChartSeries[],
|
||||||
@@ -17,6 +32,7 @@ export function drawChart(
|
|||||||
cursorX?: number | null;
|
cursorX?: number | null;
|
||||||
maxTimeDelta?: number | null;
|
maxTimeDelta?: number | null;
|
||||||
dateFormat?: (ts: number) => string;
|
dateFormat?: (ts: number) => string;
|
||||||
|
averageData?:number | null;
|
||||||
}
|
}
|
||||||
): void {
|
): void {
|
||||||
const {
|
const {
|
||||||
@@ -62,7 +78,7 @@ export function drawChart(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const series of seriesList) {
|
for (const series of seriesList) {
|
||||||
const data = series.data;
|
const data = averageData(series.data, opts.averageData ?? 1);
|
||||||
const color = series.strokeColor || '#000';
|
const color = series.strokeColor || '#000';
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
ctx.strokeStyle = color;
|
ctx.strokeStyle = color;
|
||||||
|
|||||||
Reference in New Issue
Block a user