Skip to content

Commit

Permalink
Merge pull request #22 from Zhyltix/better-and-extra-graphs
Browse files Browse the repository at this point in the history
Better and extra graphs
  • Loading branch information
leonoosterhuis committed Jan 15, 2024
2 parents 37f2216 + 279d0e5 commit 4f42258
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 53 deletions.
8 changes: 8 additions & 0 deletions app/Domain/Trip/Enum/TripEventEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ enum TripEventEnum: String
case HARSH_STEERING = 'harsh_steering';
case INEFFICIENT_STEERING = 'inefficient_steering';

case SYSTEM_START = "System Start";
case SYSTEM_END = "System End";

public function getEventTitle(mixed $value = null): string
{
return match ($this) {
Expand All @@ -37,6 +40,9 @@ public function getEventTitle(mixed $value = null): string
self::INEFFICIENT_GEAR => 'Inefficient Gear',
self::HARSH_STEERING => 'Harsh Steering',
self::INEFFICIENT_STEERING => 'Inefficient Steering',
self::SYSTEM_START => 'System Start',
self::SYSTEM_END => 'System End',

default => throw new \Exception('Unexpected match value'),
};
}
Expand Down Expand Up @@ -78,6 +84,8 @@ public function getEventSeverity(mixed $value = null): SeverityEnum
self::INEFFICIENT_GEAR => SeverityEnum::MEDIUM,
self::HARSH_STEERING => SeverityEnum::MEDIUM,
self::INEFFICIENT_STEERING => SeverityEnum::MEDIUM,
self::SYSTEM_START => SeverityEnum::LOW,
self::SYSTEM_END => SeverityEnum::LOW,
default => throw new \Exception('Unexpected match value'),
};
}
Expand Down
3 changes: 3 additions & 0 deletions app/Domain/Trip/Model/TripData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @property array $accelero
* @property array $gyroscope
* @property float $speed
* @property float $speed_limit
* @property \Illuminate\Support\Carbon $time
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
Expand All @@ -43,13 +44,15 @@ class TripData extends Model
'accelero',
'gyroscope',
'speed',
'speed_limit',
'timestamp'
];

