Add demo using highcharter

Change-Id: I01e8bedd23d4ad25e28183086097e95a86bd5e66
diff --git a/demo/highcharter.R b/demo/highcharter.R
new file mode 100644
index 0000000..cdf1362
--- /dev/null
+++ b/demo/highcharter.R
@@ -0,0 +1,122 @@
+library(RKorAPClient)
+library(highcharter)
+
+hc_freq_by_year_ci <- function(df, as.alternatives = F, ylabel = if(as.alternatives) "%" else "ipm") {
+  title <- ""
+  df <- df %>%
+    { if(! as.alternatives) ipm(.) else RKorAPClient::percent(.) }
+
+  if (!"year" %in% colnames(df)) {
+    df <- df %>% mutate(year = as.integer(queryStringToLabel(df$vc, pubDateOnly = T)))
+  }
+  if (!"condition" %in% colnames(df)) {
+    if (length(base::unique(df$query)) > 1) {
+      df <- df %>% mutate(condition = query)
+      if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = T ))) > 1) {
+        df <- df %>% mutate(condition = paste(condition, " & ",
+                                              queryStringToLabel(vc, excludePubDate = T )))
+      }
+    } else {
+      title <- base::unique(df$query)
+      if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = T ))) > 1) {
+        df <- df %>% mutate(condition = queryStringToLabel(vc, excludePubDate = T ))
+      }
+    }
+  }
+  # use the D3 palette which provides 20 attractive and distinguishable colours
+  palette <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B", "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF", "#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94", "#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5")
+  highchart() %>%
+    hc_title(text=title) %>%
+    hc_chart(zoomType="xy") %>%
+    hc_yAxis(
+      title = list(text = if (as.alternatives) "" else ylabel),
+      ceiling = if (as.alternatives) 100 else NULL,
+      floor = 0,
+      labels = if(as.alternatives) list(format = paste0("{value}\U2009", ylabel)) else NULL
+    ) %>%
+    hc_xAxis(allowDecimals=F) %>%
+    hc_add_theme(hc_theme_google(colors=palette)) %>%
+    hc_plotOptions(
+      series = list(enabled = T),
+      line = list(cursor = 'pointer', point = list(events = list(
+      click = JS("function() { window.open(this.click, 'korap'); }")
+    )))) %>%
+    hc_credits(enabled = T,
+               text = "KorAP R Client Pakckage",
+               href = "//github.com/KorAP/RKorAPClient/") %>%
+    hc_exporting(enabled = T) %>%
+    hc_tooltip(
+      formatter = JS(paste0("function (tooltip) {
+        var str = tooltip.defaultFormatter.call(this, tooltip);
+        if(Array.isArray(str))  {
+          str = str.join('');
+        }
+       for (var i = 0; i < this.points.length; i++) {
+         str = str.replace(/([0-9.,]+.?)", ylabel, "/, this.points[i].point.count+' ($1@)');
+       }
+       return str.replace(/@/g, '", ylabel, "')
+      } ")),
+      crosshairs =  T,
+      valueDecimals = 2,
+      shared = T,
+      valueSuffix = paste0('\U2009', ylabel)
+    ) %>%
+    hc_add_series_korap_frequencies(df, as.alternatives)
+}
+
+hc_add_series_korap_frequencies <- function(hc, df, as.alternatives = F) {
+  index <- 0
+  for(q in unique(df$condition)) {
+    dat <- df[df$condition==q,]
+    hc <- hc %>% hc_add_series(
+      marker = list(radius = 2),
+      name = q,
+      data = data.frame(
+        year = dat$year,
+        value = if (as.alternatives) dat$f else dat$ipm,
+        count = dat$totalResults,
+        click = dat$webUIRequestUrl
+      ),
+      hcaes(year, value),
+      type = 'line',
+      colorIndex = index,
+      zIndex = 1
+    ) %>%
+      hc_add_series(
+        name = "ci",
+        data = dat[,c('year', 'conf.low', 'conf.high')],
+        hcaes(x = year, low = conf.low, high = conf.high),
+        type = 'arearange',
+        fillOpacity = 0.3,
+        lineWidth = 0,
+        marker = list(enabled = F),
+        enableMouseTracking = F,
+        linkedTo= ':previous',
+        colorIndex = index,
+        zIndex = 0
+      )
+    index <- index+1
+  }
+  hc
+}
+
+plotHighchart <- function(query = "Schlumpf",
+                          years = c(2000:2010),
+                          as.alternatives = length(query) > 1,
+                          vc = "textType = /Zeit.*/ & availability!=QAO-NC-LOC:ids & pubDate in",
+                          kco = new("KorAPConnection", verbose=T) ) {
+  hc <-
+    frequencyQuery(kco, query, paste(vc, years), as.alternatives=as.alternatives) %>%
+    hc_freq_by_year_ci(as.alternatives)
+  print(hc)
+  hc
+}
+
+saveHCPlot <- function(hc, fname) {
+  htmlwidgets::saveWidget(hc, file=fname, selfcontained = T)
+}
+
+#h1 <-plotHighchart(c("macht []{0,3} Sinn", "ergibt []{0,3} Sinn"), c(1980:2018))
+h1 <- plotHighchart(c("Leser | Lesern | Lesers", 'Leserin | Leserinnen', 'LeserIn | LeserInnen', '"Leser[_\\*]in.*"'), c(1985:2018), as.alternatives = F)
+#plotHighchart(c("Tollpatsch", "Tolpatsch"), c(1991:2018))
+