@communitiesuk/svelte-component-library 0.1.19-beta.14 → 0.1.19-beta.15

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.
@@ -46,6 +46,11 @@
46
46
  let yTickMin = $derived(yTicks.length ? Math.min(...yTicks) : 0);
47
47
  let yTickMax = $derived(yTicks.length ? Math.max(...yTicks) : 1);
48
48
 
49
+ import { median, quantile } from "d3-array";
50
+
51
+ let distSorted = $derived(dist.slice().sort((a, b) => a - b));
52
+ let rawMedian = $derived(median(distSorted));
53
+
49
54
  // min and max at extremes
50
55
  // let histogram = bin().domain([domainMin, domainMax]).thresholds(thresholds);
51
56
 
@@ -70,7 +75,11 @@
70
75
  // won't get xTick and xTickMax if showAxis is false
71
76
  // run it anyway inside an else block
72
77
  let bins = $derived(createEqualWidthBins(xTickMin, xTickMax, 10, dist));
73
- $inspect({ xTickMin, xTickMax, bins });
78
+
79
+ let proportionInExtremeBins = $derived([
80
+ bins[0]["count"] / distSorted.length,
81
+ bins.at(-1)["count"] / distSorted.length,
82
+ ]);
74
83
 
75
84
  let nBins = $derived(bins.length);
76
85
 
@@ -101,17 +110,59 @@
101
110
  bins.length ? xScale(bins[0].x1) - xScale(bins[0].x0) : 0,
102
111
  );
103
112
 
113
+ import chroma from "chroma-js";
114
+
115
+ let midPosition = $derived((rawMedian - xTickMin) / (xTickMax - xTickMin));
116
+
117
+ let c1000 = $derived(
118
+ chroma
119
+ .scale([startColor, midColor, endColor])
120
+ .domain([0, midPosition, 1])
121
+ .colors(1000),
122
+ );
123
+
104
124
  let colors1000 = $derived(
105
125
  polarity === "reverse"
106
- ? interpolateColors(startColor, endColor, 1000, midColor).reverse() // only done once
107
- : interpolateColors(startColor, endColor, 1000, midColor),
126
+ ? c1000.reverse() // only done once
127
+ : c1000,
128
+ );
129
+
130
+ let thisDomain = $derived(
131
+ polarity === "reverse"
132
+ ? [0, midPosition, 1].reverse() // only done once
133
+ : [0, midPosition, 1],
134
+ );
135
+
136
+ // let colors1000 = $derived(
137
+ // polarity === "reverse"
138
+ // ? interpolateColors(startColor, endColor, 1000, midColor).reverse() // only done once
139
+ // : interpolateColors(startColor, endColor, 1000, midColor),
140
+ // );
141
+
142
+ let averagesForSegments = $derived(
143
+ splitGroupsAndAverages(dist, nSegments).averages,
144
+ );
145
+
146
+ let preBin = $derived(
147
+ chroma
148
+ .scale([startColor, midColor, endColor])
149
+ .domain(thisDomain)
150
+ .padding([proportionInExtremeBins[0] / 2, proportionInExtremeBins[1] / 2])
151
+ .colors(10),
108
152
  );
109
153
 
110
154
  let binColors = $derived(
111
155
  polarity === "reverse"
112
- ? assignBinColors(bins, colors1000) // only done once
113
- : assignBinColors(bins, colors1000),
156
+ ? preBin.reverse() // only done once
157
+ : preBin,
114
158
  );
159
+
160
+ // let binColors = $derived(
161
+ // polarity === "reverse"
162
+ // ? assignBinColors(bins, colors1000).reverse() // only done once
163
+ // : assignBinColors(bins, colors1000),
164
+ // );
165
+
115
166
  let interpolatedColors = $derived(binColors);
116
167
 
117
168
  function findBinIndex(binned, value) {
@@ -205,28 +205,62 @@
205
205
  createEqualWidthBins(xTickMin, xTickMax, nSegments, dist),
206
206
  );
