Skip to content

Commit 3b106e1

Browse files
authored
Add sample on time scale (#462)
1 parent 1e9c32d commit 3b106e1

File tree

7 files changed

+146
-156
lines changed

7 files changed

+146
-156
lines changed

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module.exports = {
6868
'over-scale-mode',
6969
'bar',
7070
'log',
71+
'time',
7172
],
7273
}
7374
}

docs/samples/time.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Time Scale
2+
3+
```js chart-editor
4+
// <block:data:1>
5+
const NUMBER_CFG = {count: 500, min: 0, max: 1000};
6+
const data = {
7+
datasets: [{
8+
label: 'My First dataset',
9+
borderColor: Utils.randomColor(0.4),
10+
backgroundColor: Utils.randomColor(0.1),
11+
pointBorderColor: Utils.randomColor(0.7),
12+
pointBackgroundColor: Utils.randomColor(0.5),
13+
pointBorderWidth: 1,
14+
data: Utils.hourlyPoints(NUMBER_CFG),
15+
}]
16+
};
17+
// </block:data>
18+
19+
// <block:scales:2>
20+
const scales = {
21+
x: {
22+
position: 'bottom',
23+
type: 'time',
24+
ticks: {
25+
autoSkip: true,
26+
autoSkipPadding: 50,
27+
maxRotation: 0
28+
},
29+
time: {
30+
displayFormats: {
31+
hour: 'HH:mm',
32+
minute: 'HH:mm',
33+
second: 'HH:mm:ss'
34+
}
35+
}
36+
},
37+
y: {
38+
position: 'right',
39+
ticks: {
40+
callback: (val, index, ticks) => index === 0 || index === ticks.length - 1 ? null : val,
41+
},
42+
grid: {
43+
borderColor: Utils.randomColor(1),
44+
color: 'rgba( 0, 0, 0, 0.1)',
45+
},
46+
title: {
47+
display: true,
48+
text: (ctx) => ctx.scale.axis + ' axis',
49+
}
50+
},
51+
};
52+
// </block:scales>
53+
54+
// <block:zoom:0>
55+
const zoomOptions = {
56+
zoom: {
57+
enabled: true,
58+
mode: 'xy',
59+
},
60+
pan: {
61+
enabled: true,
62+
mode: 'xy',
63+
}
64+
};
65+
// </block>
66+
67+
const panStatus = () => zoomOptions.pan.enabled ? 'enabled' : 'disabled';
68+
const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';
69+
70+
// <block:config:1>
71+
const config = {
72+
type: 'scatter',
73+
data: data,
74+
options: {
75+
scales: scales,
76+
plugins: {
77+
zoom: zoomOptions,
78+
title: {
79+
display: true,
80+
position: 'bottom',
81+
text: (ctx) => 'Zoom: ' + zoomStatus() + ', Pan: ' + panStatus()
82+
}
83+
},
84+
onClick(e) {
85+
console.log(e.type);
86+
}
87+
}
88+
};
89+
// </block:config>
90+
91+
const actions = [
92+
{
93+
name: 'Toggle zoom',
94+
handler(chart) {
95+
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
96+
chart.update();
97+
}
98+
}, {
99+
name: 'Toggle pan',
100+
handler(chart) {
101+
zoomOptions.pan.enabled = !zoomOptions.pan.enabled;
102+
chart.update();
103+
},
104+
}, {
105+
name: 'Reset zoom',
106+
handler(chart) {
107+
chart.resetZoom();
108+
}
109+
}
110+
];
111+
112+
module.exports = {
113+
actions,
114+
config,
115+
output: 'Clicks are logged here'
116+
};
117+
```

docs/scripts/register.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Chart from 'chart.js/auto';
2+
import 'chartjs-adapter-date-fns';
23
import zoomPlugin from '../../dist/chartjs-plugin-zoom.esm.js';
34

45
Chart.register(zoomPlugin);

docs/scripts/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {valueOrDefault} from 'chart.js/helpers';
2+
import {addHours} from 'date-fns';
23

34
// Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
45
let _seed = Date.now();
@@ -79,3 +80,9 @@ export function months(config) {
7980

8081
return values;
8182
}
83+
84+
export function hourlyPoints(config) {
85+
const ys = this.numbers(config);
86+
const start = new Date().valueOf();
87+
return ys.map((y, i) => ({x: addHours(start, i), y}));
88+
}

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@
3636
"@typescript-eslint/eslint-plugin": "^4.22.0",
3737
"@typescript-eslint/parser": "^4.22.0",
3838
"chart.js": "^3.1.0",
39+
"chartjs-adapter-date-fns": "^2.0.0",
3940
"chartjs-test-utils": "^0.2.2",
4041
"concurrently": "^6.0.2",
4142
"coveralls": "^3.1.0",
4243
"cross-env": "^7.0.3",
44+
"date-fns": "^2.21.1",
4345
"eslint": "^7.24.0",
4446
"eslint-config-chartjs": "^0.3.0",
4547
"eslint-plugin-es": "^4.1.0",

samples/zoom-time.html

Lines changed: 0 additions & 156 deletions
This file was deleted.

0 commit comments

Comments
 (0)