Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAP-1533 stacked area chart functional component #536

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/components/Charts/StackedAreaChart/StackedAreaChart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import './StackedAreaChart.less';
import * as d3 from 'd3';
import { stackedArea } from 'britecharts';
import { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import * as colors from '../../../constants/colors';
import {
getLastDate,
pruneIncompleteStackedAreaInterval,
} from '../../../utils/chart';
import { updateTrendsTooltip } from '../../../actions/trends';
import { cloneDeep /*, hashObject*/ } from '../../../utils';
import { isDateEqual } from '../../../utils/formatDate';
import {
selectTrendsResultsDateRangeArea,
selectTrendsTooltip,
selectTrendsColorMap,
} from '../../../reducers/trends/selectors';
import {
selectQueryCompanyReceivedMin,
selectQueryCompanyReceivedMax,
selectQueryDateInterval,
} from '../../../reducers/query/selectors';
import { selectViewIsPrintMode } from '../../../reducers/view/selectors';
import ErrorBlock from '../../Warnings/Error';

export const StackedAreaChart = () => {
const dispatch = useDispatch();
const colorMap = useSelector(selectTrendsColorMap);
const data = useSelector(selectTrendsResultsDateRangeArea);
const from = useSelector(selectQueryCompanyReceivedMin);
const to = useSelector(selectQueryCompanyReceivedMax);
const interval = useSelector(selectQueryDateInterval);
const isPrintMode = useSelector(selectViewIsPrintMode);
const tooltipInfo = useSelector(selectTrendsTooltip);

const processData = cloneDeep(data);
const dateRange = { from, to };

const filteredData = pruneIncompleteStackedAreaInterval(
processData,
dateRange,
interval,
);

const hasChart = filteredData.length > 1;

useEffect(() => {
const tooltipUpdated = (selectedState) => {
dispatch(updateTrendsTooltip(selectedState));
};

const updateTooltip = (point) => {
if (!isDateEqual(tooltipInfo.date, point.date)) {
tooltipUpdated({
date: point.date,
dateRange,
interval,
values: point.values,
});
}
};

const chartWidth = (chartID) => {
if (isPrintMode) {
return 500;
}
const container = d3.select(chartID);
return container.node().getBoundingClientRect().width;
};

const redrawChart = () => {
if (!hasChart) {
return;
}

const chartID = '#stacked-area-chart';
const container = d3.select(chartID);
const width = chartWidth(chartID);
d3.select(chartID + ' .stacked-area').remove();

const stackedAreaChart = stackedArea();
const colorData = filteredData.filter((item) => item.name !== 'Other');
const colorScheme = [...new Set(colorData.map((item) => item.name))].map(
(obj) => colorMap[obj],
);
colorScheme.push(colors.DataLens[10]);

stackedAreaChart
.margin({ left: 70, right: 10, top: 10, bottom: 40 })
.areaCurve('linear')
.initializeVerticalMarker(true)
.isAnimated(false)
.tooltipThreshold(1)
.grid('horizontal')
.aspectRatio(0.5)
.width(width)
.dateLabel('date')
.colorSchema(colorScheme)
.on('customMouseMove', updateTooltip);

container.datum(cloneDeep(filteredData)).call(stackedAreaChart);

const config = {
dateRange,
interval,
};

tooltipUpdated(getLastDate(filteredData, config));
};

redrawChart();
}, []);

Check warning on line 113 in src/components/Charts/StackedAreaChart/StackedAreaChart.js

View workflow job for this annotation

GitHub Actions / frontend

React Hook useEffect has missing dependencies: 'colorMap', 'dateRange', 'dispatch', 'filteredData', 'hasChart', 'interval', 'isPrintMode', and 'tooltipInfo.date'. Either include them or remove the dependency array

return hasChart ? (
<div className="chart-wrapper">
<p className="y-axis-label">Complaints</p>
<div id="stacked-area-chart" />
<p className="x-axis-label">Date received by the CFPB</p>
</div>
) : (
<ErrorBlock text="Cannot display chart. Adjust your date range or date interval." />
);
};
21 changes: 21 additions & 0 deletions src/components/Charts/StackedAreaChart/StackedAreaChart.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#stacked-area-chart {
.stacked-area {
.y-axis-group {
.domain {
display: none;
}
}
}
}

.chart-wrapper {
p {
font-size: 12px;
font-weight: 600;
color: var(--gray);

&.x-axis-label {
margin-left: 45%;
}
}
}
3 changes: 2 additions & 1 deletion src/components/Trends/TrendsPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import React from 'react';
import RowChart from '../Charts/RowChart';
import Select from '../RefineBar/Select';
import { Separator } from '../RefineBar/Separator';
import StackedAreaChart from '../Charts/StackedAreaChart';
//import StackedAreaChart from '../Charts/StackedAreaChart';
import { StackedAreaChart } from '../Charts/StackedAreaChart/StackedAreaChart';
import { TabbedNavigation } from '../TabbedNavigation';
import { TrendDepthToggle } from './TrendDepthToggle';
import { trendsDateWarningDismissed } from '../../actions/view';
Expand Down
1 change: 1 addition & 0 deletions src/reducers/query/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export const selectQueryStateFilters = (state) => state.query.state;
export const selectQuerySubLens = (state) => state.query.subLens;
export const selectQueryTab = (state) => state.query.tab;
export const selectQueryTotalPages = (state) => state.query.totalPages;
export const selectQueryDateInterval = (state) => state.query.dateInterval;
3 changes: 3 additions & 0 deletions src/reducers/trends/selectors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export const selectTrendsChartType = (state) => state.trends.chartType;
export const selectTrendsTooltip = (state) => state.trends.tooltip;
export const selectTrendsTotal = (state) => state.trends.total;
export const selectTrendsColorMap = (state) => state.trends.colorMap;
export const selectTrendsResults = (state) => state.trends.results;
export const selectTrendsResultsDateRangeArea = (state) =>
state.trends.results.dateRangeArea;
export const selectTrendsResultsSubProduct = (state) =>
state.trends.results['sub-product'];
Loading