Skip to content

Commit

Permalink
Quick chart performance tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
chadokruse committed Dec 22, 2024
1 parent 8895927 commit fadf607
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { onMount, onDestroy } from 'svelte';
import type { Chart } from 'chart.js';
let { rawData }: { rawData: RawData } = $props();
let { rawData, grantCount }: { rawData: RawData; grantCount: number } = $props();
type RawData = {
[key: string]: number;
Expand All @@ -16,6 +16,21 @@
displayRange: string;
}
interface TicksConfig {
stepSize?: number;
maxTicksLimit: number;
}
const ticksConfig: TicksConfig = {
stepSize: 1,
maxTicksLimit: 5,
};
// Chart.js will generate an unlimited amount of ticks if stepSize set to 1
// Causes a performance hit, and a console warning, when the number of ticks is over 1k
if (grantCount > 5) {
delete ticksConfig.stepSize;
}
function prepareAndReduceChartData(rawData: RawData): GrantRange[] {
const consolidatedData: { [range: string]: number } = {
'<$500': 0,
Expand Down Expand Up @@ -140,10 +155,7 @@
grid: {
display: false,
},
ticks: {
maxTicksLimit: 5,
stepSize: 1,
},
ticks: ticksConfig,
},
y: {
position: 'left',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
{:else if grantsFacets && grantsFacets[0].grant_count > 0}
<h2 class="mt-4 text-sm font-bold text-slate-700">Grant Clusters</h2>
<div class="w-full">
<BarGrantsSnapshot rawData={grantsFacets[0].facets.amount} />
<BarGrantsSnapshot rawData={grantsFacets[0].facets.amount} {grantCount} />
</div>
{:else}
<div class="mt-4 opacity-30">
Expand Down

0 comments on commit fadf607

Please sign in to comment.