Skip to content

Commit

Permalink
feat: translate scatter chart legend in skills tab
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Sep 19, 2024
1 parent 16a3152 commit 566a9e3
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 13 deletions.
13 changes: 12 additions & 1 deletion src/components/AdvanceAnalyticsV2/charts/ChartWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@ ChartWrapper.propTypes = {
isFetching: PropTypes.bool.isRequired,
isError: PropTypes.bool.isRequired,
chartType: PropTypes.oneOf(['ScatterChart', 'LineChart', 'BarChart']).isRequired,
chartProps: PropTypes.shape({ data: PropTypes.shape({}) }).isRequired,
chartProps: PropTypes.shape({
data: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
xKey: PropTypes.string.isRequired,
yKey: PropTypes.string.isRequired,
colorKey: PropTypes.string.isRequired,
colorMap: PropTypes.objectOf(PropTypes.string).isRequired,
hovertemplate: PropTypes.string.isRequired,
xAxisTitle: PropTypes.string,
yAxisTitle: PropTypes.string,
markerSizeKey: PropTypes.string,
customDataKeys: PropTypes.arrayOf(PropTypes.string),
}).isRequired,
loadingMessage: PropTypes.string.isRequired,
};

Expand Down
7 changes: 5 additions & 2 deletions src/components/AdvanceAnalyticsV2/charts/ScatterChart.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useMemo } from 'react';
import Plot from 'react-plotly.js';
import PropTypes from 'prop-types';
import { useIntl } from '@edx/frontend-platform/i18n';
import messages from '../messages';

/**
* ScatterChart component renders a scatter chart using Plotly.js.
Expand All @@ -21,6 +23,7 @@ import PropTypes from 'prop-types';
const ScatterChart = ({
data, xKey, yKey, colorKey, colorMap, hovertemplate, xAxisTitle, yAxisTitle, markerSizeKey, customDataKeys,
}) => {
const intl = useIntl();
const categories = Object.keys(colorMap);

const traces = useMemo(() => categories.map(category => {
Expand All @@ -30,15 +33,15 @@ const ScatterChart = ({
y: filteredData.map(item => item[yKey]),
type: 'scatter',
mode: 'markers',
name: category,
name: messages[category] ? intl.formatMessage(messages[category]) : category,
marker: {
color: colorMap[category],
size: filteredData.map(item => item[markerSizeKey] * 0.015).map(size => (size < 5 ? size + 6 : size)),
},
customdata: customDataKeys.length ? filteredData.map(item => customDataKeys.map(key => item[key])) : [],
hovertemplate,
};
}), [data, xKey, yKey, colorKey, colorMap, hovertemplate, categories, markerSizeKey, customDataKeys]);
}), [data, xKey, yKey, colorKey, colorMap, hovertemplate, categories, markerSizeKey, customDataKeys, intl]);

const layout = {
margin: { t: 0 },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { shallow } from 'enzyme';
import { mount } from 'enzyme';
import Plot from 'react-plotly.js';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import ScatterChart from './ScatterChart';

describe('ScatterChart', () => {
Expand Down Expand Up @@ -28,8 +29,10 @@ describe('ScatterChart', () => {
};

it('renders correctly', () => {
const wrapper = shallow(
<ScatterChart {...props} />,
const wrapper = mount(
<IntlProvider locale="en">
<ScatterChart {...props} />,
</IntlProvider>,
);
const plotComponent = wrapper.find(Plot);
const traces = plotComponent.prop('data');
Expand Down
2 changes: 1 addition & 1 deletion src/components/AdvanceAnalyticsV2/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { sum } from 'lodash';
import utc from 'dayjs/plugin/utc';
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
import { CHART_TYPES, CALCULATION } from './constants';
import messages from '../messages';

dayjs.extend(utc);
dayjs.extend(quarterOfYear);
import messages from '../messages';

const simulateURL = (activeTab, chartType) => {
if (!Object.values(CHART_TYPES).includes(chartType)) {
Expand Down
5 changes: 2 additions & 3 deletions src/components/AdvanceAnalyticsV2/data/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Jest test for utils.js

import { applyCalculation, applyGranularity } from './utils';
import { createIntl } from '@edx/frontend-platform/i18n';
import { applyCalculation, applyGranularity, constructChartHoverTemplate } from './utils';
import { CALCULATION, GRANULARITY } from './constants';

describe('utils', () => {
Expand Down Expand Up @@ -201,8 +202,6 @@ describe('utils', () => {
});
});
});
import { createIntl } from '@edx/frontend-platform/i18n';
import { constructChartHoverTemplate } from './utils';

describe('constructChartHoverTemplate', () => {
const intl = createIntl({
Expand Down
20 changes: 20 additions & 0 deletions src/components/AdvanceAnalyticsV2/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ const messages = defineMessages({
id: 'advance.analytics.learning.hours.label',
defaultMessage: 'Learning Hours',
},
'Common Skill': {
id: 'advance.analytics.common.skill.label',
defaultMessage: 'Common Skill',
},
'Specialized Skill': {
id: 'advance.analytics.specialized.skill.label',
defaultMessage: 'Specialized Skill',
},
'Hard Skill': {
id: 'advance.analytics.hard.skill.label',
defaultMessage: 'Hard Skill',
},
'Soft Skill': {
id: 'advance.analytics.soft.skill.label',
defaultMessage: 'Soft Skill',
},
Certification: {
id: 'advance.analytics.certification.label',
defaultMessage: 'Certification',
},
});

export default messages;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-extraneous-dependencies */
import {
render, screen, waitFor, within,
} from '@testing-library/react';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-extraneous-dependencies */
import {
render, screen, waitFor, within,
} from '@testing-library/react';
Expand Down
4 changes: 1 addition & 3 deletions src/components/AdvanceAnalyticsV2/tabs/Enrollments.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import AnalyticsTable from './AnalyticsTable';
import ChartWrapper from '../charts/ChartWrapper';
import { useEnterpriseEnrollmentsData } from '../data/hooks';
import DownloadCSVButton from '../DownloadCSVButton';
import { modifyDataToIntroduceEnrollTypeCount } from '../data/utils';
import { modifyDataToIntroduceEnrollTypeCount, constructChartHoverTemplate } from '../data/utils';

dayjs.extend(utc);
import { constructChartHoverTemplate } from '../data/utils';

const Enrollments = ({
startDate, endDate, granularity, calculation, enterpriseId,
Expand Down Expand Up @@ -188,7 +187,6 @@ const Enrollments = ({
colorMap: chartColorMap,
xAxisTitle: '',
yAxisTitle: 'Number of Enrollments',
hovertemplate: 'Subject: %{x}<br>Enrolls: %{y}',
hovertemplate: constructChartHoverTemplate(intl, {
subject: '%{x}',
enrollments: '%{y}',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-extraneous-dependencies */
import {
render, screen, waitFor, within,
} from '@testing-library/react';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-extraneous-dependencies */
import {
render, screen, waitFor, within,
} from '@testing-library/react';
Expand Down

0 comments on commit 566a9e3

Please sign in to comment.