207
207
 
208
+ let proportionInExtremeBins = $derived([
209
+ bins[0]["count"] / distSorted.length,
210
+ bins.at(-1)["count"] / distSorted.length,
211
+ ]);
212
+
208
213
  const range = $derived(
209
214
  Array.from({ length: nSegments }, (_, i) => nSegments - 1 - i),
210
215
  );
211
216
 
217
+ import chroma from "chroma-js";
218
+
219
+ let midPosition = $derived((rawMedian - newMin) / (newMax - newMin));
220
+
221
+ let c1000 = $derived(
222
+ chroma
223
+ .scale([startColor, midColor, endColor])
224
+ .domain([0, midPosition, 1])
225
+ .colors(1000),
226
+ );
227
+
212
228
  let colors1000 = $derived(
213
229
  polarity === "reverse"
214
- ? interpolateColors(startColor, endColor, 1000, midColor).reverse() // only done once
215
- : interpolateColors(startColor, endColor, 1000, midColor),
230
+ ? c1000.reverse() // only done once
231
+ : c1000,
232
+ );
233
+
234
+ let thisDomain = $derived(
235
+ polarity === "reverse"
236
+ ? [0, midPosition, 1].reverse() // only done once
237
+ : [0, midPosition, 1],
216
238
  );
217
239
 
240
+ // let colors1000 = $derived(
241
+ // polarity === "reverse"
242
+ // ? interpolateColors(startColor, endColor, 1000, midColor).reverse() // only done once
243
+ // : interpolateColors(startColor, endColor, 1000, midColor),
244
+ // );
245
+
218
246
  let averagesForSegments = $derived(
219
247
  splitGroupsAndAverages(dist, nSegments).averages,
220
248
  );
221
249
 
222
- // let binColors = $derived(assignBinColors(bins, colors1000));
223
-
224
250
  let binColors = $derived(
225
- polarity === "reverse"
226
- ? assignBinColors(bins, colors1000).reverse() // only done once
227
- : assignBinColors(bins, colors1000),
251
+ chroma
252
+ .scale([startColor, midColor, endColor])
253
+ .domain(thisDomain)
254
+ .padding([proportionInExtremeBins[0] / 2, proportionInExtremeBins[1] / 2])
255
+ .colors(10),
228
256
  );
229
257
 
258
+ // let binColors = $derived(
259
+ // polarity === "reverse"
260
+ // ? assignBinColors(bins, colors1000).reverse() // only done once
261
+ // : assignBinColors(bins, colors1000),
262
+ // );
263
+
230
264
  let interpolatedColors = $derived(
231
265
  skew
232
266
  ? binColors
@@ -675,7 +709,7 @@
675
709
  )
676
710
  : rowValue.color}
677
711
  stroke="white"
678
- stroke-width="2"
712
+ stroke-width="5"
679
713
  opacity={rowValue.opacity}
680
714
  pointer-events={rowValue.pointerEvents}
681
715
  ></circle>
@@ -691,7 +725,8 @@
691
725
  activeColors,
692
726
  )
693
727
  : rowValue.color}
694
- stroke="black"
728
+ stroke="#333333"
729
+ stroke-width="2"
695
730
  opacity={rowValue.opacity}
696
731
  pointer-events={rowValue.pointerEvents}
697
732
  ></circle>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@communitiesuk/svelte-component-library",
3
- "version": "0.1.19-beta.14",
3
+ "version": "0.1.19-beta.15",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/communitiesuk/mhclg_svelte_component_library.git"
@@ -101,6 +101,7 @@
101
101
  "@types/dompurify": "^3.0.5",
102
102
  "accessible-autocomplete": "^3.0.1",
103
103
  "choices.js": "^11.1.0",
104
+ "chroma-js": "^3.2.0",
104
105
  "csv-parser": "^3.0.0",
105
106
  "d3": "^7.9.0",
106
107
  "decimal.js": "^10.5.0",