R/utils_hc.R

Defines functions hc_gauge cores_gauge hc_donut hc_serie hc_mapa_calor custom_color_classes hc_mapa hc_custom_exporting

hc_custom_exporting <- function(hc, align = "right") {
  highcharter::hc_exporting(
    hc,
    enabled = TRUE,
    buttons = list(
      contextButton = list(
        align = align,
        menuItems = c(
          "printChart",
          "downloadPNG",
          "downloadJPEG",
          "downloadPDF",
          "separator",
          "downloadCSV",
          "downloadXLS"
        )
      )
    ),
    filename = "grafico-isdp"
  )
}

# -------------------------------------------------------------------------



hc_mapa <- function(tab, tab_geojson) {
  
  texto_tooltip <- paste0(
    '<span style="font-size:12px; font-weight: bold;">{point.munip_nome}</span><br/>',
    'População: <b>{point.proj_pop_total} habitantes</b><br/>'
  )
  
  highcharter::highchart(type = "map") %>% 
    highcharter::hc_add_series(
      mapData = tab_geojson,
      states = list(hover = list(color = "#a4edba")),
      joinBy = c("municipio_cod", "munip_cod"),
      data = tab,
      name = "Selecionado",
      borderColor = "#0a0a0a"
    ) %>%
    highcharter::hc_colorAxis(
      tickPixelInterval = 100,
      dataClasses = highcharter::color_classes(
        breaks = c(0, 0, 1, 1),
        colors = c("#f5e9e7", "#f5e9e7", "#005180")
      )
    ) %>%
    highcharter::hc_legend(
      enabled = FALSE
    ) %>%
    highcharter::hc_tooltip(
      pointFormat = texto_tooltip,
      headerFormat = NULL
    ) %>% 
    highcharter::hc_mapNavigation(
      enabled = TRUE,
      enableDoubleClickZoom = TRUE,
      enableMouseWheelZoom = TRUE,
      buttonOptions = list(
        verticalAlign = "bottom"
      )
    ) %>%
    hc_custom_exporting(align = "left")
  
}

hc_cores_mapa <- c(
  "#fed98e", 
  "#fe9929", 
  "#d95f0e", 
  "#993404"
)


# -------------------------------------------------------------------------



custom_color_classes <- function(breaks = NULL, 
                                 colors = c("#440154", "#21908C", "#FDE725"),
                                 unit = 1) {
  lbrks <- length(breaks)
  highcharter::list_parse(
    data.frame(
      from = breaks[-lbrks],
      to = c(breaks[-c(1, lbrks)] - unit, breaks[lbrks]),
      to = breaks[-1], 
      color = (grDevices::colorRampPalette(colors))(lbrks - 1), 
      stringsAsFactors = FALSE
    )
  )
}


# -------------------------------------------------------------------------

hc_mapa_calor <- function(tab, tab_geojson, variavel, unit = 0.1,
                          label = FALSE) {
  
  texto_tooltip <- paste0(
    '<span style="font-size:12px; font-weight: bold;">{point.munip_nome}</span><br/>',
    'População: <b>{point.populacao} habitantes</b><br/>',
    'Indicador: <b>{point.indicador}</b><br/>'
  )
  
  acc <- ifelse(unit == 0.1, 1, 0)
  
  unidade_medida <- pegar_unidade_de_medida(variavel)
  
  tab <- tab %>% 
    dplyr::rename(value = variavel) %>% 
    dplyr::mutate(
      populacao = formatar_numero(proj_pop_total, accuracy = 1),
      value = ifelse(is.infinite(value), NA, value),
      value = round(value, acc),
      indicador = formatar_indicador(value, unidade_medida, label)
    ) %>% 
    dplyr::distinct(munip_cod, .keep_all = TRUE)

  quebras <- round(quantile(tab$value, na.rm = TRUE), acc)
  cores <- hc_cores_mapa[1:(length(quebras) - 1)]

  highcharter::highchart(type = "map") %>% 
    highcharter::hc_add_series(
      mapData = tab_geojson,
      states = list(hover = list(color = "#a4edba")),
      joinBy = c("municipio_cod", "munip_cod"),
      data = tab,
      name = "Selecionado",
      borderColor = "#0a0a0a"
    ) %>%
    highcharter::hc_colorAxis(
      tickPixelInterval = 100,
      dataClasses = custom_color_classes(
        breaks = quebras,
        colors = cores,
        unit = unit
      )
    ) %>%
    highcharter::hc_legend(
      itemStyle = list(textDecoration = "nome"),
      itemHoverStyle = list(textDecoration = "underline"),
      valueDecimals = 1
    ) %>% 
    highcharter::hc_tooltip(
      pointFormat = texto_tooltip,
      headerFormat = NULL
    ) %>% 
    highcharter::hc_mapNavigation(
      enabled = TRUE,
      enableDoubleClickZoom = TRUE,
      enableMouseWheelZoom = TRUE,
      buttonOptions = list(
        verticalAlign = "bottom"
      )
    ) %>%
    hc_custom_exporting(align = "left")
  
}


