Skip to content

Commit 3f3dcc7

Browse files
committed
Add docs that can also be used for testing
1 parent 1eb646d commit 3f3dcc7

16 files changed

+11933
-1802
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/node_modules
22
!.eslintrc.js
3+
!.vuepress

README.md

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,12 @@
22

33
VuePress theme for projects in the Chart.js organization.
44

5-
## Install
5+
## Documentation
66

7-
```sh
8-
npm install -D vuepress-theme-chartjs
9-
```
10-
11-
## Usage
12-
13-
Minimal config in `.vuepress/config.js` to enable the theme:
14-
15-
```js
16-
module.exports = {
17-
theme: 'chartjs',
18-
};
19-
```
20-
21-
## Configuration
22-
23-
This theme is based on the VuePress default theme and support the [same configuration](https://vuepress.vuejs.org/theme/default-theme-config.html).
24-
25-
Additionally, the following options are available:
26-
27-
```js
28-
// docs/.vuepress/config.js
29-
30-
module.export = {
31-
themeConfig: {
32-
// Chart views options.
33-
chart: {
34-
// Files to import (and optionally expose) when evaluating code in a
35-
// ```chart-editor fenced code block. The first value is the path to
36-
// the file to import, relative to the docs folder. The second value
37-
// allows to expose the file exports as a global variable with the
38-
// given name. If omitted, the import is used for side-effects.
39-
imports: [
40-
['foo.js'], // Side-effect (e.g. register plugins).
41-
['bar.js', 'Bar'], // Bar will contains the bar.js exports.
42-
]
43-
},
44-
}
45-
}
46-
47-
```
7+
- [Introduction](https://vuepress-theme-chartjs.netlify.app/)
8+
- [Getting Started](https://vuepress-theme-chartjs.netlify.app/getting-started.html)
9+
- [Configuration](https://vuepress-theme-chartjs.netlify.app/configuration.html)
10+
- [Chart Editor](https://vuepress-theme-chartjs.netlify.app/chart-editor-simple.html)
4811

4912
## License
5013

docs/.imports/register.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Chart, defaults, registerables} from 'chart.js';
2+
3+
Chart.register(...registerables);
4+
5+
defaults.set('plugins', {
6+
legend: {
7+
display: false,
8+
},
9+
title: {
10+
display: false,
11+
},
12+
tooltip: {
13+
enabled: false,
14+
},
15+
});

docs/.imports/utils.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
function fallback(/* values ... */) {
2+
const ilen = arguments.length;
3+
for (let i = 0; i < ilen; ++i) {
4+
const v = arguments[i];
5+
if (v !== undefined) {
6+
return v;
7+
}
8+
}
9+
}
10+
11+
const Color = typeof window !== 'undefined' ? window.Color : {};
12+
13+
export const COLORS = [
14+
'#FF3784',
15+
'#36A2EB',
16+
'#4BC0C0',
17+
'#F77825',
18+
'#9966FF',
19+
'#00A8C6',
20+
'#379F7A',
21+
'#CC2738',
22+
'#8B628A',
23+
'#8FBE00',
24+
'#606060',
25+
];
26+
27+
// Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
28+
let _seed = Date.now();
29+
30+
export function srand(seed) {
31+
_seed = seed;
32+
}
33+
34+
export function rand(min, max) {
35+
min = min === undefined ? 0 : min;
36+
max = max === undefined ? 1 : max;
37+
_seed = (_seed * 9301 + 49297) % 233280;
38+
return min + (_seed / 233280) * (max - min);
39+
}
40+
41+
export function numbers(config) {
42+
const cfg = config || {};
43+
const min = fallback(cfg.min, 0);
44+
const max = fallback(cfg.max, 1);
45+
const from = fallback(cfg.from, []);
46+
const count = fallback(cfg.count, 8);
47+
const decimals = fallback(cfg.decimals, 8);
48+
const continuity = fallback(cfg.continuity, 1);
49+
const dfactor = Math.pow(10, decimals) || 0;
50+
const data = [];
51+
52+
for (let i = 0; i < count; ++i) {
53+
const value = (from[i] || 0) + this.rand(min, max);
54+
if (this.rand() <= continuity) {
55+
data.push(Math.round(dfactor * value) / dfactor);
56+
} else {
57+
data.push(null);
58+
}
59+
}
60+
61+
return data;
62+
}
63+
64+
export function color(offset) {
65+
const count = COLORS.length;
66+
const index = offset === undefined ? ~~rand(0, count) : offset;
67+
return COLORS[index % count];
68+
}
69+
70+
export function colors(config) {
71+
const cfg = config || {};
72+
const c = cfg.color || color(0);
73+
const count = cfg.count !== undefined ? cfg.count : 8;
74+
const method = cfg.mode ? Color.prototype[cfg.mode] : null;
75+
const values = [];
76+
77+
for (let i = 0; i < count; ++i) {
78+
const f = i / count;
79+
const v = method ?
80+
method.call(Color(c), f).rgbString() :
81+
color(i);
82+
83+
values.push(v);
84+
}
85+
86+
return values;
87+
}
88+
89+
export function transparentize(c, opacity) {
90+
const alpha = opacity === undefined ? 0.5 : 1 - opacity;
91+
return Color(c).alpha(alpha).rgbString();
92+
}

docs/.vuepress/config.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = {
2+
theme: '../',
3+
dest: 'dist/docs',
4+
title: 'vuepress-theme-chartjs',
5+
description: 'VuePress theme for projects in the Chart.js organization',
6+
head: [
7+
['link', {rel: 'icon', href: '/favicon.png'}],
8+
],
9+
themeConfig: {
10+
repo: 'simonbrunel/vuepress-theme-chartjs',
11+
logo: '/favicon.png',
12+
lastUpdated: 'Last Updated',
13+
editLinks: true,
14+
docsDir: 'docs',
15+
chart: {
16+
imports: [
17+
['.imports/register.js'],
18+
['.imports/utils.js', 'Utils'],
19+
],
20+
},
21+
sidebar: [
22+
'',
23+
'getting-started',
24+
'configuration',
25+
{
26+
title: 'Chart Editor',
27+
sidebarDepth: 0,
28+
children: [
29+
'chart-editor-simple',
30+
'chart-editor-blocks',
31+
'chart-editor-output',
32+
'chart-editor-actions',
33+
],
34+
},
35+
],
36+
},
37+
};

docs/.vuepress/public/favicon.ico

32.2 KB
Binary file not shown.

docs/.vuepress/public/favicon.png

10.1 KB
Loading

docs/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Introduction
2+
3+
VuePress theme for projects in the Chart.js organization.
4+
5+
## Table of Contents
6+
7+
- [Getting Started](getting-started.md)
8+
- [Configuration](configuration.md)
9+
- [Chart Editor](chart-editor-simple.md)
10+
11+
## License
12+
13+
`vuepress-theme-chartjs` is available under the [MIT license](https://github.com/simonbrunel/vuepress-theme-chartjs/blob/master/LICENSE.md).

docs/chart-editor-actions.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Actions
3+
---
4+
5+
# Chart Editor Actions
6+
7+
Actions allow users to manually trigger custom logic.
8+
9+
## Usage
10+
11+
````md
12+
```js
13+
module.exports = {
14+
actions: [
15+
{
16+
name: 'Action Name',
17+
handler(chart) {
18+
// Do something with chart...
19+
}
20+
}
21+
]
22+
}
23+
```
24+
````
25+
26+
## Demo
27+
28+
```js chart-editor
29+
// <block:init:1>
30+
const DATA_COUNT = 10;
31+
32+
Utils.srand(2);
33+
// </block:init>
34+
35+
const config = /* <block:config:0> */{
36+
type: 'line',
37+
data: {
38+
labels: new Array(DATA_COUNT).fill(0).map((_, i) => i),
39+
datasets: [
40+
{
41+
data: Utils.numbers({
42+
count: DATA_COUNT,
43+
max: 50,
44+
min: 0,
45+
})
46+
},
47+
{
48+
data: Utils.numbers({
49+
count: DATA_COUNT,
50+
max: 25,
51+
min: 75,
52+
})
53+
}
54+
]
55+
},
56+
options: {
57+
elements: {
58+
line: {
59+
borderColor: (ctx) => Utils.color(ctx.datasetIndex),
60+
},
61+
point: {
62+
backgroundColor: (ctx) => Utils.color(ctx.datasetIndex),
63+
}
64+
}
65+
}
66+
} /* </block:config> */;
67+
68+
const actions = /* <block:actions:2> */[
69+
{
70+
name: 'Randomize',
71+
handler: function(chart) {
72+
chart.data.datasets.forEach(function(dataset, i) {
73+
dataset.data = dataset.data.map(function(value) {
74+
return Utils.rand(0, 100);
75+
});
76+
});
77+
78+
chart.update();
79+
}
80+
},
81+
{
82+
name: 'Alert',
83+
handler: function(chart) {
84+
alert('Action clicked for chart ' + chart.id);
85+
}
86+
}
87+
]/* </block:actions> */;
88+
89+
module.exports = {
90+
actions,
91+
config,
92+
}
93+
```

docs/chart-editor-blocks.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: Blocks
3+
---
4+
5+
# Chart Editor Blocks
6+
7+
Blocks provides a way to display specific parts of the code in separate tabs.
8+
9+
## Usage
10+
11+
````md
12+
```js chart-editor
13+
// <block:{name}:{order}>
14+
[ code ]
15+
// </block:{name}>
16+
```
17+
````
18+
19+
For example:
20+
21+
````md
22+
```js chart-editor
23+
// <block:config:0>
24+
const config = {
25+
foo: 'bar',
26+
};
27+
// </block:config>
28+
29+
const data = /* <block:data:1> */{
30+
foo: 'bar',
31+
}/* </block:data> */;
32+
```
33+
````
34+
35+
## Demo
36+
37+
```js chart-editor
38+
// <block:init:2>
39+
const DATA_COUNT = 42;
40+
41+
Utils.srand(8);
42+
// </block:init>
43+
44+
const config = /* <block:config:0> */{
45+
type: 'line',
46+
data: {
47+
labels: new Array(DATA_COUNT).fill(0).map((_, i) => i),
48+
datasets: [
49+
{
50+
data: Utils.numbers({
51+
count: DATA_COUNT,
52+
max: 10,
53+
min: 0,
54+
})
55+
},
56+
{
57+
data: Utils.numbers({
58+
count: DATA_COUNT,
59+
max: 20,
60+
min: 10,
61+
})
62+
}
63+
]
64+
},
65+
options: {
66+
aspectRatio: 1.5,
67+
elements: {
68+
line: {
69+
borderColor: (ctx) => Utils.color(ctx.datasetIndex),
70+
tension: 0.4,
71+
},
72+
point: {
73+
backgroundColor: (ctx) => Utils.color(ctx.datasetIndex),
74+
radius: (ctx) => (ctx.dataIndex / DATA_COUNT) * 6,
75+
}
76+
}
77+
}
78+
} /* </block:config> */;
79+
80+
module.exports = {
81+
config,
82+
}
83+
```

0 commit comments

Comments
 (0)