1 /* 2 * Copyright 2013 AndroidPlot.com 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 17 package com.androidplot.pie; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import com.androidplot.Plot; 22 import com.androidplot.ui.AnchorPosition; 23 import com.androidplot.ui.SizeLayoutType; 24 import com.androidplot.ui.SizeMetrics; 25 import com.androidplot.util.PixelUtils; 26 import com.androidplot.ui.XLayoutStyle; 27 import com.androidplot.ui.YLayoutStyle; 28 29 public class PieChart extends Plot<Segment, SegmentFormatter, PieRenderer> { 30 31 private static final int DEFAULT_PIE_WIDGET_H_DP = 18; 32 private static final int DEFAULT_PIE_WIDGET_W_DP = 10; 33 34 private static final int DEFAULT_PIE_WIDGET_Y_OFFSET_DP = 0; 35 private static final int DEFAULT_PIE_WIDGET_X_OFFSET_DP = 0; 36 setPieWidget(PieWidget pieWidget)37 public void setPieWidget(PieWidget pieWidget) { 38 this.pieWidget = pieWidget; 39 } 40 41 @SuppressWarnings("FieldCanBeLocal") 42 private PieWidget pieWidget; 43 PieChart(Context context, String title)44 public PieChart(Context context, String title) { 45 super(context, title); 46 } 47 PieChart(Context context, String title, RenderMode mode)48 public PieChart(Context context, String title, RenderMode mode) { 49 super(context, title, mode); 50 } 51 PieChart(Context context, AttributeSet attributes)52 public PieChart(Context context, AttributeSet attributes) { 53 super(context, attributes); 54 } 55 56 @Override onPreInit()57 protected void onPreInit() { 58 pieWidget = new PieWidget( 59 getLayoutManager(), 60 this, 61 new SizeMetrics( 62 PixelUtils.dpToPix(DEFAULT_PIE_WIDGET_H_DP), 63 SizeLayoutType.FILL, 64 PixelUtils.dpToPix(DEFAULT_PIE_WIDGET_W_DP), 65 SizeLayoutType.FILL)); 66 67 pieWidget.position( 68 PixelUtils.dpToPix(DEFAULT_PIE_WIDGET_X_OFFSET_DP), 69 XLayoutStyle.ABSOLUTE_FROM_CENTER, 70 PixelUtils.dpToPix(DEFAULT_PIE_WIDGET_Y_OFFSET_DP), 71 YLayoutStyle.ABSOLUTE_FROM_CENTER, 72 AnchorPosition.CENTER); 73 74 pieWidget.setPadding(10, 10, 10, 10); 75 76 // TODO: can't remember why this getClass() check is neccessary. test if it actually is... 77 /*if (getClass().equals(PieChart.class) && attrs != null) { 78 loadAttrs(context, attrs); 79 }*/ 80 } 81 getPieWidget()82 public PieWidget getPieWidget() { 83 return pieWidget; 84 } 85 addSegment(Segment segment, SegmentFormatter formatter)86 public void addSegment(Segment segment, SegmentFormatter formatter) { 87 addSeries(segment, formatter); 88 } 89 removeSegment(Segment segment)90 public void removeSegment(Segment segment) { 91 removeSeries(segment); 92 } 93 } 94