Skip to content

Commit abe5d80

Browse files
author
timqian
committed
update doc for stacked bar
1 parent 875abc2 commit abe5d80

File tree

6 files changed

+72
-21
lines changed

6 files changed

+72
-21
lines changed

docs/07-stacked-bar.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Stacked bar chart
3+
---
4+
5+
A stacked bar chart provides a way of showing data values represented as vertical bars
6+
7+
<p class="codepen" data-height="429" data-theme-id="default" data-default-tab="result" data-user="timqian" data-slug-hash="KKwPrEm" style="height: 429px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="chart.xkcd stacked bar">
8+
<span>See the Pen <a href="https://codepen.io/timqian/pen/KKwPrEm">
9+
chart.xkcd stacked bar</a> by timqian (<a href="https://codepen.io/timqian">@timqian</a>)
10+
on <a href="https://codepen.io">CodePen</a>.</span>
11+
</p>
12+
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
13+
14+
## JS part
15+
16+
```js
17+
new chartXkcd.StackedBar(svg, {
18+
title: 'Issues and PR Submissions',
19+
xLabel: 'Month',
20+
yLabel: 'Count',
21+
data: {
22+
labels: ['Jan', 'Feb', 'Mar', 'April', 'May'],
23+
datasets: [{
24+
label: 'Issues',
25+
data: [12, 19, 11, 29, 17],
26+
}, {
27+
label: 'PRs',
28+
data: [3, 5, 2, 4, 1],
29+
}, {
30+
label: 'Merges',
31+
data: [2, 3, 0, 1, 1],
32+
}],
33+
},
34+
});
35+
```
36+
37+
## Customize chart by defining options
38+
39+
- `yTickCount`: customize tick numbers you want to see on the y axis
40+
- `dataColors`: array of colors for different datasets
41+
- `fontFamily`: customize font family used in the chart
42+
- `unxkcdify`: disable xkcd effect (default `false`)
43+
- `strokeColor`: stroke colors (default `black`)
44+
- `backgroundColor`: color for BG (default `white`)
45+
- `legendPosition`: specify where you want to place the legend (default `chartXkcd.config.positionType.upLeft`)
46+
Possible values:
47+
- up left: `chartXkcd.config.positionType.upLeft`
48+
- up right: `chartXkcd.config.positionType.upLeft`
49+
- bottom left: `chartXkcd.config.positionType.downLeft`
50+
- bottom right: `chartXkcd.config.positionType.downRight`

docs/07-pie.md docs/08-pie.md

File renamed without changes.

docs/08-radar.md docs/09-radar.md

File renamed without changes.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chart.xkcd",
3-
"version": "1.1.11",
3+
"version": "1.1.12",
44
"description": "xkcd style chart lib",
55
"jsdelivr": "dist/chart.xkcd.min.js",
66
"unpkg": "dist/chart.xkcd.min.js",

src/Radar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Radar {
7575
items: [],
7676
position: { x: 0, y: 0, type: config.positionType.downRight },
7777
unxkcdify: this.options.unxkcdify,
78-
strokeColor: 'this.options.strokeColor',
78+
strokeColor: this.options.strokeColor,
7979
backgroundColor: this.options.backgroundColor,
8080
});
8181

src/StackedBar.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,6 @@ class StackedBar {
100100
.domain([0, Math.max(...allCols)])
101101
.range([this.height, 0]);
102102

103-
// Merge all the lists into a single list, and store the
104-
// offests in a corresponding list. Use dataLength to
105-
// track how many total columns there should be.
106-
const mergedData = this.data.datasets
107-
.reduce((pre, cur) => pre.concat(cur.data), []);
108-
109-
const dataLength = this.data.datasets[0].data.length;
110-
111-
const offsets = this.data.datasets
112-
.reduce((r, x, i) => {
113-
if (i > 0) {
114-
r.push(x.data.map((y, j) => this.data.datasets[i - 1].data[j] + r[i - 1][j]));
115-
} else {
116-
r.push(new Array(x.data.length).fill(0));
117-
}
118-
return r;
119-
}, []).flat();
120-
121103
const graphPart = this.chart.append('g');
122104

123105
// axis
@@ -137,6 +119,24 @@ class StackedBar {
137119
stroke: this.options.strokeColor,
138120
});
139121

122+
// Merge all the lists into a single list, and store the
123+
// offests in a corresponding list. Use dataLength to
124+
// track how many total columns there should be.
125+
const mergedData = this.data.datasets
126+
.reduce((pre, cur) => pre.concat(cur.data), []);
127+
128+
const dataLength = this.data.datasets[0].data.length;
129+
130+
const offsets = this.data.datasets
131+
.reduce((r, x, i) => {
132+
if (i > 0) {
133+
r.push(x.data.map((y, j) => this.data.datasets[i - 1].data[j] + r[i - 1][j]));
134+
} else {
135+
r.push(new Array(x.data.length).fill(0));
136+
}
137+
return r;
138+
}, []).flat();
139+
140140
// Bars
141141
graphPart.selectAll('.xkcd-chart-stacked-bar')
142142
.data(mergedData)
@@ -161,7 +161,7 @@ class StackedBar {
161161

162162
const tooltipItems = this.data.datasets.map((dataset, j) => ({
163163
color: this.options.dataColors[j],
164-
text: `${this.data.datasets[j].label || ''}: ${this.data.datasets[j].data[i]}`,
164+
text: `${this.data.datasets[j].label || ''}: ${this.data.datasets[j].data[i % dataLength]}`,
165165
})).reverse();
166166

167167
let tooltipPositionType = config.positionType.downRight;
@@ -172,6 +172,7 @@ class StackedBar {
172172
} else if (tipX < this.width / 2 && tipY > this.height / 2) {
173173
tooltipPositionType = config.positionType.upRight;
174174
}
175+
175176
tooltip.update({
176177
title: this.data.labels[i],
177178
items: tooltipItems,

0 commit comments

Comments
 (0)