script.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const targetColor = U.obs();
  2. const randomizeTargetColor = () => {
  3. targetColor.value = d3
  4. .hsl(Math.random() * 360, Math.random(), Math.random())
  5. .formatHex();
  6. };
  7. U.element("colorSelect", ({ randomButton }, form) => {
  8. U.field(form.elements.colorText, { obs: targetColor });
  9. U.field(form.elements.colorPicker, { obs: targetColor });
  10. U.form(form);
  11. randomButton.addEventListener("click", randomizeTargetColor);
  12. });
  13. targetColor.subscribe((hex, { previous }) => {
  14. const style = document.querySelector(":root").style;
  15. style.setProperty("--background", hex);
  16. style.setProperty("--highlight", getContrastingTextColor(hex));
  17. if (previous) {
  18. const prevButton = document.createElement("button");
  19. prevButton.innerText = previous;
  20. const { h, s, l } = d3.hsl(previous);
  21. prevButton.style = `
  22. color: ${getContrastingTextColor(previous)};
  23. --button-bg: ${previous};
  24. --button-bg-hover: ${d3.hsl(h, s, clamp(0.25, l * 1.25, 0.9)).formatHex()};
  25. `;
  26. prevButton.addEventListener("click", () => (targetColor.value = previous));
  27. document.getElementById("prevColors").prepend(prevButton);
  28. }
  29. });
  30. randomizeTargetColor();
  31. const addMetricForm = (legendText, initialKind, initialMetric) => {
  32. const metricKind = U.obs(initialKind);
  33. let metric;
  34. U.template("metric-select-template", ({ form, legend }) => {
  35. legend.innerText = legendText;
  36. form.elements[initialKind].disabled = false;
  37. // form.elements.sortMetric = form.elements[initialKind].value = initialMetric;
  38. form.elements.metricKind.value = initialKind;
  39. U.reactive(() => {
  40. form.elements.whole.disabled = metricKind.value !== "whole";
  41. form.elements.mean.disabled = metricKind.value !== "mean";
  42. form.elements.statistic.disabled = metricKind.value !== "statistic";
  43. // metric.value = form.elements[metricKindObs.value].value;
  44. });
  45. const metrics = U.obs([
  46. U.field(form.elements.whole),
  47. U.field(form.elements.mean),
  48. U.field(form.elements.statistic),
  49. ]);
  50. U.form(form, {
  51. onChange: {
  52. metricKind(kind) {
  53. metricKind.value = kind;
  54. },
  55. },
  56. });
  57. metric = U.obs(() => {
  58. const [whole, mean, statistic] = metrics.value;
  59. return { whole, mean, statistic }[metricKind.value];
  60. });
  61. return "sidebar";
  62. });
  63. return metric;
  64. };
  65. const sortMetric = addMetricForm("Sort Metric", "whole", "alpha");
  66. const clusterMetric = addMetricForm("Cluster Metric", "statistic", "importance");