Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Add weekly chart
Browse files Browse the repository at this point in the history
  • Loading branch information
aslihanozfidan committed Aug 4, 2020
1 parent 6eeed55 commit a7b9bba
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@ func GetHtml() string {
deploymentFrequencyChart: ""
}
function getWeek(date) {
const dayNumber = (date.getDay() + 6) % 7;
date.setDate(date.getDate() - dayNumber + 3);
let tempDate = new Date(date.getFullYear(), 0, 4);
let dayDiff = (date - tempDate) / 86400000;
return 1 + Math.ceil(dayDiff / 7);
}
function prepareData(data) {
let newData = Object.assign([], data)
let months = []
let years = []
let weeks = []
let days = []
let monthly = []
let weekly = []
newData.sort(function (a, b) {
return new Date(a.date) - new Date(b.date);
Expand All @@ -50,6 +62,8 @@ func GetHtml() string {
const month = ALL_MONTHS_SHORT[date.getMonth()]
const day = date.getDate()
const year = date.getFullYear()
const week = getWeek(date)
if (!item.month) {
item.month = month
}
Expand All @@ -59,6 +73,9 @@ func GetHtml() string {
if (!item.year) {
item.year = year
}
if (!item.week) {
item.week = week
}
if (!days.includes(day)) {
days.push(day)
}
Expand All @@ -68,17 +85,35 @@ func GetHtml() string {
if (!years.includes(year)) {
years.push(year)
}
if (!weeks.includes(week)) {
weeks.push({ week, year })
}
});
years.forEach(year => {
if (!monthly.includes(year)) {
monthly[year] = []
}
let totalValue = 0
totalValue = newData.filter(item => item.year == year).reduce(function getSum(total, item) {
return total + parseInt(item.value);
}, totalValue)
if (!weekly.includes(year)) {
weekly[year] = []
for (let i = 0; i < 52; i++) {
weekly[year].push({
label: i + 1,
totalValue: 0
})
}
}
weeks.forEach((week) => {
totalValue = 0
let totalWeeks = weeks.filter(item =>
item.week == week.week && year == week.year
)
weekly[year][week.week - 1].totalValue = totalWeeks.length
});
ALL_MONTHS_SHORT.forEach(mon => {
totalValue = 0
Expand All @@ -90,14 +125,14 @@ func GetHtml() string {
label: mon,
totalValue
})
});
});
return {
data: newData,
monthly
monthly,
weekly
}
}
Expand All @@ -109,11 +144,11 @@ func GetHtml() string {
var red = randomNum();
var green = randomNum();
var blue = randomNum();
return red + ", " + green + ", 235";
return red + ", " + green + ", " + blue;
}
function getDataSets(data) {
const a = Object.entries(data).map(([key, values]) => {
return Object.entries(data).map(([key, values]) => {
return {
label: key,
data: values.map(item => item.totalValue), fill: false,
Expand All @@ -126,16 +161,14 @@ func GetHtml() string {
borderWidth: 5
}
})
return a
}
function getChartData(chartId, type) {
const data = prepareData(chartData[chartId])
const activeLabelType = LABEL_TYPES.find(labelType => labelType == type)
return {
labels: activeLabelType == "monthly" ? ALL_MONTHS_SHORT : Object.keys(data[activeLabelType]),
labels: Object.values(data[activeLabelType])[0].map(item => item.label),
datasets: getDataSets(data[activeLabelType])
}
}
Expand Down

0 comments on commit a7b9bba

Please sign in to comment.