lucid-ui
Version:
A UI component library from AppNexus.
131 lines • 12.6 kB
JavaScript
"use strict";
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Lines = void 0;
var lodash_1 = __importDefault(require("lodash"));
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("react-peek/prop-types"));
var style_helpers_1 = require("../../util/style-helpers");
var component_types_1 = require("../../util/component-types");
var chart_helpers_1 = require("../../util/chart-helpers");
var d3Shape = __importStar(require("d3-shape"));
var chartConstants = __importStar(require("../../constants/charts"));
var Line_1 = __importDefault(require("../Line/Line"));
var cx = style_helpers_1.lucidClassNames.bind('&-Lines');
var arrayOf = prop_types_1.default.arrayOf, func = prop_types_1.default.func, number = prop_types_1.default.number, object = prop_types_1.default.object, bool = prop_types_1.default.bool, string = prop_types_1.default.string;
var isUniform = function (array) {
return lodash_1.default.every(array, function (val) { return val === lodash_1.default.first(array); });
};
var defaultProps = {
xField: 'x',
yFields: ['y'],
isStacked: false,
colorOffset: 0,
palette: chartConstants.PALETTE_7,
};
var Lines = function (props) {
var className = props.className, data = props.data, isStacked = props.isStacked, palette = props.palette, colorMap = props.colorMap, colorOffset = props.colorOffset, xScale = props.xScale, xField = props.xField, yFields = props.yFields, yScaleOriginal = props.yScale, yStackedMax = props.yStackedMax, passThroughs = __rest(props, ["className", "data", "isStacked", "palette", "colorMap", "colorOffset", "xScale", "xField", "yFields", "yScale", "yStackedMax"]);
// 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)
: chart_helpers_1.groupByFields(data, yFields);
var stackedArea = d3Shape
.area()
.defined(function (a) { return lodash_1.default.isFinite(a[0]) && lodash_1.default.isFinite(a[1]); })
.x(function (a, i) { return xScale(data[i][xField]); })
.y0(function (a) { return yScale(a[1]); })
.y1(function (a) { return yScale(a[0]); });
var area = d3Shape
.area()
.defined(function (a) { return lodash_1.default.isFinite(a) || lodash_1.default.isDate(a); })
.x(function (a, i) { return xScale(data[i][xField]); })
.y(function (a, i) { return yScale(a); });
// 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 || lodash_1.default.max(lodash_1.default.flatten(lodash_1.default.last(transformedData))),
]);
}
return (react_1.default.createElement("g", __assign({}, component_types_1.omitProps(passThroughs, undefined, lodash_1.default.keys(exports.Lines.propTypes)), { className: cx(className, '&') }), lodash_1.default.map(transformedData, function (d, dIndex) { return (react_1.default.createElement("g", { key: dIndex },
react_1.default.createElement(Line_1.default, { color: lodash_1.default.get(colorMap, yFields[dIndex], palette[(dIndex + colorOffset) % palette.length]), d: (isStacked ? stackedArea(d) : area(d)),
// Fixes a bug in chrome by forcing a reflow of the element (ANXR-1405)
style: isUniform(d) ? { transform: 'scaleX(0.999)' } : undefined }))); })));
};
exports.Lines = Lines;
exports.Lines.defaultProps = defaultProps;
exports.Lines.displayName = 'Lines';
exports.Lines.peek = {
description: "\n\t\t*For use within an `svg`*\n\n\t\tLines are typically used to represent continuous data and can be\n\t\tstacked.\n\t",
categories: ['visualizations', 'chart primitives'],
madeFrom: ['Line'],
};
exports.Lines.propTypes = {
className: string(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\tAppended to the component-specific class names set on the root element.\n\t"], ["\n\t\tAppended to the component-specific class names set on the root element.\n\t"]))),
top: number(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\tTop\n\t"], ["\n\t\tTop\n\t"]))),
left: number(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\tLeft\n\t"], ["\n\t\tLeft\n\t"]))),
palette: arrayOf(string)(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\tTakes one of the palettes exported from `lucid.chartConstants`.\n\t\tAvailable palettes:\n\n\t\t- `PALETTE_7` (default)\n\t\t- `PALETTE_30`\n\t\t- `PALETTE_MONOCHROME_0_5`\n\t\t- `PALETTE_MONOCHROME_1_5`\n\t\t- `PALETTE_MONOCHROME_2_5`\n\t\t- `PALETTE_MONOCHROME_3_5`\n\t\t- `PALETTE_MONOCHROME_4_5`\n\t\t- `PALETTE_MONOCHROME_5_5`\n\t\t- `PALETTE_MONOCHROME_6_5`\n\t"], ["\n\t\tTakes one of the palettes exported from \\`lucid.chartConstants\\`.\n\t\tAvailable palettes:\n\n\t\t- \\`PALETTE_7\\` (default)\n\t\t- \\`PALETTE_30\\`\n\t\t- \\`PALETTE_MONOCHROME_0_5\\`\n\t\t- \\`PALETTE_MONOCHROME_1_5\\`\n\t\t- \\`PALETTE_MONOCHROME_2_5\\`\n\t\t- \\`PALETTE_MONOCHROME_3_5\\`\n\t\t- \\`PALETTE_MONOCHROME_4_5\\`\n\t\t- \\`PALETTE_MONOCHROME_5_5\\`\n\t\t- \\`PALETTE_MONOCHROME_6_5\\`\n\t"]))),
colorMap: object(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\t\tYou can pass in an object if you want to map fields to\n\t\t`lucid.chartConstants` or custom colors:\n\n\t\t\t{\n\t\t\t\t'imps': COLOR_0,\n\t\t\t\t'rev': COLOR_3,\n\t\t\t\t'clicks': '#abc123',\n\t\t\t}\n\t"], ["\n\t\tYou can pass in an object if you want to map fields to\n\t\t\\`lucid.chartConstants\\` or custom colors:\n\n\t\t\t{\n\t\t\t\t'imps': COLOR_0,\n\t\t\t\t'rev': COLOR_3,\n\t\t\t\t'clicks': '#abc123',\n\t\t\t}\n\t"]))),
data: arrayOf(object).isRequired(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n\t\tDe-normalized data, e.g.\n\n\t\t\t[\n\t\t\t\t{ x: 'one' , y: 1 },\n\t\t\t\t{ x: 'two' , y: 2 },\n\t\t\t\t{ x: 'three' , y: 2 },\n\t\t\t\t{ x: 'four' , y: 3 },\n\t\t\t\t{ x: 'five' , y: 4 },\n\t\t\t]\n\n\n\t\tOr (be sure to set `yFields` to `['y0', 'y1', 'y2', 'y3']`)\n\n\t\t\t[\n\t\t\t\t{ x: 'one' , y0: 1 , y1: 2 , y2: 3 , y3: 5 },\n\t\t\t\t{ x: 'two' , y0: 2 , y1: 3 , y2: 4 , y3: 6 },\n\t\t\t\t{ x: 'three' , y0: 2 , y1: 4 , y2: 5 , y3: 6 },\n\t\t\t\t{ x: 'four' , y0: 3 , y1: 6 , y2: 7 , y3: 7 },\n\t\t\t\t{ x: 'five' , y0: 4 , y1: 8 , y2: 9 , y3: 8 },\n\t\t\t\t{ x: 'six' , y0: 20 , y1: 8 , y2: 9 , y3: 1 },\n\t\t\t]\n\t"], ["\n\t\tDe-normalized data, e.g.\n\n\t\t\t[\n\t\t\t\t{ x: 'one' , y: 1 },\n\t\t\t\t{ x: 'two' , y: 2 },\n\t\t\t\t{ x: 'three' , y: 2 },\n\t\t\t\t{ x: 'four' , y: 3 },\n\t\t\t\t{ x: 'five' , y: 4 },\n\t\t\t]\n\n\n\t\tOr (be sure to set \\`yFields\\` to \\`['y0', 'y1', 'y2', 'y3']\\`)\n\n\t\t\t[\n\t\t\t\t{ x: 'one' , y0: 1 , y1: 2 , y2: 3 , y3: 5 },\n\t\t\t\t{ x: 'two' , y0: 2 , y1: 3 , y2: 4 , y3: 6 },\n\t\t\t\t{ x: 'three' , y0: 2 , y1: 4 , y2: 5 , y3: 6 },\n\t\t\t\t{ x: 'four' , y0: 3 , y1: 6 , y2: 7 , y3: 7 },\n\t\t\t\t{ x: 'five' , y0: 4 , y1: 8 , y2: 9 , y3: 8 },\n\t\t\t\t{ x: 'six' , y0: 20 , y1: 8 , y2: 9 , y3: 1 },\n\t\t\t]\n\t"]))),
xScale: func.isRequired(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n\t\tThe scale for the x axis. Must be a d3 scale. Lucid exposes the\n\t\t`lucid.d3Scale` library for use here.\n\t"], ["\n\t\tThe scale for the x axis. Must be a d3 scale. Lucid exposes the\n\t\t\\`lucid.d3Scale\\` library for use here.\n\t"]))),
yScale: func.isRequired(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\n\t\tThe scale for the y axis. Must be a d3 scale. Lucid exposes the\n\t\t`lucid.d3Scale` library for use here.\n\t"], ["\n\t\tThe scale for the y axis. Must be a d3 scale. Lucid exposes the\n\t\t\\`lucid.d3Scale\\` library for use here.\n\t"]))),
yStackedMax: number(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\n\t\tTypically this number can be derived from the yScale. However when we're\n\t\t`isStacked` we need to calculate a new domain for the yScale based on\n\t\tthe sum of the data. If you need explicit control of the y max when\n\t\tstacking, pass it in here.\n\t"], ["\n\t\tTypically this number can be derived from the yScale. However when we're\n\t\t\\`isStacked\\` we need to calculate a new domain for the yScale based on\n\t\tthe sum of the data. If you need explicit control of the y max when\n\t\tstacking, pass it in here.\n\t"]))),
xField: string(templateObject_10 || (templateObject_10 = __makeTemplateObject(["\n\t\tThe field we should look up your x data by.\n\t"], ["\n\t\tThe field we should look up your x data by.\n\t"]))),
yFields: arrayOf(string)(templateObject_11 || (templateObject_11 = __makeTemplateObject(["\n\t\tThe field(s) we should look up your y data by. Each entry represents a\n\t\tseries. Your actual y data should be numeric.\n\t"], ["\n\t\tThe field(s) we should look up your y data by. Each entry represents a\n\t\tseries. Your actual y data should be numeric.\n\t"]))),
isStacked: bool(templateObject_12 || (templateObject_12 = __makeTemplateObject(["\n\t\tThis will stack the data instead of grouping it. In order to stack the\n\t\tdata we have to calculate a new domain for the y scale that is based on\n\t\tthe `sum` of the data.\n\t"], ["\n\t\tThis will stack the data instead of grouping it. In order to stack the\n\t\tdata we have to calculate a new domain for the y scale that is based on\n\t\tthe \\`sum\\` of the data.\n\t"]))),
colorOffset: number(templateObject_13 || (templateObject_13 = __makeTemplateObject(["\n\t\tSometimes you might not want the colors to start rotating at the blue\n\t\tcolor, this number will be added the line index in determining which\n\t\tcolor the lines are.\n\t"], ["\n\t\tSometimes you might not want the colors to start rotating at the blue\n\t\tcolor, this number will be added the line index in determining which\n\t\tcolor the lines are.\n\t"]))),
};
exports.default = exports.Lines;
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13;
//# sourceMappingURL=Lines.js.map