| 1 |
import { hcl } from "d3-color"; |
| 2 |
import { scaleOrdinal } from "d3-scale"; |
| 3 |
import { derived, get as store_get } from "svelte/store"; |
| 4 |
|
| 5 |
import { accounts, currencies_sorted, operating_currency } from "../stores"; |
| 6 |
import { currentTimeFilterDateFormat } from "../stores/format"; |
| 7 |
|
| 8 |
/** |
| 9 |
* Set the time filter to the given value (formatted according to the current interval). |
| 10 |
* @param date - a date. |
| 11 |
* @returns A URL for the given interval. |
| 12 |
*/ |
| 13 |
export function urlForTimeFilter(date: Date): string { |
| 14 |
const url = new URL(window.location.href); |
| 15 |
url.searchParams.set("time", store_get(currentTimeFilterDateFormat)(date)); |
| 16 |
return url.toString(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Include zero in the extent. |
| 21 |
* |
| 22 |
* For convenience this also turns a empty extent into [0,1]. |
| 23 |
*/ |
| 24 |
export function includeZero([from, to]: |
| 25 |
| [number, number] |
| 26 |
| [undefined, undefined]): [number, number] { |
| 27 |
if (from === undefined) { |
| 28 |
return [0, 1]; |
| 29 |
} |
| 30 |
return [Math.min(0, from), Math.max(0, to)]; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Pad the extent by a factor of 0.05. |
| 35 |
* |
| 36 |
* For convenience this also turns a empty extent into [0,1]. |
| 37 |
*/ |
| 38 |
export function padExtent([from, to]: |
| 39 |
| [number, number] |
| 40 |
| [undefined, undefined]): [number, number] { |
| 41 |
if (from === undefined) { |
| 42 |
return [0, 1]; |
| 43 |
} |
| 44 |
const diff = to - from; |
| 45 |
return [from - diff * 0.03, to + diff * 0.03]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Filter ticks to have them not overlap. |
| 50 |
* @param domain - The domain of values to filter. |
| 51 |
* @param count - The number of ticks that should be returned. |
| 52 |
*/ |
| 53 |
export function filterTicks(domain: string[], count: number): string[] { |
| 54 |
if (domain.length <= count) { |
| 55 |
return domain; |
| 56 |
} |
| 57 |
const showIndices = Math.ceil(domain.length / count); |
| 58 |
return domain.filter((d, i) => i % showIndices === 0); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Generate an array of colors. |
| 63 |
* |
| 64 |
* Uses the HCL color space in an attempt to generate colours that are |
| 65 |
* to be perceived to be of the same brightness. |
| 66 |
* @param count - the number of colors to generate. |
| 67 |
* @param chroma - optional, the chroma channel value. |
| 68 |
* @param luminance - optional, the luminance channel value. |
| 69 |
*/ |
| 70 |
export function hclColorRange( |
| 71 |
count: number, |
| 72 |
chroma = 45, |
| 73 |
luminance = 70, |
| 74 |
): string[] { |
| 75 |
const offset = 270; |
| 76 |
const delta = 360 / count; |
| 77 |
const colors = [...Array(count).keys()].map((index) => { |
| 78 |
const hue = (index * delta + offset) % 360; |
| 79 |
return hcl(hue, chroma, luminance); |
| 80 |
}); |
| 81 |
return colors.map((c) => c.toString()); |
| 82 |
} |
| 83 |
|
| 84 |
export const colors10 = hclColorRange(10); |
| 85 |
export const colors15 = hclColorRange(15, 30, 80); |
| 86 |
|
| 87 |
/* |
| 88 |
* The color scales for the charts. |
| 89 |
* |
| 90 |
* The scales for treemap and sunburst charts will be initialised with all |
| 91 |
* accounts on page init and currencies with all commodities. |
| 92 |
*/ |
| 93 |
export const scatterplotScale = scaleOrdinal(colors10); |
| 94 |
|
| 95 |
export const treemapScale = derived(accounts, ($accounts) => |
| 96 |
scaleOrdinal(colors15).domain($accounts), |
| 97 |
); |
| 98 |
|
| 99 |
export const sunburstScale = derived(accounts, ($accounts) => |
| 100 |
scaleOrdinal(colors10).domain($accounts), |
| 101 |
); |
| 102 |
|
| 103 |
export const currenciesScale = derived( |
| 104 |
[operating_currency, currencies_sorted], |
| 105 |
([$operating_currency, $currencies_sorted]) => |
| 106 |
scaleOrdinal(colors10).domain([ |
| 107 |
...$operating_currency, |
| 108 |
...$currencies_sorted, |
| 109 |
]), |
| 110 |
); |