Skip to content

Commit

Permalink
added option to gzip json
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Sep 3, 2024
1 parent 91df7eb commit 5afa0fb
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
htmlwidgets,
jsonlite,
magrittr
Depends:
R (>= 2.10)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ importFrom(htmlwidgets,createWidget)
importFrom(htmlwidgets,shinyRenderWidget)
importFrom(htmlwidgets,shinyWidgetOutput)
importFrom(htmlwidgets,sizingPolicy)
importFrom(jsonlite,as_gzjson_b64)
importFrom(magrittr,"%>%")
importFrom(utils,modifyList)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# uPlot (development version)
# uPlot v0.0.0.9300

* Updated uplot to 1.6.30.
13 changes: 11 additions & 2 deletions R/uPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#' a `data.frame` where first column is the x-axis and the others the series to plot.
#' @param options Options to generate the plot.
#' @param ... Additional arguments.
#' @param use_gzipped_json Use [jsonlite::as_gzjson_b64()] to compress JSON data.
#' @param width,height A numeric input in pixels.
#' @param elementId Use an explicit element ID for the widget.
#'
#' @return An `htmlwidget` object of class `"uPlot"`.
#'
#' @importFrom htmlwidgets createWidget sizingPolicy
#' @importFrom grDevices palette
#' @importFrom jsonlite as_gzjson_b64
#'
#' @export
#'
Expand All @@ -30,7 +32,13 @@
#' )
#' )
#' )
uPlot <- function(data, options = list(), ..., width = NULL, height = NULL, elementId = NULL) {
uPlot <- function(data,
options = list(),
...,
use_gzipped_json = FALSE,
width = NULL,
height = NULL,
elementId = NULL) {

options <- as.list(options)
series_nms <- names(data)
Expand All @@ -48,10 +56,11 @@ uPlot <- function(data, options = list(), ..., width = NULL, height = NULL, elem

x <- list(
config = list(
data = data,
data = if (use_gzipped_json) as_gzjson_b64(data) else data,
options = options,
...
),
use_gzipped_json = use_gzipped_json,
series_nms = series_nms
)

Expand Down
2 changes: 1 addition & 1 deletion inst/htmlwidgets/uPlot.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions man/uPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"homepage": "https://github.com/pvictor/uPlot#readme",
"devDependencies": {
"css-loader": "^6.7.1",
"pako": "^2.1.0",
"style-loader": "^3.3.1",
"uplot": "^1.6.30",
"webpack": "^5.73.0",
Expand Down
9 changes: 9 additions & 0 deletions srcjs/widgets/uPlot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import uPlot from "uplot";
import "uplot/dist/uPlot.min.css";
import * as utils from "../modules/utils";
import { stackedChart } from "../modules/stack";
import { ungzip } from "pako";

const resizer = el => {
const func = (u, init) => {
Expand Down Expand Up @@ -118,6 +119,14 @@ HTMLWidgets.widget({
options.width = width;
options.height = height;
data = x.config.data;
if (x.use_gzipped_json) {
const gezipedData = atob(data);
const gzipedDataArray = Uint8Array.from(gezipedData, c => c.charCodeAt(0));
const ungzipedData = ungzip(gzipedDataArray);
const decodedData = new TextDecoder().decode(ungzipedData);
data = JSON.parse(decodedData);
}
console.log(data);
if (x.stacked) {
if (!options.hooks)
options.hooks = {};
Expand Down

0 comments on commit 5afa0fb

Please sign in to comment.