datastorm-open / rAmCharts

API for Amcharts
48 stars 16 forks source link

plotting Trendlines in rAmCharts #73

Closed rainervi closed 6 years ago

rainervi commented 6 years ago

Hello,

I have a problem plotting a chart and trendlines together in a shiny app using rAmCharts.

First I tried with amOHLC where the chart is plotted, but not the trendline.

After more and more research I am not sure, if I can add a trendline to an amOHLC-chart - as expected. So I started all over with an amSerialChart, but still the trendlines are not shown on the chart.

Unfortunately I could not find an example, showing how to add trendlines in rAmCharts. Very thankful for your help Best regards Rainer

Code of attempt using amOHLC library(rAmCharts) library(quantmod) library(pipeR)

function to convert a xts into the data.frame format for amOHLC

dfForAmChart <- function(x){ df <- data.frame(index(x), x[,c(1, 4,3, 2)]) dim(df) colnames(df) <- c("category", "open", "close", "low", "high") rownames(df) <- 1:nrow(df) dim(df) df }

collect stock data for the plot

data <- getSymbols("SPY", src = "google", from = "2017-01-01", auto.assign = FALSE) df <- dfForAmChart(data) print(head(df))

plots the chart as expected

amOHLC(df)

initializing a trendline

tl <- trendLine(initialValue = df[11,2], initialDate = df[11,1], finalValue = df[95,2], finalDate = df[95,1])

shows correct data within the values shown on the chart

print(tl)

attempt to plot chart and trendlines, but trendlines are not shown

pipeR::pipeline( amOHLC(df), addTrendLine(tl), addTrendLine(dashLength = 3, initialValue = tail(df,100)[21,2], initialDate = tail(df,100)[21,1], finalValue = tail(df,100)[85,2], finalDate = tail(df,100)[85,1], lineColor = '#888888') )

#

Code of attempt using amSerialChart

data('data_candleStick1') data_candleStick1[, -1] <- apply(data_candleStick1[, -1], 2, round, 2) dim(data_candleStick1) tl <- trendLine(initialValue = data_candleStick1[3,2], initialDate = data_candleStick1[3,1], finalValue = data_candleStick1[9,2], finalDate = data_candleStick1[9,1]) print(tl)

Plot

pipeR::pipeline( amSerialChart(dataProvider = data_candleStick1, categoryField = 'category', dataDateFormat = 'YYYY-MM-DD'), addTitle(text = 'Candle'), setChartCursor(), setCategoryAxis(labelRotation = 45), addGraph(id = 'g1', openField = 'open', closeField = 'close', highField = 'high', lowField = 'low', valueField = 'close', fillColors = '#F0FC98', lineColor = '#CD98FC', negativeFillColors = '#98FC98', negativeLineColor = '#005321', type = 'candlestick', fillAlphas = 0.8, balloonText = 'Open:[[open]]
Low:[[low]]
High:[[high]]
Close:[[close]]
'), addTrendLine(tl) )

TitouanRobert commented 6 years ago

Hello,

You can use initialCategory and finalCategory in trendLine

data('data_candleStick1')
data_candleStick1[, -1] <- apply(data_candleStick1[, -1], 2, round, 2)
tl <- trendLine(initialValue = data_candleStick1[3,2], initialCategory = data_candleStick1[3,1],
                finalValue = data_candleStick1[9,2], finalCategory = data_candleStick1[9,1])
amOHLC(data_candleStick1)%>>%addTrendLine(tl)

It's possible to use amCharts API to see arguments of trendLine : https://docs.amcharts.com/3/javascriptcharts/TrendLine

Best regards

rainervi commented 6 years ago

Hello TitouanRobert,

thank you very much for your fast help. Unfortunately I have not found the solution myself, although I have been to api("TrendLine") many times.

Best regards Rainer