Skip to content

Commit 3b5ad97

Browse files
authored
Merge pull request #453 from kelvin2go/feature/413-contribution-graph-enhancements
add github like contribution graph css and ceil for 1,2 report count
2 parents 8c26bbb + 157a563 commit 3b5ad97

File tree

2 files changed

+75
-59
lines changed

2 files changed

+75
-59
lines changed

components/ContributionChart.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp';
99
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
1010
import { addDays, format } from 'date-fns';
1111

12-
const SCALING_FACTOR = 5;
1312
const MAX_SCALE = 4;
1413

1514
const useStyles = makeStyles(theme => ({
@@ -38,6 +37,13 @@ const useStyles = makeStyles(theme => ({
3837
},
3938
},
4039
root: {
40+
overflow: 'hidden',
41+
'align-items': 'flex-end !important',
42+
'flex-direction': 'column !important',
43+
display: 'flex !important',
44+
'& .react-calendar-heatmap': {
45+
minHeight: 140,
46+
},
4147
'& svg': {
4248
background: '#fff',
4349

@@ -128,15 +134,25 @@ function Legend({ count }) {
128134
);
129135
}
130136

131-
function scaleColor(count) {
132-
return Math.max(Math.min(Math.round(count / SCALING_FACTOR), MAX_SCALE), 0);
137+
function scaleColor(count, maxContribution) {
138+
const varingScalingFactor =
139+
maxContribution.count < 10
140+
? 10 / MAX_SCALE
141+
: maxContribution.count / MAX_SCALE;
142+
return Math.max(
143+
Math.min(Math.ceil(count / varingScalingFactor), MAX_SCALE),
144+
0
145+
);
133146
}
134147

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

141157
return (
142158
<Card>
@@ -170,7 +186,7 @@ export default function ContributionChart({ startDate, endDate, data }) {
170186
if (!value) {
171187
return classes.colorCofacts0;
172188
}
173-
const scale = scaleColor(value.count);
189+
const scale = scaleColor(value.count, maxContribution);
174190
return classes[`colorCofacts${scale}`];
175191
}}
176192
transformDayElement={(element, value, index) => {

0 commit comments

Comments
 (0)