Skip to content

Commit

Permalink
Weight Chart
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Dec 25, 2023
1 parent 258758a commit 2cf77a3
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 9 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Light and easy workout diary
- [Quick start](https://github.com/aceberg/exercisediary#quick-start)
- [Config](https://github.com/aceberg/exercisediary#config)
- [Options](https://github.com/aceberg/exercisediary#options)
- [Roadmap](https://github.com/aceberg/exercisediary#roadmap)
- [Thanks](https://github.com/aceberg/exercisediary#thanks)


Expand Down Expand Up @@ -47,7 +48,15 @@ Configuration can be done through config file or environment variables

| Key | Description | Default |
| -------- | ----------- | ------- |
| -c | Path to config dir | /data/ExerciseDiary |
| -c | Path to config dir | /data/ExerciseDiary |

## Roadmap
- [x] HeatMap
- [x] Left/Right buttons for date
- [x] Local Themes and JS
- [x] BodyWeight Chart
- [ ] Login/Password
- [ ] Statistics page

## Thanks
- All go packages listed in [dependencies](https://github.com/aceberg/exercisediary/network/dependencies)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions internal/db/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func Create(path string) {
"REPS" INTEGER
);`
exec(path, sqlStatement)

sqlStatement = `CREATE TABLE IF NOT EXISTS weight (
"ID" INTEGER PRIMARY KEY,
"DATE" TEXT,
"WEIGHT" INTEGER
);`
exec(path, sqlStatement)
}

// InsertEx - insert one exercise into DB
Expand Down Expand Up @@ -61,6 +68,17 @@ func InsertSet(path string, ex models.Set) {
exec(path, sqlStatement)
}

// InsertW - insert weight
func InsertW(path string, ex models.BodyWeight) {

sqlStatement := `INSERT INTO weight (DATE, WEIGHT)
VALUES ('%s','%d');`

sqlStatement = fmt.Sprintf(sqlStatement, ex.Date, ex.Weight)

exec(path, sqlStatement)
}

// DeleteEx - delete one exercise
func DeleteEx(path string, id int) {

Expand Down
13 changes: 13 additions & 0 deletions internal/db/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ func SelectSet(path string) (sets []models.Set) {

return sets
}

// SelectW - select all weight from DB
func SelectW(path string) (w []models.BodyWeight) {

mu.Lock()
dbx := connect(path)
err := dbx.Select(&w, "SELECT * FROM weight ORDER BY ID ASC")
mu.Unlock()

check.IfError(err)

return w
}
12 changes: 10 additions & 2 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type Set struct {

// AllExData - all sets and exercises
type AllExData struct {
Exs []Exercise
Sets []Set
Exs []Exercise
Sets []Set
Weight []BodyWeight
}

// HeatMapData - data for HeatMap
Expand All @@ -51,6 +52,13 @@ type HeatMapData struct {
V int
}

// BodyWeight - store weight
type BodyWeight struct {
ID int `db:"ID"`
Date string `db:"DATE"`
Weight int `db:"WEIGHT"`
}

// GuiData - web gui data
type GuiData struct {
Config Conf
Expand Down
6 changes: 6 additions & 0 deletions internal/web/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func indexHandler(c *gin.Context) {

exData.Exs = db.SelectEx(appConfig.DBPath)
exData.Sets = db.SelectSet(appConfig.DBPath)
exData.Weight = db.SelectW(appConfig.DBPath)

guiData.Config = appConfig
guiData.ExData = exData
Expand All @@ -27,6 +28,11 @@ func indexHandler(c *gin.Context) {
return guiData.ExData.Exs[i].Place < guiData.ExData.Exs[j].Place
})

// Sort weight by Date
sort.Slice(guiData.ExData.Weight, func(i, j int) bool {
return guiData.ExData.Weight[i].Date < guiData.ExData.Weight[j].Date
})

c.HTML(http.StatusOK, "header.html", guiData)
c.HTML(http.StatusOK, "index.html", guiData)
}
Expand Down
53 changes: 53 additions & 0 deletions internal/web/public/js/weight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var dates = [];
var ws = [];

function splitWeight(weight) {
dates = [];
ws = [];

weight = weight.slice(-7)

for (let i = 0 ; i < 7; i++) {
dates.push(weight[i].Date);
ws.push(weight[i].Weight)
}
// console.log('LDATA =', dates, ws);
};

function weightChart(weight, wcolor) {

splitWeight(weight);
// console.log('FDATA =', dates, ws);

const ctx = document.getElementById('weight-chart');

new Chart(ctx, {
type: 'line',
data: {
labels: dates,
datasets: [{
data: ws,
borderColor: wcolor,
borderWidth: 1
}]
},
options: {
scales: {
x: {
ticks: {
display: false
},
},
y: {
beginAtZero: false
}
},
plugins:{
legend: {
display: false
}
}
}
});

};
4 changes: 2 additions & 2 deletions internal/web/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.3.0"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
</head>
<body>
Expand All @@ -41,7 +41,7 @@
<li><a class="dropdown-item" href="/exercise/?id=new">Add Exercise</a></li>
<!-- <li><a class="dropdown-item" href="/stats/">Stats</a></li> -->
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" target="_blank" href="https://github.com/aceberg/ExerciseDiary">About</a></li>
<li><a class="dropdown-item" target="_blank" href="https://github.com/aceberg/ExerciseDiary">GitHub</a></li>
</ul>
</li>
</div>
Expand Down
52 changes: 48 additions & 4 deletions internal/web/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,60 @@
</head>
<script src="./fs/public/js/heatmap.js"></script>
<script src="./fs/public/js/index.js"></script>
<script src="./fs/public/js/weight.js"></script>
<link rel="stylesheet" type="text/css" href="./fs/public/css/index.css" />
<!-- <body onload='setFormDateSets({{ .ExData.Sets }})'> -->
<body>
<br>
<!-- Add Weight Modal -->
<div class="modal" id="mWeight">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add weight</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
<span aria-hidden="true"></span>
</button>
</div>
<div class="modal-body">
<form action="/weight/" method="post" name="sets">
<table class="table table-borderless">
<tr>
<td>Date</td>
<td><input name="date" type="date" class="form-control"></td>
</tr>
<tr>
<td>Weight</td>
<td><input name="weight" type="number" class="form-control"></td>
</tr>
<tr>
<td>
<button type="submit" class="btn btn-primary">Save</button>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
<!-- End Modal -->
<div class="container">
<div class="row">
<div class="col">
<div class="card border-primary">
<div class="card-body">
<div class="chart-container">
<canvas id="matrix-chart" style="max-height: 100px; width: 820px;" class="m-auto">
<div class="hstack gap-2">
<div class="chart-container m-auto">
<canvas id="matrix-chart" style="max-height: 100px; width: 820px;">
</div>
<div class="chart-container m-auto">
<canvas id="weight-chart" style="max-height: 100px;">
</div>
<div class="m-auto">
<button class="btn del-set-button" title="Add weight" data-bs-toggle="modal" data-bs-target="#mWeight">
<h4><i class="bi bi-window-plus"></i></h4>
</button>
</div>
</div>
</div>
</div>
Expand All @@ -33,7 +76,7 @@ <h2 class="accordion-header">
<div class="accordion-body">
{{ range $.ExData.Exs }}
{{ if eq .Group $gr }}
<div class="d-grid gap-2 d-md-block">
<div class="hstack gap-2">
<a href="/exercise/?id={{ .ID }}"><button class="btn add-exercise-button" title="Edit">
<i class="bi bi-pencil-square"></i>
</button></a>
Expand Down Expand Up @@ -108,6 +151,7 @@ <h5 class="modal-title">{{ .Name }}</h5>
<script>
setFormDate({{ .ExData.Sets }});
makeChart({{ .HeatMap }}, {{ .Config.HeatColor }}, {{ .ExData.Sets }});
weightChart({{ .ExData.Weight }}, {{ .Config.HeatColor }});
</script>
{{ template "footer.html" }}
{{ end }}
1 change: 1 addition & 0 deletions internal/web/webgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Gui(dirPath, nodePath string) {
router.POST("/exercise/", saveExerciseHandler) // exercise.go
router.POST("/exdel/", deleteExerciseHandler) // exercise.go
router.POST("/set/", setHandler) // set.go
router.POST("/weight/", weightHandler) // weight.go

err := router.Run(address)
check.IfError(err)
Expand Down
26 changes: 26 additions & 0 deletions internal/web/weight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package web

import (
// "log"
"net/http"
"strconv"

"github.com/gin-gonic/gin"

"github.com/aceberg/ExerciseDiary/internal/db"
"github.com/aceberg/ExerciseDiary/internal/models"
)

func weightHandler(c *gin.Context) {
var w models.BodyWeight

w.Date = c.PostForm("date")
weightStr := c.PostForm("weight")

w.Weight, _ = strconv.Atoi(weightStr)

// log.Println("WEIGHT =", w)
db.InsertW(appConfig.DBPath, w)

c.Redirect(http.StatusFound, "/")
}

0 comments on commit 2cf77a3

Please sign in to comment.