forked from halfhp/androidplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplePieChartActivity.java
198 lines (160 loc) · 6.72 KB
/
SimplePieChartActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright 2015 AndroidPlot.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.androidplot.demos;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.SeekBar;
import android.widget.TextView;
import com.androidplot.pie.PieChart;
import com.androidplot.pie.PieRenderer;
import com.androidplot.pie.Segment;
import com.androidplot.pie.SegmentFormatter;
import com.androidplot.util.*;
import java.util.*;
/**
* The simplest possible example of using AndroidPlot to plot some data.
*/
public class SimplePieChartActivity extends Activity
{
public static final int SELECTED_SEGMENT_OFFSET = 50;
private TextView donutSizeTextView;
private SeekBar donutSizeSeekBar;
public PieChart pie;
private Segment s1;
private Segment s2;
private Segment s3;
private Segment s4;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.pie_chart);
// initialize our XYPlot reference:
pie = (PieChart) findViewById(R.id.mySimplePieChart);
// enable the legend:
pie.getLegend().setVisible(true);
final float padding = PixelUtils.dpToPix(30);
pie.getPie().setPadding(padding, padding, padding, padding);
// detect segment clicks:
pie.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
PointF click = new PointF(motionEvent.getX(), motionEvent.getY());
if(pie.getPie().containsPoint(click)) {
Segment segment = pie.getRenderer(PieRenderer.class).getContainingSegment(click);
if(segment != null) {
final boolean isSelected = getFormatter(segment).getOffset() != 0;
deselectAll();
setSelected(segment, !isSelected);
pie.redraw();
}
}
return false;
}
private SegmentFormatter getFormatter(Segment segment) {
return pie.getFormatter(segment, PieRenderer.class);
}
private void deselectAll() {
List<Segment> segments = pie.getRegistry().getSeriesList();
for(Segment segment : segments) {
setSelected(segment, false);
}
}
private void setSelected(Segment segment, boolean isSelected) {
SegmentFormatter f = getFormatter(segment);
if(isSelected) {
f.setOffset(SELECTED_SEGMENT_OFFSET);
} else {
f.setOffset(0);
}
}
});
donutSizeSeekBar = (SeekBar) findViewById(R.id.donutSizeSeekBar);
donutSizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
pie.getRenderer(PieRenderer.class).setDonutSize(seekBar.getProgress()/100f,
PieRenderer.DonutMode.PERCENT);
pie.redraw();
updateDonutText();
}
});
donutSizeTextView = (TextView) findViewById(R.id.donutSizeTextView);
updateDonutText();
s1 = new Segment("s1", 3);
s2 = new Segment("s2", 1);
s3 = new Segment("s3", 7);
s4 = new Segment("s4", 9);
EmbossMaskFilter emf = new EmbossMaskFilter(
new float[]{1, 1, 1}, 0.4f, 10, 8.2f);
SegmentFormatter sf1 = new SegmentFormatter(this, R.xml.pie_segment_formatter1);
sf1.getLabelPaint().setShadowLayer(3, 0, 0, Color.BLACK);
sf1.getFillPaint().setMaskFilter(emf);
SegmentFormatter sf2 = new SegmentFormatter(this, R.xml.pie_segment_formatter2);
sf2.getLabelPaint().setShadowLayer(3, 0, 0, Color.BLACK);
sf2.getFillPaint().setMaskFilter(emf);
SegmentFormatter sf3 = new SegmentFormatter(this, R.xml.pie_segment_formatter3);
sf3.getLabelPaint().setShadowLayer(3, 0, 0, Color.BLACK);
sf3.getFillPaint().setMaskFilter(emf);
SegmentFormatter sf4 = new SegmentFormatter(this, R.xml.pie_segment_formatter4);
sf4.getLabelPaint().setShadowLayer(3, 0, 0, Color.BLACK);
sf4.getFillPaint().setMaskFilter(emf);
pie.addSegment(s1, sf1);
pie.addSegment(s2, sf2);
pie.addSegment(s3, sf3);
pie.addSegment(s4, sf4);
pie.getBorderPaint().setColor(Color.TRANSPARENT);
pie.getBackgroundPaint().setColor(Color.TRANSPARENT);
}
@Override
public void onStart() {
super.onStart();
setupIntroAnimation();
}
protected void updateDonutText() {
donutSizeTextView.setText(donutSizeSeekBar.getProgress() + "%");
}
protected void setupIntroAnimation() {
final PieRenderer renderer = pie.getRenderer(PieRenderer.class);
// start with a zero degrees pie:
renderer.setExtentDegs(0);
// animate a scale value from a starting val of 0 to a final value of 1:
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
// use an animation pattern that begins and ends slowly:
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float scale = valueAnimator.getAnimatedFraction();
renderer.setExtentDegs(360 * scale);
pie.redraw();
}
});
// the animation will run for 1.5 seconds:
animator.setDuration(1500);
animator.start();
}
}