---
title: "DSTG - grafovi klase"
output: 
  flexdashboard::flex_dashboard:
    social: menu
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{css, echo=FALSE}
.sidebar { overflow: auto; }
.dataTables_scrollBody {
    height:95% !important;
    max-height:95% !important;
}
/*.chart-stage-flex {
    overflow:auto !important;
}*/
```

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(readxl)
library(igraph)
library(htmlwidgets)
library(visNetwork)
library(RColorBrewer)

studenti1 <- read_excel("ocjene_DSTG_2021_2022.xlsx") %>%
  mutate_at(vars(KOL1:UKUPNO), as.numeric) %>%
  unite("Puno ime", Ime, Prezime, sep = " ") %>%
  select(`Puno ime`, KOL1, KOL2) %>%
  mutate(klasa1 = case_when(KOL1 <= 7.5 ~ "1", 
                           KOL1 <= 15 ~ "2",
                           KOL1 <= 22.5 ~ "3",
                           .default = "4"),
         klasa2 = case_when(KOL2 <= 7.5 ~ "1", 
                            KOL2 <= 15 ~ "2",
                            KOL2 <= 22.5 ~ "3",
                            .default = "4")) %>%
  mutate_at(vars(klasa1, klasa2), ~fct_relevel(., c("1","2","3","4"))) %>%
  select(`Puno ime`, KOL1, KOL2, klasa1, klasa2)

studenti2 <- read_excel("ocjene_DSTG_2022_2023.xlsx") %>%
  mutate_at(vars(KOL1:UKUPNO), as.numeric) %>%
  unite("Puno ime", Ime, Prezime, sep = " ") %>%
  mutate(KOL1 = 6/5 * KOL1, KOL2 = 6/5 * KOL2) %>%
  select(`Puno ime`, KOL1, KOL2) %>%
  mutate(klasa1 = case_when(KOL1 <= 7.5 ~ "1", 
                            KOL1 <= 15 ~ "2",
                            KOL1 <= 22.5 ~ "3",
                            .default = "4"),
         klasa2 = case_when(KOL2 <= 7.5 ~ "1", 
                            KOL2 <= 15 ~ "2",
                            KOL2 <= 22.5 ~ "3",
                            .default = "4")) %>%
  mutate_at(vars(klasa1, klasa2), ~fct_relevel(., c("1","2","3","4"))) %>%
  select(`Puno ime`, KOL1, KOL2, klasa1, klasa2)

br <- nrow(studenti1) + nrow(studenti2)
studenti <- studenti1 %>% bind_rows(studenti2) %>%
  mutate(label = paste0("student ", 1:br), id = 1:br) %>%
  relocate(c("id", "label"), .before = KOL1)

nodes1 <- studenti %>% select(id, label, KOL1, klasa1)
nodes2 <- studenti %>% select(id, label, KOL2, klasa2)
edges <- expand_grid(from = studenti$id, to = studenti$id) %>%
  filter(from < to) %>%
  mutate(razKOL1 = abs(studenti$KOL1[from] - studenti$KOL1[to]),
         razKOL2 = abs(studenti$KOL2[from] - studenti$KOL2[to]))
edges1 <- edges %>% filter(between(razKOL1, 0, 1)) %>% select(from, to)
edges2 <- edges %>% filter(between(razKOL2, 0, 1)) %>% select(from, to)

# unutrasnost
boja1 <- c(brewer.pal(11, "PiYG")[4], brewer.pal(11, "RdYlBu")[4],
           brewer.pal(11, "RdYlBu")[8], brewer.pal(11, "PiYG")[8])
#unutrasnjost highlight
boja2 <- c(brewer.pal(11, "PiYG")[5], brewer.pal(11, "RdYlBu")[5],
           brewer.pal(11, "RdYlBu")[7], brewer.pal(11, "PiYG")[7])
#rub
boja3 <- c(brewer.pal(11, "PiYG")[3], brewer.pal(11, "RdYlBu")[3],
           brewer.pal(11, "RdYlBu")[9], brewer.pal(11, "PiYG")[9])

nodes1$color.background <- boja1[nodes1$klasa1]
nodes1$color.border <- boja3[nodes1$klasa1]
nodes1$color.highlight.background <- boja2[nodes1$klasa1] 
nodes1$color.highlight.border <- boja3[nodes1$klasa1]
nodes1$group <- nodes1$klasa1
nodes1$title <- paste0("KOL 1: ", round(nodes1$KOL1,2))

net <- visNetwork(nodes1, edges1, width = "100%", height = 900) %>% 
  visPhysics(solver = 'forceAtlas2Based', minVelocity = 5,
             stabilization = list(iterations = 50, onlyDynamicEdges = TRUE),
             forceAtlas2Based = list(avoidOverlap = 0.9))
net <- visOptions(net, highlightNearest = TRUE) %>% 
  visGroups(groupname = "1", color = boja1[1]) %>%
  visGroups(groupname = "2", color = boja1[2]) %>%
  visGroups(groupname = "3", color = boja1[3]) %>%
  visGroups(groupname = "4", color = boja1[4]) %>%
  visLegend(width = 0.02, position = "right", main = "Klasa KOL1")

nodes2$color.background <- boja1[nodes2$klasa2]
nodes2$color.border <- boja3[nodes2$klasa2]
nodes2$color.highlight.background <- boja2[nodes2$klasa2] 
nodes2$color.highlight.border <- boja3[nodes2$klasa2]
nodes2$group <- nodes2$klasa2
nodes2$title <- paste0("KOL 2: ", round(nodes2$KOL2,2))

net2 <- visNetwork(nodes2, edges2, width = "100%", height = 900) %>% 
  visPhysics(solver = 'forceAtlas2Based', minVelocity = 5,
             stabilization = list(iterations = 50, onlyDynamicEdges = TRUE),
             forceAtlas2Based = list(avoidOverlap = 0.9))
net2 <- visOptions(net2, highlightNearest = TRUE) %>% 
  visGroups(groupname = "1", color = boja1[1]) %>%
  visGroups(groupname = "2", color = boja1[2]) %>%
  visGroups(groupname = "3", color = boja1[3]) %>%
  visGroups(groupname = "4", color = boja1[4]) %>%
  visLegend(width = 0.02, position = "right", main = "Klasa KOL2")
```

# KOL1 graf

```{r}
net
```

# KOL2 graf

```{r}
net2
```

