@yuneta/gobj-ui 7.28.0 → 7.28.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.
@@ -16000,6 +16000,10 @@ function register_c_yui_map() {
16000
16000
  * axes: yui_uplot_axes([{}]), // X and Y, with the ink of the theme
16001
16001
  * }, data, $el);
16002
16002
  *
16003
+ * A vertical axis is as wide as its longest label
16004
+ * (yui_uplot_axis_size, 7.28.1): uPlot's fixed 50px cut every
16005
+ * value past five digits.
16006
+ *
16003
16007
  * The palette is six colours that clear 3:1 on BOTH grounds
16004
16008
  * (worst 3.39 on white, 3.71 on near-black), ordered so the first
16005
16009
  * two are the pair that survives colour blindness -- blue and
@@ -16047,6 +16051,29 @@ var YUI_UPLOT_PALETTE = [
16047
16051
  }
16048
16052
  ];
16049
16053
  /************************************************************
16054
+ * The width of a vertical axis: its longest label, measured
16055
+ * in its own font, plus its ticks and its gap. uPlot gives a
16056
+ * Y axis a fixed 50px, which cuts any label wider than ~5
16057
+ * digits ("150,000" was drawn as ",0,000"). uPlot asks again
16058
+ * while the size settles (`cycle`): past the first answer the
16059
+ * last size stands, or the layout never converges.
16060
+ ************************************************************/
16061
+ function yui_uplot_axis_size(self, values, axis_idx, cycle) {
16062
+ let axis = self.axes[axis_idx];
16063
+ if (cycle > 1 && axis._size) return axis._size;
16064
+ let size = (axis.ticks && axis.ticks.size || 10) + (axis.gap || 5);
16065
+ let longest = (values || []).reduce((acc, v) => {
16066
+ let txt = v == null ? "" : String(v);
16067
+ return txt.length > acc.length ? txt : acc;
16068
+ }, "");
16069
+ if (longest && self.ctx) {
16070
+ self.ctx.font = Array.isArray(axis.font) ? axis.font[0] : axis.font || self.ctx.font;
16071
+ let dpr = typeof devicePixelRatio === "number" && devicePixelRatio > 0 ? devicePixelRatio : 1;
16072
+ size += self.ctx.measureText(longest).width / dpr;
16073
+ }
16074
+ return Math.max(50, Math.ceil(size));
16075
+ }
16076
+ /************************************************************
16050
16077
  * The axes the host asked for -- at least the X and the Y,
16051
16078
  * the two uPlot always draws -- with the ink it did not ask
16052
16079
  * about. A stroke, a grid or a ticks the host gave is kept.
@@ -16055,9 +16082,10 @@ var YUI_UPLOT_PALETTE = [
16055
16082
  function yui_uplot_axes(axes) {
16056
16083
  let list = Array.isArray(axes) ? axes.slice() : [axes || {}];
16057
16084
  while (list.length < 2) list.push({});
16058
- return list.map((ax) => {
16085
+ return list.map((ax, i) => {
16059
16086
  let a = Object.assign({}, ax || {});
16060
16087
  if (a.stroke === void 0) a.stroke = yui_uplot_axis_ink;
16088
+ if (i > 0 && (a.side === void 0 || a.side === 1 || a.side === 3) && a.size === void 0) a.size = yui_uplot_axis_size;
16061
16089
  a.grid = Object.assign({ stroke: yui_uplot_grid_ink }, a.grid || {});
16062
16090
  a.ticks = Object.assign({ stroke: yui_uplot_grid_ink }, a.ticks || {});
16063
16091
  return a;
@@ -15995,6 +15995,10 @@ function register_c_yui_map() {
15995
15995
  * axes: yui_uplot_axes([{}]), // X and Y, with the ink of the theme
15996
15996
  * }, data, $el);
15997
15997
  *
15998
+ * A vertical axis is as wide as its longest label
15999
+ * (yui_uplot_axis_size, 7.28.1): uPlot's fixed 50px cut every
16000
+ * value past five digits.
16001
+ *
15998
16002
  * The palette is six colours that clear 3:1 on BOTH grounds
15999
16003
  * (worst 3.39 on white, 3.71 on near-black), ordered so the first
16000
16004
  * two are the pair that survives colour blindness -- blue and
@@ -16042,6 +16046,29 @@ var YUI_UPLOT_PALETTE = [
16042
16046
  }
16043
16047
  ];
16044
16048
  /************************************************************
16049
+ * The width of a vertical axis: its longest label, measured
16050
+ * in its own font, plus its ticks and its gap. uPlot gives a
16051
+ * Y axis a fixed 50px, which cuts any label wider than ~5
16052
+ * digits ("150,000" was drawn as ",0,000"). uPlot asks again
16053
+ * while the size settles (`cycle`): past the first answer the
16054
+ * last size stands, or the layout never converges.
16055
+ ************************************************************/
16056
+ function yui_uplot_axis_size(self, values, axis_idx, cycle) {
16057
+ let axis = self.axes[axis_idx];
16058
+ if (cycle > 1 && axis._size) return axis._size;
16059
+ let size = (axis.ticks && axis.ticks.size || 10) + (axis.gap || 5);
16060
+ let longest = (values || []).reduce((acc, v) => {
16061
+ let txt = v == null ? "" : String(v);
16062
+ return txt.length > acc.length ? txt : acc;
16063
+ }, "");
16064
+ if (longest && self.ctx) {
16065
+ self.ctx.font = Array.isArray(axis.font) ? axis.font[0] : axis.font || self.ctx.font;
16066
+ let dpr = typeof devicePixelRatio === "number" && devicePixelRatio > 0 ? devicePixelRatio : 1;
16067
+ size += self.ctx.measureText(longest).width / dpr;
16068
+ }
16069
+ return Math.max(50, Math.ceil(size));
16070
+ }
16071
+ /************************************************************
16045
16072
  * The axes the host asked for -- at least the X and the Y,
16046
16073
  * the two uPlot always draws -- with the ink it did not ask
16047
16074
  * about. A stroke, a grid or a ticks the host gave is kept.
@@ -16050,9 +16077,10 @@ var YUI_UPLOT_PALETTE = [
16050
16077
  function yui_uplot_axes(axes) {
16051
16078
  let list = Array.isArray(axes) ? axes.slice() : [axes || {}];
16052
16079
  while (list.length < 2) list.push({});
16053
- return list.map((ax) => {
16080
+ return list.map((ax, i) => {
16054
16081
  let a = Object.assign({}, ax || {});
16055
16082
  if (a.stroke === void 0) a.stroke = yui_uplot_axis_ink;
16083
+ if (i > 0 && (a.side === void 0 || a.side === 1 || a.side === 3) && a.size === void 0) a.size = yui_uplot_axis_size;
16056
16084
  a.grid = Object.assign({ stroke: yui_uplot_grid_ink }, a.grid || {});
16057
16085
  a.ticks = Object.assign({ stroke: yui_uplot_grid_ink }, a.ticks || {});
16058
16086
  return a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuneta/gobj-ui",
3
- "version": "7.28.0",
3
+ "version": "7.28.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/gobj-ui.cjs.js",
@@ -42,7 +42,7 @@ const {
42
42
  } = await import("@yuneta/gobj-js");
43
43
  const {register_c_yui_uplot} = await import("./c_yui_uplot.js");
44
44
  const {
45
- yui_uplot_axes, yui_uplot_axis_ink, yui_uplot_grid_ink,
45
+ yui_uplot_axes, yui_uplot_axis_ink, yui_uplot_grid_ink, yui_uplot_axis_size,
46
46
  } = await import("./yui_uplot_ink.js");
47
47
 
48
48
  const logged = [];
@@ -67,6 +67,35 @@ afterEach(() => {
67
67
  document.documentElement.removeAttribute("data-theme");
68
68
  });
69
69
 
70
+ describe("the width of a vertical axis", () => {
71
+ const fake = (px_per_char) => ({
72
+ ctx: {font: "", measureText: (t) => ({width: t.length * px_per_char})},
73
+ axes: [{}, {ticks: {size: 10}, gap: 5, font: ["12px sans-serif"]}]
74
+ });
75
+
76
+ test("the Y axes are sized by their labels, the X is not; a host's size stands", () => {
77
+ const axes = yui_uplot_axes([{}, {}, {side: 1}, {side: 2}, {size: 80}]);
78
+ expect(axes[0].size).toBeUndefined();
79
+ expect(axes[1].size).toBe(yui_uplot_axis_size);
80
+ expect(axes[2].size).toBe(yui_uplot_axis_size);
81
+ expect(axes[3].size).toBeUndefined();
82
+ expect(axes[4].size).toBe(80);
83
+ });
84
+
85
+ test("fits the longest label, never under uPlot's 50px", () => {
86
+ const self = fake(7);
87
+ expect(yui_uplot_axis_size(self, ["0", "50,000", "150,000"], 1, 0)).toBe(Math.ceil(15 + 7 * 7));
88
+ expect(yui_uplot_axis_size(fake(7), ["0", "5"], 1, 0)).toBe(50);
89
+ expect(yui_uplot_axis_size(fake(7), null, 1, 0)).toBe(50);
90
+ });
91
+
92
+ test("past the first cycle the size settled stands", () => {
93
+ const self = fake(7);
94
+ self.axes[1]._size = 66;
95
+ expect(yui_uplot_axis_size(self, ["1,000,000,000"], 1, 2)).toBe(66);
96
+ });
97
+ });
98
+
70
99
  describe("yui_uplot_axes()", () => {
71
100
  test("gives the X and the Y axes, the two uPlot draws", () => {
72
101
  expect(yui_uplot_axes([{}]).length).toBe(2);
@@ -28,6 +28,10 @@
28
28
  * axes: yui_uplot_axes([{}]), // X and Y, with the ink of the theme
29
29
  * }, data, $el);
30
30
  *
31
+ * A vertical axis is as wide as its longest label
32
+ * (yui_uplot_axis_size, 7.28.1): uPlot's fixed 50px cut every
33
+ * value past five digits.
34
+ *
31
35
  * The palette is six colours that clear 3:1 on BOTH grounds
32
36
  * (worst 3.39 on white, 3.71 on near-black), ordered so the first
33
37
  * two are the pair that survives colour blindness -- blue and
@@ -63,6 +67,33 @@ const YUI_UPLOT_PALETTE = [
63
67
  {stroke: "#d1344b", fill: "rgba(209,52,75,0.10)"}, /* 4.88 / 3.71 */
