Skip to content
Permalink
Browse files

Added Weight Chart

  • Loading branch information
ADefWebserver committed Nov 10, 2019
1 parent ad14d91 commit 33e9362b4774b8a50d8fe277023dda1eb22ca98e
Showing with 150 additions and 26 deletions.
  1. +1 −0 FatSecretWebApp/FatSecretWebApp.csproj
  2. +136 −26 FatSecretWebApp/Pages/GetData.razor
  3. +13 −0 FatSecretWebApp/Pages/_Host.cshtml
@@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ChartJs.Blazor" Version="1.0.1" />
<PackageReference Include="Radzen.Blazor" Version="1.1.21" />
</ItemGroup>

@@ -8,7 +8,19 @@
@using fatsecret.platform.FatSecretFood;
@using fatsecret.platform.FatSecretWeight;

@using ChartJs.Blazor.Charts
@using ChartJs.Blazor.ChartJS
@using ChartJs.Blazor.ChartJS.Common.Properties
@using ChartJs.Blazor.ChartJS.Common.Enums
@using ChartJs.Blazor.ChartJS.Common.Legends
@using ChartJs.Blazor.ChartJS.Common.Axes
@using ChartJs.Blazor.ChartJS.Common.Axes.Ticks
@using ChartJs.Blazor.ChartJS.Common.Time
@using ChartJs.Blazor.ChartJS.LineChart
@using ChartJs.Blazor.Util

<h4>@ErrorMesage</h4>

@if ((strRequestToken != "") && (AuthSecret == ""))
{
<a target="_blank" href="https://www.fatsecret.com/oauth/authorize?oauth_token=@strRequestToken">
@@ -48,6 +60,7 @@
<RadzenButton ButtonStyle="ButtonStyle.Secondary"
Click="GetWeight" Text="Get Weight (click once and wait)" /><br /><br />
}
<ChartJsLineChart @ref="_lineChartJs" Config="@_lineConfig" Width="100" Height="50" />
<RadzenGrid AllowFiltering="true" AllowPaging="true" PageSize="12" AllowSorting="true"
Data="colFatSecretWeightOutput" TItem="FatSecretWeightOutput" ColumnWidth="200px">
<Columns>
@@ -159,6 +172,12 @@
bool ShowGetExerciseButton = true;
public List<FatSecretExerciseOutput> colFatSecretExerciseOutput = new List<FatSecretExerciseOutput>();

// Charting
LineConfig _lineConfig;
ChartJsLineChart _lineChartJs;

private LineDataset<TimeTuple<int>> _WeightDataSet;

protected override async Task OnInitializedAsync()
{
ConsumerKey = Configuration["ConsumerKey"];
@@ -170,6 +189,82 @@
FatSecretAPI api = new FatSecretAPI(ConsumerKey, ConsumerSecret);
api.GetAuthToken(out strRequestToken, out strOauthTokenSecret);
await Task.CompletedTask;

// Charting
_lineConfig = new LineConfig
{
Options = new LineOptions
{
Responsive = true,
Title = new OptionsTitle
{
Display = true,
Text = "Line Chart"
},
Legend = new Legend
{
Display = false
},
Tooltips = new Tooltips
{
Mode = InteractionMode.Nearest,
Intersect = false
},
Scales = new Scales
{
yAxes = new List<CartesianAxis>
{
new LinearCartesianAxis
{
ScaleLabel = new ScaleLabel
{
LabelString = "Weight"
}
}
},
xAxes = new List<CartesianAxis>
{
new TimeAxis
{
Distribution = TimeDistribution.Linear,
Ticks = new TimeTicks
{
Source = TickSource.Data
},
Time = new TimeOptions
{
Unit = TimeMeasurement.Day,
Round = TimeMeasurement.Day,
TooltipFormat = "DD.MM.YYYY",
DisplayFormats = TimeDisplayFormats.DE_CH
},
ScaleLabel = new ScaleLabel
{
LabelString = "Date"
}
}
}
},
Hover = new LineOptionsHover
{
Intersect = true,
Mode = InteractionMode.Y
}
}
};

_WeightDataSet = new LineDataset<TimeTuple<int>>
{
BackgroundColor = ColorUtil.FromDrawingColor(System.Drawing.Color.White),
BorderColor = ColorUtil.FromDrawingColor(System.Drawing.Color.Red),
Label = "Weight per Day",
Fill = false,
BorderWidth = 2,
PointRadius = 2,
PointBorderWidth = 2,
SteppedLine = SteppedLine.False,
Hidden = false
};
}

