Skip to content

Commit

Permalink
Merge pull request #453 from kelvin2go/feature/413-contribution-graph…
Browse files Browse the repository at this point in the history
…-enhancements

add github like contribution graph css and ceil for 1,2 report count
  • Loading branch information
MrOrz committed Oct 20, 2021
2 parents 8c26bbb + 157a563 commit 3b5ad97
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 59 deletions.
24 changes: 20 additions & 4 deletions components/ContributionChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp';
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
import { addDays, format } from 'date-fns';

const SCALING_FACTOR = 5;
const MAX_SCALE = 4;

const useStyles = makeStyles(theme => ({
Expand Down Expand Up @@ -38,6 +37,13 @@ const useStyles = makeStyles(theme => ({
},
},
root: {
overflow: 'hidden',
'align-items': 'flex-end !important',
'flex-direction': 'column !important',
display: 'flex !important',
'& .react-calendar-heatmap': {
minHeight: 140,
},
'& svg': {
background: '#fff',

Expand Down Expand Up @@ -128,15 +134,25 @@ function Legend({ count }) {
);
}

function scaleColor(count) {
return Math.max(Math.min(Math.round(count / SCALING_FACTOR), MAX_SCALE), 0);
function scaleColor(count, maxContribution) {
const varingScalingFactor =
maxContribution.count < 10
? 10 / MAX_SCALE
: maxContribution.count / MAX_SCALE;
return Math.max(
Math.min(Math.ceil(count / varingScalingFactor), MAX_SCALE),
0
);
}

export default function ContributionChart({ startDate, endDate, data }) {
const [showPlot, setShowPlot] = useState(true);
const classes = useStyles({ showPlot });
const firstDay = new Date(startDate);
const total = data.reduce((sum, value) => sum + value.count, 0);
const maxContribution = data.reduce((prev, current) => {
return prev.count > current.count ? prev : current;
});

return (
<Card>
Expand Down Expand Up @@ -170,7 +186,7 @@ export default function ContributionChart({ startDate, endDate, data }) {
if (!value) {
return classes.colorCofacts0;
}
const scale = scaleColor(value.count);
const scale = scaleColor(value.count, maxContribution);
return classes[`colorCofacts${scale}`];
}}
transformDayElement={(element, value, index) => {
Expand Down
Loading

0 comments on commit 3b5ad97

Please sign in to comment.