RE: [Gvsig_desarrolladores] Problema añadiendo una leyenda de valores unicos por codigo.

Oscar Garcia oscarg en dielmo.com
Jue Nov 6 20:35:03 CET 2008


Prueba de esta otra forma.

 

Un saludo, Oscar

 

/**

       * Aplica la leyenda por intervalos de anyos.

       */

private void legendaAnyo() {

int k, j;

            String filterName="ANY";

            SelectableDataSource dsCaminos;

            int value;

            int maxValue=Integer.MIN_VALUE, minValue=Integer.MAX_VALUE;

            

            int campo;

            try {

                  dsCaminos = lyrShapeCaminos.getRecordset();

                  campo = dsCaminos.getFieldIndexByName(filterName);

            

                  for (j=0 ; j<dsCaminos.getRowCount(); j++) {

                        

                        value=Integer.parseInt(dsCaminos.getFieldValue(j,
campo).toString());

      

                        if (minValue > value)

                             minValue = value;

                        

                        if (maxValue < value)

                             maxValue = value; 

                  }

            

                  FSymbol myDefaultSymbol;

                  // Creamos el primer y último color.

              Color startColor = new Color(20,255,20); 

              Color endColor = new Color(40,150,40); 

                  VectorialIntervalLegend legend = null;

                  legend =
LegendFactory.createVectorialIntervalLegend(FShape.LINE);

                  FInterval[] arrayIntervalos = null;

                  arrayIntervalos = calculateEqualIntervals (10, minValue,
maxValue, filterName, lyrShapeCaminos);                  

              FInterval elIntervalo;

      

              int r;

              int g;

              int b;

              int stepR;

              int stepG;

              int stepB;

              r = startColor.getRed();

              g = startColor.getGreen();

              b = startColor.getBlue();

              stepR = (endColor.getRed() - r) / arrayIntervalos.length;

              stepG = (endColor.getGreen() - g) / arrayIntervalos.length;

              stepB = (endColor.getBlue() - b) / arrayIntervalos.length;

              

              legend.clear();

              legend.useDefaultSymbol(true);  

              for (k = 0; k < arrayIntervalos.length; k++) {

                  elIntervalo = arrayIntervalos[k];

                  

                  Color c = new Color(r, g, b);

                  //si no esta creado el simbolo se crea

                  myDefaultSymbol = new FSymbol(FShape.LINE, c);

                  myDefaultSymbol.setDescription(elIntervalo.getMin() +

                            " - " +

                            elIntervalo.getMax());

                  myDefaultSymbol.setSize(3);

                  myDefaultSymbol.setSizeInPixels(true);

                  

                  //////////////////////////////////////

                  // CALCULAMOS UN COLOR APROPIADO

                  r = r + stepR;

                  g = g + stepG;

                  b = b + stepB;

                  

                  /////////////////////////////////

                  legend.addSymbol(elIntervalo, myDefaultSymbol);

              }

              

              lyrShapeCaminos.setLegend(legend);

              

            } catch (DriverException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

            } catch (com.hardcode.gdbms.engine.data.driver.DriverException
e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

            } catch (FieldNotFoundException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

            }

      }

    

      /**

     * EQUAL INTERVAL Devuelve un Array con el número de intervalos que se

     * quieren crear. Los intervalos se crean con un tamaño igual entre
ellos.

     *

     * @param numIntervals número de intervalos

     * @param minValue Valor mínimo.

     * @param maxValue Valor máximo.

     * @param fieldName Nombre del campo

     *

     * @return Array con los intervalos.

     */

    FInterval[] calculateEqualIntervals(int numIntervals, double minValue,

        double maxValue, String fieldName) {

        FInterval[] theIntervalArray = new FInterval[numIntervals];

        double step = (maxValue - minValue) / numIntervals;

 

        if (numIntervals > 1) {

            theIntervalArray[0] = new FInterval(minValue, minValue + step);

 

            for (int i = 1; i < (numIntervals - 1); i++) {

                theIntervalArray[i] = new FInterval(minValue + (i * step) +

                        0.01, minValue + ((i + 1) * step));

            }

 

            theIntervalArray[numIntervals - 1] = new FInterval(minValue +

                    ((numIntervals - 1) * step) + 0.01, maxValue);

        } else {

            theIntervalArray[0] = new FInterval(minValue, maxValue);

        }

 

        return theIntervalArray;

    }

 

  _____  

De: gvsig_desarrolladores-bounces en runas.cap.gva.es
[mailto:gvsig_desarrolladores-bounces en runas.cap.gva.es] En nombre de jose
Diez Pastor
Enviado el: jueves, 06 de noviembre de 2008 20:26
Para: gvsig_desarrolladores en runas.cap.gva.es
Asunto: [Gvsig_desarrolladores] Problema añadiendo una leyenda de valores
unicos por codigo.

 

Hola a todos, mi intencion es que teniendo una capa vectorial, cambiar el
color de algunos elementos, tendiendo en cuenta el valor de su campo ID, el
problema es que no consigo hacerlo correctamente y la capa desaparece.

Me he fijado un poco como lo hace gvSIG, debugeando desde eclipse pero
seguramente se me este olvidando algo, o lo este haciendo mal.

Estoy haciendo esto, el Color c es el nuevo color que le quiero poner al
elemento y String i es el valor del campo.


private void setColorItem(FLayer layer, Color c, String i) {   
        ClassifiableVectorial aux = (ClassifiableVectorial) layer;
        VectorialUniqueValueLegend uvLegend=null;
        Legend legend = null;
       
        try {
            uvLegend = LegendFactory.
createVectorialUniqueValueLegend(aux.getShapeType());
        } catch (DriverException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
       
       
      
        FSymbol symbol = new FSymbol(4);    // el simbolo que tendra el
elemento que quiero cambiar
        FSymbol defaultSymbol = (FSymbol)
aux.getLegend().getDefaultSymbol();   //default, el que ya tenia la capa.
        float line=1; //ancho borde
        symbol.setShapeVisible(true);
        symbol.setOutlined(true);  
        symbol.setOutlineColor(c);
        symbol.setColor(c);
        symbol.setStroke(new BasicStroke(line,
BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
       
        uvLegend.setFieldName("ID");   //quiero discriminar utilizando el
campo de nombre ID
        uvLegend.addSymbol(i, symbol);
       
        uvLegend.setDefaultSymbol(defaultSymbol);
        uvLegend.useDefaultSymbol(true);
       
        legend = (Legend)uvLegend;
        VectorialLegend l =(VectorialLegend)legend;
        l.setLabelField(null);
       
       
        try {
            aux.setLegend((VectorialLegend)legend);         //aux era la
capa sobre la que se aplica la leyenda.
        } catch (FieldNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DriverException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        FLyrVect lyrVect = (FLyrVect) layer;  
        lyrVect.createSpatialIndex();
        layer.getMapContext().callLegendChanged();
       
    }



  _____  

Descubre durante 3 meses gratis la protección total de One Care
<http://www.vivelive.com/onecare/> 

------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: http://runas.cap.gva.es/pipermail/gvsig_desarrolladores/attachments/20081106/98e825a5/attachment.htm


Más información sobre la lista de distribución gvSIG_desarrolladores