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

Create pie chart #6

Open
wants to merge 27 commits into
base: master
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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export(eChartOutput)
export(eXAxis)
export(eYAxis)
export(echart)
export(ecandlestick)
export(egauge)
export(renderEChart)
importFrom(htmlwidgets,JS)
importFrom(magrittr,"%>%")
22 changes: 22 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ data_bar = function(x, y, series = NULL, type = 'bar') {

}

data_pie = function(x, y, series = NULL, type = 'pie') {
if (is.null(series)) {
xy=data.frame(name=x,value=y)
}
if (!is.null(series)) {
xy=data.frame(name=series,value=y)
}
xy$name=as.character(xy$name)
data1=list()
for (i in 1:dim(xy)[1]){
data1[[i]]=list(name=xy[i,1],value=xy[i,2])
}
return(list(list(type = type,
showScale = TRUE,
showScaleText = TRUE,
data = data1
)
)
)

}

data_line = function(x, y, series = NULL) {
if (is.numeric(x)) {
return(data_scatter(x, y, series, type = 'line'))
Expand Down
74 changes: 74 additions & 0 deletions R/ecandlestick.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

ecandlestick=function(data,open=NULL,close=NULL,high=NULL,low=NULL,date=NULL,title=""){

if (class(data)[1]=='xts' & class(data)[2]=='zoo'){
data1=data[,c(1,4,3,2)]
date1= rownames(as.data.frame(data))
} else{
open = evalFormula(open, data)
close = evalFormula(close, data)
high= evalFormula(high, data)
low= evalFormula(low, data)
data1=cbind(open,close,high,low)
date1= evalFormula(date, data)
}

k1 = list(
title = list(
text = title,
x = 'center',
y = 'top'
),
tooltip = list(
trigger = 'axis',
formatter = JS("function (params) {
var res = params[0].seriesName + ' ' + params[0].name;
res += '<br/>open : ' + params[0].value[0] + '<br/>high : ' + params[0].value[3];
res += '<br/>close : ' + params[0].value[1] + '<br/>low : ' + params[0].value[2];
return res;
}")
),
toolbox = list(
show = TRUE,
feature = list(
restore = list(show = TRUE),
dataZoom = list(show=TRUE),
saveAsImage = list(show = TRUE)
)
),

dataZoom =list(
show =TRUE,
realtime=TRUE,
start = 50,
end = 100
),

xAxis=list(
type = 'category',
boundaryGap = TRUE,
axisTick=list( onGap=FALSE),
splitLine=list(show=FALSE),
data = date1
),
yAxis =list(
type='value',
scale=TRUE,
boundaryGap=c(0.01, 0.01)
),
series = list(
list(
type = 'k',
data = data1
)
)
)
echart(k1)
}







27 changes: 25 additions & 2 deletions R/echart.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,39 @@ echart.data.frame = function(
x = x, y = y
), TOJSON_ARGS = list(pretty = TRUE))


params =params %>%
etoolbox(type,mark=TRUE,dataView=TRUE,restore=TRUE,
saveAsImage=TRUE,magicType=TRUE) %>%
eConfig(tip=TRUE,calculable=TRUE)
if (type=='pie' & is.null(series)){
series=x
}

if (!is.null(series)) {
params$legend = list(data = levels(as.factor(series)))
params$legend = list(data = levels(as.factor(series)),
x='left')

}



chart = htmlwidgets::createWidget(
'echarts', params, width = width, height = height, package = 'recharts',
dependencies = getDependency(NULL)
)
if (type=='pie'){
chart$x$xAxis=NULL
chart$x$yAxis=NULL
return(chart)
}

chart %>%
eAxis('x', name = xlab) %>%
eAxis('y', name = ylab)



chart %>% eAxis('x', name = xlab) %>% eAxis('y', name = ylab)
}

#' @export
Expand Down
68 changes: 68 additions & 0 deletions R/egauge.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
egauge=function(value,mini=0,maxi=100,radius=80, axis_width=20,
startAngle=225,endAngle=-45,
center=c(50,50),name='Gauge',splitNumber=5){
center=paste0(center,'%')
radius=paste0(radius,'%')
gaugex = list(
tooltip =list(
formatter= "{a} <br/>{b} : {c}"
),
toolbox= list(
show = TRUE,
feature = list(
mark = list(show= TRUE),
restore = list(show= TRUE),
saveAsImage= list(show= TRUE)
)
),
series = list(
list(
name='YA',
type='gauge',
center = center,
radius = radius,
min=mini,
max=maxi,
startAngle=startAngle,
endAngle=endAngle,
splitNumber=splitNumber,
axisLine=list(
lineStyle=list(
width= axis_width
)
),
axisTick=list(
length=axis_width+6,
lineStyle=list(
color='auto'
)
),
# axisLabel=list(
# formatter=JS("function(v){
# switch (v + '') {
# case '0' : return 'H';
# case '25' : return 'Water';
# case '50' : return 'C';
# }
# }"
# )
# ),
splitLine=list(
length=axis_width+10,
lineStyle=list(
color='auto'
)
),
pointer=list(
width=10
),
# title=list(
# offsetCenter=c('0%','0%')
# ),
data=list(list(value= value, name= name))
)
)
)

echart(gaugex)
}
41 changes: 41 additions & 0 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,44 @@ axisType = function(data, which = c('x', 'y')) {
str(data)
stop('Unable to derive the axis type automatically from the ', which, ' variable')
}

# configure of echart
eConfig=function(params,tip=TRUE,calculable=TRUE){
if (tip){
params$tooltip = list(
trigger = 'item'
)
}


params$calculable=calculable

return(params)

}
# toolbox of echart
etoolbox=function(params,type,mark=TRUE,dataZoom=TRUE,dataView=TRUE,restore=TRUE,
saveAsImage=TRUE,magicType=TRUE){
params$toolbox = list(
show = TRUE,
feature = list(
mark = list(show=mark),
dataZoom = list(show=dataZoom),
dataView = list(show=dataView,readOnly=FALSE),
restore = list(show = restore),
saveAsImage = list(show = saveAsImage)
)
)
if (type=='pie' | type=='funnel'){
params$toolbox$feature$magicType = list(show = magicType, type = c('pie', 'funnel'))
}
if (type=='bar' | type=='line'){
params$toolbox$feature$magicType = list(show = magicType, type = c('bar', 'line', 'stack', 'tiled'))
}
if (type=='force' | type=='chord'){
params$toolbox$feature$magicType = list(show = magicType, type = c('force', 'chord'))
}
return(params)

}

12 changes: 12 additions & 0 deletions example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# library(recharts)
# pie chart
x=data.frame(name=c('a','b','c'),value=1:3)
echart(x,~name,~value,type='pie')

# candlestick chart
require(quantmod)
getSymbols("^TWII")
ecandlestick(TWII)
test=as.data.frame(TWII)
test$date=rownames(test)
ecandlestick(test,~TWII.Open,~TWII.Close,~TWII.Low,~TWII.High,~date)
2 changes: 1 addition & 1 deletion inst/htmlwidgets/echarts.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dependencies:
- name: echarts
version: 2.2.1
version: 2.2.3
src: htmlwidgets/lib/echarts
script: echarts-all.js
56 changes: 29 additions & 27 deletions inst/htmlwidgets/lib/echarts/echarts-all.js

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions pie.html

Large diffs are not rendered by default.