@paulchang880703/chart-components
Version:
Reusable chart components based on MUI X Charts (Line, Bar, Gauge)
44 lines (43 loc) • 2.17 kB
JavaScript
var _excluded = ["data"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React from "react";
import PropTypes from "prop-types";
import { LineChart as MuiLineChart } from "@mui/x-charts/LineChart";
import { Alert, Box } from "@mui/material";
/**
* data = {
* xLabels: ["Jan", "Feb", ...],
* series: [{ label: "Revenue", data: [10,20...] }, ...]
* }
*/
export default function LineChart(_ref) {
var data = _ref.data,
rest = _objectWithoutProperties(_ref, _excluded);
var isValid = Array.isArray(data === null || data === void 0 ? void 0 : data.xLabels) && data.xLabels.length && Array.isArray(data === null || data === void 0 ? void 0 : data.series);
if (!isValid) {
console.error("LineChart: invalid data", data);
return /*#__PURE__*/React.createElement(Alert, {
severity: "error"
}, "LineChart \u8CC7\u6599\u683C\u5F0F\u932F\u8AA4");
}
return /*#__PURE__*/React.createElement(Box, _extends({
width: "100%"
}, rest), /*#__PURE__*/React.createElement(MuiLineChart, {
xAxis: [{
scaleType: 'point',
data: data.xLabels
}],
series: data.series
}));
}
LineChart.propTypes = {
data: PropTypes.shape({
xLabels: PropTypes.arrayOf(PropTypes.any).isRequired,
series: PropTypes.arrayOf(PropTypes.shape({
label: PropTypes.string,
data: PropTypes.arrayOf(PropTypes.number).isRequired
})).isRequired
}).isRequired
};