@vx/responsive

The @vx/responsive package is here to help you make responsive graphs.

With Enhancers

withScreenSize

withParentSize

With Components

ParentSize

ScaleSVG

withScreenSize

If you would like your graph to adapt to the screen size, you can use withScreenSize(). The resulting component will pass screenWidth and screenHeight props to the wrapped component containing the respective screen dimensions.

Example:

import { withScreenSize } from '@vx/responsive';
// or
// import * as Responsive from '@vx/responsive';
// Responsive.withScreenSize(...);

let chartToRender = withScreenSize(MySuperCoolVxChart);

// ... Render the chartToRender somewhere

withParentSize

If you would like your graph to adapt to it's parent component's size, you can use withParentSize(). The resulting component will pass parentWidth and parentHeight props to the wrapped component containing the respective parent's dimensions.

Example:

import { withParentSize } from '@vx/responsive';
// or
// import * as Responsive from '@vx/responsive';
// Responsive.withParentSize(...);

let chartToRender = withParentSize(MySuperCoolVxChart);

// ... Render the chartToRender somewhere

ParentSize

You might do the same thing using the ParentSize component.

Example:

import { ParentSize } from '@vx/responsive';
// or
// import * as Responsive from '@vx/responsive';
// <Responsive.ParentSize />;

let chartToRender = (
  <ParentSize>
    {parent => (
      <MySuperCoolVxChart
        parentWidth={parent.width}
        parentHeight={parent.height}
        parentTop={parent.top}
        parentLeft={parent.left}
        // this is the referer to the wrapper component
        parentRef={parent.ref}
        // this function can be called inside MySuperCoolVxChart to cause a resize of the wrapper component
        resizeParent={parent.resize}
      />
    )}
  </ParentSize>
);

// ... Render the chartToRender somewhere

ScaleSVG

You can also create a responsive chart with a specific viewBox with the ScaleSVG component.

Example:

import { ScaleSVG } from '@vx/responsive';
// or
// import * as Responsive from '@vx/responsive';
// <Responsive.ScaleSVG />

let chartToRender = (
  <ScaleSVG width={400} height={400}>
    <MySuperCoolVXChart />
  </ScaleSVG>
);

// ... Render the chartToRender somewhere

Installation

npm install --save @vx/responsive

Examples

Components

APIs

#<ParentSize />

# ParentSize.childrenstring | number | boolean | {} | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ... 4 more ... | undefinedoptional

Child render function ({ width, height, top, left, ref, resize }) => ReactNode.

# ParentSize.classNamestring | undefinedoptional

Optional className to add to the parent div wrapper used for size measurement.

# ParentSize.debounceTimenumber | undefinedoptional

Child render updates upon resize are delayed until debounceTime milliseconds after the last resize event is observed.

Default 300

# ParentSize.enableDebounceLeadingCallboolean | undefinedoptional

Optional flag to toggle leading debounce calls. When set to true this will ensure that the component always renders immediately. (defaults to true)

Default true

# ParentSize.parentSizeStylesCSSProperties | undefinedoptional

Optional style object to apply to the parent div wrapper used for size measurement.

Default { width: '100%', height: '100%' }

#<ScaleSVG />

# ScaleSVG.childrenReactNodeoptional

Child SVG to scale, rendered as the child of the parent wrappers provided by this component <div><svg>{children}</svg></div>.

# ScaleSVG.heightstring | number | undefinedoptional

Height of the desired SVG.

# ScaleSVG.innerRef((instance: SVGSVGElement | null) => void) | RefObject<SVGSVGElement> | null | undefinedoptional

Ref to the parent <svg /> used for scaling.

# ScaleSVG.preserveAspectRatiostring | undefinedoptional

Whether to preserve SVG aspect ratio.

Default xMinYMin meet

# ScaleSVG.widthstring | number | undefinedoptional

Width of the desired SVG.

# ScaleSVG.xOriginstring | number | undefinedoptional

xOrigin of the desired SVG.

Default 0

# ScaleSVG.yOriginstring | number | undefinedoptional

yOrigin of the desired SVG.

Default 0