64
68
  ];
65
69
 
70
+ /************************************************************
71
+ * The width of a vertical axis: its longest label, measured
72
+ * in its own font, plus its ticks and its gap. uPlot gives a
73
+ * Y axis a fixed 50px, which cuts any label wider than ~5
74
+ * digits ("150,000" was drawn as ",0,000"). uPlot asks again
75
+ * while the size settles (`cycle`): past the first answer the
76
+ * last size stands, or the layout never converges.
77
+ ************************************************************/
78
+ function yui_uplot_axis_size(self, values, axis_idx, cycle)
79
+ {
80
+ let axis = self.axes[axis_idx];
81
+ if(cycle > 1 && axis._size) {
82
+ return axis._size;
83
+ }
84
+ let size = ((axis.ticks && axis.ticks.size) || 10) + (axis.gap || 5);
85
+ let longest = (values || []).reduce((acc, v) => {
86
+ let txt = v == null ? "" : String(v);
87
+ return txt.length > acc.length ? txt : acc;
88
+ }, "");
89
+ if(longest && self.ctx) {
90
+ self.ctx.font = Array.isArray(axis.font) ? axis.font[0] : (axis.font || self.ctx.font);
91
+ let dpr = (typeof devicePixelRatio === "number" && devicePixelRatio > 0) ? devicePixelRatio : 1;
92
+ size += self.ctx.measureText(longest).width / dpr;
93
+ }
94
+ return Math.max(50, Math.ceil(size));
95
+ }
96
+
66
97
  /************************************************************
67
98
  * The axes the host asked for -- at least the X and the Y,
68
99
  * the two uPlot always draws -- with the ink it did not ask
@@ -75,11 +106,17 @@ function yui_uplot_axes(axes)
75
106
  while(list.length < 2) {
76
107
  list.push({});
77
108
  }
78
- return list.map((ax) => {
109
+ return list.map((ax, i) => {
79
110
  let a = Object.assign({}, ax || {});
80
111
  if(a.stroke === undefined) {
81
112
  a.stroke = yui_uplot_axis_ink;
82
113
  }
114
+ /* A vertical axis (uPlot's sides 1 and 3; the Y default is 3)
115
+ * as wide as its labels, unless the host sized it */
116
+ let vertical = i > 0 && (a.side === undefined || a.side === 1 || a.side === 3);
117
+ if(vertical && a.size === undefined) {
118
+ a.size = yui_uplot_axis_size;
119
+ }
83
120
  a.grid = Object.assign({stroke: yui_uplot_grid_ink}, a.grid || {});
84
121
  a.ticks = Object.assign({stroke: yui_uplot_grid_ink}, a.ticks || {});
85
122
  return a;
@@ -88,6 +125,7 @@ function yui_uplot_axes(axes)
88
125
 
89
126
  export {
90
127
  yui_uplot_axes,
128
+ yui_uplot_axis_size,
91
129
  yui_uplot_axis_ink,
92
130
  yui_uplot_grid_ink,
93
131
  YUI_UPLOT_PALETTE,