Skip to content

Commit

Permalink
feat: add line series
Browse files Browse the repository at this point in the history
  • Loading branch information
tyn1998 committed Oct 3, 2022
1 parent dce0fed commit c4ac0c8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
32 changes: 32 additions & 0 deletions src/utils/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const generateDataByMonth = (originalData: any) => {
const objectData: any = {};
// format month string
Object.keys(originalData).forEach((key) => {
// e.g. 20204 -> 2020-4
const date = key.slice(0, 4) + '-' + key.slice(4);
objectData[date] = originalData[key];
});
// get the oldest month and the newest one
const orderedMonths = Object.keys(objectData).sort((a, b) => {
const dateA = new Date(a);
const dateB = new Date(b);
if (dateA < dateB) return -1;
else if (dateA > dateB) return 1;
else return 0;
});
const oldestMonth = orderedMonths[0];
const newestMonth = orderedMonths[orderedMonths.length - 1];
// insert no-event months (assigned to 0) and generate final data
const arrayData: [string, number][] = [];
const start = new Date(oldestMonth);
const end = new Date(newestMonth);
for (let i = start; i <= end; i.setMonth(i.getMonth() + 1)) {
const date = i.getFullYear() + '-' + (i.getMonth() + 1);
if (!objectData.hasOwnProperty(date)) {
arrayData.push([date, 0]);
} else {
arrayData.push([date, objectData[date]]);
}
}
return arrayData;
};
13 changes: 11 additions & 2 deletions src/views/RepoDetailForkView/ForkBars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ const ForkBars: React.FC<ForkBarsProps> = (props) => {
focus: 'series',
},
yAxisIndex: 0,
animationDelay: function (idx: any) {
return idx * 10;
},
{
type: 'line',
symbol: 'none',
lineStyle: {
color: COLORS.FG_COLOR,
},
data: data,
emphasis: {
focus: 'series',
},
yAxisIndex: 0,
},
],
animationEasing: 'elasticOut',
Expand Down
10 changes: 2 additions & 8 deletions src/views/RepoDetailForkView/RepoDetailForkView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';

import { getGithubTheme, getMessageByLocale } from '../../utils/utils';
import Settings, { loadSettings } from '../../utils/settings';
import { generateDataByMonth } from '../../utils/data';
import { getRepoDetail } from '../../api/repo';
import ReactTooltip from 'react-tooltip';
import ForkBars from './ForkBars';
Expand All @@ -13,14 +14,7 @@ interface RepoDetailForkViewProps {
}

const generateForkBarsData = (fork: any) => {
const data: [string, number][] = [];
Object.keys(fork).forEach((value, index) => {
// format date string
// 20204 -> 2020-4
const date = value.slice(0, 4) + '-' + value.slice(4);
data.push([date, fork[value]]);
});
return data;
return generateDataByMonth(fork);
};

const RepoDetailForkView: React.FC<RepoDetailForkViewProps> = ({
Expand Down
10 changes: 2 additions & 8 deletions src/views/RepoDetailStarView/RepoDetailStarView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';

import { getGithubTheme, getMessageByLocale } from '../../utils/utils';
import Settings, { loadSettings } from '../../utils/settings';
import { generateDataByMonth } from '../../utils/data';
import { getRepoDetail } from '../../api/repo';
import ReactTooltip from 'react-tooltip';
import StarBars from '../RepoDetailStarView/StarBars';
Expand All @@ -13,14 +14,7 @@ interface RepoDetailStarViewProps {
}

const generateStarBarsData = (star: any) => {
const data: [string, number][] = [];
Object.keys(star).forEach((value, index) => {
// format date string
// 20204 -> 2020-4
const date = value.slice(0, 4) + '-' + value.slice(4);
data.push([date, star[value]]);
});
return data;
return generateDataByMonth(star);
};

const RepoDetailStarView: React.FC<RepoDetailStarViewProps> = ({
Expand Down
13 changes: 11 additions & 2 deletions src/views/RepoDetailStarView/StarBars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ const StarBars: React.FC<StarBarsProps> = (props) => {
focus: 'series',
},
yAxisIndex: 0,
animationDelay: function (idx: any) {
return idx * 10;
},
{
type: 'line',
symbol: 'none',
lineStyle: {
color: COLORS.FG_COLOR,
},
data: data,
emphasis: {
focus: 'series',
},
yAxisIndex: 0,
},
],
animationEasing: 'elasticOut',
Expand Down

0 comments on commit c4ac0c8

Please sign in to comment.