Related
I am putting some results together in a nested list (with arrays). The expected results must be EXACTLY as follows:
{
"item1": "TEXT",
"item2": "MORE TEXT",
"item3": [
"STILL TEXT"
],
"item4": [
"TEXT AGAIN"
],
"values": [
{
"start": 0,
"end": 99
}
]
}
I put all my results together like this:
listToJson <- c(list(item1 = "TEXT",
item2 = "MORE TEXT",
item3 = "STILL TEXT",
item4 = "TEXT AGAIN",
values = list(start = 99,
end = 0)))
write_json(listToJson, path = "test.json", auto_unbox = TRUE , null = "null")
The problem is that the results doesn't have array elements (see below). item3 and item4 should be arrays. How can I change my code to get the expected results in that exact format?
{
"item1":"TEXT",
"item2":"MORE TEXT",
"item3":"STILL TEXT",
"item4":"TEXT AGAIN",
"values":{
"start":99,
"end":0}
}
You can just use as.array for those specific items.
library(jsonlite)
listToJson <- c(
list(
item1 = "TEXT",
item2 = "MORE TEXT",
item3 = as.array("STILL TEXT"),
item4 = as.array("TEXT AGAIN"),
values = as.array(list(start = 99,
end = 0))
)
)
write_json(listToJson, path = "test.json", auto_unbox = TRUE , null = "null")
Output
{
"item1":"TEXT",
"item2":"MORE TEXT",
"item3":[
"STILL TEXT"
],
"item4":[
"TEXT AGAIN"
],
"values":[
{
"start":0,
"end":99
}
]
}
I am following: http://radacad.com/create-custom-visual-with-r-and-json-part3
Here it is mentioned that with below R code, we can plot the chart:
Values<-data.frame(X,Y,Legend,Row,Column)
g=ggplot(Values, aes(x=X, y=Y,colour = Legend))
I am using similar implementations.
Below code renders blank chart with only axis:
Values <- data.frame(mpg_test, cyl_test)
p <- plot_ly(Values,
x = ~mpg_test,
y = ~cyl_test,
name = "SF Zoo",
type = "bar"
)
Empty chart
Below code also renders empty chart:
Values <- data.frame(mpg_test, cyl_test)
p <- plot_ly(Values,
x = mpg_test,
y = cyl_test,
name = "SF Zoo",
type = "bar"
)
Below code renders correct chart:
Values <- data.frame(mpg_test, cyl_test)
p <- plot_ly(Values,
x = ~mpg_test[1:nrow(Values), ],
y = ~cyl_test[1:nrow(Values), ],
name = "SF Zoo",
type = "bar"
)
Correct chart:
My doubt is, as per the tutorial I must get columns in mpg_test and cyl_test, then why I am getting something like dataframe in here.
Does, plotly handles things differently with the usage of ~ ?
Please help. Also, is there any way I can use the mpg_test and cyl_test without nrow(Values) inside plot_ly
script.r
source('./r_files/flatten_HTML.r')
############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
####################################################
################### Actual code ####################
Values <- data.frame(mpt_test, cyl_test)
p <- plot_ly(Values,
x = ~mpt_test[1:nrow(Values), ],
y = ~cyl_test[1:nrow(Values), ],
name = "SF Zoo",
type = "bar"
)
####################################################
############# Create and save widget ###############
#p = ggplotly(p);
internalSaveWidget(p, 'out.html');
####################################################
capabilities.json
{
"dataRoles": [
{
"displayName": "Mileage Per Gallon",
"kind": "GroupingOrMeasure",
"name": "mpg_test"
},
{
"displayName": "Cylinder Size",
"kind": "GroupingOrMeasure",
"name": "cyl_test"
}
],
"dataViewMappings": [
{ "conditions": [
{
"mpg_test": {
"max": 1
},
"cyl_test": {
"max": 1
}
}
],
"scriptResult": {
"dataInput": {
"table": {
"rows": {
"select": [
{
"for": {
"in": "mpg_test"
}
},
{
"for": {
"in": "cyl_test"
}
}
],
"dataReductionAlgorithm": {
"top": {}
}
}
}
},
"script": {
"scriptProviderDefault": "R",
"scriptOutputType": "html",
"source": {
"objectName": "rcv_script",
"propertyName": "source"
},
"provider": {
"objectName": "rcv_script",
"propertyName": "provider"
}
}
}
}
],
"objects": {
"rcv_script": {
"properties": {
"provider": {
"type": {
"text": true
}
},
"source": {
"type": {
"scripting": {
"source": true
}
}
}
}
}
},
"suppressDefaultTitle": true
}
I'm fairly new to handling spatial data and leaflet in general. I'm having difficulty creating a popup for my map. Basically what I want in the popup is the coordinates of my polygon and one of the properties (the type of class). Below is an test example of my geoJSON file:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"class": "blah"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-81.7987060546875,
32.74570253945518
],
[
-81.6229248046875,
32.16631295696736
],
[
-80.958251953125,
32.4263401615464
],
[
-81.2713623046875,
32.791892438123696
],
[
-81.7437744140625,
32.97180377635759
],
[
-81.7987060546875,
32.74570253945518
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blah2"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-82.056884765625,
33.55512901742288
],
[
-81.4471435546875,
33.247875947924385
],
[
-81.40869140625,
33.80653802509606
],
[
-82.078857421875,
33.88865750124075
],
[
-82.40295410156249,
33.58716733904656
],
[
-82.056884765625,
33.55512901742288
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blahh3"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-83.485107421875,
32.930318199070534
],
[
-83.07861328125,
31.863562548378965
],
[
-82.21618652343749,
32.11049589629439
],
[
-82.97973632812499,
33.22030778968541
],
[
-83.726806640625,
33.211116472416855
],
[
-83.485107421875,
32.930318199070534
]
]
]
}
}
]
}
Here is the code I have so far to create my map, but I'm struggling/don't even know where to start on creating a popup that includes my coordinates and property:
blahTest <- geojson_read("/file/path/...", what = "sp")
fpal <- colorFactor("viridis", blahTest$class)
leaflet(blahTest) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
color = ~fpal(class)) %>%
addLegend(colors = ~fpal, opacity = 1.0, labels = ~fpal) %>%
addPopups()
Thanks in advance!
Overview
Using #patL's suggestion, I used the popup parameter within leaflet::addPolygon() function to add label both the class and coordinates that define each polygon within blahTest.
I saved your sample data as .gejson file and imported it using sf::read_sf() and produced the HTML table within the popup using the htmlTable package after reading How to add an html table to leaflet popup.
# load necessary package
library( htmlTable )
library( leaflet )
library( sf )
# load necessary data
blahTest <-
read_sf(
dsn = "test.geojson"
, layer = "OGRGeoJSON"
)
# map data values to colors
fpal <- colorFactor( palette = "viridis", domain = blahTest$class)
# create map
my.map <-
leaflet( data = blahTest ) %>%
addTiles() %>%
addPolygons( stroke = FALSE
, smoothFactor = 0.3
, fillOpacity = 1
, color = ~fpal( class )
, popup = paste0(
"<b>Class: </b>"
, blahTest$class
, "<br>"
, "<b>Coordinates: </b>"
, "<br>"
, lapply(
X = blahTest$geometry
, FUN = function( i )
htmlTable(
x = i[[1]]
, header = c( "Longitude", "Latitude" )
)
)
) ) %>%
addLegend( pal = fpal
, values = ~class
, labels = ~class
, title = "Legend"
, position = "topright"
, opacity = 1.0 )
# view map
my.map
# end of script #
I'm trying to create a leaflet plot with each geographical area a specific color based on a property. Here is an test example of my geojson file:
I'm pretty new to geojson and leaflet in general
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"class": "blah"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-81.7987060546875,
32.74570253945518
],
[
-81.6229248046875,
32.16631295696736
],
[
-80.958251953125,
32.4263401615464
],
[
-81.2713623046875,
32.791892438123696
],
[
-81.7437744140625,
32.97180377635759
],
[
-81.7987060546875,
32.74570253945518
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blah2"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-82.056884765625,
33.55512901742288
],
[
-81.4471435546875,
33.247875947924385
],
[
-81.40869140625,
33.80653802509606
],
[
-82.078857421875,
33.88865750124075
],
[
-82.40295410156249,
33.58716733904656
],
[
-82.056884765625,
33.55512901742288
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blahh3"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-83.485107421875,
32.930318199070534
],
[
-83.07861328125,
31.863562548378965
],
[
-82.21618652343749,
32.11049589629439
],
[
-82.97973632812499,
33.22030778968541
],
[
-83.726806640625,
33.211116472416855
],
[
-83.485107421875,
32.930318199070534
]
]
]
}
}
]
}
What I want to do is add a legend based off the class properties, such that each different class is associated with a specific color. However, when I try and plot and add the legend I get an error on the plot. Below is my R code:
blahTest <- geojson_read("/file/path/...", what = "sp")
fpal <- colorFactor("viridis", blahTest$class)
leaflet(blahTest) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
color = ~fpal(class)) %>%
addLegend(colors = ~fpal, opacity = 1.0, labels = ~fpal)
Just some slight modifications to your addLegend argument gives the required output:
# read geojson in r
library(rgdal)
blahTest = readOGR("...", "OGRGeoJSON")
library(leaflet)
# create palette
fpal <- colorFactor(
palette = "viridis",
domain = blahTest$class
)
# plot
leaflet(blahTest) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
color = ~pal(class)) %>%
addLegend("bottomright",
pal = fpal,
values = ~class,
title = "Legend",
opacity = 1,
labels = ~fpal
)
i am facing quite a problem which is to create the nice graph from http://www.amcharts.com/ but i need to retrieve data from my sql database. But i don't know how to place inside. Please guide me. Below is the way how the graph displayed, but i wanted to work with data from database. Thank you.
<script type="text/javascript">
var chartData = generateChartData();
function generateChartData() {
var chartData = [];
var firstDate = new Date(2012, 0, 1);
firstDate.setDate(firstDate.getDate() - 500);
firstDate.setHours(0, 0, 0, 0);
for (var i = 0; i < 500; i++) {
var newDate = new Date(firstDate);
newDate.setDate(newDate.getDate() + i);
var value = Math.round(Math.random() * (40 + i)) + 100 + i;
chartData.push({
date: newDate,
value: value
});
}
return chartData;
}
AmCharts.makeChart("chartdiv", {
type: "stock",
pathToImages: "../amcharts/images/",
dataSets: [{
color: "#b0de09",
fieldMappings: [{
fromField: "value",
toField: "value"
}],
dataProvider: chartData,
categoryField: "date"
}],
panels: [{
showCategoryAxis: true,
title: "Value",
eraseAll: false,
labels: [{
x: 0,
y: 100,
text: "Click on the pencil icon on top-right to start drawing",
align: "center",
size: 16
}],
stockGraphs: [{
id: "g1",
valueField: "value",
bullet: "round",
bulletColor: "#FFFFFF",
bulletBorderColor: "#00BBCC",
bulletBorderAlpha: 1,
bulletBorderThickness: 2,
bulletSize: 7,
lineThickness: 2,
lineColor: "#00BBCC",
useDataSetColors: false
}],
stockLegend: {
valueTextRegular: " ",
markerType: "none"
},
drawingIconsEnabled: true
}],
chartScrollbarSettings: {
graph: "g1"
},
chartCursorSettings: {
valueBalloonsEnabled: true
},
periodSelector: {
position: "bottom",
periods: [{
period: "DD",
count: 10,
label: "10 days"
}, {
period: "MM",
count: 1,
label: "1 month"
}, {
period: "YYYY",
count: 1,
label: "1 year"
}, {
period: "YTD",
label: "YTD"
}, {
period: "MAX",
label: "MAX"
}]
}
});
</script>
Can you generate this script in your code behind ( using a string builder for example ) then use this
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "'" + YourStringBuild.toString() + "'", true);