# -------------------------------------------------------------------------

hc_serie <- function(dados, nome_formatado, unidade_de_medida, 
                     text_color = "#666666", type = "line", xlab = "",
                     ylab = "") {
  
  perc <- ifelse(
    stringr::str_detect(formatar_indicador(1, unidade_de_medida), "%"),
    "%",
    ""
  )
  
  texto_tooltip <- paste0(
    '<b>{point.y:.1f}', perc, " ", unidade_de_medida, '</b><br/>'
  )
  
  highcharter::highchart() %>% 
    highcharter::hc_chart(
      zoomType = "x"
    ) %>% 
    highcharter::hc_series(
      list(
        data = dados, 
        name = nome_formatado, 
        type = type
      )
    ) %>% 
    highcharter::hc_xAxis(
      type = "date",
      labels = list(style = list(color = text_color)),
      title = list(text = xlab, style = list(color = text_color))
    ) %>%
    highcharter::hc_yAxis(
      labels = list(style = list(color = text_color)),
      title = list(text = ylab, style = list(color = text_color))
    ) %>%
    highcharter::hc_tooltip(
      pointFormat = texto_tooltip,
      headerFormat = NULL
    ) %>% 
    highcharter::hc_legend(enabled = FALSE)
  
}


# -------------------------------------------------------------------------

hc_donut <- function(tab, name, cor, size = "280%") {
  highcharter::highchart() %>% 
    highcharter::hc_chart(
      plotBackgroundColor = "transparent",
      plotBorderWidth = 0,
      plotShadow = FALSE
    ) %>% 
    highcharter::hc_series(
      list(
        data = tab, 
        name = name,
        type = "pie",
        innerSize = "50%"
      )
    ) %>% 
    highcharter::hc_plotOptions(
      pie = list(
        dataLabels = list(
          enabled = TRUE,
          distance = -30,
          format = "{point.name}: {point.percentage:.1f}%",
          style = list(
            fontWeight = "bold",
            color = "white",
            fontSize = "12px"
          )
        ),
        startAngle = -90,
        endAngle = 90,
        center = c("50%", "95%"),
        size = size
      )
    ) %>% 
    highcharter::hc_tooltip(
      useHTML = TRUE,
      headerFormat = "<small>{series.name}</small><br>",
      pointFormat = "{point.name}: <b>{point.percentage:.1f}%</b>"
    ) %>% 
    highcharter::hc_colors(colors = cor)
}


# -------------------------------------------------------------------------

cores_gauge <- function() {
  c("#9e32e6", "#eb5294", "#f0f564", "#3fba6c", "#7697e8")
}

hc_gauge <- function(tab, indicador, faixas, cores) {
  highcharter::highchart() |> 
    highcharter::hc_series(
      list(
        name = toupper(indicador),
        data = list(
          list(
            y = tab[[indicador]],
            dataLabels = list(
              padding = 17
            )
          )
        )
      )
    ) |>
    highcharter::hc_chart(
      type = "gauge"
    ) |> 
    highcharter::hc_pane(
      startAngle = -150,
      endAngle = 150,
      background = list(
        backgroundColor = "transparent",
        borderWidth = 0
      )
    ) |> 
    highcharter::hc_yAxis(
      min = min(faixas),
      max = max(faixas),
      minorTicks = FALSE,
      tickPositions = faixas,
      tickLength = 20,
      title = list(
        text = tab[[paste0(indicador, "_label")]],
        style = list(
          `font-size` = "25px",
          color = "black"
        )
      ),
      labels = list(
        distance = -35,
        style = list(fontSize = "12px")
      ),
      tickColor = "black",
      plotBands = list(
        list(
          from = faixas[1],
          to = faixas[2],
          color = cores[1],
          thickness = 20
        ),
        list(
          from = faixas[2],
          to = faixas[3],
          color = cores[2],
          thickness = 20
        ),
        list(
          from = faixas[3],
          to = faixas[4],
          color = cores[3],
          thickness = 20
        ),
        list(
          from = faixas[4],
          to = faixas[5],
          color = cores[4],
          thickness = 20
        ),
        list(
          from = faixas[5],
          to = faixas[6],
          color = cores[5],
          thickness = 20
        )
      )
    ) |> 
    highcharter::hc_plotOptions(
      series =  list(
        dataLabels = list(
          style = list(fontSize = "20px")
        )
      )
    )
  
}
openvironment/ods6 documentation built on Feb. 7, 2023, 9:24 a.m.