@vector-business-services/cristal-ds-web 0.24.0 → 0.25.0

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.
package/dist/index.cjs CHANGED
@@ -605,6 +605,26 @@ var comAlfa = (cor, alfa) => {
605
605
  const [r = 0, g = 0, b = 0] = normal.match(/[\d.]+/g)?.map(Number) ?? [];
606
606
  return `rgba(${r}, ${g}, ${b}, ${alfa})`;
607
607
  };
608
+ var senoide = {
609
+ id: "crSenoide",
610
+ beforeDatasetsDraw(chart) {
611
+ chart.data.datasets.forEach((_, indice) => {
612
+ const meta = chart.getDatasetMeta(indice);
613
+ if (meta.type !== "line" || !meta.dataset || !chart.isDatasetVisible(indice)) return;
614
+ const linha = meta.dataset;
615
+ linha.updateControlPoints(chart.chartArea, meta.iScale?.axis);
616
+ const pontos = linha.points;
617
+ pontos.forEach((ponto, i) => {
618
+ const antes = pontos[i - 1];
619
+ const depois = pontos[i + 1];
620
+ ponto.cp1x = antes && !antes.skip ? (antes.x + ponto.x) / 2 : ponto.x;
621
+ ponto.cp1y = ponto.y;
622
+ ponto.cp2x = depois && !depois.skip ? (ponto.x + depois.x) / 2 : ponto.x;
623
+ ponto.cp2y = ponto.y;
624
+ });
625
+ });
626
+ }
627
+ };
608
628
  var cruz = (cor) => ({
609
629
  id: "crCruz",
610
630
  afterDatasetsDraw(chart) {
@@ -621,59 +641,7 @@ var cruz = (cor) => ({
621
641
  ctx.restore();
622
642
  }
623
643
  });
624
- var tracar = (ctx, pontos) => {
625
- pontos.forEach((ponto, i) => {
626
- const antes = pontos[i - 1];
627
- if (!antes) return ctx.moveTo(ponto.x, ponto.y);
628
- const meio = (antes.x + ponto.x) / 2;
629
- ctx.bezierCurveTo(meio, antes.y, meio, ponto.y, ponto.x, ponto.y);
630
- });
631
- };
632
- var trechos = (pontos) => pontos.reduce(
633
- (lista, ponto) => {
634
- if (ponto.skip) lista.push([]);
635
- else lista[lista.length - 1].push(ponto);
636
- return lista;
637
- },
638
- [[]]
639
- ).filter((trecho) => trecho.length > 0);
640
- var curvaDoFigma = {
641
- id: "crCurva",
642
- beforeDatasetsDraw(chart) {
643
- const { ctx, chartArea } = chart;
644
- const series = chart.data.datasets.map((dataset, indice) => ({ dataset, meta: chart.getDatasetMeta(indice), indice })).filter(({ indice }) => chart.isDatasetVisible(indice)).reverse();
645
- ctx.save();
646
- ctx.beginPath();
647
- ctx.rect(chartArea.left, chartArea.top, chartArea.width, chartArea.height);
648
- ctx.clip();
649
- series.forEach(({ dataset, meta }) => {
650
- const cor = String(dataset.borderColor);
651
- const base = Math.min(meta.yScale?.getPixelForValue(0) ?? chartArea.bottom, chartArea.bottom);
652
- const degrade = ctx.createLinearGradient(0, chartArea.top, 0, chartArea.bottom);
653
- degrade.addColorStop(0, comAlfa(cor, 0.1));
654
- degrade.addColorStop(1, comAlfa(cor, 0.02));
655
- trechos(meta.data).forEach((trecho) => {
656
- ctx.beginPath();
657
- tracar(ctx, trecho);
658
- ctx.lineTo(trecho[trecho.length - 1].x, base);
659
- ctx.lineTo(trecho[0].x, base);
660
- ctx.closePath();
661
- ctx.fillStyle = degrade;
662
- ctx.fill();
663
- });
664
- });
665
- series.forEach(({ dataset, meta }) => {
666
- ctx.strokeStyle = String(dataset.borderColor);
667
- ctx.lineWidth = 2;
668
- trechos(meta.data).forEach((trecho) => {
669
- ctx.beginPath();
670
- tracar(ctx, trecho);
671
- ctx.stroke();
672
- });
673
- });
674
- ctx.restore();
675
- }
676
- };
644
+ var semMovimento = () => typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
677
645
  var formatar = (eixo, valor) => eixo?.format ? eixo.format(valor) : String(valor);
678
646
  var Chart = react.forwardRef(function Chart2({
679
647
  type = "line",
@@ -687,6 +655,7 @@ var Chart = react.forwardRef(function Chart2({
687
655
  helpLabel = "Ajuda",
688
656
  actions,
689
657
  height = 266,
658
+ legend,
690
659
  id,
691
660
  className,
692
661
  ...props
@@ -725,8 +694,19 @@ var Chart = react.forwardRef(function Chart2({
725
694
  data: s.data,
726
695
  yAxisID: s.axis === "right" ? "y2" : "y",
727
696
  borderColor: cor,
728
- // A linha e a área são desenhadas pelo plugin curvaDoFigma.
729
- showLine: false,
697
+ borderWidth: 2,
698
+ // Liga a curva (Béziers) do Chart.js; os controles viram os da senoide (plugin senoide).
699
+ cubicInterpolationMode: "monotone",
700
+ // Área até o zero, em degradê da cor da série: 10% em cima e 2% embaixo.
701
+ fill: "origin",
702
+ backgroundColor: ({ chart }) => {
703
+ const area = chart.chartArea;
704
+ if (!area) return comAlfa(cor, 0.1);
705
+ const degrade = chart.ctx.createLinearGradient(0, area.top, 0, area.bottom);
706
+ degrade.addColorStop(0, comAlfa(cor, 0.1));
707
+ degrade.addColorStop(1, comAlfa(cor, 0.02));
708
+ return degrade;
709
+ },
730
710
  pointRadius: 0,
731
711
  pointHoverRadius: 4,
732
712
  pointHoverBorderWidth: 2,
@@ -736,6 +716,18 @@ var Chart = react.forwardRef(function Chart2({
736
716
  })
737
717
  };
738
718
  }, [tema, labels, series, type, stacked]);
719
+ const ultimoQuadro = react.useRef(0);
720
+ const marcarAnimando = react.useCallback(() => {
721
+ ultimoQuadro.current = performance.now();
722
+ raiz.current?.setAttribute("data-cr-animando", "");
723
+ }, []);
724
+ const desmarcarAnimando = react.useCallback(() => {
725
+ const tentar = () => {
726
+ if (performance.now() - ultimoQuadro.current < 100) setTimeout(tentar, 50);
727
+ else raiz.current?.removeAttribute("data-cr-animando");
728
+ };
729
+ setTimeout(tentar, 50);
730
+ }, []);
739
731
  const options = react.useMemo(() => {
740
732
  if (!tema) return void 0;
741
733
  const fonte = { ...tema.fonte, weight: tema.fonte.weight };
@@ -758,7 +750,9 @@ var Chart = react.forwardRef(function Chart2({
758
750
  return {
759
751
  responsive: true,
760
752
  maintainAspectRatio: false,
761
- animation: false,
753
+ // A animação do Chart.js (a do p-chart), sem ela com prefers-reduced-motion. Enquanto anima, a
754
+ // raiz leva data-cr-animando (os testes de imagem esperam o fim).
755
+ animation: semMovimento() ? false : { onProgress: marcarAnimando, onComplete: desmarcarAnimando },
762
756
  interaction: { mode: "index", intersect: false },
763
757
  layout: { padding: { top: 8 } },
764
758
  scales: {
@@ -796,9 +790,14 @@ var Chart = react.forwardRef(function Chart2({
796
790
  }
797
791
  }
798
792
  };
799
- }, [tema, yAxis, y2Axis, temDireita, series, type, stacked]);
793
+ }, [tema, yAxis, y2Axis, temDireita, series, type, stacked, marcarAnimando, desmarcarAnimando]);
794
+ react.useLayoutEffect(() => {
795
+ if (data && options && !semMovimento()) raiz.current?.setAttribute("data-cr-animando", "");
796
+ }, [data, options]);
800
797
  const card = Boolean(title || actions);
801
- const classes2 = ["cr-chart", card && "cr-chart--card", className].filter(Boolean).join(" ");
798
+ const preencher = height === "100%";
799
+ const comLegenda = legend ?? series.length > 1;
800
+ const classes2 = ["cr-chart", card && "cr-chart--card", preencher && "cr-chart--fill", className].filter(Boolean).join(" ");
802
801
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { ...props, ref: raiz, id, className: classes2, children: [
803
802
  card && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "cr-chart__header", children: [
804
803
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "cr-chart__title-row", children: [
@@ -811,13 +810,13 @@ var Chart = react.forwardRef(function Chart2({
811
810
  actions && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "cr-chart__actions", children: actions })
812
811
  ] }),
813
812
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "cr-chart__body", children: [
814
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "cr-chart__plot", style: { height }, children: data && options && /* @__PURE__ */ jsxRuntime.jsx(
813
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "cr-chart__plot", style: preencher ? void 0 : { height }, children: data && options && /* @__PURE__ */ jsxRuntime.jsx(
815
814
  chart.Chart,
816
815
  {
817
816
  type,
818
817
  data,
819
818
  options,
820
- plugins: type === "line" ? [curvaDoFigma, cruz(tema.cruz)] : [],
819
+ plugins: type === "line" ? [senoide, cruz(tema.cruz)] : [],
821
820
  width: "100%",
822
821
  height: "100%",
823
822
  pt: {
@@ -830,7 +829,7 @@ var Chart = react.forwardRef(function Chart2({
830
829
  },
831
830
  type
832
831
  ) }),
833
- series.length > 1 && /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "cr-chart__legend", children: series.map((s, i) => /* @__PURE__ */ jsxRuntime.jsxs("li", { className: "cr-chart__legend-item", children: [
832
+ comLegenda && /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "cr-chart__legend", children: series.map((s, i) => /* @__PURE__ */ jsxRuntime.jsxs("li", { className: "cr-chart__legend-item", children: [
834
833
  /* @__PURE__ */ jsxRuntime.jsx(
835
834
  "span",
836
835
  {