lucid-ui
Version:
A UI component library from AppNexus.
119 lines (106 loc) • 5.07 kB
JavaScript
import _get from "lodash/get";
import _map from "lodash/map";
import _keys from "lodash/keys";
import _flatten from "lodash/flatten";
import _max from "lodash/max";
import _isDate from "lodash/isDate";
import _isFinite from "lodash/isFinite";
import _last from "lodash/last";
import _isArray from "lodash/isArray";
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps } from '../../util/component-types';
import { groupByFields } from '../../util/chart-helpers';
import * as d3Shape from 'd3-shape';
import * as chartConstants from '../../constants/charts';
import Point from '../Point/Point';
var cx = lucidClassNames.bind('&-Points');
var arrayOf = PropTypes.arrayOf,
func = PropTypes.func,
number = PropTypes.number,
object = PropTypes.object,
bool = PropTypes.bool,
string = PropTypes.string;
function isValidSeries(series) {
if (_isArray(series)) {
var last = _last(series);
return _isFinite(last) || _isDate(last);
}
return _isFinite(series) || _isDate(series);
}
var defaultProps = {
xField: 'x',
yFields: ['y'],
colorOffset: 0,
hasStroke: true,
isStacked: false,
palette: chartConstants.PALETTE_7
};
export var Points = function Points(props) {
var className = props.className,
data = props.data,
palette = props.palette,
colorMap = props.colorMap,
colorOffset = props.colorOffset,
xField = props.xField,
hasStroke = props.hasStroke,
xScale = props.xScale,
yFields = props.yFields,
yStackedMax = props.yStackedMax,
isStacked = props.isStacked,
yScaleOriginal = props.yScale,
passThroughs = _objectWithoutProperties(props, ["className", "data", "palette", "colorMap", "colorOffset", "xField", "hasStroke", "xScale", "yFields", "yStackedMax", "isStacked", "yScale"]); // Copy the original so we can mutate it
var yScale = yScaleOriginal.copy(); // If we are stacked, we need to calculate a new domain based on the sum of
// the various series' y data. One row per series.
var transformedData = isStacked ? d3Shape.stack().keys(yFields)(data) : groupByFields(data, yFields); // If we are stacked, we need to calculate a new domain based on the sum of
// the various group's y data
if (isStacked) {
yScale.domain([yScale.domain()[0], yStackedMax || _max(_flatten(_last(transformedData)))]);
}
return /*#__PURE__*/React.createElement("g", _extends({}, omitProps(passThroughs, undefined, _keys(Points.propTypes)), {
className: cx(className, '&')
}), _map(transformedData, function (d, dIndex) {
return _map(d, function (series, seriesIndex) {
if (isValidSeries(series)) {
return /*#__PURE__*/React.createElement(Point, {
key: "".concat(seriesIndex).concat(dIndex)
/* Since data contains x and y values, data values may not have a uniform type that always matches
the expected input of the xScale */
//@ts-ignore
,
x: xScale(data[seriesIndex][xField]),
y: yScale(_isArray(series) ? _last(series) : series),
hasStroke: hasStroke,
kind: dIndex + colorOffset,
color: _get(colorMap, yFields[dIndex], palette[(dIndex + colorOffset) % palette.length])
});
}
});
}));
};
Points.defaultProps = defaultProps;
Points.displayName = 'Points';
Points.peek = {
description: "\n\t\t*For use within an `svg`*\n\n\t\tPut some points on that data.\n\t",
categories: ['visualizations', 'chart primitives'],
madeFrom: ['Point']
};
Points.propTypes = {
className: string,
palette: arrayOf(string),
colorMap: object,
data: arrayOf(object).isRequired,
xScale: func.isRequired,
yScale: func.isRequired,
xField: string,
yFields: arrayOf(string),
yStackedMax: number,
colorOffset: number,
hasStroke: bool,
isStacked: bool
};
export default Points;