Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[axis] render prop for axis, full control over rendering #165

Merged
merged 1 commit into from
Oct 5, 2017

Conversation

hshoff
Copy link
Member

@hshoff hshoff commented Oct 5, 2017

This is meant to address

and any future customization/extension by passing render control to the user.

So now you can do:

     <AxisBottom
        top={height - margin.bottom}
        left={margin.left}
        scale={xScale}
        numTicks={numTicksForWidth(width)}
        label="time"
      >
        {props => {
          const tickLabelSize = 10;
          const tickRotate = 45;
          const tickColor = '#8e205f';
          const axisCenter =
            (props.axisToPoint.x - props.axisFromPoint.x) / 2;
          return (
            <g className="my-custom-bottom-axis">
              {props.ticks.map((tick, i) => {
                const tickX = tick.to.x;
                const tickY =
                  tick.to.y + tickLabelSize + props.tickLength;
                return (
                  <Group
                    key={`vx-tick-${tick.value}-${i}`}
                    className={'vx-axis-tick'}
                  >
                    <Line
                      from={tick.from}
                      to={tick.to}
                      stroke={tickColor}
                    />
                    <text
                      transform={`translate(${tickX}, ${tickY}) rotate(${tickRotate})`}
                      fontSize={tickLabelSize}
                      textAnchor="middle"
                      fill={tickColor}
                    >
                      {tick.formattedValue}
                    </text>
                  </Group>
                );
              })}
              <text
                textAnchor="middle"
                transform={`translate(${axisCenter}, 50)`}
                fontSize="8"
              >
                {props.label}
              </text>
            </g>
          );
        }}
      </AxisBottom>

and you get something like:

screen shot 2017-10-05 at 2 18 57 pm

the children function will be called with (hopefully) everything you need to make your cool custom axis:

// children function props
{
  axisFromPoint: { x: 0, y: 10.5 },
  axisToPoint: { x: 0, y: 0.5 },
  horizontal: false,
  tickSign: -1,
  numTicks: 10,
  label: "test axis",
  rangePadding: 0,
  tickLength: 8,
  tickFormat: "function",
  tickPosition: "function",
  ticks: [
    {
      value: 0,
      index: 0,
      from: [Object],
      to: [Object],
      formattedValue: "0"
    }
    // ...
  ]
}

This is an enhancement and not a breaking change so all existing Axis implementations should be fine. This is just if you want more control.

cc @williaster

@hshoff hshoff added this to the v0.0.141 milestone Oct 5, 2017
@hshoff hshoff merged commit 7b845ac into master Oct 5, 2017
@hshoff hshoff deleted the harry-axis-141 branch October 5, 2017 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant