Package org.krysalis.jcharts.properties

Examples of org.krysalis.jcharts.properties.PieChart2DProperties


   *
   **********************************************************************************************/
  public void init()
  {
    //---all charts of this type of pie chart are going to share the same properties.
    this.properties = new PieChart2DProperties();

    this.legendProperties = new LegendProperties();
    this.legendProperties.setNumColumns( 2 );
    this.legendProperties.setPlacement( LegendProperties.RIGHT );

View Full Code Here


   * Draws the chart
   *
   ************************************************************************************************/
  protected void renderChart()
  {
    PieChart2DProperties properties = (PieChart2DProperties) this.iPieChartDataSet.getChartTypeProperties();
    FontRenderContext fontRenderContext = super.getGraphics2D().getFontRenderContext();

    this.pieChartDataProcessor = new PieChartDataProcessor( this.iPieChartDataSet );
    this.pieChartDataProcessor.processData();


    //---cache calcs used more than once
    float edgePaddingTimesTwo = super.getChartProperties().getEdgePadding() * 2;
    float halfImageWidth = super.getImageWidth() / 2;
    float halfImageHeight = super.getImageHeight() / 2;


    //---render the TITLE. If no title, this will return zero.
    float chartTitleHeightPlusPadding = super.renderChartTitle( this.iPieChartDataSet.getChartTitle(), fontRenderContext );


    //---figure out what size is needed to hold all the components of the pie chart, then size the pie accordingly
    float widthAvailable = super.getImageWidth() - edgePaddingTimesTwo;
    float heightAvailable = super.getImageHeight() - edgePaddingTimesTwo;
    heightAvailable -= chartTitleHeightPlusPadding;


    //---take labels sizing into consideration if needed.
    if( !properties.getPieLabelType().equals( PieLabelType.NO_LABELS ) )
    {
      this.pieLabels = new PieLabels( properties, this.iPieChartDataSet, fontRenderContext );

      //---if there is only one item in pie, label will be below plot so width is not a concern
      if( iPieChartDataSet.getNumberOfDataItems() != 1 )
      {
        widthAvailable -= this.pieLabels.getWidestLabelTimesTwo();
        widthAvailable -= ( properties.getTickLength() * 2 );
      }

      heightAvailable -= this.pieLabels.getTallestLabelTimesTwo();
      heightAvailable -= ( properties.getTickLength() * 2 );
    }


    //---if there is a legend...
    if( this.getLegend() != null )
    {
      float legendX = 0f;
      float legendY = 0f;

      //---calculate all the legend rendering coordinates and positions.
      this.getLegend().calculateDrawingValues( iPieChartDataSet );


      //---adjust width and height based on the Legend size.
      if( ( this.getLegend().getLegendProperties().getPlacement() == LegendAreaProperties.RIGHT )
        || ( this.getLegend().getLegendProperties().getPlacement() == LegendAreaProperties.LEFT ) )
      {
        widthAvailable -= this.getLegend().getWidth();
        widthAvailable -= this.getLegend().getLegendProperties().getChartPadding();


        //---diameter of pie will be at least one pixel, even if the legend takes up the whole image.
        //---this will keep the renderer from blowing up.
        this.diameter = Math.max( widthAvailable, 1.0f );

        //---make sure we do not make the pie diameter taller than the image
        this.diameter = Math.min( this.diameter, heightAvailable );


        //---calculate the entire width of everything to be drawn so can center everything
        float plotWidth = this.diameter;
        plotWidth += this.getLegend().getWidth();
        plotWidth += this.getLegend().getLegendProperties().getChartPadding();
        if( this.pieLabels != null )
        {
          plotWidth += ( this.pieLabels.getWidestLabel() * 2 );
          plotWidth += ( properties.getTickLength() * 2 );
        }

        if( this.getLegend().getLegendProperties().getPlacement() == LegendAreaProperties.RIGHT )
        {
          //---pie's diameter may not fill image width as may be image height constrained.
          this.pieX = halfImageWidth - ( plotWidth / 2 );

          if( this.pieLabels != null )
          {
            this.pieX += this.pieLabels.getWidestLabel();
            this.pieX += properties.getTickLength();
            legendX += this.pieLabels.getWidestLabel();
            legendX += properties.getTickLength();
          }

          //---position legend based on the pie position
          legendX += this.pieX + this.diameter;
          legendX += this.getLegend().getLegendProperties().getChartPadding();
        }
        else
        {
          legendX = halfImageWidth - ( plotWidth / 2 );

          if( this.pieLabels != null )
          {
            this.pieX = legendX;
            this.pieX += this.getLegend().getWidth();
            this.pieX += this.getLegend().getLegendProperties().getChartPadding();
            this.pieX += this.pieLabels.getWidestLabel();
            this.pieX += properties.getTickLength();
          }
        }

        //---center the legend vertically
        legendY = halfImageHeight - ( this.getLegend().getHeight() / 2 );

        //---center the pie vertically
        this.pieY = halfImageHeight - ( this.diameter / 2 );
      }
      //---else the legend is either under or on top of the pie
      else
      {
        heightAvailable-= this.getLegend().getHeight();
        heightAvailable-= this.getLegend().getLegendProperties().getChartPadding();

        //---diameter of pie will be at least one pixel, even if the legend takes up the whole image.
        //---this will keep the renderer from blowing up.
        this.diameter = Math.max( heightAvailable, 1.0f );

        //---make sure we do not make the pie diameter wider than the image
        this.diameter = Math.min( this.diameter, widthAvailable );


        if( this.getLegend().getLegendProperties().getPlacement() == LegendAreaProperties.BOTTOM )
        {
          this.pieY = super.getChartProperties().getEdgePadding();
          this.pieY += chartTitleHeightPlusPadding;

          legendY+= this.diameter;

          if( this.pieLabels != null )
          {
            //---adds label height from top of pie
            this.pieY += this.pieLabels.getTallestLabel();
            this.pieY += properties.getTickLength();

            //---add label hight from bottom of pie
            legendY += this.pieLabels.getTallestLabel();
            legendY += properties.getTickLength();
          }

          legendY += this.pieY;
          legendY+= this.getLegend().getLegendProperties().getChartPadding();
        }
        else
        {
          legendY = super.getChartProperties().getEdgePadding();
          legendY += chartTitleHeightPlusPadding;

          this.pieY= legendY;
          this.pieY+= this.getLegend().getHeight();
          this.pieY+= this.getLegend().getLegendProperties().getChartPadding();

          if( this.pieLabels != null )
          {
            //---adds label height from top of pie
            this.pieY += this.pieLabels.getTallestLabel();
            this.pieY += properties.getTickLength();
          }
        }

        //---center the legend horizontally
        legendX = halfImageWidth - ( this.getLegend().getWidth() / 2 );
View Full Code Here

   * @param pieChart2D
   ************************************************************************************************/
  static void render( PieChart2D pieChart2D )
  {
    Graphics2D g2d = pieChart2D.getGraphics2D();
    PieChart2DProperties properties = (PieChart2DProperties) pieChart2D.iPieChartDataSet.getChartTypeProperties();

    //---set the border Stroke
    properties.getBorderChartStroke().setupGraphics2D( g2d );


    //---the following only for Image Map-----------------------------
    //---IMAGE MAP
    //---number of subdivisions to break each slice into to 'fill' slice
    int subdivisions = 3;
    float halfDiameter = 0;
    float xPieMiddle = 0;
    float yPieMiddle = 0;
    float imageMapPoints[][] = null;
    if( pieChart2D.getImageMap() != null )
    {
      halfDiameter = (float) ( pieChart2D.diameter / 2.0 );
      xPieMiddle = halfDiameter + pieChart2D.pieX;
      yPieMiddle = halfDiameter + pieChart2D.pieY;
      imageMapPoints = new float[pieChart2D.iPieChartDataSet.getNumberOfDataItems() * ( subdivisions + 1 )][2];
    }


    //---get the starting degree
    float currentDegrees = properties.getZeroDegreeOffset();

    double percentageOfPie = 0;



    //---if only one item in chart, just draw border around outside.
    //---if do a draw of the arc, will get a line in the pie as arc has a start and end.
    if( pieChart2D.iPieChartDataSet.getNumberOfDataItems() == 1 )
    {
      Arc2D.Double arc = new Arc2D.Double( pieChart2D.pieX,
                               pieChart2D.pieY,
                               pieChart2D.diameter,
                               pieChart2D.diameter,
                               currentDegrees,
                               360,
                               Arc2D.OPEN );

      g2d.setPaint( pieChart2D.iPieChartDataSet.getPaint( 0 ) );
      g2d.fill( arc );

      properties.getBorderChartStroke().draw( g2d, arc );

      //---if only a single value use a circle map
      //---IMAGE MAP
      if( pieChart2D.getImageMap() != null )
      {
        CircleMapArea circleMapArea = new CircleMapArea( xPieMiddle, yPieMiddle, pieChart2D.iPieChartDataSet.getValue( 0 ), null, pieChart2D.iPieChartDataSet.getLegendLabel( 0 ) );
        circleMapArea.setRadius( (int) pieChart2D.diameter );
        pieChart2D.getImageMap().addImageMapArea( circleMapArea );
      }

//      System.out.println( pieChart2D.pieLabels.getTextTag( 0 ).getText() );

      if( pieChart2D.pieLabels != null )
      {
        float x = pieChart2D.pieX + ( pieChart2D.diameter / 2 ) - ( pieChart2D.pieLabels.getTextTag( 0 ).getWidth() / 2 );
        float y = pieChart2D.pieY - properties.getTickLength();
  //      System.out.println( "x=" + x );
  //      System.out.println( "y=" + y );

        properties.getValueLabelFont().setupGraphics2D( g2d );
        g2d.drawString( pieChart2D.pieLabels.getTextTag( 0 ).getText(), x, y );
      }
    }
    else
    {
      Arc2D.Double arc = new Arc2D.Double( pieChart2D.pieX,
                               pieChart2D.pieY,
                               pieChart2D.diameter,
                               pieChart2D.diameter,
                               currentDegrees,
                               360,
                               Arc2D.PIE );
      //---IMAGE MAP
      int mapCounter = 0;

      for( int i = 0; i < pieChart2D.iPieChartDataSet.getNumberOfDataItems(); i++ )
      {
        percentageOfPie = pieChart2D.pieChartDataProcessor.getPercentageOfPie( i );

        arc.setAngleStart( currentDegrees );
        arc.setAngleExtent( percentageOfPie );

        //---set the color, and fill the pie piece.
        g2d.setPaint( pieChart2D.iPieChartDataSet.getPaint( i ) );
        g2d.fill( arc );

        properties.getBorderChartStroke().draw( g2d, arc );


        //---if we are going to display labels
        if( pieChart2D.pieLabels != null )
        {
          //---get the angle the center of slice
          double sliceCenterDegrees = ( currentDegrees ) + percentageOfPie / 2;

          if( sliceCenterDegrees > 360 )
          {
            sliceCenterDegrees -= 360;
          }


          double sliceCenterRadians = Math.toRadians( sliceCenterDegrees );

          //---compute the cos and sin of the label angle.
          double cosOfLabel = Math.cos( sliceCenterRadians );
          double sinOfLabel = Math.sin( sliceCenterRadians );

          halfDiameter = (float) ( pieChart2D.diameter / 2.0 );


          //---end point of the label border line.
          float borderXstart = (float) ( cosOfLabel * halfDiameter );
          float borderYstart = (float) -( sinOfLabel * halfDiameter );

          //---end point of the label border line.
          float borderXend = (float) ( cosOfLabel * ( halfDiameter + properties.getTickLength() ) );
          float borderYend = (float) -( sinOfLabel * ( halfDiameter + properties.getTickLength() ) );


          xPieMiddle = halfDiameter + pieChart2D.pieX;
          yPieMiddle = halfDiameter + pieChart2D.pieY;

          properties.getValueLabelFont().setupGraphics2D( g2d );

          g2d.draw( new Line2D.Double( xPieMiddle + borderXstart,
                              yPieMiddle + borderYstart,
                              xPieMiddle + borderXend,
                              yPieMiddle + borderYend ) );

//System.out.println( pieChart2D.textTagGroup.getTextTag( i ).getText() + "  sliceCenterDegrees= " + sliceCenterDegrees );

          float labelY = yPieMiddle + borderYend;
          if( sliceCenterDegrees > 60 && sliceCenterDegrees < 120 )
          {
            labelY -= pieChart2D.pieLabels.getTextTag( i ).getFontDescent();
          }
          else if( sliceCenterDegrees > 240 && sliceCenterDegrees < 300 )
          {
            labelY += pieChart2D.pieLabels.getTextTag( i ).getFontAscent();
          }


          if( sliceCenterDegrees > 90 && sliceCenterDegrees < 270 )
          {
            g2d.drawString( pieChart2D.pieLabels.getTextTag( i ).getText(),
                       xPieMiddle + borderXend - pieChart2D.pieLabels.getTextTag( i ).getWidth() - properties.getTickLength(),
                       labelY );
          }
          else
          {
            g2d.drawString( pieChart2D.pieLabels.getTextTag( i ).getText(),
                       xPieMiddle + borderXend + properties.getTickLength(),
                       labelY );
          }
        }


View Full Code Here

   *********************************************************************************/
  public DesignerPieChart( int width, int height ) throws ChartDataException
  {
    super( width, height );

    this.pieChart2DProperties = new PieChart2DProperties();

    super.setTitle( "Cars that Own" );
    super.setLegendLabels( new String[]{"BMW", "Audi", "Lexus"} );
    super.setPaints( new Paint[]{ Color.blue, Color.gray, Color.red } );
    super.setData( new double[][]{ {50d, 30d, 20d} } );
View Full Code Here

    this.setSize( 500, 500 );
    this.panel = new JPanel( true );
    this.panel.setSize( 500, 500 );
    this.getContentPane().add( this.panel );

    this.pieChart2DProperties = new PieChart2DProperties();
    this.legendProperties= new LegendProperties();
    this.chartProperties= new ChartProperties();

    this.setVisible( true );
View Full Code Here


  /******************************************************************************************/
  private void basicChart() throws Throwable
  {
    PieChart2DProperties pieChart2DProperties= new PieChart2DProperties();
    this.outputChart( pieChart2DProperties, "pieChartBasic" );
  }
View Full Code Here


  /******************************************************************************************/
  private void zeroDegreeOffsetChart() throws Throwable
  {
    PieChart2DProperties pieChart2DProperties= new PieChart2DProperties();
    pieChart2DProperties.setZeroDegreeOffset( 45f );
    this.outputChart( pieChart2DProperties, "pieChartZeroOffset" );
  }
View Full Code Here


  /******************************************************************************************/
  private void borderStroke() throws Throwable
  {
    PieChart2DProperties pieChart2DProperties= new PieChart2DProperties();
    pieChart2DProperties.setBorderChartStroke( new ChartStroke( new BasicStroke( 4f ), Color.black ) );
    this.outputChart( pieChart2DProperties, "pieChartBorderStroke" );
  }
View Full Code Here

  }


  private void pieValueLabels() throws ChartDataException
  {
    PieChart2DProperties pieChart2DProperties= new PieChart2DProperties();
    pieChart2DProperties.setPieLabelType( PieLabelType.VALUE_LABELS );
    this.outputChart( pieChart2DProperties, "pieChartValueLabels" );
  }
View Full Code Here

    this.outputChart( pieChart2DProperties, "pieChartValueLabels" );
  }

  private void pieLegendLabels() throws ChartDataException
  {
    PieChart2DProperties pieChart2DProperties= new PieChart2DProperties();
    pieChart2DProperties.setPieLabelType( PieLabelType.LEGEND_LABELS );

    double[] data= { 81d, 55d, 39d, 20.6d };
    String[] labels= { "BMW M5", "BMW M3", "Viper GTS-R", "Audi S6" };
    Paint[] paints= { Color.lightGray, Color.green, Color.blue, Color.red };
View Full Code Here

TOP

Related Classes of org.krysalis.jcharts.properties.PieChart2DProperties

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.