Column

Sales by Country (Europe)

Column

Sales Forecast

Sales by Category

Best Sellers

---
title: "Sales Report with Highcharter"
author: "Joshua Kunst"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
library(highcharter)
library(dplyr)
library(viridisLite)
library(forecast)
library(treemap)
library(arules)
library(flexdashboard)
library(purrr)
library(jsonlite)
library(tibble)

thm <- 
  hc_theme(
    colors = c("#1a6ecc", "#434348", "#90ed7d"),
    chart = list(
      backgroundColor = "transparent",
      style = list(fontFamily = "Source Sans Pro")
    ),
    xAxis = list(
      gridLineWidth = 1
    )
  )
```

Column {data-width=600}
-----------------------------------------------------------------------

### Sales by Country (Europe)

```{r}
# 1) Pobierz geojson mapy Europy (obiekt list)
eu_map <- jsonlite::fromJSON(
  "https://code.highcharts.com/mapdata/custom/europe.geo.json",
  simplifyVector = FALSE
)

# 2) Zbuduj ramkę z kluczami 'hc-key' + przykładowe wartości sprzedaży
eu_data <- highcharter::get_data_from_map(eu_map) %>%
  dplyr::mutate(
    value = round(runif(dplyr::n(), 10, 100))
  )

# 3) Skala kolorów
n <- 4
colstops <- data.frame(
  q = 0:n/n,
  c = substring(viridisLite::viridis(n + 1), 0, 7)
) %>% highcharter::list_parse2()

# 4) Rysowanie mapy (większa wysokość dla czytelności)
highcharter::highchart(type = "map") %>%
  highcharter::hc_add_series_map(
    map = eu_map,
    df  = eu_data,
    value = "value",
    joinBy = "hc-key",
    name = "Sales",
    dataLabels = list(enabled = TRUE, format = "{point.name}")
  ) %>%
  highcharter::hc_chart(height = 650) %>%
  highcharter::hc_colorAxis(stops = colstops) %>%
  highcharter::hc_legend(valueDecimals = 0) %>%
  highcharter::hc_mapNavigation(enabled = TRUE) %>%
  highcharter::hc_add_theme(thm)
```

Column {.tabset data-width=400}
-----------------------------------------------------------------------

### Sales Forecast

```{r}
AirPassengers %>% 
  forecast(level = 90) %>% 
  hchart() %>% 
  hc_add_theme(thm)
```

### Sales by Category

```{r, fig.keep='none'}
data("Groceries", package = "arules")
dfitems <- tibble::as_tibble(Groceries@itemInfo)

set.seed(10)

dfitemsg <- dfitems %>%
  mutate(category = gsub(" ", "-", level1),
         subcategory = gsub(" ", "-", level2)) %>%
  group_by(category, subcategory) %>% 
  summarise(sales = n() ^ 3, .groups = "drop") %>% 
  sample_n(31)

tm <- treemap(dfitemsg, index = c("category", "subcategory"),
              vSize = "sales", vColor = "sales",
              type = "value", palette = rev(viridis(6)))

hctreemap(tm, allowDrillToNode = TRUE, layoutAlgorithm = "squarified") %>% 
  hc_add_theme(thm)
```

### Best Sellers

```{r}
set.seed(2)

nprods <- 10

dfitems %>% 
  sample_n(nprods) %>% 
  .$labels %>% 
  rep(times = sort(sample(1e4:2e4, size = nprods), decreasing = TRUE)) %>% 
  factor(levels = unique(.)) %>% 
  hchart(showInLegend = FALSE, name = "Sales", pointWidth = 10) %>% 
  hc_add_theme(thm) %>% 
  hc_chart(type = "bar")
```