@giancosta86/omnicourse
Version:
Interactive, React-based drill-down analysis of your learning experience
94 lines • 4.99 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubjectChart = void 0;
const react_1 = __importStar(require("react"));
const recharts_1 = require("recharts");
const SubjectTooltip_1 = require("./SubjectTooltip");
const ChartSettings_1 = require("./ChartSettings");
const LearningContext_1 = require("../LearningContext");
const SubjectChart = ({ subjects, chartSettings }) => {
var _a;
const { selectedTaxonomyKey, taxonomyPath, currentLevel, onMobile, pushToTaxonomyPath } = (0, LearningContext_1.useLearningContext)();
const [repaintNeeded, setRepaintNeeded] = (0, react_1.useState)(false);
(0, react_1.useLayoutEffect)(() => {
if (repaintNeeded) {
setRepaintNeeded(false);
}
}, [repaintNeeded]);
const data = (0, react_1.useMemo)(() => subjects.map(subject => {
return {
name: subject.name,
value: subject.totalMinutes
};
}), [subjects]);
const actualChartSettings = (0, react_1.useMemo)(() => (0, ChartSettings_1.createActualChartSettings)(chartSettings), [chartSettings]);
const colorPalette = actualChartSettings.colorPalette;
const performDrillDown = (0, react_1.useCallback)((subject) => {
pushToTaxonomyPath(subject);
setRepaintNeeded(true);
}, [pushToTaxonomyPath]);
const createCells = (0, react_1.useCallback)(() => {
if (!taxonomyPath) {
throw new Error("Missing taxonomy path!");
}
return subjects.map((subject, index) => (react_1.default.createElement(recharts_1.Cell, { cursor: "pointer", key: subject.name, fill: colorPalette[(taxonomyPath.subjectsInPreviousLevels + index) %
colorPalette.length], onClick: () => performDrillDown(subject) })));
}, [colorPalette, subjects, performDrillDown, taxonomyPath]);
const onLegendLabelClicked = (0, react_1.useCallback)((legendLabel) => {
const subjectName = legendLabel.value;
const subject = subjects.find(subject => subject.name == subjectName);
if (!subject) {
throw new Error("Legend labels out of sync!");
}
performDrillDown(subject);
}, [subjects, performDrillDown]);
if (!selectedTaxonomyKey) {
throw new Error("There should be a taxonomy key here!");
}
if (!currentLevel) {
throw new Error("There should be a taxonomy level here!");
}
if (repaintNeeded) {
return (react_1.default.createElement("div", { style: {
width: "100%",
height: actualChartSettings.chartHeight
} }));
}
return (react_1.default.createElement(recharts_1.ResponsiveContainer, { className: "chartBox", width: "100%", height: actualChartSettings.chartHeight },
react_1.default.createElement(recharts_1.PieChart, null,
react_1.default.createElement(recharts_1.Pie, { data: data, dataKey: "value", isAnimationActive: actualChartSettings.isAnimationActive, animationDuration: actualChartSettings.animationDuration, label: item => actualChartSettings.canShowLabel({
taxonomyKey: selectedTaxonomyKey,
taxonomyLevel: currentLevel,
item,
onMobile
})
? item.name
: null, labelLine: !onMobile, cx: "50%", cy: "50%", outerRadius: actualChartSettings.outerRadius, innerRadius: actualChartSettings.innerRadius }, createCells()),
react_1.default.createElement(recharts_1.Tooltip, { content: react_1.default.createElement(SubjectTooltip_1.SubjectTooltip, { totalMinutes: (_a = currentLevel === null || currentLevel === void 0 ? void 0 : currentLevel.totalMinutes) !== null && _a !== void 0 ? _a : 0 }) }),
react_1.default.createElement(recharts_1.Legend, { iconType: "star", onClick: onLegendLabelClicked }))));
};
exports.SubjectChart = SubjectChart;
//# sourceMappingURL=index.js.map