Skip to content

Commit

Permalink
feat(legends): init legends package
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Dec 6, 2017
1 parent 489f36f commit c27aae4
Show file tree
Hide file tree
Showing 12 changed files with 883 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/nivo-bar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"jsnext:main": "es/index.js",
"dependencies": {
"@nivo/core": "0.32.0",
"@nivo/legend": "0.32.0",
"d3-scale": "^1.0.6",
"d3-shape": "^1.2.0",
"react-motion": "^0.5.2",
Expand Down
115 changes: 115 additions & 0 deletions packages/nivo-bar/src/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import setDisplayName from 'recompose/setDisplayName'
import enhance from './enhance'
import { BarPropTypes } from './props'
import { Container, SvgWrapper } from '@nivo/core'
import { BoxLegendSvg } from '@nivo/legends'
import { Grid, Axes } from '@nivo/core'
import { CartesianMarkers } from '@nivo/core'

Expand Down Expand Up @@ -254,6 +255,120 @@ const Bar = ({
yScale={result.yScale}
theme={theme}
/>
<g transform={`translate(${-margin.left},${-margin.top})`}>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={24}
direction="row"
anchor="top"
height={36}
data={keys}
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={24}
direction="column"
anchor="top-right"
height={36}
data={keys}
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={24}
direction="column"
anchor="right"
justify={true}
height={36}
data={keys}
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={64}
itemHeight={42}
direction="row"
anchor="bottom"
height={36}
data={keys}
itemDirection="top-to-bottom"
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={36}
direction="column"
anchor="bottom-right"
itemDirection="bottom-to-top"
height={36}
data={keys}
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={24}
margin={margin}
direction="column"
anchor="left"
itemDirection="right-to-left"
height={36}
data={keys}
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={24}
direction="column"
anchor="bottom-left"
itemDirection="right-to-left"
justify={true}
height={36}
data={keys}
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={36}
direction="column"
anchor="top-left"
itemDirection="top-to-bottom"
height={36}
data={keys}
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={42}
direction="column"
anchor="top"
itemDirection="top-to-bottom"
justify={true}
height={36}
data={keys}
/>
<BoxLegendSvg
containerWidth={outerWidth}
containerHeight={outerHeight}
itemWidth={100}
itemHeight={42}
direction="column"
anchor="bottom"
itemDirection="bottom-to-top"
justify={true}
height={36}
data={keys}
/>
</g>
</SvgWrapper>
)
}}
Expand Down
20 changes: 20 additions & 0 deletions packages/nivo-legends/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# src (will be transpiled)
/src

# logs
*.log*

# OSX
.DS_Store

# config/build
.babelrc

# storybook
/.storybook
/stories
/storybook-static

# test
/coverage
/tests
37 changes: 37 additions & 0 deletions packages/nivo-legends/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@nivo/legends",
"version": "0.32.0",
"license": "MIT",
"main": "./lib/index.js",
"module": "es/index.js",
"jsnext:main": "es/index.js",
"dependencies": {
"recompose": "^0.26.0"
},
"devDependencies": {
"@nivo/babel-preset": "0.32.0",
"babel-cli": "^6.26.0",
"babel-jest": "^20.0.3",
"cross-env": "^5.0.5",
"jest": "^21.0.1",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-test-renderer": "^15.6.1"
},
"peerDependencies": {
"prop-types": "^15.5.10",
"react": ">= 15.6.1 < 17.0.0"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "jest --verbose ./tests",
"test:cover": "jest --verbose --coverage ./tests",
"build:commonjs": "rm -rf lib && cross-env NODE_ENV=commonjs babel src --out-dir lib",
"build:commonjs:watch": "npm run build:commonjs -- --watch",
"build:es": "rm -rf es && cross-env NODE_ENV=es babel src --out-dir es",
"build:es:watch": "npm run build:es -- --watch",
"build": "npm run build:commonjs && npm run build:es"
}
}
24 changes: 24 additions & 0 deletions packages/nivo-legends/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
export const DIRECTION_ROW = 'row'
export const DIRECTION_COLUMN = 'column'

export const ANCHOR_TOP = 'top'
export const ANCHOR_TOP_RIGHT = 'top-right'
export const ANCHOR_RIGHT = 'right'
export const ANCHOR_BOTTOM_RIGHT = 'bottom-right'
export const ANCHOR_BOTTOM = 'bottom'
export const ANCHOR_BOTTOM_LEFT = 'bottom-left'
export const ANCHOR_LEFT = 'left'
export const ANCHOR_TOP_LEFT = 'top-left'

