Question

I have a smartGwt ListGrid which i use for showing stock market data. I want to be able to highlight the value of a cell. For example - if its current value is greater than the last value, turn green and turn red if it is lower. I looked at the showcase for smartGWT for any such capability but i only found this sample code for highlighting.

        new Hilite() {{  
            setFieldNames("area", "gdp");  
            setTextColor("#FFFFFF");  
            setBackgroundColor("#639966");  
            setCriteria(new AdvancedCriteria(OperatorId.AND, new Criterion[] {  
                            new Criterion("gdp", OperatorId.GREATER_THAN, 1000000),  
                            new Criterion("area", OperatorId.LESS_THAN, 500000)}));  
            setCssText("color:#3333FF;background-color:#CDEB8B;");  
            setHtmlAfter(" " + Canvas.imgHTML("[SKIN]/actions/back.png"));  
            setId("1");  
        }} 

Here the "gdp" or "area" fields are highlighted if their values are greater or less than a fixed number. Is it possible to use similar highlighting but the value should be compared to the previous value in the cell?

Thanks and regards Mukul

Was it helpful?

Solution

Previous values are not stored anywhere in the model. So the comparison cannot be made out of the box.

A possible solution to this is to create duplicate hidden list grid fields like areaPrevious or gdpPrevious. When the data changes you populate gdp/area and gdpPrevious/areaPrevious fields. Instead of using hilites you use cellFormatters.

gdpField.setCellFormatter(new CellFormatter(){
    public String format(Object value, ListGridRecord record, int rowNum, int colNum){
        if( record.getAttribute("gdpPrevious") < record.getAttribute("gdp")){
             return "<div style=\"width:14px;height:14px;background-color:green;\">+value+        "</div>";
        }else{
              return "<div style=\"width:14px;height:14px;background-color:red;\">"+value+        "</div>";
        }
    }
});

OTHER TIPS

In addition to Abhijith's method, Found an alternative method for doing this in the SmartGWT Enterprise showcase - Pasting here for future reference. They accomplished this by overriding the getCellCSSText method -

            stockQuotesGrid = new ListGrid() {  
            private int blinkPeriod = 2000;  

            // Cell animation - go bright green or red on a change, then fades.  
            protected String getCellCSSText(ListGridRecord record, final int rowNum,  
                    final int colNum) {  
                // changeValue column  
                Date lastUpdated = record.getAttributeAsDate("lastUpdated");  
                if (colNum == 2 && lastUpdated != null) {  
                    long delta = System.currentTimeMillis() - lastUpdated.getTime();  
                    if (delta < blinkPeriod) {  
                        // refresh 10x / second  
                        new Timer() {  
                            public void run() {  
                                stockQuotesGrid.refreshCell(rowNum, colNum);  
                            }  
                        }.schedule(100);  
                        float changeValue = record.getAttributeAsFloat("changeValue");  

                        float ratio = ((float) (blinkPeriod - delta)) / blinkPeriod;  
                        int color = 255 - Math.round(200 * ratio);  

                        if (changeValue > 0) {  
                            return "background-color:#" + Integer.toHexString(color) + "FF"  
                                    + Integer.toHexString(color);  
                        } else if (changeValue < 0) {  
                            return "background-color:#FF" + Integer.toHexString(color)  
                                    + Integer.toHexString(color);  
                        }  
                    } else {  
                        record.setAttribute("lastUpdated", (Date) null);  
                    }  
                }  
                // no style override  
                return null;  
            };  
        };  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top