Skip to content

Commit

Permalink
feat: implement chord
Browse files Browse the repository at this point in the history
  • Loading branch information
swsoyee committed Feb 27, 2021
1 parent 498747d commit 86003ad
Show file tree
Hide file tree
Showing 9 changed files with 13,081 additions and 9,932 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(calendarOutput)
export(n_area_bump)
export(n_bump)
export(n_calendar)
export(n_chord)
export(n_line)
export(n_options)
export(n_scatter)
Expand Down
83 changes: 83 additions & 0 deletions R/chord.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#' Chord Visualization
#'
#' Chord diagram.
#'
#' @param data data set in data.frame.
#' @param matrix a matrix used to compute the chord diagram.
#' @param keys keys used to identify each cell in the matrix.
#' @param render "svg" (responsive) or "canvas". "canvas" is well suited for
#' large data sets as it does not impact DOM tree depth, however you'll lose the
#' isomorphic rendering ability.
#' @param ... additional arguments.
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
#' string and have \code{'px'} appended.
#' @param element_id element id of widgets.
#'
#' @import htmlwidgets
#' @return a nivo Chord component
#' @export
#' @seealso \href{https://nivo.rocks/chord/}{Additional arguments}
#'
#' @examples
#' library(nivor)
#'
#' # generate data
#' set.seed(1)
#' data <- matrix(round(rexp(25) * 100, 0), 5, 5)
#'
#' # the simplest use
#' n_chord(
#' matrix = data,
#' keys = c("A", "B", "C", "D", "E")
#' )
n_chord <- function(
matrix,
keys,
render = c("svg", "canvas"),
...,
width = NULL,
height = NULL,
element_id = NULL) {
render <- match.arg(render, c("svg", "canvas"))

if (missing(matrix)) {
stop("matrix should be passed in.")
}
if (!(is.matrix(matrix) || is.data.frame(matrix))) {
stop("matrix only accepte a matrix or data.frame.")
}
if (nrow(matrix) != ncol(matrix)) {
stop("row and column are not equal in matrix.")
}
if (nrow(matrix) != length(keys)) {
stop("keys and dimension of matrix are not equal.")
}

# describe a React component to send to the browser for rendering.
component <- reactR::reactMarkup(
htmltools::tag(
ifelse(
render == "canvas",
"ResponsiveChordCanvas",
"ResponsiveChord"
),
list(
matrix = unname(as.list(as.data.frame(matrix))),
keys = keys,
# assume extra arguments are props
...
)
)
)

# create widget
htmlwidgets::createWidget(
name = "calendar", # TODO it's a bug, should be rename to nivo
component,
width = width,
height = height,
package = "nivor",
elementId = element_id
)
}
22,820 changes: 12,889 additions & 9,931 deletions inst/htmlwidgets/calendar.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/calendar.js.map

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions man/n_chord.Rd

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 @@ -7,6 +7,7 @@
"@nivo/core": "^0.67.0",
"@nivo/line": "^0.67.0",
"@nivo/scatterplot": "^0.67.0",
"@nivo/chord": "^0.67.0",
"prop-types": "^15.7.2"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions srcjs/nivo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ResponsiveCalendar, ResponsiveCalendarCanvas } from '@nivo/calendar';
import { ResponsiveAreaBump, ResponsiveBump } from '@nivo/bump';
import { ResponsiveLine, ResponsiveLineCanvas } from '@nivo/line';
import { ResponsiveScatterPlot, ResponsiveScatterPlotCanvas } from "@nivo/scatterplot";
import { ResponsiveChord, ResponsiveChordCanvas } from "@nivo/chord";

reactWidget(
'calendar',
Expand All @@ -16,6 +17,8 @@ reactWidget(
ResponsiveLineCanvas,
ResponsiveScatterPlot,
ResponsiveScatterPlotCanvas,
ResponsiveChord,
ResponsiveChordCanvas,
},
{},
);
20 changes: 20 additions & 0 deletions tests/testthat/test-chord.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
describe("n_chord()", {
it("matrix is not matrix", {
data <- 1
expected <- "matrix only accepte a matrix or data.frame."
expect_error(n_chord(data), expected)
})

it("matrix dimension are not equal", {
data <- matrix(round(rexp(20) * 100, 0), 5, 4)
expected <- "row and column are not equal in matrix."
expect_error(n_chord(data), expected)
})

it("matrix dimension and keys are not equal", {
data <- matrix(round(rexp(25) * 100, 0), 5, 5)
keys <- "a"
expected <- "keys and dimension of matrix are not equal."
expect_error(n_chord(data, keys), expected)
})
})
26 changes: 26 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,19 @@
lodash.memoize "^4.1.2"
lodash.range "^3.2.0"

"@nivo/chord@^0.67.0":
version "0.67.0"
resolved "https://registry.yarnpkg.com/@nivo/chord/-/chord-0.67.0.tgz#7b856a4373c7b31411d8fa8b4e7089a0cd67ccad"
integrity sha512-V6F5D7Ihfc6hxjndvJnjQtoyuoM2od3Hv3DRs/UzJi2TIQfVdrvROC1qQzl79MM83ojA76UPsviV9Y/ssgnKRw==
dependencies:
"@nivo/colors" "0.67.0"
"@nivo/legends" "0.67.0"
"@nivo/tooltip" "0.67.0"
d3-chord "^1.0.6"
d3-shape "^1.3.5"
lodash "^4.17.11"
react-motion "^0.5.2"

"@nivo/[email protected]":
version "0.67.0"
resolved "https://registry.yarnpkg.com/@nivo/colors/-/colors-0.67.0.tgz#5a2adc4b55406466efb8bbafecc08249e4e94e69"
Expand Down Expand Up @@ -2169,13 +2182,26 @@ cyclist@^1.0.1:
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz"
integrity sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=

d3-array@1:
version "1.2.4"
resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-1.2.4.tgz#635ce4d5eea759f6f605863dbcfc30edc737f71f"
integrity sha512-KHW6M86R+FUPYGb3R5XiYjXPq7VzwxZ22buHhAEVG5ztoEcZZMLov530mmccaqA1GghZArjQV46fuc8kUqhhHw==

d3-array@^2.3.0:
version "2.11.0"
resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-2.11.0.tgz"
integrity sha512-26clcwmHQEdsLv34oNKq5Ia9tQ26Y/4HqS3dQzF42QBUqymZJ+9PORcN1G52bt37NsL2ABoX4lvyYZc+A9Y0zw==
dependencies:
internmap "^1.0.0"

d3-chord@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/d3-chord/-/d3-chord-1.0.6.tgz#309157e3f2db2c752f0280fedd35f2067ccbb15f"
integrity sha512-JXA2Dro1Fxw9rJe33Uv+Ckr5IrAa74TlfDEhE/jfLOaXegMQFQTAgAw9WnZL8+HxVBRXaRGCkrNU7pJeylRIuA==
dependencies:
d3-array "1"
d3-path "1"

"d3-color@1 - 2", d3-color@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/d3-color/-/d3-color-2.0.0.tgz"
Expand Down

0 comments on commit 86003ad

Please sign in to comment.