Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit cec1604

Browse files
committed
fix: update charts
1 parent dfbfc2e commit cec1604

7 files changed

Lines changed: 128 additions & 56 deletions

File tree

app/Controllers/Http/ChartsController.ts

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { bind } from '@adonisjs/route-model-binding'
12
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
23
import Database from '@ioc:Adonis/Lucid/Database'
34
import Vote from 'App/Models/Vote'
5+
import Year from 'App/Models/Year'
46
import { DateTime } from 'luxon'
57

68
export default class ChartsController {
79
public async index({ view }: HttpContextContract) {
8-
return view.render('votes/charts/index')
10+
const years = await Year.query().orderBy('year', 'desc')
11+
return view.render('votes/charts/index', { years })
912
}
1013

1114
public async totalByDay({ view, request }: HttpContextContract) {
@@ -52,14 +55,17 @@ export default class ChartsController {
5255
})
5356
}
5457

55-
public async topAssociations({ request, view }: HttpContextContract) {
58+
@bind()
59+
public async topAssociations({ request, view }: HttpContextContract, year: Year) {
5660
const limit = request.input('limit', 10)
5761

5862
const votes = await Database.from('votes')
59-
.select('name as label')
63+
.select('associations.name as label')
64+
.where('votes.year_id', year.id)
6065
.count('votes.id as value')
61-
.join('associations', 'votes.association_id', '=', 'associations.id')
62-
.groupBy('association_id', 'name')
66+
.join('participations', 'votes.participation_id', '=', 'participations.id')
67+
.join('associations', 'participations.association_id', '=', 'associations.id')
68+
.groupBy('votes.participation_id', 'associations.name')
6369
.orderBy('value', 'desc')
6470
.limit(limit)
6571

@@ -81,29 +87,34 @@ export default class ChartsController {
8187
/**
8288
* Used to draw a chart of the most voted association day by day
8389
*/
84-
public async topAssociationsByDay({ view, request }: HttpContextContract) {
90+
@bind()
91+
public async topAssociationsByDay({ view, request }: HttpContextContract, year: Year) {
8592
const limit = request.input('limit', 10)
8693

87-
// Get the ten most voted associations ids
88-
const associationsIds = Database.from(
94+
// Get the ten most voted associations ids for the given year
95+
const participationsIds = Database.from(
8996
Database.from('votes')
90-
.select('association_id')
97+
.select('participation_id')
98+
.where('votes.year_id', year.id)
9199
.count('id')
92-
.groupBy('association_id')
100+
.groupBy('participation_id')
93101
.orderBy('count', 'desc')
94102
.limit(limit)
95103
.as('most_voted')
96-
).select('association_id')
104+
).select('participation_id')
97105

98106
// Group votes by date and association id
99107
const result = await Database.from('votes')
100108
.select('name')
101109
.select(Database.raw("date_trunc('day', votes.created_at) as date"))
102-
.count('association_id')
103-
.join('associations', 'votes.association_id', '=', 'associations.id')
104-
.whereIn('association_id', associationsIds)
110+
.where('votes.year_id', year.id)
111+
.whereIn('votes.participation_id', participationsIds)
112+
.count('participation_id')
113+
.join('participations', 'votes.participation_id', '=', 'participations.id')
114+
.join('associations', 'participations.association_id', '=', 'associations.id')
105115
.groupBy('name')
106116
.groupByRaw("date_trunc('day', votes.created_at)")
117+
.orderBy('name')
107118
.orderBy('date')
108119

109120
const votes = result as unknown as {
@@ -123,14 +134,14 @@ export default class ChartsController {
123134
label: vote.name,
124135
data: [
125136
{
126-
x: DateTime.fromISO(vote.date.toISOString()).toFormat('dd/LL'),
137+
x: vote.date.toISOString(),
127138
y: vote.count,
128139
},
129140
],
130141
})
131142
} else {
132143
acc[index].data.push({
133-
x: DateTime.fromISO(vote.date.toISOString()).toFormat('dd/LL'),
144+
x: vote.date.toISOString(),
134145
y: vote.count,
135146
})
136147
}
@@ -144,33 +155,33 @@ export default class ChartsController {
144155
})
145156
}
146157

147-
public async acceptNewsClassement({ view }) {
148-
const result = await Database.from('votes')
149-
.select('accept_classement')
150-
.count('id')
151-
.groupBy('accept_classement')
152-
.orderBy('accept_classement')
153-
154-
return view.render('votes/charts/accept-news-classement', {
155-
votes: {
156-
labels: JSON.stringify(result.map((vote) => (vote.accept_classement ? 'Oui' : 'Non'))),
157-
data: JSON.stringify(result.map((vote) => vote.count)),
158-
},
159-
})
160-
}
161-
162-
public async acceptNewsActivities({ view }) {
163-
const result = await Database.from('votes')
164-
.select('accept_activities')
165-
.count('id')
166-
.groupBy('accept_activities')
167-
.orderBy('accept_activities')
168-
169-
return view.render('votes/charts/accept-news-activities', {
170-
votes: {
171-
labels: JSON.stringify(result.map((vote) => (vote.accept_activities ? 'Oui' : 'Non'))),
172-
data: JSON.stringify(result.map((vote) => vote.count)),
173-
},
174-
})
175-
}
158+
// public async acceptNewsClassement({ view }) {
159+
// const result = await Database.from('votes')
160+
// .select('accept_classement')
161+
// .count('id')
162+
// .groupBy('accept_classement')
163+
// .orderBy('accept_classement')
164+
165+
// return view.render('votes/charts/accept-news-classement', {
166+
// votes: {
167+
// labels: JSON.stringify(result.map((vote) => (vote.accept_classement ? 'Oui' : 'Non'))),
168+
// data: JSON.stringify(result.map((vote) => vote.count)),
169+
// },
170+
// })
171+
// }
172+
173+
// public async acceptNewsActivities({ view }) {
174+
// const result = await Database.from('votes')
175+
// .select('accept_activities')
176+
// .count('id')
177+
// .groupBy('accept_activities')
178+
// .orderBy('accept_activities')
179+
180+
// return view.render('votes/charts/accept-news-activities', {
181+
// votes: {
182+
// labels: JSON.stringify(result.map((vote) => (vote.accept_activities ? 'Oui' : 'Non'))),
183+
// data: JSON.stringify(result.map((vote) => vote.count)),
184+
// },
185+
// })
186+
// }
176187
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
"alpinejs": "^3.12.0",
8585
"aws-sdk": "^2.1353.0",
8686
"chart.js": "^4.2.1",
87+
"chartjs-adapter-date-fns": "^3.0.0",
88+
"date-fns": "^2.30.0",
8789
"disposable-email-domains": "^1.0.62",
8890
"edge-iconify": "^1.0.1",
8991
"file-loader": "^6.2.0",

pnpm-lock.yaml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/chart.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import Chart from 'chart.js/auto'
2+
import 'chartjs-adapter-date-fns'
23

34
up.compiler('canvas#chart', function (element, data) {
5+
data.datasets.forEach((dataset) => {
6+
dataset.data = dataset.data.map((item) => ({
7+
x: new Date(item.x).valueOf(),
8+
y: item.y,
9+
}))
10+
})
11+
console.log(data)
412
new Chart(element, {
513
type: 'line',
614
data,
@@ -9,6 +17,15 @@ up.compiler('canvas#chart', function (element, data) {
917
y: {
1018
beginAtZero: true,
1119
},
20+
x: {
21+
type: 'time',
22+
time: {
23+
unit: 'day',
24+
displayFormats: {
25+
day: 'dd/MM/yyyy',
26+
},
27+
},
28+
},
1229
},
1330
},
1431
})

0 commit comments

Comments
 (0)