Skip to content

Commit

Permalink
Support legend
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Jun 27, 2021
1 parent 8df4dcb commit 4b40b6b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ <h1 class="text-3xl">svelte-heatmap</h1>
startDate: moment().subtract(5, 'months').toDate(),
endDate: moment().toDate(),
view: 'monthly',
displayLegend: false,
},
target: document.querySelector('#monthly'),
});
Expand Down
19 changes: 17 additions & 2 deletions src/SvelteHeatmap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
{/each}
</g>
{/if}
{#if displayLegend}
<Legend
translateX={width - (wholeColors.length + 5) * cellSize}
translateY={height - legendHeight / 2}
height={legendHeight}
colors={wholeColors}
{cellSize}
{fontFamily}
{fontSize}/>
{/if}
</svg>

<script>
Expand All @@ -64,6 +74,7 @@ import {
import Month from './views/Month.svelte';
import Week from './views/Week.svelte';
import Legend from './views/Legend.svelte';
export let allowOverflow = false;
export let cellGap = 2;
Expand All @@ -83,6 +94,8 @@ export let monthLabelHeight = 12;
export let monthLabels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
export let startDate = null;
export let view = 'weekly';
export let displayLegend = true;
export let legendHeight = 30;
const isNewMonth = (chunks, index) => {
const chunk = chunks[index];
Expand Down Expand Up @@ -114,9 +127,9 @@ $: chunks = view === 'monthly'
$: weekRect = (7 * cellRect) - cellGap;
$: height = view === 'monthly'
$: height = (view === 'monthly'
? (6 * cellRect) - cellGap + monthLabelHeight // <- max of 6 rows in monthly view
: weekRect + monthLabelHeight;
: weekRect + monthLabelHeight) + (displayLegend? legendHeight: 0);
$: width = view === 'monthly'
? ((weekRect + monthGap) * chunks.length) - monthGap
Expand All @@ -125,4 +138,6 @@ $: width = view === 'monthly'
$: dayLabelPosition = index => {
return (cellRect * index) + (cellRect / 2) + monthLabelHeight;
}
$: wholeColors = [emptyColor, ...colors];
</script>
42 changes: 42 additions & 0 deletions src/views/Legend.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<g transform="translate({translateX}, {translateY})">
{#each colors as color, index}
<rect
fill={color}
height={cellSize}
width={cellSize}
x={20 + cellSize * index}
{y}
alignment-baseline="middle"
/>
{/each}
<text
x="0"
y={cellSize}
font-family={fontFamily}
font-size={fontSize}
alignment-baseline="middle"
>
Less
</text>
<text
x={15 + cellSize * (colors.length + 1)}
y={cellSize}
font-family={fontFamily}
font-size={fontSize}
alignment-baseline="middle"
>
More
</text>
</g>

<script>
export let translateX;
export let translateY;
export let height;
export let colors;
export let cellSize;
export let fontSize;
export let fontFamily;
$: y = (height - cellSize) / 4;
</script>

0 comments on commit 4b40b6b

Please sign in to comment.