Menu

How to create graphs in ART with groovy and mongodb ?

ART Help
2020-11-12
2020-11-12
  • sravan reportool bekkem

    I'm using groovy script to connect to mongodb. In SQL I can see mentioning we're mentioning x and y axis in the graph configuration itself.
    I'm not sure how to create graphs when we're using groovy script with mongo db. Any simple example would help. Thanks in advance.

     
  • Timothy Anyona

    Timothy Anyona - 2020-11-12

    It should be the same as when you're using sql. Mostly, you have to provide certain values in certain columns so you would do the same for the groovy results. So for example for a pie chart, you'll return a list of maps and each map would have 2 columns one with the category and one with the value.

     
  • Timothy Anyona

    Timothy Anyona - 2020-11-12

    Here's an example with an sql source. The important part is constructing the map.

    import groovy.sql.Sql
    import art.connectionpool.DbConnections
    
    def conn = null
    def sql = null
    List<Map<String, Object>> result = new ArrayList<>()
    
    try{
        conn = DbConnections.getConnection("test")
        sql = new Sql(conn)
    
        sql.query('SELECT description, value from testdata') { resultSet ->
            def md = resultSet.getMetaData()
            def columnCount = md.getColumnCount()
            while (resultSet.next()) {
                Map<String, Object> map = new LinkedHashMap<>()
                for(int i=1; i<=columnCount;i++){
                    def columnName = md.getColumnLabel(i)
                    def value = resultSet.getObject(i)
                    map.put(columnName, value)
                }
                result.add(map)
            }
        }
    } finally {
        if (conn!=null){
            try{
                conn.close();
            }catch(IOException e){
    
            }  
        }
        if (sql!=null){
            try{
                sql.close();
            }catch(IOException e){
    
            }  
        }
    }
    
    return result
    
     

Log in to post a comment.