Skip to content

Commit 4b40b6b

Browse files
committed
Support legend
1 parent 8df4dcb commit 4b40b6b

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ <h1 class="text-3xl">svelte-heatmap</h1>
120120
startDate: moment().subtract(5, 'months').toDate(),
121121
endDate: moment().toDate(),
122122
view: 'monthly',
123+
displayLegend: false,
123124
},
124125
target: document.querySelector('#monthly'),
125126
});

src/SvelteHeatmap.svelte

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@
5353
{/each}
5454
</g>
5555
{/if}
56+
{#if displayLegend}
57+
<Legend
58+
translateX={width - (wholeColors.length + 5) * cellSize}
59+
translateY={height - legendHeight / 2}
60+
height={legendHeight}
61+
colors={wholeColors}
62+
{cellSize}
63+
{fontFamily}
64+
{fontSize}/>
65+
{/if}
5666
</svg>
5767

5868
<script>
@@ -64,6 +74,7 @@ import {
6474
6575
import Month from './views/Month.svelte';
6676
import Week from './views/Week.svelte';
77+
import Legend from './views/Legend.svelte';
6778
6879
export let allowOverflow = false;
6980
export let cellGap = 2;
@@ -83,6 +94,8 @@ export let monthLabelHeight = 12;
8394
export let monthLabels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
8495
export let startDate = null;
8596
export let view = 'weekly';
97+
export let displayLegend = true;
98+
export let legendHeight = 30;
8699
87100
const isNewMonth = (chunks, index) => {
88101
const chunk = chunks[index];
@@ -114,9 +127,9 @@ $: chunks = view === 'monthly'
114127
115128
$: weekRect = (7 * cellRect) - cellGap;
116129
117-
$: height = view === 'monthly'
130+
$: height = (view === 'monthly'
118131
? (6 * cellRect) - cellGap + monthLabelHeight // <- max of 6 rows in monthly view
119-
: weekRect + monthLabelHeight;
132+
: weekRect + monthLabelHeight) + (displayLegend? legendHeight: 0);
120133
121134
$: width = view === 'monthly'
122135
? ((weekRect + monthGap) * chunks.length) - monthGap
@@ -125,4 +138,6 @@ $: width = view === 'monthly'
125138
$: dayLabelPosition = index => {
126139
return (cellRect * index) + (cellRect / 2) + monthLabelHeight;
127140
}
141+
142+
$: wholeColors = [emptyColor, ...colors];
128143
</script>

src/views/Legend.svelte

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<g transform="translate({translateX}, {translateY})">
2+
{#each colors as color, index}
3+
<rect
4+
fill={color}
5+
height={cellSize}
6+
width={cellSize}
7+
x={20 + cellSize * index}
8+
{y}
9+
alignment-baseline="middle"
10+
/>
11+
{/each}
12+
<text
13+
x="0"
14+
y={cellSize}
15+
font-family={fontFamily}
16+
font-size={fontSize}
17+
alignment-baseline="middle"
18+
>
19+
Less
20+
</text>
21+
<text
22+
x={15 + cellSize * (colors.length + 1)}
23+
y={cellSize}
24+
font-family={fontFamily}
25+
font-size={fontSize}
26+
alignment-baseline="middle"
27+
>
28+
More
29+
</text>
30+
</g>
31+
32+
<script>
33+
export let translateX;
34+
export let translateY;
35+
export let height;
36+
export let colors;
37+
export let cellSize;
38+
export let fontSize;
39+
export let fontFamily;
40+
41+
$: y = (height - cellSize) / 4;
42+
</script>

0 commit comments

Comments
 (0)