# demo custom renderer.  This takes a ds[n,4] and plots dots
# with each record: [x,y,rad,color]
# Note, the plan is to redo how custom renderers are done, so this may change.

from org.das2.qds import SemanticOps
from org.das2.graph import Renderer

import java 
from java.lang import IllegalArgumentException

cb= dom.plots[0].zaxis.controller.getDasAxis()
dom.plots[0].zaxis.visible= True

class MyPCL( java.beans.PropertyChangeListener ):
   def propertyChange(self,e):
      dom.plots[0].controller.dasPlot.invalidateCacheImage()

cb.addPropertyChangeListener( MyPCL() )  #TODO: there's a jython way to do this

class MyRend( Renderer ):
  def render( self, g, xAxis, yAxis, mon=None ):
     ds= self.getDataSet()
     if ds==None: return
     if ( ds.rank()!=2 or ds.length(0)!=4 ):
        self.getParent().postException( self, IllegalArgumentException( "dataset is not ds[n,4]" ) )
        return
     xds= ds[:,0]
     yds= ds[:,1]
     rds= ds[:,2]/2
     cds= ds[:,3]

     xu= SemanticOps.getUnits( xds )
     yu= SemanticOps.getUnits( yds )
     cu= SemanticOps.getUnits( cds )
     ru= SemanticOps.getUnits( rds )

     for i in xrange( yds.length() ):
        x= xAxis.transform( xds.value(i), xu )
        y= yAxis.transform( yds.value(i), yu )        
        g.setColor( Color( cb.rgbTransform( cds.value(i), cu ) ) )
        x2= xAxis.transform( xds.value(i)+rds.value(i), xu ) - x
        g.fillOval( int(x-x2/2),int(y-x2/2),int(x2/2),int(x2/2) )

ds= randn( 1000,4 )

plot( ds, renderer=MyRend(), xrange='-5 to 5', yrange='-5 to 5', zrange='0 to 3' )
