@streetscape.gl/monochrome
Version:
A toolkit of React components for streetscape.gl
399 lines (361 loc) • 12.9 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import AutoSizer from '../shared/autosizer';
import XYPlot from 'react-vis/dist/plot/xy-plot';
import AreaSeries from 'react-vis/dist/plot/series/area-series';
import LineSeries from 'react-vis/dist/plot/series/line-series';
import MarkSeries from 'react-vis/dist/plot/series/mark-series';
import HorizontalGridLines from 'react-vis/dist/plot/horizontal-grid-lines';
import VerticalGridLines from 'react-vis/dist/plot/vertical-grid-lines';
import XAxis from 'react-vis/dist/plot/axis/x-axis';
import YAxis from 'react-vis/dist/plot/axis/y-axis';
import Crosshair from 'react-vis/dist/plot/crosshair';
import { scaleLinear } from 'd3-scale';
import { withTheme } from '../shared/theme';
import { ChartContainer, CrosshairItemTitle, CrosshairItemLegend, CrosshairItemValue } from './styled-components';
var noop = function noop() {};
var DEFAULT_MARGIN = {
left: 32,
right: 20,
top: 20,
bottom: 32
};
var Chart = function (_PureComponent) {
_inherits(Chart, _PureComponent);
var _super = _createSuper(Chart);
function Chart() {
_classCallCheck(this, Chart);
return _super.apply(this, arguments);
}
_createClass(Chart, [{
key: "_getScaleSettings",
value: function _getScaleSettings() {
var _this$props = this.props,
data = _this$props.data,
dataFilter = _this$props.dataFilter,
xDomain = _this$props.xDomain,
yDomain = _this$props.yDomain,
getX = _this$props.getX,
getY0 = _this$props.getY0,
getY = _this$props.getY;
if (xDomain && yDomain) {
return {
xDomain: xDomain,
yDomain: yDomain
};
}
var x = xDomain || [Infinity, -Infinity];
var y = yDomain || [0, 0];
for (var key in data) {
if (dataFilter(key)) {
var values = data[key];
if (Array.isArray(values) && values.length > 0) {
x = xDomain || values.reduce(function (acc, d) {
var x0 = getX(d);
acc[0] = Math.min(acc[0], x0);
acc[1] = Math.max(acc[1], x0);
return acc;
}, x);
y = yDomain || values.reduce(function (acc, d) {
var y1 = getY(d);
var y0 = getY0(d);
acc[0] = Math.min(acc[0], y1);
acc[1] = Math.max(acc[1], y1);
if (Number.isFinite(y0)) {
acc[0] = Math.min(acc[0], y0);
acc[1] = Math.max(acc[1], y0);
}
return acc;
}, y);
}
}
}
if (!yDomain) {
y = scaleLinear().domain(y).nice().domain();
}
return {
xDomain: x,
yDomain: y
};
}
}, {
key: "_getColor",
value: function _getColor(key) {
var getColor = this.props.getColor;
switch (_typeof(getColor)) {
case 'object':
return getColor[key];
case 'function':
return getColor(key);
default:
return getColor;
}
}
}, {
key: "_renderSeries",
value: function _renderSeries() {
var _this = this;
var _this$props2 = this.props,
data = _this$props2.data,
dataFilter = _this$props2.dataFilter,
highlightSeries = _this$props2.highlightSeries,
getX = _this$props2.getX,
getY0 = _this$props2.getY0,
getY = _this$props2.getY,
xDomain = _this$props2.xDomain;
var areas = [];
var lines = [];
Object.keys(data).forEach(function (key) {
if (!dataFilter(key)) {
return;
}
var datums = xDomain ? data[key].filter(function (point) {
var x = getX(point);
return x >= xDomain[0] && x <= xDomain[1];
}) : data[key];
if (!datums.length) {
return;
}
var isArea = Number.isFinite(getY0(datums[0]));
var Type = isArea ? AreaSeries : LineSeries;
var color = _this._getColor(key);
var series = React.createElement(Type, {
key: "value-".concat(key, "-line"),
data: datums,
getX: getX,
getY: getY,
getY0: getY0,
color: color,
fill: color,
strokeWidth: highlightSeries === key ? 4 : 2,
onNearestX: _this.props.onNearestX.bind(_this, key),
onSeriesMouseOver: function onSeriesMouseOver() {
return _this.props.onSeriesMouseOver(key);
},
onSeriesMouseOut: function onSeriesMouseOut() {
return _this.props.onSeriesMouseOut(key);
}
});
if (isArea) {
areas.push(series);
} else {
lines.push(series);
}
});
return areas.concat(lines);
}
}, {
key: "_renderCrosshair",
value: function _renderCrosshair() {
var _this2 = this;
var highlightValues = this.props.highlightValues;
if (!highlightValues) {
return null;
}
var _this$props3 = this.props,
theme = _this$props3.theme,
style = _this$props3.style,
unit = _this$props3.unit,
dataFilter = _this$props3.dataFilter,
formatTitle = _this$props3.formatTitle,
formatValue = _this$props3.formatValue,
getX = _this$props3.getX,
getY = _this$props3.getY,
getY0 = _this$props3.getY0,
xDomain = _this$props3.xDomain;
var keys = Object.keys(highlightValues).filter(function (key) {
var value = highlightValues[key];
var x = getX(value);
return dataFilter(key) && (!xDomain || x >= xDomain[0] && x <= xDomain[1]);
});
var crosshairItems = keys.map(function (key, i) {
var value = highlightValues[key];
var color = _this2._getColor(key);
var x = getX(value);
var y = getY(value);
var y0 = getY0(value);
var styleProps = {
theme: theme,
name: key,
displayName: formatTitle(key),
color: color,
isFirst: i === 0,
isLast: i === keys.length - 1
};
return {
x: x,
y: y,
title: React.createElement(CrosshairItemTitle, _extends({}, styleProps, {
userStyle: style.crosshairTitle
}), React.createElement(CrosshairItemLegend, _extends({}, styleProps, {
userStyle: style.crosshairLegend
})), styleProps.displayName),
value: React.createElement(CrosshairItemValue, _extends({}, styleProps, {
userStyle: style.crosshairValue
}), Number.isFinite(y0) && "".concat(formatValue(y0), ", "), formatValue(y), unit && React.createElement("span", null, unit)),
color: color
};
});
return [React.createElement(Crosshair, {
key: "crosshair",
values: crosshairItems,
titleFormat: function titleFormat() {
return null;
},
itemsFormat: function itemsFormat(values) {
return values;
}
}), React.createElement(MarkSeries, {
key: "hovered-values",
data: crosshairItems,
stroke: "#fff",
strokeWidth: 2,
getFill: function getFill(d) {
return d.color;
},
fillType: "literal"
})];
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _this$props4 = this.props,
theme = _this$props4.theme,
width = _this$props4.width,
height = _this$props4.height,
style = _this$props4.style,
formatYTick = _this$props4.formatYTick,
formatXTick = _this$props4.formatXTick,
xTicks = _this$props4.xTicks,
yTicks = _this$props4.yTicks,
horizontalGridLines = _this$props4.horizontalGridLines,
verticalGridLines = _this$props4.verticalGridLines,
onMouseEnter = _this$props4.onMouseEnter,
onMouseMove = _this$props4.onMouseMove,
onMouseLeave = _this$props4.onMouseLeave,
onClick = _this$props4.onClick;
return React.createElement(ChartContainer, {
theme: theme,
userStyle: style.chart,
tooltipStyle: style.crosshair,
style: {
width: width,
height: height
}
}, React.createElement(AutoSizer, null, function (_ref) {
var chartWidth = _ref.width,
chartHeight = _ref.height;
return React.createElement(XYPlot, _extends({
width: chartWidth,
height: chartHeight,
margin: style.margin || DEFAULT_MARGIN
}, _this3._getScaleSettings(), {
onClick: onClick,
onMouseEnter: onMouseEnter,
onMouseMove: onMouseMove,
onMouseLeave: onMouseLeave
}), xTicks > 0 && React.createElement(XAxis, {
title: "",
tickFormat: formatXTick,
tickTotal: xTicks
}), yTicks > 0 && React.createElement(YAxis, {
title: "",
tickFormat: formatYTick,
tickTotal: yTicks
}), horizontalGridLines > 0 && React.createElement(HorizontalGridLines, {
tickTotal: horizontalGridLines
}), verticalGridLines > 0 && React.createElement(VerticalGridLines, {
tickTotal: verticalGridLines
}), _this3._renderSeries(), _this3._renderCrosshair());
}));
}
}]);
return Chart;
}(PureComponent);
_defineProperty(Chart, "propTypes", {
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
style: PropTypes.object,
unit: PropTypes.string,
data: PropTypes.object,
dataFilter: PropTypes.func,
onClick: PropTypes.func,
onMouseEnter: PropTypes.func,
onMouseMove: PropTypes.func,
onMouseLeave: PropTypes.func,
onNearestX: PropTypes.func,
onSeriesMouseOver: PropTypes.func,
onSeriesMouseOut: PropTypes.func,
getX: PropTypes.func,
getY0: PropTypes.func,
getY: PropTypes.func,
xDomain: PropTypes.array,
yDomain: PropTypes.array,
xTicks: PropTypes.number,
yTicks: PropTypes.number,
horizontalGridLines: PropTypes.number,
verticalGridLines: PropTypes.number,
highlightSeries: PropTypes.string,
highlightValues: PropTypes.object,
formatYTick: PropTypes.func,
formatTitle: PropTypes.func,
formatValue: PropTypes.func,
formatXTick: PropTypes.func,
getColor: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func])
});
_defineProperty(Chart, "defaultProps", {
width: '100%',
height: 300,
style: {},
data: {},
dataFilter: function dataFilter(key) {
return true;
},
unit: '',
onClick: noop,
onMouseEnter: noop,
onMouseMove: noop,
onMouseLeave: noop,
onNearestX: noop,
onSeriesMouseOver: noop,
onSeriesMouseOut: noop,
getX: function getX(d) {
return d.x;
},
getY0: function getY0(d) {
return null;
},
getY: function getY(d) {
return d.y;
},
xTicks: 4,
yTicks: 4,
horizontalGridLines: 4,
verticalGridLines: 4,
formatTitle: function formatTitle(value) {
return String(value);
},
formatValue: function formatValue(value) {
return String(value);
},
formatXTick: function formatXTick(value) {
return String(value);
},
formatYTick: function formatYTick(value) {
return String(value);
},
getColor: '#000'
});
export default withTheme(Chart);
//# sourceMappingURL=chart.js.map