Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  added font parameter
  Added Show one/all renamed Group to Groups
  fixed bug when colors > 20 (remove transparency)
  changed default font to arial added unique to Array JS
  allow NA values
  • Loading branch information
nachocab committed Sep 14, 2013
2 parents 669e3d1 + 0fde9f9 commit 8d9eaff
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: clickme
Maintainer: Nacho Caballero <nachocab@gmail.com>
Author: Nacho Caballero <nachocab@gmail.com>
Version: 0.3.0
Version: 0.3.1
License: GPL-3
Title: Straight from R to JS: Create interactive visualizations from R
URL: https://github.com/nachocab/clickme
Expand Down
1 change: 1 addition & 0 deletions R/Chart-params.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Chart$methods(
# Set the default parameters
get_params = function(){
params$port <<- params$port %or% 8000
params$font <<- params$font %or% "Rockwell, Helvetica, Arial, sans"

params$title <<- params$title %or% params$main %or% internal$file$names$template # alias (main)

Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ default_colors <- function(n = 9){
} else if (n <= 19) {
colors <- d3_category19
} else {
colors <- rainbow(n)
colors <- gsub("..$", "", rainbow(n)) # d3 doesn't like the transparency bytes #000000FF, so we remove them
}
colors
}
Expand Down
4 changes: 0 additions & 4 deletions inst/shared_assets/clickme.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ body{
font-size: 20px;
}

text {
font-family: Rockwell, Helvetica, Arial, sans;
}

.hide {
display: none;
}
Expand Down
14 changes: 13 additions & 1 deletion inst/shared_assets/d3_aux.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,16 @@ my_light_red = "#b90000"
if (opts.class)
div.attr("class", opts.class)

div
div

Array::unique = ->
o = {}
l = @length
r = []
i = 0
while i < l
o[this[i]] = this[i]
i += 1
for i of o
r.push o[i]
r
16 changes: 16 additions & 0 deletions inst/shared_assets/d3_aux.js

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

64 changes: 57 additions & 7 deletions inst/templates/Points/template/template.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<title>{{{ params$title }}}</title>

{{{ get_assets() }}}

<style>
text{
font-family: {{{ params$font }}};
}
</style>
</head>

<body>
Expand All @@ -16,7 +22,7 @@
</div>
<script type="text/javascript">
(function() {
var clip, color_legend_counts, color_scale, color_title, data, distance_between_show_names_and_color_groups, g_color_group_keys, g_points, g_toggle_names, keyuped, mouseout, mouseover, opacity, plot, point_names, points, redraw, search, search_clear, search_input, sidebar, static_radius, toggle_names, toggle_points, transform;
var clip, color_legend_counts, color_scale, color_title, data, deselect_color_groups, distance_between_show_names_and_color_groups, g_color_group_keys, g_color_title, g_points, g_toggle_names, keyuped, mouseout, mouseover, opacity, plot, point_names, points, redraw, search, search_clear, search_input, show_all_colors, sidebar, single_group, static_radius, toggle_names, toggle_points, transform;

data = {{ data }};

Expand Down Expand Up @@ -155,15 +161,26 @@
});
};
if (color_scale.range().length > 1) {
sidebar.append("text").attr({
"class": "color_title",
g_color_title = sidebar.append("text").attr({
"x": -static_radius,
"y": distance_between_show_names_and_color_groups,
"dy": ".35em"
}).style({
});
g_color_title.append("tspan").style({
"font-size": "16px",
"font-weight": "bold"
}).text(color_title);
if (color_scale.range().length > 2) {
single_group = g_color_title.append("tspan").attr({
"fill": "#949494",
"dx": "20px"
}).style({
"font-size": "16px",
"font-weight": "bold"
}).text("Show one").on("click", function() {
return deselect_color_groups();
});
}
g_color_group_keys = sidebar.selectAll(".color_group_key").data(color_scale.domain().reverse()).enter().append("g").attr({
"transform": function(d, i) {
return "translate(0, " + (i * (static_radius * 2 + 15) + distance_between_show_names_and_color_groups + 30) + ")";
Expand All @@ -188,7 +205,14 @@
}
}

show_all_colors = function() {
g_points.classed("hide", false);
g_color_group_keys.classed("hide", false);
return single_group.text("Show one");
};

toggle_points = function(category) {
var categories;
g_points.filter(function(d) {
return d.color_group === category;
}).classed("hide", function() {
Expand All @@ -199,9 +223,35 @@
}).classed("hide", function() {
return !d3.select(this).classed("hide");
});
if (g_points.filter(":not(.hide)")[0].length === 0) {
g_color_group_keys.classed("hide", false);
return g_points.classed("hide", false);
categories = g_points.filter(":not(.hide)").data().map(function(d) {
return d.color_group;
}).unique();
if (categories.length === 0) {
return show_all_colors();
} else if (categories.length === 1) {
return single_group.text("Show all");
} else {
return single_group.text("Show one");
}
};

deselect_color_groups = function() {
var categories, visible_category, visible_points;
visible_points = g_points.filter(":not(.hide)");
categories = visible_points.data().map(function(d) {
return d.color_group;
}).unique();
if (single_group.text() === "Show one") {
visible_category = categories.reverse()[0];
g_points.filter(function(d) {
return d.color_group !== visible_category;
}).classed("hide", true);
g_color_group_keys.filter(function(d) {
return d !== visible_category;
}).classed("hide", true);
return single_group.text("Show all");
} else {
return show_all_colors();
}
};

Expand Down
54 changes: 48 additions & 6 deletions inst/templates/Points/template/template.coffee.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<title>{{{ params$title }}}</title>

{{{ get_assets() }}}

<style>
text{
font-family: {{{ params$font }}};
}
</style>
</head>

<body>
Expand Down Expand Up @@ -152,17 +158,29 @@

# Draw color legend only when there is more than one color
if color_scale.range().length > 1
sidebar.append("text")
g_color_title = sidebar.append("text")
.attr(
"class": "color_title"
"x": -static_radius
"y": distance_between_show_names_and_color_groups
"dy": ".35em")

g_color_title.append("tspan")
.style(
"font-size": "16px"
"font-weight": "bold")
.text(color_title)

if color_scale.range().length > 2
single_group = g_color_title.append("tspan")
.attr(
"fill": "#949494"
"dx": "20px")
.style(
"font-size": "16px"
"font-weight": "bold")
.text("Show one")
.on("click", ()-> deselect_color_groups())

g_color_group_keys = sidebar.selectAll(".color_group_key")
.data(color_scale.domain().reverse())
.enter().append("g")
Expand All @@ -185,6 +203,11 @@
.text((d) -> "#{d} (#{color_legend_counts[d]})")
.on("click", (d)-> toggle_points(d))

show_all_colors = () ->
g_points.classed("hide", false)
g_color_group_keys.classed("hide", false)
single_group.text("Show one")

toggle_points = (category)->
# if the elements with the category were hidden, then show; if not hidden, hide.
g_points.filter((d)-> d.color_group == category).classed("hide", ()->
Expand All @@ -195,10 +218,29 @@
!d3.select(this).classed("hide")
)

# show all categories if they are all hidden
if g_points.filter(":not(.hide)")[0].length == 0
g_color_group_keys.classed("hide", false)
g_points.classed("hide", false)
categories = g_points.filter(":not(.hide)").data().map((d)-> d.color_group).unique()


if categories.length == 0
show_all_colors()
else if categories.length == 1
single_group.text("Show all")
else
single_group.text("Show one")


deselect_color_groups = ()->
visible_points = g_points.filter(":not(.hide)")
categories = visible_points.data().map((d)-> d.color_group).unique()
if single_group.text() == "Show one"
visible_category = categories.reverse()[0]

g_points.filter((d)-> d.color_group != visible_category).classed("hide", true)
g_color_group_keys.filter((d)-> d != visible_category).classed("hide", true)
single_group.text("Show all")
else
show_all_colors()


# d3.select(window).on("keydown", () ->
# # switch (d3.event.keyCode) {
Expand Down
6 changes: 6 additions & 0 deletions inst/templates/Points/tests/test-Points-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ test_that("extra fields get added", {
points$get_params()
points$get_data()
expect_equivalent(points$data, data.frame(x = c("a", "b", "c"), y = 1:3, point_name = as.character(1:3), radius = 5, extra1 = c(10,20,30), extra2 = c(100, 200, 300)))

params <- list(x = c("a", "b", "c", "d"), y = c(1:3, NA), extra = data.frame(extra1 = c(10,20,30,40), extra2 = c(100,200,300,400)))
points <- Points$new(params)
points$get_params()
points$get_data()
expect_equivalent(points$data, data.frame(x = c("a", "b", "c"), y = 1:3, point_name = as.character(1:3), radius = 5, extra1 = c(10,20,30), extra2 = c(100, 200, 300)))
})

test_that("limits reduce the size of the data", {
Expand Down
2 changes: 2 additions & 0 deletions inst/templates/Points/translator/Points-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Points$methods(

data <<- apply_axes_limits(data)

data <<- na.omit(data)

params$formats <<- validate_formats(params$formats)

},
Expand Down
4 changes: 2 additions & 2 deletions inst/templates/Points/translator/Points-placeholders.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Points$methods(
color_scale <- gettextf("d3.scale.linear()
.domain(%s)
.range(%s)
.interpolate(d3.interpolateLab);",
.interpolate(d3.interpolateLab)",
to_json(params$color_domain),
to_json(color_range))
} else {
Expand All @@ -23,7 +23,7 @@ Points$methods(
} else {
color_range <- as.list(unname(params$palette[unique(data$color_group)]))
}
color_scale <- gettextf("d3.scale.ordinal().range(%s);", to_json(color_range))
color_scale <- gettextf("d3.scale.ordinal().range(%s)", to_json(color_range))
}

color_scale
Expand Down
2 changes: 1 addition & 1 deletion inst/templates/Points/translator/Points.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Points <- setRefClass("Points",

params$xlab <<- params$xlab %or% "x"
params$ylab <<- params$ylab %or% "y"
params$color_title <<- params$color_title %or% "Group"
params$color_title <<- params$color_title %or% "Groups"

params$palette <<- validate_palette(params$palette)

Expand Down

0 comments on commit 8d9eaff

Please sign in to comment.