Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map-with-Latitude-Longitude #68

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions vignettes/gallery/Map-with-Latitude-Longitude/index.rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: "Gallery: Map-with-Latitude-Longitude"
---

```{r setup, include=FALSE}
library(r2d3)
knitr::opts_chunk$set(echo=FALSE, comment=NA)
```

<div id="source-link">
<https://github.com/rstudio/r2d3/tree/master/vignettes/gallery/Map-with-Latitude-Longitude>
</div>

<div class="row">

<div class="col-sm-3">
<div class="list-group">
<a href="../bubbles/" class="list-group-item">bubbles</a>
<a href="../bullets/" class="list-group-item">bullets</a>
<a href="../calendar/" class="list-group-item">calendar</a>
<a href="../cartogram/" class="list-group-item">cartogram</a>
<a href="../chord/" class="list-group-item">chord</a>
<a href="../circlepacking/" class="list-group-item">circlepacking</a>
<a href="../dendogram/" class="list-group-item">dendogram</a>
<a href="../forcegraph/" class="list-group-item">forcegraph</a>
<a href="../morley/" class="list-group-item">morley</a>
<a href="../population/" class="list-group-item">population</a>
<a href="../radialtree/" class="list-group-item">radialtree</a>
<a href="../stackedbars/" class="list-group-item">stackedbars</a>
<a href="../streamgraph/" class="list-group-item">streamgraph</a>
<a href="../sunburst/" class="list-group-item">sunburst</a>
<a href="../treemap/" class="list-group-item">treemap</a>
<a href="../voronoi/" class="list-group-item">voronoi</a>
<a href="../Map-with-Latitude-Longitude/" class="list-group-item">Map-with-Latitude-Longitude</a>

</div>
</div>

<div class="col-sm-9">

```{r, echo=TRUE, eval=FALSE}
r2d3(data = jsonlite::read_json("test.json"),css = "map.css", d3_version = 3, dependencies = c("topojson.min.js", "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"), script = "map.js")
```

```{r, echo=FALSE}
r2d3(data = jsonlite::read_json("test.json"),css = "map.css", d3_version = 3, dependencies = c("topojson.min.js", "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"), script = "map.js")
```


#### Map.js