protected $casts = [
'accelero' => 'array',
'gyroscope' => 'array',
'speed' => 'float',
'speed_limit' => 'float',
'timestamp' => 'datetime'
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ public function createDataEntry(Request $request, string $id): ApiEloquentSucess
// Validate the request data
$validator = Validator::make($request->all(), [
'data' => 'required|array',
'data.*' => 'required|array|size:4',
'data.*' => 'required|array|size:5',
'data.*.*' => 'required',
'data.*.0' => 'required|numeric',
'data.*.1' => 'required|numeric',
'data.*.2' => 'required|array|size:3',
'data.*.3' => 'required|array|size:3',
'data.*.4' => 'numeric',
]);

if ($validator->fails()) {
Expand All @@ -115,13 +116,14 @@ public function createDataEntry(Request $request, string $id): ApiEloquentSucess

// Create data entries for the trip
foreach ($dataEntries as $dataEntry) {
list($timestamp, $speed, $accelero, $gyroscope) = $dataEntry;
list($timestamp, $speed, $accelero, $gyroscope, $speed_limit) = $dataEntry;

$trip->data()->create([
'timestamp' => $timestamp,
'speed' => $speed,
'accelero' => $accelero,
'gyroscope' => $gyroscope,
'speed_limit' => $speed_limit,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ class GraphController extends Controller

private string $timeUnitInterval = 'minute';

private array $dotData = [];

public function getDotData(): array
{
return $this->dotData;
}

public function setDotData(array $dotData): void
{
$this->dotData = $dotData;
}

public function getTimeUnitInterval(): string
{
return $this->timeUnitInterval;
Expand Down Expand Up @@ -373,6 +385,7 @@ private function toArray()
'dataLabelsEnabled' => $this->dataLabelsEnabled,
'zoomEnabled' => $this->zoomEnabled,
'timeUnitInterval' => $this->timeUnitInterval,
'dotData' => $this->dotData,
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\UserInterface\Domain\Trip\Controllers\Graph\Types;

use App\Domain\Trip\Model\TripData;
use App\Helpers\Graph\Enums\AxisFormat;
use App\Helpers\Graph\Enums\GraphType;
use App\Helpers\Graph\Enums\ValueType;
use App\UserInterface\Domain\Trip\Controllers\Graph\GraphController;
use Illuminate\Contracts\View\View;

class WarningGraph extends GraphController
{
public function __construct()
{
parent::__construct('warning_graph');
$this->setGraphName('Speed and Braking')
->setGraphXName('Time')
->setGraphYName('Speed / Braking')
->setGraphType(GraphType::LINE)
->setXValueType(ValueType::NUMBER)
->setYValueType(ValueType::NUMBER)
->setXAxisFormat(AxisFormat::LINEAR)
->setYAxisFormat(AxisFormat::LINEAR)
->setTooltipFormat([['{speed} km/h'], ['{brakepower} %']])
->setColors(['#1e88e5', '#e53935']) // Twee kleuren voor twee datasets
->setLegendDisplay(true)
->setAxisLabels([
[
'x' => 'timestamp',
'y' => 'speed',
],
[
'x' => 'timestamp',
'y' => 'brakepower',
],
[
'x' => 'timestamp',
'y' => 'type',
]
])
->setDataLabelsEnabled(false)
->setZoomEnabled(true)
->setTimeUnitInterval("millisecond")
->setGraphData([])
->setDotData([]);
}


public function render(): View
{
return $this->make();
}

}
25 changes: 25 additions & 0 deletions app/UserInterface/Domain/Trip/Controllers/TripReviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
use App\Domain\Shared\Interface\BreadCrumbInterface;
use App\Domain\Shared\ValueObject\BreadCrumbValueObject;
use App\Domain\Shared\ValueObject\RouteValueObject;
use App\Domain\Trip\Helper\TripProfileParser;
use App\Domain\Trip\Model\Trip;
use App\Domain\Trip\Model\TripData;
use App\Domain\Trip\Model\TripEvent;
use App\Helpers\View\Abstract\AbstractViewController;
use App\Helpers\View\ValueObject\ButtonValueObject;
use App\Helpers\View\ValueObject\PageHeaderValueOject;
use App\Infrastructure\Laravel\Controller;
use App\UserInterface\Domain\Trip\Controllers\Graph\Types\BrakingGraph;
use App\UserInterface\Domain\Trip\Controllers\Graph\Types\SpeedGraph;
use App\UserInterface\Domain\Trip\Controllers\Graph\Types\WarningGraph;
use DateTime;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -49,6 +52,28 @@ public function getGraph(Request $request)

return $speed_brakeGraph->render();

case "warnings":
$warningGraph = new WarningGraph();

$trip_events = TripEvent::whereTripId($request->input('id'))->get([])->toArray();
$trip_events = array_map(function ($event) {
$event['timestamp'] = strtotime($event['timestamp']);
return $event;
}, $trip_events);

$dbData = TripData::where('trip_id', $request->input('id'))
->orderBy('timestamp', 'asc')
->get(['speed', 'timestamp', "accelero"])
->toArray();

$warningGraph->setGraphData([$this->formatTimeStamp($dbData), $this->calculateBrakePower($dbData)]);
$warningGraph->setDotData($trip_events);


return $warningGraph->render();



default:
$data = [];
return $data;
Expand Down
78 changes: 27 additions & 51 deletions app/UserInterface/Domain/Trip/Resources/views/graph_base.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function convertSingleArray(currentArray, xyFieldValue) {
if (currentItem[yAxisLabel] !== undefined) {
var yValue = currentItem[yAxisLabel];
convertedData.push({ timestamp: time, [yAxisLabel]: yValue });
convertedData.push({timestamp: time, [yAxisLabel]: yValue});
} else {
console.error(yAxisLabel + " is undefined for input data at index", i, currentItem);
}
Expand All @@ -45,53 +45,6 @@ function convertSingleArray(currentArray, xyFieldValue) {
return convertedData;
}
function convertDataArray(inputData, xyFieldValues) {
var data = [];
// Controleer of inputData een 2D-array of 1D-array is
if (Array.isArray(inputData[0])) {
// 2D-array, verwerk elke subarray
for (var j = 0; j < inputData.length; j++) {
data.push(convertSingleArray(inputData[j], xyFieldValues[j]));
}
} else {
// 1D-array, verwerk als enkele array
data.push(convertSingleArray(inputData, xyFieldValues[0]));
}
return data;
}
function convertSingleArray(currentArray, xyFieldValue) {
var convertedData = [];
var xAxisLabel = xyFieldValue['x'];
var yAxisLabel = xyFieldValue['y'];
for (var i = 0; i < currentArray.length; i++) {
var currentItem = currentArray[i];
if (currentItem.timestamp) {
var dateString = currentItem.timestamp;
var time = new Date(dateString).getTime();
if (currentItem[yAxisLabel] !== undefined) {
var yValue = currentItem[yAxisLabel];
convertedData.push({ timestamp: time, [yAxisLabel]: yValue });
} else {
console.error(yAxisLabel + " is undefined for input data at index", i, currentItem);
}
} else {
console.error("Timestamp is undefined for input data at index", i, currentItem);
}
}
return convertedData;
}
var root = am5.Root.new("chartdiv");
root.setThemes([am5themes_Animated.new(root)]);
Expand Down Expand Up @@ -133,9 +86,6 @@ function convertSingleArray(currentArray, xyFieldValue) {
var colors = @json($colors);
var series1 = chart.series.push(am5xy.LineSeries.new(root, {
name: "{{ $graphName }}",
xAxis: xAxis,
Expand Down Expand Up @@ -203,6 +153,32 @@ function convertSingleArray(currentArray, xyFieldValue) {
sbseries2.data.setAll(data2);
}
var dotData = @json($dotData);
var dotDataConverted = convertSingleArray(dotData, xyFieldValues[2]);
var dotDataMap = {};
dotDataConverted.forEach(function(dotItem) {
dotDataMap[dotItem[xyFieldValues[2]["x"]]] = dotItem.type;
});
series1.bullets.push(function(root, series, dataItem) {
var type = dotDataMap[dataItem.dataContext[xyFieldValues[0]["x"]]];
if (type) {
return am5.Bullet.new(root, {
sprite: am5.Circle.new(root, {
radius: 5,
fill: am5.color("#ff0000"),
tooltipText: type
})
});
}
});
series1.appear(1000);
if (data2.length > 0) {
series2.appear(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<select id="chart_type" class="form-control">
<option selected="selected" value="speed">Speed</option>
<option value="speed_braking">Speed & Braking</option>
<option value="warnings">Speed, braking & Warnings</option>
</select>
</div>
<div class="col-9">
Expand Down

0 comments on commit 4f42258

Please sign in to comment.