Victory Native XL

repository·main·Indexed 22 days ago

https://github.com/formidablelabs/victory-native-xl

A high-performance charting library for React Native that leverages D3 for math, Skia for rendering, and Reanimated for smooth animations. It provides a suite of components for 2D visualizations, including CartesianChart, Line, Bar, StackedBar, Area, and StackedArea, along with specialized tools like AnimatedPath for synchronized animations and the CURVES object for various spline and step interpolations.

Tokens
55.3K
Snippets
82
Records
183
Agent score
77%

What's inside victory-native-xl

  1. Introduction to Victory Native (XL)

    main

    Victory Native (XL) is a high-performance rewrite of the original Victory Native. It is designed to provide flexibility and ease of use while maintaining high frame rates for data visualizations on mobile devices.

    Unlike the original version which relied on SVG, Victory Native (XL) is built on a modern graphics stack to ensure smooth animations (targeting 100+ FPS) even on low-end devices.

  2. What is the CartesianChart component?

    main

    The CartesianChart component is the core component of victory-native. It serves two primary responsibilities:

    1. Data Ingestion: It accepts raw data and configuration options (such as axis settings).
    2. Data Transformation: It transforms raw data into a structured format that can be consumed by other victory-native components (like Line) via a render function.

    To use it, you provide your data, specify which keys represent the x-axis and y-axes, and then use the render function to draw your chart elements based on the transformed data points.

    <CartesianChart
      data={DATA}
      xKey="day"
      yKeys={["lowTmp", "highTmp"]}
      axisOptions={{ font }}
    >
      {({ points }) => (
        <Line points={points.highTmp} color="red" strokeWidth={3} />
      )}
    </CartesianChart>
  3. Use `useHorizontalStackedBarPaths` for horizontal charts

    main

    If you are rendering a horizontal stacked bar chart using CartesianChart orientation="horizontal", you should use the useHorizontalStackedBarPaths hook instead of useStackedBarPaths.

    Its arguments are identical to useStackedBarPaths, but the barOptions callback provides horizontal-specific orientation fields: isLeft and isRight instead of isBottom and isTop.

  4. Understand data mapping in horizontal mode

    main

    When using HorizontalBar within a CartesianChart with orientation="horizontal", the coordinate system and chartPressState values map as follows:

    Coordinate Mapping

    • points[key].x: The value endpoint on screen (horizontal position).
    • points[key].y: The category center on screen (vertical position).
    • points[key].xValue: The raw category value.
    • points[key].yValue: The raw numeric value.

    chartPressState Mapping

    When interacting with the chart, the press state preserves semantic names:

    • state.x.value.value: The raw category from xKey.
    • state.x.position.value: The category's vertical screen position.
    • state.y[key].value.value: The numeric series value.
    • state.y[key].position.value: The value's horizontal screen position.
  5. Access OHLC data via Chart Press State

    main

    The Candlestick component integrates with the CartesianChart press model. When a user touches the chart, the nearest x value is selected, and the press state exposes the corresponding OHLC values. This allows for mobile-friendly scrubbing without requiring the user to touch the candle body exactly.

    Available properties on the press state:

    • state.matchedIndex.value
    • state.x.value.value
    • state.x.position.value
    • state.y.open.value.value
    • state.y.high.value.value
    • state.y.low.value.value
    • state.y.close.value.value
  6. Customize axis label and tick behavior

    main

    Axis Label Formatting

    • Multiline Labels: Formatters can return newline-delimited strings (e.g., "Jan\n2024"). Layout measurement uses the widest line and total line height to reserve space.
    • Hiding Labels: Returning an empty string "" from formatXLabel or formatYLabel hides the label and prevents it from reserving fallback or offset space.

    Tick Configuration

    • Downsampling: When tickValues are combined with tickCount, the tick values are downsampled to the requested count.
    • Disabling Ticks: Passing tickCount={0} renders no ticks or tick labels, even if explicit tickValues are provided.
  7. The Victory Native (XL) technology stack

    main

    Victory Native (XL) leverages several high-performance libraries to handle rendering, animations, and gestures:

    • React Native Skia: Provides the Skia rendering engine (the same engine used in Google Chrome) for sophisticated and performant graphics.
    • React Native Reanimated (v3): Handles high-performance UI animations.
    • React Native Gesture Handler (v2): Provides performant ways to handle user gestures.
    • D3: Used for mathematical and data-driven logic.
  8. How Pie.Chart children and render functions work

    main

    By default, Pie.Chart renders a simple <Pie.Slice /> for every data point. However, you can provide a children render function to customize the rendering of each slice. This allows you to inject components like <Pie.Slice />, <Pie.Label />, <Pie.SliceAngularInsets />, or <LinearGradient /> for each segment.

    The render function receives an object containing a slice property of type PieSliceData, which provides the calculated geometry and data for that specific segment.

  9. Use HorizontalBarGroup for grouped horizontal bars

    main

    The HorizontalBarGroup component is used within a CartesianChart with orientation="horizontal" to render multiple numeric series as separate bars within the same category row.

    Key behaviors:

    • Each category group is centered on the category's position.
    • Bars within a group are offset vertically.
    • Positive values extend to the right from zero; negative values extend to the left.
    • For stacked horizontal bars instead of grouped ones, use HorizontalStackedBar.
    import { CartesianChart, HorizontalBarGroup } from "victory-native";
    
    const DATA = [
      { category: "North", product: 72, services: 44 },
      { category: "West", product: 58, services: 62 },
      { category: "South", product: 41, services: 38 },
    ];
    
    export function MyChart() {
      return (
        <CartesianChart
          orientation="horizontal"
          data={DATA}
          xKey="category"
          yKeys={["product", "services"]}
          domain={{ x: [0, 100] }}
          domainPadding={{ top: 32, bottom: 32, right: 24 }}
          xAxis={{ tickCount: 5 }}
          yAxis={[{ yKeys: ["product", "services"] }]}
        >
          {({ points, chartBounds }) => (
            <HorizontalBarGroup
              chartBounds={chartBounds}
              betweenGroupPadding={0.35}
              withinGroupPadding={0.2}
              roundedCorners={{ topRight: 6, bottomRight: 6 }}
            >
              <HorizontalBarGroup.Bar points={points.product} color="teal" />
              <HorizontalBarGroup.Bar points={points.services} color="purple" />
            </HorizontalBarGroup>
          )}
        </CartesianChart>
      );
    }