Sunday, September 23, 2012

dvt:pieGraph custom colors using static or programatic series

To set the colors of slices in a pieGraph you can use the below options.

1. Static settings
       If the groups for slices are are constant and are available in specific order you can used the below config.


          <dvt:seriesSet>
            <dvt:series index="0" color="#006ba5"/>
            <dvt:series index="1" color="#026ba5"/>
            <dvt:series index="2" color="#036ba5"/>
            <dvt:series index="3" color="#046ba5"/>
            <dvt:series index="4" color="#056ba5"/>
          </dvt:seriesSet>


2. Dynamic settings
       If the groups for slices are optional. i.e the query does not return specific rows in cases and to assign the specific colors for specific groups you can use this option.

  • Create a public method in bean that returns a map. Here you can write logic to determine the colors.
  • Map it seriesMap attribute of dvt:series.

        <dvt:pieGraph id="pieGraph1" value="#{bindings.DeptEmpCount.graphModel}"
                      subType="PIE">
          <dvt:background>
            <dvt:specialEffects/>
          </dvt:background>
          <dvt:graphPieFrame/>
          <dvt:seriesSet seriesMap="#{homeBean.pieSeries}"/>
          <dvt:sliceLabel/>
          <dvt:pieLabel rendered="true"/>
          <dvt:legendArea automaticPlacement="AP_NEVER"/>
        </dvt:pieGraph>


    private Map<Integer,Color> colorMap;    
    public HomeBean() {
        super();
        colorMap = new HashMap<Integer,Color>();
        colorMap.put(10, new Color(111111));
        colorMap.put(20, new Color(222222));
        colorMap.put(30, new Color(333333));
        colorMap.put(40, new Color(444444));
        colorMap.put(50, new Color(555555));
        colorMap.put(60, new Color(666666));
        colorMap.put(70, new Color(777777));
        colorMap.put(80, new Color(888888));
        colorMap.put(90, new Color(999999));
        colorMap.put(100, new Color(900000));
        colorMap.put(110, new Color(800000));
    }
    
    public Map getPieSeries(){
        Map seriesMap = new HashMap();
        BindingContext context = BindingContext.getCurrent();
        DCBindingContainer bindings =  (DCBindingContainer)context.getCurrentBindingsEntry();
        DCIteratorBinding iteratorBinding  = (DCIteratorBinding)bindings.getIterBindings().get("DeptEmpCountIterator");
        RowSetIterator iter = iteratorBinding.getViewObject().createRowSetIterator(null);
        int index =0;
        while(iter.hasNext()){
            Integer departmentId = (Integer)iter.next().getAttribute("DepartmentId");
            Series s = new Series();
            s.setColor(colorMap.get(departmentId));
            seriesMap.put(index, s);
            index++;
        }
        return seriesMap;
    }

You can download the complete example here.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...