export const DIRECTION_LEFT_TO_RIGHT = 'left-to-right'
export const DIRECTION_RIGHT_TO_LEFT = 'right-to-left'
export const DIRECTION_TOP_TO_BOTTOM = 'top-to-bottom'
export const DIRECTION_BOTTOM_TO_TOP = 'bottom-to-top'
9 changes: 9 additions & 0 deletions packages/nivo-legends/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
export * from './svg'
141 changes: 141 additions & 0 deletions packages/nivo-legends/src/svg/BoxLegendSvg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import React from 'react'
import PropTypes from 'prop-types'
import LegendSvg from './LegendSvg'
import {
DIRECTION_ROW,
DIRECTION_COLUMN,
DIRECTION_BOTTOM_TO_TOP,
DIRECTION_LEFT_TO_RIGHT,
DIRECTION_RIGHT_TO_LEFT,
DIRECTION_TOP_TO_BOTTOM,
ANCHOR_TOP,
ANCHOR_TOP_RIGHT,
ANCHOR_RIGHT,
ANCHOR_BOTTOM_RIGHT,
ANCHOR_BOTTOM,
ANCHOR_BOTTOM_LEFT,
ANCHOR_LEFT,
ANCHOR_TOP_LEFT,
} from '../constants'

const BoxLegendSvg = ({
data,

containerWidth,
containerHeight,
anchor,
direction,
justify,

itemWidth,
itemHeight,
itemDirection,
itemsSpacing,
symbolSize,
symbolSpacing,
}) => {
let x = 0
let y = 0

let width = itemWidth
let height = itemHeight
if (direction === DIRECTION_ROW) {
width = itemWidth * data.length
} else if (direction === DIRECTION_COLUMN) {
height = itemHeight * data.length
}

switch (anchor) {
case ANCHOR_TOP:
x = (containerWidth - width) / 2
break

case ANCHOR_TOP_RIGHT:
x = containerWidth - width
break

case ANCHOR_RIGHT:
x = containerWidth - width
y = (containerHeight - height) / 2
break

case ANCHOR_BOTTOM_RIGHT:
x = containerWidth - width
y = containerHeight - height
break

case ANCHOR_BOTTOM:
x = (containerWidth - width) / 2
y = containerHeight - height
break

case ANCHOR_BOTTOM_LEFT:
y = containerHeight - height
break

case ANCHOR_LEFT:
y = (containerHeight - height) / 2
break
}

return (
<LegendSvg
data={data}
x={x}
y={y}
width={width}
height={height}
direction={direction}
justify={justify}
itemWidth={itemWidth}
itemHeight={itemHeight}
itemDirection={itemDirection}
itemsSpacing={itemsSpacing}
symbolSize={symbolSize}
symbolSpacing={symbolSpacing}
/>
)
}

BoxLegendSvg.propTypes = {
containerWidth: PropTypes.number.isRequired,
containerHeight: PropTypes.number.isRequired,
anchor: PropTypes.oneOf([
ANCHOR_TOP,
ANCHOR_TOP_RIGHT,
ANCHOR_RIGHT,
ANCHOR_BOTTOM_RIGHT,
ANCHOR_BOTTOM,
ANCHOR_BOTTOM_LEFT,
ANCHOR_LEFT,
ANCHOR_TOP_LEFT,
]).isRequired,
direction: PropTypes.oneOf([DIRECTION_ROW, DIRECTION_COLUMN]).isRequired,
justify: PropTypes.bool,

itemWidth: PropTypes.number.isRequired,
itemHeight: PropTypes.number.isRequired,
itemDirection: PropTypes.oneOf([
DIRECTION_LEFT_TO_RIGHT,
DIRECTION_RIGHT_TO_LEFT,
DIRECTION_TOP_TO_BOTTOM,
DIRECTION_BOTTOM_TO_TOP,
]),
itemsSpacing: PropTypes.number.isRequired,
symbolSize: PropTypes.number,
symbolSpacing: PropTypes.number,
}

BoxLegendSvg.defaultProps = {
itemsSpacing: 0,
}

export default BoxLegendSvg
Loading

0 comments on commit c27aae4

Please sign in to comment.