• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.Color;
20 import android.graphics.Paint;
21 import com.androidplot.ui.SeriesRenderer;
22 import com.androidplot.ui.Formatter;
23 
24 public class SegmentFormatter extends Formatter<PieChart> {
25 
26     private static final int DEFAULT_FILL_COLOR = Color.TRANSPARENT;
27     private static final int DEFAULT_EDGE_COLOR = Color.BLACK;
28     private static final int DEFAULT_LABEL_COLOR = Color.WHITE;
29     private static final float DEFAULT_EDGE_THICKNESS = 3;
30     private static final float DEFAULT_LABEL_MARKER_THICKNESS = 3;
31     private static final float DEFAULT_LABEL_FONT_SIZE = 18;
32 
33     private Paint innerEdgePaint;
34     private Paint outerEdgePaint;
35     private Paint radialEdgePaint;
36     private Paint fillPaint;
37 
38     private Paint labelPaint;
39     private Paint labelMarkerPaint;
40 
41     {
setFillPaint(new Paint())42         setFillPaint(new Paint());
43         // outer edge:
setOuterEdgePaint(new Paint())44         setOuterEdgePaint(new Paint());
45         getOuterEdgePaint().setStyle(Paint.Style.STROKE);
46         getOuterEdgePaint().setStrokeWidth(DEFAULT_EDGE_THICKNESS);
47         getOuterEdgePaint().setAntiAlias(true);
48 
49         // inner edge:
setInnerEdgePaint(new Paint())50         setInnerEdgePaint(new Paint());
51         getInnerEdgePaint().setStyle(Paint.Style.STROKE);
52         getInnerEdgePaint().setStrokeWidth(DEFAULT_EDGE_THICKNESS);
53         getInnerEdgePaint().setAntiAlias(true);
54 
55         // radial edge:
setRadialEdgePaint(new Paint())56         setRadialEdgePaint(new Paint());
57         getRadialEdgePaint().setStyle(Paint.Style.STROKE);
58         getRadialEdgePaint().setStrokeWidth(DEFAULT_EDGE_THICKNESS);
59         getRadialEdgePaint().setAntiAlias(true);
60 
61         // label paint:
setLabelPaint(new Paint())62         setLabelPaint(new Paint());
63         getLabelPaint().setColor(DEFAULT_LABEL_COLOR);
64         getLabelPaint().setTextSize(DEFAULT_LABEL_FONT_SIZE);
65         getLabelPaint().setAntiAlias(true);
66         getLabelPaint().setTextAlign(Paint.Align.CENTER);
67         //getLabelPaint().setShadowLayer(5, 4, 4, Color.BLACK);
68 
69         // label marker paint:
setLabelMarkerPaint(new Paint())70         setLabelMarkerPaint(new Paint());
71         getLabelMarkerPaint().setColor(DEFAULT_LABEL_COLOR);
72         getLabelMarkerPaint().setStrokeWidth(DEFAULT_LABEL_MARKER_THICKNESS);
73     }
74 
75     /**
76      * Should only be used in conjunction with calls to configure()...
77      */
SegmentFormatter()78     public SegmentFormatter() {}
79 
SegmentFormatter(Integer fillColor)80     public SegmentFormatter(Integer fillColor) {
81         if(fillColor != null) {
82             getFillPaint().setColor(fillColor);
83         } else {
84             getFillPaint().setColor(DEFAULT_FILL_COLOR);
85         }
86     }
87 
SegmentFormatter(Integer fillColor, Integer borderColor)88     public SegmentFormatter(Integer fillColor, Integer borderColor) {
89         this(fillColor);
90         getInnerEdgePaint().setColor(borderColor);
91         getOuterEdgePaint().setColor(borderColor);
92         getRadialEdgePaint().setColor(borderColor);
93     }
94 
SegmentFormatter(Integer fillColor, Integer outerEdgeColor, Integer innerEdgeColor, Integer radialEdgeColor)95     public SegmentFormatter(Integer fillColor, Integer outerEdgeColor,
96                             Integer innerEdgeColor, Integer radialEdgeColor) {
97         this(fillColor);
98 
99 
100         if(getOuterEdgePaint() != null) {
101             getOuterEdgePaint().setColor(outerEdgeColor);
102         } else {
103             outerEdgePaint = new Paint();
104             getOuterEdgePaint().setColor(DEFAULT_EDGE_COLOR);
105         }
106 
107         if (getInnerEdgePaint() != null) {
108             getInnerEdgePaint().setColor(innerEdgeColor);
109         } else {
110             outerEdgePaint = new Paint();
111             getInnerEdgePaint().setColor(DEFAULT_EDGE_COLOR);
112         }
113 
114         if (getRadialEdgePaint() != null) {
115             getRadialEdgePaint().setColor(radialEdgeColor);
116         } else {
117             radialEdgePaint = new Paint();
118             getRadialEdgePaint().setColor(DEFAULT_EDGE_COLOR);
119         }
120     }
121 
122     @Override
getRendererClass()123     public Class<? extends SeriesRenderer> getRendererClass() {
124         return PieRenderer.class;
125     }
126 
127     @Override
getRendererInstance(PieChart plot)128     public SeriesRenderer getRendererInstance(PieChart plot) {
129         return new PieRenderer(plot);
130     }
131 
getInnerEdgePaint()132     public Paint getInnerEdgePaint() {
133         return innerEdgePaint;
134     }
135 
setInnerEdgePaint(Paint innerEdgePaint)136     public void setInnerEdgePaint(Paint innerEdgePaint) {
137         this.innerEdgePaint = innerEdgePaint;
138     }
139 
getOuterEdgePaint()140     public Paint getOuterEdgePaint() {
141         return outerEdgePaint;
142     }
143 
setOuterEdgePaint(Paint outerEdgePaint)144     public void setOuterEdgePaint(Paint outerEdgePaint) {
145         this.outerEdgePaint = outerEdgePaint;
146     }
147 
getRadialEdgePaint()148     public Paint getRadialEdgePaint() {
149         return radialEdgePaint;
150     }
151 
setRadialEdgePaint(Paint radialEdgePaint)152     public void setRadialEdgePaint(Paint radialEdgePaint) {
153         this.radialEdgePaint = radialEdgePaint;
154     }
155 
getFillPaint()156     public Paint getFillPaint() {
157         return fillPaint;
158     }
159 
setFillPaint(Paint fillPaint)160     public void setFillPaint(Paint fillPaint) {
161         this.fillPaint = fillPaint;
162     }
163 
getLabelPaint()164     public Paint getLabelPaint() {
165         return labelPaint;
166     }
167 
setLabelPaint(Paint labelPaint)168     public void setLabelPaint(Paint labelPaint) {
169         this.labelPaint = labelPaint;
170     }
171 
getLabelMarkerPaint()172     public Paint getLabelMarkerPaint() {
173         return labelMarkerPaint;
174     }
175 
setLabelMarkerPaint(Paint labelMarkerPaint)176     public void setLabelMarkerPaint(Paint labelMarkerPaint) {
177         this.labelMarkerPaint = labelMarkerPaint;
178     }
179 }
180