void GetRequestToken()
@@ -242,7 +337,8 @@

try
{
objFatSecretWeight = JsonSerializer.Deserialize<FatSecretWeight>(response);
objFatSecretWeight = JsonSerializer.Deserialize<FatSecretWeight>
(response);
foreach (var item in objFatSecretWeight.month.day)
{
colFatSecretWeightOutput.Add(new FatSecretWeightOutput
@@ -252,13 +348,14 @@
});

colCollection.Add($"{FatSecretUtility.GetDateFromFatSecretDay(item.date_int)}," +
$"{FatSecretUtility.GetPoundsFromKilos(item.weight_kg)}," +
$"{item.weight_kg}\r\n");
$"{FatSecretUtility.GetPoundsFromKilos(item.weight_kg)}," +
$"{item.weight_kg}\r\n");
}
}
catch
{
objFatSecretWeightSingle = JsonSerializer.Deserialize<FatSecretWeightSingle>(response);
objFatSecretWeightSingle = JsonSerializer.Deserialize<FatSecretWeightSingle>
(response);
if (objFatSecretWeightSingle.month.day != null)
{
colFatSecretWeightOutput.Add(new FatSecretWeightOutput
@@ -268,15 +365,20 @@
});

colCollection.Add($"" +
$"{FatSecretUtility.GetDateFromFatSecretDay(objFatSecretWeightSingle.month.day.date_int)}," +
$"{FatSecretUtility.GetPoundsFromKilos(objFatSecretWeightSingle.month.day.weight_kg)}," +
$"{objFatSecretWeightSingle.month.day.weight_kg}\r\n");
$"{FatSecretUtility.GetDateFromFatSecretDay(objFatSecretWeightSingle.month.day.date_int)}," +
$"{FatSecretUtility.GetPoundsFromKilos(objFatSecretWeightSingle.month.day.weight_kg)}," +
$"{objFatSecretWeightSingle.month.day.weight_kg}\r\n");
}
}

currentDate = currentDate.AddMonths(1);
}

// Charting
_WeightDataSet.AddRange(colFatSecretWeightOutput.Select(p => new TimeTuple<int>(new Moment(p.Date), Convert.ToInt32(p.Weight))));
_lineConfig.Data.Datasets.Add(_WeightDataSet);
_lineChartJs.Update();

response = string.Join("", colCollection);
await ExportData("Weight", response);
}
@@ -295,7 +397,9 @@
// https://platform.fatsecret.com/api/Default.aspx?screen=rapiref&method=exercise_entries.get_month
FatSecretAPI api = new FatSecretAPI(ConsumerKey, ConsumerSecret);

List<string> colCollection = new List<string>();
List<string>
colCollection = new List<string>
();
colCollection.Add("Date,Calories\r\n");

DateTime currentDate = dtStartDate.Date.AddDays(1 - dtStartDate.Day);
@@ -307,7 +411,8 @@

try
{
objFatSecretExercise = JsonSerializer.Deserialize<FatSecretExercise>(response);
objFatSecretExercise = JsonSerializer.Deserialize<FatSecretExercise>
(response);
foreach (var item in objFatSecretExercise.month.day)
{
colFatSecretExerciseOutput.Add(new FatSecretExerciseOutput
@@ -321,7 +426,8 @@
}
catch
{
objFatSecretExerciseSingle = JsonSerializer.Deserialize<FatSecretExerciseSingle>(response);
objFatSecretExerciseSingle = JsonSerializer.Deserialize<FatSecretExerciseSingle>
(response);
if (objFatSecretExerciseSingle.month.day != null)
{
colFatSecretExerciseOutput.Add(new FatSecretExerciseOutput
@@ -331,8 +437,8 @@
});

colCollection.Add($"" +
$"{FatSecretUtility.GetDateFromFatSecretDay(objFatSecretExerciseSingle.month.day.date_int)}," +
$"{objFatSecretExerciseSingle.month.day.calories}\r\n");
$"{FatSecretUtility.GetDateFromFatSecretDay(objFatSecretExerciseSingle.month.day.date_int)}," +
$"{objFatSecretExerciseSingle.month.day.calories}\r\n");
}
}