```{r, results='asis'}
cat("```js\n")
writeLines(readLines("map.js", warn = FALSE))
cat("```\n")
```

#### Map.css

```{r, results='asis'}
cat("```css\n")
writeLines(readLines("map.css", warn = FALSE))
cat("```\n")
```


</div>

</div>


```{js}
$('#source-link').appendTo($('.page-header')).css('display', 'block');
$('.template-vignette>.row>.col-md-9').attr('class', 'col-md-12');
$('a.list-group-item[href="../Map-with-Latitude-Longitude/"]').addClass('active');
$('a[href="../../../articles/gallery.html"]').parent().addClass('active');
```


44 changes: 44 additions & 0 deletions vignettes/gallery/Map-with-Latitude-Longitude/map.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.canton-boundary {
fill: none;
stroke: #fff;
stroke-linejoin: round;
}

text {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 10px;
text-anchor: middle;
}


.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: black;
color: #fff;
border-radius: 2px;
opacity: 0.001%;
}
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: black;
content: "\25BC";
position: absolute;
text-align: center;
opacity: 0.001%;
}

.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
opacity: 0.001%;
}
.area:hover {
fill: brown;
}
198 changes: 198 additions & 0 deletions vignettes/gallery/Map-with-Latitude-Longitude/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// !preview r2d3 data = jsonlite::read_json("test.json"), d3_version = 3, dependencies = c("topojson.min.js", "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js")

// Based on http://bl.ocks.org/lokesh005/7640d9b562bf59b561d6
var places = [
{
name: "1 Gorkha Rifles",
nam: "https://en.wikipedia.org/wiki/1_Gorkha_Rifles",
location: {
latitude: 30.975441,
longitude: 76.990228
}
},
{
name: "3 Gorkha Rifles",
nam: "https://en.wikipedia.org/wiki/3_Gorkha_Rifles",
location: {
latitude: 25.317645,
longitude:82.973914
}
}
,
{
name: "5 Gorkha Rifles",
nam: "https://en.wikipedia.org/wiki/5_Gorkha_Rifles",
location: {
latitude: 25.578773,
longitude:91.893254
}
}
,
{
name: "11 Gorkha Rifles",
nam: "https://en.wikipedia.org/wiki/11_Gorkha_Rifles",
location: {
latitude: 26.846694,
longitude: 80.946166
}
}
,
{
name: "Gharwal Rifles",
nam: "https://en.wikipedia.org/wiki/Gharwal_Rifles",
location: {
latitude: 29.837746,
longitude: 78.687107
}
}
,
{
name: "Brigade of the Guards",
nam: "https://en.wikipedia.org/wiki/Brigade_of_the_Guards",
location: {
latitude: 21.227531,
longitude: 79.190083
}
}
,
{
name: "Bihar Regiment",
nam: "https://en.wikipedia.org/wiki/Bihar_Regiment",
location: {
latitude: 25.620667,
longitude: 85.049325
}
}
,
{
name: "Parachute Regiment",
nam: "https://en.wikipedia.org/wiki/Parachute_Regiment",
location: {
latitude: 12.971599,
longitude: 77.594563
}
}
,
{
name: "Ramgarh Cantonment",
nam: "https://en.wikipedia.org/wiki/Ramgarh_Cantonment",
location: {
latitude:23.633224,
longitude: 85.514874
}
}
,
{
name: "Madras Regiment",
nam: "https://en.wikipedia.org/wiki/Madras_Regiment",
location: {
latitude: 11.363560,
longitude: 76.788471
}
}
,
{
name: "Grenadiers Regiment",
nam: "https://en.wikipedia.org/wiki/Grenadiers_Regiment",
location: {
latitude: 23.181467,
longitude: 79.986407
}
}
,
{
name: "Maratha Light Infantry",
nam: "https://en.wikipedia.org/wiki/Maratha_Light_Infantry",
location: {
latitude: 15.849695,
longitude: 74.497674
}
},
{
name: "Rajputana Rifles",
nam: "https://en.wikipedia.org/wiki/Rajputana_Rifles",
location: {
latitude: 28.596128,
longitude: 77.158738
}
},
{
name: "Kumaon Regiment",
nam: "https://en.wikipedia.org/wiki/Kumaon_Regiment",
location: {
latitude: 29.643362,
longitude: 79.432182
}
}
,
{
name: "Sikh Light Infantry",
nam: "https://en.wikipedia.org/wiki/Sikh_Light_Infantry",
location: {
latitude: 27.367290,
longitude: 79.622137
}
}
];
var width = 960,
height = 600;
var color= d3.scale.ordinal()
.domain([1,2,3,4,5,6,7,8,9])
.range("red", "steelblue");

var projection = d3.geo.mercator().scale(1100).translate([-1000,800]);
var path = d3.geo.path()
.projection(projection);

var root = svg
.attr("viewBox", "0 0 900 800")
.attr("preserveAspectRatio", "xMidYMid meet");
var data;
r2d3.onRender(function(us, svg, width, height, options) {


var cantons = topojson.feature(us, us.objects.india);

//svg.call(tip);
var group=root.selectAll("g")
.data(cantons.features)
.enter()
.append("g");
//.on('mouseover', tip.show)
//.on('mouseout', tip.hide)


var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-5, 0])
.style("left", "300px")
.style("top", "400px")
.html(function(d) {
return ("<a href="+d.nam+" target='_blank'>"+d.name +"</a>");
});

root.call(tip);


root.selectAll(".pin")
.data(places)
.enter().append("circle", ".pin")
.attr("r", 5)
.attr("transform", function(d) {
return "translate(" + projection([
d.location.longitude,
d.location.latitude
]) + ")";
})
.on('mouseover', tip.show)
.on('click', tip.hide);

//var projection = d3.geo.mercator().scale(900).translate([-600,700]);
var path= d3.geo.path().projection(projection);

var areas= group.append("path")
.attr("d", path)
.attr("class", "area")
.attr("fill","steelblue");

});
1 change: 1 addition & 0 deletions vignettes/gallery/Map-with-Latitude-Longitude/test.json

Large diffs are not rendered by default.

Loading