Search code examples
javadata-visualizationdynamic-data

Dynamic data transform in data visualization


I have a question about data visualization in java. I work for marketing team and I need to build report for them.

My model:

class AdReportEntity {
    String date;
    String clientName;
    AdInfoEntity adInfo;
    int cost;
    int impression;
    int click;
    ...

}

AdInfoEntity:

class AdInfoEntity{

String campaignName;
String adgroupName;
String adname;
...

}

Client side have 2 main component:

  1. bar chart show daily cost (sum of all client). enter image description here
  2. the table show the detail:
    date | clientName | campaignName |adgroupName| adName| cost| impression| click

The problem:
The requirement is more dynamic chart and table. Some time user want to display the chart by Client, or by AdName, or by campaignName, or maybe just group by first 3 char of campaignName, etc.
How to implement the server for more dynamic?

My solution is to SUM base on the param user request,Ex:

 String groupColumn=request.getParameter("column");
    switch(groupColumn){
    case "clientName":
        doSumByClientName();
    case "adName":
        doSumByAdName();
    case "spec1":
        doSumBySubStringAdName():
        ....
    }

I have the same problem for sorting and filtering.
I'm also tried column base for my entity but it's hard to deal with depend properties like ctr=click/impression.


Solution

  • You might take a look at Tablesaw, (https://github.com/lwhite1/tablesaw), Not sure it does everything you want but it certainly does sorting and filtering in Java. Here's some code.

    Table tornados = Table.createFromCsv("data/tornadoes_1950-2014.csv");
    tornadoes.setName("tornadoes");
    
    tornados.structure().print();
    output:
        Structure of tornadoes_1950-2014.csv
        Index Column Name Column Type 
        0     Date        LOCAL_DATE  
        1     Time        LOCAL_TIME  
        2     State       CATEGORY    
        3     Scale       SHORT_INT   
        4     Injuries    SHORT_INT   
        5     Fatalities  SHORT_INT   
        6     Start Lat   FLOAT       
        7     Start Lon   FLOAT       
        8     Length      FLOAT       
        9     Width       FLOAT    
    
    tornados.shape()
    output:
        59945 rows X 10 cols
    
    // gets the month from every date, and counts by month
    CategoryColumn month = tornadoes.dateColumn("Date").month();
    month.summary();
    output:
    
        Column: Date month
        Category  Count 
        JANUARY   1417  
        MARCH     3935  
        JULY      6305  
        MAY       13072 
        SEPTEMBER 3142  
        OCTOBER   2332  
        DECEMBER  1355  
        NOVEMBER  2364  
        FEBRUARY  1672  
        APRIL     8533  
        AUGUST    4066  
        JUNE      11633 
    
    
    Table injuriesByScale = tornados.median("Injuries").by("Scale");
    output:
        Median injuries by Tornado Scale
        Scale Median [Injuries] 
        -9    0.0               
        0     0.0               
        1     0.0               
        2     0.0               
        3     2.0               
        4     14.0              
        5     141.0             
    
    // query:
    Table fatal =  
         tornados.selectWhere(column("Fatalities").isGreaterThan(0));
    // sort
    Table sorted = tornados.sortOn("State");
    

    It does lots of other stuff, too, including some stats, simple plots, etc.

    Full disclosure, I'm the main author of Tablesaw, and it's still a bit rough around the edges.