@@ -357,7 +463,9 @@
// https://platform.fatsecret.com/api/Default.aspx?screen=rapiref&method=food_entries.get_month
FatSecretAPI api = new FatSecretAPI(ConsumerKey, ConsumerSecret);

List<string> colCollection = new List<string>();
List<string>
colCollection = new List<string>
();
colCollection.Add("Date,Calories,Carbohydrates,Fats,Proteins\r\n");

DateTime currentDate = dtStartDate.Date.AddDays(1 - dtStartDate.Day);
@@ -369,7 +477,8 @@

try
{
objFatSecretFood = JsonSerializer.Deserialize<FatSecretFood>(response);
objFatSecretFood = JsonSerializer.Deserialize<FatSecretFood>
(response);
foreach (var item in objFatSecretFood.month.day)
{
colFatSecretFoodOutput.Add(new FatSecretFoodOutput
@@ -382,13 +491,14 @@
});

colCollection.Add(
$"{FatSecretUtility.GetDateFromFatSecretDay(item.date_int)}," +
$"{item.calories},{item.carbohydrate},{item.fat},{item.protein}\r\n");
$"{FatSecretUtility.GetDateFromFatSecretDay(item.date_int)}," +
$"{item.calories},{item.carbohydrate},{item.fat},{item.protein}\r\n");
}
}
catch
{
objFatSecretFoodSingle = JsonSerializer.Deserialize<FatSecretFoodSingle>(response);
objFatSecretFoodSingle = JsonSerializer.Deserialize<FatSecretFoodSingle>
(response);
if (objFatSecretFoodSingle.month.day != null)
{
colFatSecretFoodOutput.Add(new FatSecretFoodOutput
@@ -401,11 +511,11 @@
});

colCollection.Add($"" +
$"{FatSecretUtility.GetDateFromFatSecretDay(objFatSecretFoodSingle.month.day.date_int)}," +
$"{objFatSecretFoodSingle.month.day.calories}," +
$"{objFatSecretFoodSingle.month.day.carbohydrate}," +
$"{objFatSecretFoodSingle.month.day.fat}," +
$"{objFatSecretFoodSingle.month.day.protein}\r\n");
$"{FatSecretUtility.GetDateFromFatSecretDay(objFatSecretFoodSingle.month.day.date_int)}," +
$"{objFatSecretFoodSingle.month.day.calories}," +
$"{objFatSecretFoodSingle.month.day.carbohydrate}," +
$"{objFatSecretFoodSingle.month.day.fat}," +
$"{objFatSecretFoodSingle.month.day.protein}\r\n");
}
}

@@ -429,9 +539,9 @@

byte[] bytes = Encoding.ASCII.GetBytes(data);
await JsRuntime.InvokeVoidAsync(
"jsFunctions.saveFile",
strFinalFileName,
bytes,
"application/octet-stream");
"jsFunctions.saveFile",
strFinalFileName,
bytes,
"application/octet-stream");
}
}
@@ -30,5 +30,18 @@

<script src="_framework/blazor.server.js"></script>
<script src="jsFunctions.js"></script>

<!-- Since ChartJs is dependent on moment.js, it is important to first include the reference to moment.js and then to ChartJs -->
<!-- Reference the included moment.js javascript file. -->
<script src="_content/ChartJs.Blazor/moment-with-locales.min.js" type="text/javascript" language="javascript"></script>

<!-- Reference the included ChartJs javascript file. -->
<script src="_content/ChartJs.Blazor/Chart.min.js" type="text/javascript" language="javascript"></script>

<!-- This is the glue between the C# code and the ChartJs charts -->
<script src="_content/ChartJs.Blazor/ChartJsInterop.js" type="text/javascript" language="javascript"></script>

<!-- Some styling -->
<link rel="stylesheet" href="_content/ChartJs.Blazor/ChartJSBlazor.css" />
</body>
</html>

0 comments on commit 33e9362

Please sign in to comment.
You can’t perform that action at this time.