2 * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.achartengine.chart;
18 import java.util.ArrayList;
19 import java.util.List;
21 import org.achartengine.model.CategorySeries;
22 import org.achartengine.model.Point;
23 import org.achartengine.model.SeriesSelection;
24 import org.achartengine.renderer.DefaultRenderer;
26 import android.graphics.Canvas;
27 import android.graphics.Paint;
28 import android.graphics.Paint.Style;
29 import android.graphics.RectF;
32 * The pie chart rendering class.
34 public class PieChart extends RoundChart {
35 /** Handles returning values when tapping on PieChart. */
36 private PieMapper mPieMapper;
39 * Builds a new pie chart instance.
41 * @param dataset the series dataset
42 * @param renderer the series renderer
44 public PieChart(CategorySeries dataset, DefaultRenderer renderer) {
45 super(dataset, renderer);
46 mPieMapper = new PieMapper();
50 * The graphical representation of the pie chart.
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
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);
67 int right = x + width;
68 int sLength = mDataset.getItemCount();
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);
75 if (mRenderer.isFitLegend()) {
76 legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize,
79 int bottom = y + height - legendSize;
80 drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
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());
86 if (mCenterX == NO_VALUE) {
87 mCenterX = (left + right) / 2;
89 if (mCenterY == NO_VALUE) {
90 mCenterY = (bottom + top) / 2;
93 // Hook in clip detection after center has been calculated
94 mPieMapper.setDimensions(radius, mCenterX, mCenterY);
95 boolean loadPieCfg = !mPieMapper.areAllSegmentPresent(sLength);
97 mPieMapper.clearPieSegments();
100 float shortRadius = radius * 0.9f;
101 float longRadius = radius * 1.1f;
103 RectF oval = new RectF(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY
105 List<RectF> prevLabelsBounds = new ArrayList<RectF>();
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(),
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);
121 // Save details for getSeries functionality
123 mPieMapper.addPieSegment(i, value, currentAngle, angle);
125 currentAngle += angle;
127 prevLabelsBounds.clear();
128 drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
129 drawTitle(canvas, x, y, width, paint);
132 public SeriesSelection getSeriesAndPointForScreenCoordinate(Point screenPoint) {
133 return mPieMapper.getSeriesAndPointForScreenCoordinate(screenPoint);