Skip to content

Commit 4858567

Browse files
authored
Merge pull request #7 from kurkle/vuepress-samples
Add derived-chart-type
2 parents 4cca569 + 78909fd commit 4858567

File tree

6 files changed

+92
-2
lines changed

6 files changed

+92
-2
lines changed

docs/.vuepress/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
'ga': 'UA-28909194-3'
2020
}
2121
],
22+
/* COMMENTED OUT FOR SAMPLES DEV, BECAUSE KEEPS CRASHING ON HOT RELOAD
2223
[
2324
'vuepress-plugin-typedoc',
2425
@@ -31,7 +32,7 @@ module.exports = {
3132
parentCategory: 'API',
3233
},
3334
},
34-
],
35+
],*/
3536
],
3637
chainWebpack(config) {
3738
config.merge({
@@ -107,6 +108,7 @@ module.exports = {
107108
title: 'Advanced',
108109
children: [
109110
'advanced/derived-axis-type',
111+
'advanced/derived-chart-type',
110112
]
111113
},
112114
{

docs/samples/advanced/derived-axis-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Derived Axis Type - Log2
1+
# Derived Axis Type
22

33
```js chart-editor
44
// <block:setup:1>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Derived Chart Type
2+
3+
```js chart-editor
4+
// <block:setup:1>
5+
const DATA_COUNT = 7;
6+
const NUMBER_CFG = {count: DATA_COUNT, min: -100, max: 100, rmin: 1, rmax: 20};
7+
const data = {
8+
datasets: [
9+
{
10+
label: 'My First dataset',
11+
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.blue, 0.5),
12+
borderColor: Utils.CHART_COLORS.blue,
13+
borderWidth: 1,
14+
boxStrokeStyle: 'red',
15+
data: Utils.bubbles(NUMBER_CFG)
16+
}
17+
],
18+
};
19+
// </block:setup>
20+
21+
// <block:config:0>
22+
const config = {
23+
type: 'derivedBubble',
24+
data: data,
25+
options: {
26+
responsive: true,
27+
plugins: {
28+
title: {
29+
display: true,
30+
text: 'Derived Chart Type'
31+
},
32+
}
33+
}
34+
};
35+
36+
// </block:config>
37+
38+
module.exports = {
39+
actions: [],
40+
config: config,
41+
};
42+
```
43+
44+
## DerivedBubble Implementation
45+
46+
<<< @/docs/scripts/derived-bubble.js

docs/scripts/derived-bubble.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {Chart, BubbleController} from 'chart.js';
2+
3+
class Custom extends BubbleController {
4+
draw() {
5+
// Call bubble controller method to draw all the points
6+
super.draw(arguments);
7+
8+
// Now we can do some custom drawing for this dataset.
9+
// Here we'll draw a box around the first point in each dataset,
10+
// using `boxStrokeStyle` dataset option for color
11+
var meta = this.getMeta();
12+
var pt0 = meta.data[0];
13+
14+
const {x, y} = pt0.getProps(['x', 'y']);
15+
const {radius} = pt0.options;
16+
17+
var ctx = this.chart.ctx;
18+
ctx.save();
19+
ctx.strokeStyle = this.options.boxStrokeStyle;
20+
ctx.lineWidth = 1;
21+
ctx.strokeRect(x - radius, y - radius, 2 * radius, 2 * radius);
22+
ctx.restore();
23+
}
24+
}
25+
Custom.id = 'derivedBubble';
26+
Custom.defaults = {
27+
// Custom defaults. Bubble defaults are inherited.
28+
boxStrokeStyle: 'red'
29+
};
30+
// Overrides are only inherited, but not merged if defined
31+
// Custom.overrides = Chart.overrides.bubble;
32+
33+
// Stores the controller so that the chart initialization routine can look it up
34+
Chart.register(Custom);

docs/scripts/register.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Chart, registerables} from '../../dist/chart.esm';
22
import Log2Axis from './log2';
3+
import './derived-bubble';
34

45
Chart.register(...registerables);
56
Chart.register(Log2Axis);

docs/scripts/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export function points(config) {
4545
return xs.map((x, i) => ({x, y: ys[i]}));
4646
}
4747

48+
export function bubbles(config) {
49+
return this.points(config).map(pt => {
50+
pt.r = this.rand(config.rmin, config.rmax);
51+
return pt;
52+
});
53+
}
54+
4855
export function labels(config) {
4956
var cfg = config || {};
5057
var min = cfg.min || 0;

0 commit comments

Comments
 (0)