|
7 | 7 | createScale, |
8 | 8 | getRange, |
9 | 9 | isScaleBand, |
| 10 | + isScaleTime, |
10 | 11 | makeAccessor, |
11 | 12 | type AnyScale, |
12 | 13 | type DomainType, |
|
40 | 41 | import TransformContext, { type TransformContextValue } from './TransformContext.svelte'; |
41 | 42 | import BrushContext, { type BrushContextValue } from './BrushContext.svelte'; |
42 | 43 | import { layerClass } from '$lib/utils/attributes.js'; |
| 44 | + import type { TimeInterval } from 'd3-time'; |
43 | 45 |
|
44 | 46 | const defaultPadding = { top: 0, right: 0, bottom: 0, left: 0 }; |
45 | 47 |
|
|
149 | 151 | cGet: (d: T) => any; |
150 | 152 | x1Get: (d: T) => any; |
151 | 153 | y1Get: (d: T) => any; |
| 154 | + xInterval: TimeInterval | null; |
| 155 | + yInterval: TimeInterval | null; |
152 | 156 | radial: boolean; |
153 | 157 | tooltip: TooltipContextValue<T>; |
154 | 158 | geo: GeoContextValue; |
|
640 | 644 | */ |
641 | 645 | yBaseline?: number | null; |
642 | 646 |
|
| 647 | + /** |
| 648 | + * Time interval to use for the x-axis when using a time scale. |
| 649 | + */ |
| 650 | + xInterval?: TimeInterval | null; |
| 651 | +
|
| 652 | + /** |
| 653 | + * Time interval to use for the y-axis when using a time scale. |
| 654 | + */ |
| 655 | + yInterval?: TimeInterval | null; |
| 656 | +
|
643 | 657 | /* Props passed to ChartContext */ |
644 | 658 |
|
645 | 659 | /** |
|
738 | 752 | rRange: rRangeProp, |
739 | 753 | xBaseline = null, |
740 | 754 | yBaseline = null, |
| 755 | + xInterval = null, |
| 756 | + yInterval = null, |
741 | 757 | meta = {}, |
742 | 758 | children: _children, |
743 | 759 | radial = false, |
|
780 | 796 |
|
781 | 797 | const _xDomain: DomainType | undefined = $derived.by(() => { |
782 | 798 | if (xDomainProp !== undefined) return xDomainProp; |
| 799 | +
|
| 800 | + if (xInterval != null && Array.isArray(data) && data.length > 0) { |
| 801 | + const lastXValue = accessor(xProp)(data[data.length - 1]); |
| 802 | + return [null, xInterval.offset(lastXValue)]; |
| 803 | + } |
| 804 | +
|
783 | 805 | if (xBaseline != null && Array.isArray(data)) { |
784 | 806 | const xValues = data.flatMap(accessor(xProp)); |
785 | 807 | return [min([xBaseline, ...xValues]), max([xBaseline, ...xValues])]; |
|
788 | 810 |
|
789 | 811 | const _yDomain: DomainType | undefined = $derived.by(() => { |
790 | 812 | if (yDomainProp !== undefined) return yDomainProp; |
| 813 | +
|
| 814 | + if (yInterval != null && Array.isArray(data) && data.length > 0) { |
| 815 | + const lastYValue = accessor(yProp)(data[data.length - 1]); |
| 816 | + return [null, yInterval.offset(lastYValue)]; |
| 817 | + } |
| 818 | +
|
791 | 819 | if (yBaseline != null && Array.isArray(data)) { |
792 | 820 | const yValues = data.flatMap(accessor(yProp)); |
793 | 821 | return [min([yBaseline, ...yValues]), max([yBaseline, ...yValues])]; |
|
798 | 826 | _yRangeProp ?? (radial ? ({ height }: { height: number }) => [0, height / 2] : undefined) |
799 | 827 | ); |
800 | 828 |
|
801 | | - const yReverse = $derived(yScaleProp ? !isScaleBand(yScaleProp) : true); |
| 829 | + const yReverse = $derived( |
| 830 | + yScaleProp ? !isScaleBand(yScaleProp) && !isScaleTime(yScaleProp) : true |
| 831 | + ); |
802 | 832 |
|
803 | 833 | const x = $derived(makeAccessor(xProp)); |
804 | 834 | const y = $derived(makeAccessor(yProp)); |
|
1247 | 1277 | get y1Scale() { |
1248 | 1278 | return y1Scale; |
1249 | 1279 | }, |
| 1280 | + get xInterval() { |
| 1281 | + return xInterval; |
| 1282 | + }, |
| 1283 | + get yInterval() { |
| 1284 | + return yInterval; |
| 1285 | + }, |
1250 | 1286 | get radial() { |
1251 | 1287 | return radial; |
1252 | 1288 | }, |
|
0 commit comments