简体   繁体   中英

Plotting graph using R and Java

I used following code to calculating prediction in R using Java that code work fine it shows me output but, I want draw plot using same code on my predicted output code was given below

import org.rosuda.REngine.Rserve.RConnection;
public class demo{
public static void main(String args[]){
    try{
         System.out.println("INFO: Trying to connect to R ");
         RConnection c = new RConnection();
         c.eval("library('RMongo')");
         c.eval(" db <- mongoDbConnect('dbname', '127.0.0.1', '27017')");
         c.eval("query <- dbGetQuery(db,'collection_name','{\"hostId\" : \"300.3.3.3\"}')");
         c.eval("date <- query$Date");
         c.eval("cpu <- query$cpuUtilization");
         c.eval("memory <- query$memory");
         c.eval("df <- data.frame(date=1377843220)");
         c.eval("res <- lm(cbind(memory,cpu)~ date -1 )");
         int[] d= c.eval("predict(res,df)").asIntegers();
         for (Integer td : d) {
             System.out.println(td);
            }
         c.close();
        }
    catch(Exception e){
     System.out.println("ERROR: In Connection to R ");
     System.out.println("The Exception is "+ e.getMessage());
     e.printStackTrace();
     }
}
}

So any one knows how to draw plot using R in java code ?

Use a JFrame with a JGDPanel from JavaGD and plot something via JRI. Look into JGD: http://cran.r-project.org/web/packages/JavaGD/JavaGD.pdf Look into JRI: http://rosuda.org/JRI/

Another alternative is using R to generate the data you want and using native Java graphics libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM