@automattic/charts 4.3.0 → 4.4.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.d.cts CHANGED
@@ -1068,6 +1068,10 @@ interface BarChartProps extends BaseChartProps<SeriesData[]>, SeriesVisibilityPr
1068
1068
  tooltipPlacement?: 'auto' | 'beside';
1069
1069
  /** Tooltip top anchor in SVG coordinates; negative offsets are supported. */
1070
1070
  tooltipAnchorTop?: number;
1071
+ /** Inline styles merged over the tooltip box defaults. */
1072
+ tooltipStyle?: CSSProperties;
1073
+ /** CSS class for each primary bar, in addition to visx-bar. */
1074
+ barClassName?: (datum: DataPointDate) => string | undefined;
1071
1075
  orientation?: 'horizontal' | 'vertical';
1072
1076
  withPatterns?: boolean;
1073
1077
  showZeroValues?: boolean;
package/dist/index.d.ts CHANGED
@@ -1068,6 +1068,10 @@ interface BarChartProps extends BaseChartProps<SeriesData[]>, SeriesVisibilityPr
1068
1068
  tooltipPlacement?: 'auto' | 'beside';
1069
1069
  /** Tooltip top anchor in SVG coordinates; negative offsets are supported. */
1070
1070
  tooltipAnchorTop?: number;
1071
+ /** Inline styles merged over the tooltip box defaults. */
1072
+ tooltipStyle?: CSSProperties;
1073
+ /** CSS class for each primary bar, in addition to visx-bar. */
1074
+ barClassName?: (datum: DataPointDate) => string | undefined;
1071
1075
  orientation?: 'horizontal' | 'vertical';
1072
1076
  withPatterns?: boolean;
1073
1077
  showZeroValues?: boolean;
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { getStringWidth as getStringWidth$1 } from "./visx/text/index.js";
2
2
  import { formatNumber, formatNumberCompact } from "@automattic/number-formatters";
3
- import { AnimatedAreaSeries, AnimatedAreaStack, AreaSeries, Axis, BarGroup, BarSeries, DataContext, Grid, TooltipContext, XYChart, buildChartTheme, useEventEmitter } from "@visx/xychart";
3
+ import { AnimatedAreaSeries, AnimatedAreaStack, AreaSeries, Axis, BarGroup, BarSeries, DataContext, GlyphSeries, Grid, TooltipContext, XYChart, buildChartTheme, useEventEmitter } from "@visx/xychart";
4
4
  import { __, _x, sprintf } from "@wordpress/i18n";
5
5
  import clsx from "clsx";
6
6
  import * as React from "react";
@@ -5659,9 +5659,8 @@ const createGroupScale = (keys, bandwidth, padding) => scaleBand({
5659
5659
  //#endregion
5660
5660
  //#region src/charts/bar-chart/private/comparison-bars-geometry.ts
5661
5661
  /**
5662
- * Output position of a value scale's baseline: zero if in-domain, else the
5663
- * nearest range edge. Mirrors visx's getScaleBaseline so comparison shadows
5664
- * sit on the same baseline as primary bars.
5662
+ * Mirrors visx's getScaleBaseline so comparison shadows and primary bars
5663
+ * share the same baseline, including out-of-domain zero values.
5665
5664
  *
5666
5665
  * @param {ValueScale} scale - The continuous value scale.
5667
5666
  * @return {number} The baseline output position in pixels.
@@ -5672,7 +5671,7 @@ function getValueScaleBaseline(scale) {
5672
5671
  const maybeZero = scale(0);
5673
5672
  const [minOutput, maxOutput] = isDescending ? [b, a] : [a, b];
5674
5673
  if (isDescending) return Number.isFinite(maybeZero) ? Math.min(Math.max(minOutput, maybeZero), maxOutput) : maxOutput;
5675
- return Number.isFinite(maybeZero) ? Math.min(Math.max(maybeZero, minOutput), maxOutput) : minOutput;
5674
+ return Number.isFinite(maybeZero) ? Math.max(maybeZero, minOutput) : minOutput;
5676
5675
  }
5677
5676
  /**
5678
5677
  * Compute the rect for a comparison "shadow" bar, centered on the paired
@@ -5708,6 +5707,51 @@ function computeComparisonRect(params) {
5708
5707
  height: valueLength
5709
5708
  };
5710
5709
  }
5710
+ //#endregion
5711
+ //#region src/charts/bar-chart/private/classified-bar-series.tsx
5712
+ const ClassifiedBarSeries = ({ barClassName, primaryKeys, groupPadding, ...props }) => {
5713
+ const { xScale, yScale, horizontal } = useContext(DataContext);
5714
+ const bandScale = horizontal ? yScale : xScale;
5715
+ const valueScale = horizontal ? xScale : yScale;
5716
+ const groupScale = useMemo(() => createGroupScale(primaryKeys, bandScale?.bandwidth?.() ?? 0, groupPadding), [
5717
+ primaryKeys,
5718
+ bandScale,
5719
+ groupPadding
5720
+ ]);
5721
+ const renderGlyph = useCallback(({ datum, x, y, color, onBlur, onFocus, onPointerMove, onPointerOut, onPointerUp }) => {
5722
+ if (!bandScale?.bandwidth || !valueScale) return null;
5723
+ const baseline = getValueScaleBaseline(valueScale);
5724
+ const position = Number(bandScale((horizontal ? props.yAccessor : props.xAccessor)(datum))) + (groupScale(props.dataKey) ?? 0);
5725
+ const value = horizontal ? x : y;
5726
+ return /* @__PURE__ */ jsx("rect", {
5727
+ className: clsx("visx-bar", barClassName(datum)),
5728
+ tabIndex: onFocus || onBlur ? 0 : void 0,
5729
+ onBlur,
5730
+ onFocus,
5731
+ onPointerMove,
5732
+ onPointerOut,
5733
+ onPointerUp,
5734
+ x: horizontal ? baseline + Math.min(0, value - baseline) : position,
5735
+ y: horizontal ? position : baseline + Math.min(0, value - baseline),
5736
+ width: horizontal ? Math.abs(value - baseline) : groupScale.bandwidth(),
5737
+ height: horizontal ? groupScale.bandwidth() : Math.abs(value - baseline),
5738
+ fill: color
5739
+ });
5740
+ }, [
5741
+ bandScale,
5742
+ valueScale,
5743
+ horizontal,
5744
+ groupScale,
5745
+ props.dataKey,
5746
+ props.xAccessor,
5747
+ props.yAccessor,
5748
+ barClassName
5749
+ ]);
5750
+ return /* @__PURE__ */ jsx(GlyphSeries, {
5751
+ ...props,
5752
+ renderGlyph
5753
+ });
5754
+ };
5711
5755
  /**
5712
5756
  * Upper clamp on the computed group padding, so bars can never collapse to zero width
5713
5757
  * even at very large `widthFactor` values.
@@ -5951,7 +5995,7 @@ const renderTooltipRow = (label, value) => /* @__PURE__ */ jsx("div", {
5951
5995
  className: bar_chart_module_default["bar-chart__tooltip-row"],
5952
5996
  children: sprintf(__("%1$s: %2$s", "jetpack-charts"), label, value)
5953
5997
  });
