@humanforest/charts 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/FSparkline.vue +21 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@humanforest/charts",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src"
@@ -24,7 +24,7 @@
24
24
  "vue": "^3.5.22"
25
25
  },
26
26
  "dependencies": {
27
- "@humanforest/tokens": "^0.2.0"
27
+ "@humanforest/tokens": "^0.3.1"
28
28
  },
29
29
  "publishConfig": {
30
30
  "access": "public"
@@ -100,10 +100,22 @@ const yCompare = (d: Point) => d.c;
100
100
 
101
101
  const hasCompare = computed(() => !!props.compare?.some((v) => v !== null && v !== undefined));
102
102
 
103
- // Bars are measured from zero, not from the data's own floor. The line mark frames min-to-max
104
- // because it reads as a shape; a bar reads as a LENGTH, and a bar that starts anywhere but zero has
105
- // a length no longer proportional to its value. The domain therefore always contains zero, which
106
- // also puts negative readings below the baseline instead of quietly flipping them.
103
+ // The line marks share one scale, framed by the floor and ceiling of everything drawn on it — a
104
+ // line reads as a shape, and a shape wants its own range. Set by hand because an Unovis area is
105
+ // measured from a baseline: left to itself, `area` pulls the floor to zero and flattens the line it
106
+ // is filling under. `null` when there is nothing to draw, so the container keeps its own fallback.
107
+ const domain = computed<[number, number] | null>(() => {
108
+ const vals = [...props.data, ...(props.compare ?? [])].filter((v): v is number => typeof v === 'number');
109
+ return vals.length ? [Math.min(...vals), Math.max(...vals)] : null;
110
+ });
111
+ // An area is a band from its baseline to baseline + y, not a fill up to y, so reaching the line
112
+ // from the floor of the frame means passing the height above that floor.
113
+ const yFromFloor = (d: Point) => (d.v === null || !domain.value ? null : d.v - domain.value[0]);
114
+
115
+ // Bars are the exception: they are measured from zero, not from the data's own floor. A bar reads
116
+ // as a LENGTH, and a bar that starts anywhere but zero has a length no longer proportional to its
117
+ // value. The bar domain therefore always contains zero, which also puts negative readings below the
118
+ // baseline instead of quietly flipping them.
107
119
  const barDomain = computed(() => {
108
120
  const vals = props.data.filter((v): v is number => v !== null);
109
121
  const lo = Math.min(0, ...vals);
@@ -134,15 +146,11 @@ const last = computed(() => {
134
146
  for (let i = props.data.length - 1; i >= 0; i--) if (props.data[i] !== null) return props.data[i] as number;
135
147
  return null;
136
148
  });
137
- const bounds = computed(() => {
138
- const vals = props.data.filter((v): v is number => v !== null);
139
- return { min: Math.min(...vals), max: Math.max(...vals) };
140
- });
141
149
  // Fraction of the height the final point sits at, for the endpoint dot.
142
150
  const endpointTop = computed(() => {
143
- const { min, max } = bounds.value;
144
- if (last.value === null || max === min) return 50;
145
- return (1 - (last.value - min) / (max - min)) * 100;
151
+ const [lo, hi] = domain.value ?? [0, 0];
152
+ if (last.value === null || hi === lo) return 50;
153
+ return (1 - (last.value - lo) / (hi - lo)) * 100;
146
154
  });
147
155
  </script>
148
156
 
@@ -179,6 +187,7 @@ const endpointTop = computed(() => {
179
187
  :data="points"
180
188
  :height="height"
181
189
  :margin="{ top: 2, right: endpoint ? 4 : 0, bottom: 0, left: 0 }"
190
+ :y-domain="domain ?? undefined"
182
191
  :duration="motionDuration('slow')"
183
192
  >
184
193
  <!-- Comparison first, so the current series draws over it. Dashed and thinner: the dash says
@@ -194,7 +203,7 @@ const endpointTop = computed(() => {
194
203
  :opacity="0.45"
195
204
  :curve-type="curve ? CurveType.MonotoneX : CurveType.Linear"
196
205
  />
197
- <VisArea v-if="area" :x="x" :y="y" :color="color" :opacity="0.12" :curve-type="curve ? CurveType.MonotoneX : CurveType.Linear" />
206
+ <VisArea v-if="area" :x="x" :y="yFromFloor" :baseline="domain?.[0] ?? 0" :color="color" :opacity="0.12" :curve-type="curve ? CurveType.MonotoneX : CurveType.Linear" />
198
207
  <VisLine :x="x" :y="y" :color="color" :line-width="1.5" :curve-type="curve ? CurveType.MonotoneX : CurveType.Linear" />
199
208
  </VisXYContainer>
200
209
  <span