Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Actions Support + Minor Bug + gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
hugo082 committed May 27, 2017
1 parent 94888bf commit ba42d33
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 24 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Created by .ignore support plugin (hsz.mobi)
### macOS template
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

2 changes: 1 addition & 1 deletion Core/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct()
public function encode(): array {
$lines = array();
$labels = array();
/** @var Line $item */
/** @var Line $line */
foreach ($this->lines as $line) {
$encode = $line->encode();
$labels = $encode["labels"];
Expand Down
13 changes: 12 additions & 1 deletion Core/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ class Graph
*/
private $method;

/**
* @var array
*/
private $actions;


public function __construct(string $id, string $type, string $title, string $service, string $method)
public function __construct(string $id, string $type, string $title, string $service, string $method, array $actions)
{
$this->id = $id;
$this->type = $type;
$this->title = $title;
$this->service = $service;
$this->method = $method;
$this->actions = $actions;
$this->data = new Data();
}

Expand All @@ -64,6 +70,7 @@ public function __construct(string $id, string $type, string $title, string $ser
public function encode() : array {
return array(
"informations" => $this->encodeGraphInformations(),
"actions" => $this->actions,
"data" => $this->data->encode()
);
}
Expand Down Expand Up @@ -171,4 +178,8 @@ public function setService(string $service)
{
$this->service = $service;
}

public static function fromArray(array $data): ?Graph {
return new Graph($data["id"], $data["type"], $data["title"], $data["service"], $data["method"], $data["actions"]);
}
}
6 changes: 5 additions & 1 deletion Core/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function defaultLabelForDate(string $format, string $increments, $value =
if ($lastDate == null || $lastDate < $item->getDate())
$lastDate = $item->getDate();
}
if ($firstDate == null || $lastDate == null)
return;
$date = clone $firstDate;
$date->modify($increments);
if ($date <= $firstDate)
Expand Down Expand Up @@ -135,7 +137,9 @@ public function setValueForItemWithLabel(string $label, float $value, bool $desi

public function encode(): array {
$res = array(
"label" => $this->label
"label" => $this->label,
"labels" => array(),
"data" => array()
);
/** @var DataItem $item */
foreach ($this->items as $item)
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ private function addEntitiesSection(ArrayNodeDefinition $node)
->enumNode('type')->isRequired()->cannotBeEmpty()
->values(Configuration::TYPES)
->end()
->arrayNode('actions')
->prototype('array')->addDefaultsIfNotSet()
->children()
->scalarNode('title')->isRequired()->cannotBeEmpty()->end()
->scalarNode('id')->cannotBeEmpty()->end()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
5 changes: 1 addition & 4 deletions DependencyInjection/DBStatisticExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ private function loadBase(array $config, ContainerBuilder $container)
*/
private function loadGraphs(array $config, ContainerBuilder $container)
{
foreach ($config['graphs'] as $name => $values) {
foreach ($config['graphs'] as $name => &$values) {
$values['id'] = $name;

if (!isset($values['service']))
$values['service'] = $config['service'];

$config['graphs'][$name] = $values;
}
$container->setParameter($this->getAlias().'.graphs', $config['graphs']);
}
Expand Down
7 changes: 5 additions & 2 deletions Manager/GraphManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public function getGraphWithID(string $graphID, array $parameters) : ?Graph {
}

private function decodeGraphs(array $graphs) {
foreach ($graphs as $graph)
$this->graphs[$graph["id"]] = new Graph($graph["id"], $graph["type"], $graph["title"], $graph["service"], $graph["method"]);
foreach ($graphs as $graph) {
$g = Graph::fromArray($graph);
if ($g != null)
$this->graphs[$graph["id"]] = $g;
}
}
}
49 changes: 35 additions & 14 deletions Resources/public/js/statcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
* Created by hugofouquet on 26/03/2017.
*/

function loadGraph(gId) {
function loadGraph(gID) {
$.ajax({
url: "/statistic/data/" + gId,
url: "/statistic/data/" + gID,
data: null,
success: function( res ) {
var graph = res.graph;
var response = res.response;
var container = $("div[data-id='" + response.id + "']");
if (response.statusCode === 200) {
var elementID = "elm_" + gId;
var elementID = "elm_" + graph.informations.id;
$('<canvas>').attr({
id: elementID
}).appendTo(container);
insertButtons([{title:"hello", id:"helloid"}], container);
insertActions(graph, container);
showGraph(graph, elementID);
} else {
container.append("<h3 class='graph-error graph-error-title'>Error Loading Graph</h3>");
Expand All @@ -26,23 +26,44 @@ function loadGraph(gId) {
}

function showGraph(graph, elemId){
new Chart(elemId, {
window["graphs"][graph.informations.id] = new Chart(elemId, {
type: graph.informations.type,
data: graph.data,
options: graph.options
});
}

function insertButtons(buttons, parent) {
var buttonsContainer = $('<div>').attr({
class: "buttons-container"
}).prependTo(parent);
function updateGraph(gID, action) {
$.ajax({
url: "/statistic/data/" + gID,
data: action,
success: function( res ) {
var chart = window["graphs"][res.response.id];
if (res.response.statusCode === 200 && typeof chart !== "undefined") {
chart.data.labels = res.graph.data.labels;
chart.data.datasets = res.graph.data.datasets;
chart.update();
} else
console.error("Impossible to update graph with id : " + gID);
}
});
}

for (var i = 0; i < buttons.length; i++) {
var button = buttons[i];
$('<button>').attr({
id: button.id
}).text(button.title).prependTo(buttonsContainer);
function insertActions(graph, parent) {
var actionsContainer = $('<div>').attr({
class: "actions-container"
}).prependTo(parent);
for (var i = 0; i < graph.actions.length; i++) {
var action = graph.actions[i];
$('<button>')
.text(action.title)
.prependTo(actionsContainer)
.data("id", graph.informations.id)
.data("action", action)
.click(function () {
var data = $(this).data();
updateGraph(data.id, data.action);
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Default/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
display: inline-block;
background-color: #cdcdcd;
}
div[data-type=graph] div.buttons-container {
div[data-type=graph] div.actions-container {
text-align: center;
}
Expand Down

0 comments on commit ba42d33

Please sign in to comment.