5954
- const BarChartInternal = ({ data, chartId: providedChartId, width, height, className, margin, withTooltips = false, showLegend = false, legend = {}, gridVisibility: gridVisibilityProp, renderTooltip, tooltipPlacement, tooltipAnchorTop, options = {}, orientation = "vertical", withPatterns = false, showZeroValues = false, withBandHighlight = false, onBandHighlightChange, defaultHiddenSeries, animation, children, gap = "md", onPointerDown, onPointerUp, onDatumActivate }) => {
5998
+ const BarChartInternal = ({ data, chartId: providedChartId, width, height, className, margin, withTooltips = false, showLegend = false, legend = {}, gridVisibility: gridVisibilityProp, renderTooltip, tooltipPlacement, tooltipAnchorTop, tooltipStyle, barClassName, options = {}, orientation = "vertical", withPatterns = false, showZeroValues = false, withBandHighlight = false, onBandHighlightChange, defaultHiddenSeries, animation, children, gap = "md", onPointerDown, onPointerUp, onDatumActivate }) => {
5955
5999
  if (!withTooltips && (withBandHighlight || onBandHighlightChange)) warnOnce("bar-chart-band-highlight-without-tooltips", "BarChart: withBandHighlight and onBandHighlightChange require withTooltips.");
5956
6000
  const legendInteractive = legend.interactive ?? false;
5957
6001
  const legendCollapseGroups = legend.collapseGroups ?? false;
@@ -6321,7 +6365,19 @@ const BarChartInternal = ({ data, chartId: providedChartId, width, height, class
6321
6365
  getElementStyles,
6322
6366
  resolveFill: resolveComparisonFill
6323
6367
  }),
6324
- /* @__PURE__ */ jsx(BarGroup, {
6368
+ barClassName ? /* @__PURE__ */ jsx("g", {
6369
+ className: "visx-bar-group",
6370
+ children: primaryEntries.map(({ series: seriesData, index }) => /* @__PURE__ */ jsx(ClassifiedBarSeries, {
6371
+ dataKey: seriesData.label,
6372
+ data: seriesData.data,
6373
+ xAccessor: chartOptions.accessors.xAccessor,
6374
+ yAccessor: chartOptions.accessors.yAccessor,
6375
+ colorAccessor: getBarBackground(index),
6376
+ barClassName,
6377
+ primaryKeys,
6378
+ groupPadding
6379
+ }, seriesData.label))
6380
+ }) : /* @__PURE__ */ jsx(BarGroup, {
6325
6381
  padding: groupPadding,
6326
6382
  children: primaryEntries.map(({ series: seriesData, index }) => /* @__PURE__ */ jsx(BarSeries, {
6327
6383
  dataKey: seriesData?.label,
@@ -6351,6 +6407,7 @@ const BarChartInternal = ({ data, chartId: providedChartId, width, height, class
6351
6407
  withTooltips && /* @__PURE__ */ jsx(AccessibleTooltip, {
6352
6408
  tooltipPlacement,
6353
6409
  tooltipAnchorTop,
6410
+ style: tooltipStyle,
6354
6411
  detectBounds: true,
6355
6412
  snapTooltipToDatumX: true,
6356
6413
  snapTooltipToDatumY: true,