create changelog entry
[debian/openrocket] / android-libraries / achartengine / src / org / achartengine / chart / PieChart.java
1 /**
2  * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL
3  *  
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *  
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *  
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.achartengine.chart;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.achartengine.model.CategorySeries;
22 import org.achartengine.model.Point;
23 import org.achartengine.model.SeriesSelection;
24 import org.achartengine.renderer.DefaultRenderer;
25
26 import android.graphics.Canvas;
27 import android.graphics.Paint;
28 import android.graphics.Paint.Style;
29 import android.graphics.RectF;
30
31 /**
32  * The pie chart rendering class.
33  */
34 public class PieChart extends RoundChart {
35   /** Handles returning values when tapping on PieChart. */
36   private PieMapper mPieMapper;
37
38   /**
39    * Builds a new pie chart instance.
40    * 
41    * @param dataset the series dataset
42    * @param renderer the series renderer
43    */
44   public PieChart(CategorySeries dataset, DefaultRenderer renderer) {
45     super(dataset, renderer);
46     mPieMapper = new PieMapper();
47   }
48
49   /**
50    * The graphical representation of the pie chart.
51    * 
52    * @param canvas the canvas to paint to
53    * @param x the top left x value of the view to draw to
54    * @param y the top left y value of the view to draw to
55    * @param width the width of the view to draw to
56    * @param height the height of the view to draw to
57    * @param paint the paint
58    */
59   @Override
60   public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
61     paint.setAntiAlias(mRenderer.isAntialiasing());
62     paint.setStyle(Style.FILL);
63     paint.setTextSize(mRenderer.getLabelsTextSize());
64     int legendSize = getLegendSize(mRenderer, height / 5, 0);
65     int left = x;
66     int top = y;
67     int right = x + width;
68     int sLength = mDataset.getItemCount();
69     double total = 0;
70     String[] titles = new String[sLength];
71     for (int i = 0; i < sLength; i++) {
72       total += mDataset.getValue(i);
73       titles[i] = mDataset.getCategory(i);
74     }
75     if (mRenderer.isFitLegend()) {
76       legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize,
77           paint, true);
78     }
79     int bottom = y + height - legendSize;
80     drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
81
82     float currentAngle = mRenderer.getStartAngle();
83     int mRadius = Math.min(Math.abs(right - left), Math.abs(bottom - top));
84     int radius = (int) (mRadius * 0.35 * mRenderer.getScale());
85
86     if (mCenterX == NO_VALUE) {
87       mCenterX = (left + right) / 2;
88     }
89     if (mCenterY == NO_VALUE) {
90       mCenterY = (bottom + top) / 2;
91     }
92
93     // Hook in clip detection after center has been calculated
94     mPieMapper.setDimensions(radius, mCenterX, mCenterY);
95     boolean loadPieCfg = !mPieMapper.areAllSegmentPresent(sLength);
96     if (loadPieCfg) {
97       mPieMapper.clearPieSegments();
98     }
99
100     float shortRadius = radius * 0.9f;
101     float longRadius = radius * 1.1f;
102
103     RectF oval = new RectF(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY
104         + radius);
105     List<RectF> prevLabelsBounds = new ArrayList<RectF>();
106
107     for (int i = 0; i < sLength; i++) {
108       paint.setColor(mRenderer.getSeriesRendererAt(i).getColor());
109       float value = (float) mDataset.getValue(i);
110       float angle = (float) (value / total * 360);
111       canvas.drawArc(oval, currentAngle, angle, true, paint);
112       drawLabel(canvas, mDataset.getCategory(i), mRenderer, prevLabelsBounds, mCenterX, mCenterY,
113           shortRadius, longRadius, currentAngle, angle, left, right, mRenderer.getLabelsColor(),
114           paint, true);
115       if (mRenderer.isDisplayValues()) {
116         drawLabel(canvas, getLabel(mDataset.getValue(i)), mRenderer, prevLabelsBounds, mCenterX,
117             mCenterY, shortRadius / 2, longRadius / 2, currentAngle, angle, left, right,
118             mRenderer.getLabelsColor(), paint, false);
119       }
120
121       // Save details for getSeries functionality
122       if (loadPieCfg) {
123         mPieMapper.addPieSegment(i, value, currentAngle, angle);
124       }
125       currentAngle += angle;
126     }
127     prevLabelsBounds.clear();
128     drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
129     drawTitle(canvas, x, y, width, paint);
130   }
131
132   public SeriesSelection getSeriesAndPointForScreenCoordinate(Point screenPoint) {
133     return mPieMapper.getSeriesAndPointForScreenCoordinate(screenPoint);
134   }
135
136 }