Skip to content

Commit 5a16571

Browse files
committed
add pie view
1 parent 852e068 commit 5a16571

11 files changed

Lines changed: 103 additions & 174 deletions

File tree

‎AndroidCharts/src/main/java/com/dacer/androidcharts/BarView.java‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
*/
1616
public class BarView extends View {
1717
private ArrayList<Integer> dataList;
18-
1918
private Paint bgPaint;
20-
private Paint redPaint;
19+
private Paint fgPaint;
2120
private Rect rect;
2221
private int mViewWidth;
2322
private int mViewHeight;
2423
private int barWidth;
2524
private int topMargin;
26-
private final int GRAY_COLOR = Color.parseColor("#F6F6F6");
27-
private final int RED_COLOR = Color.parseColor("#FC496D");
25+
private final int BACKGROUND_COLOR = Color.parseColor("#F6F6F6");
26+
private final int FOREGROUND_COLOR = Color.parseColor("#FC496D");
2827

2928

3029

@@ -35,19 +34,11 @@ public BarView(Context context, AttributeSet attrs){
3534
super(context, attrs);
3635
bgPaint = new Paint();
3736
bgPaint.setAntiAlias(true);
38-
bgPaint.setColor(GRAY_COLOR);
39-
redPaint = new Paint(bgPaint);
40-
redPaint.setColor(RED_COLOR);
37+
bgPaint.setColor(BACKGROUND_COLOR);
38+
fgPaint = new Paint(bgPaint);
39+
fgPaint.setColor(FOREGROUND_COLOR);
4140
rect = new Rect();
4241
topMargin = MyUtils.dip2px(context,5);
43-
44-
if(isInEditMode()){
45-
ArrayList<Integer> barDataList = new ArrayList<Integer>();
46-
for(int i=0; i<7; i++){
47-
barDataList.add((int)(Math.random() * 100));
48-
}
49-
setDataList(barDataList);
50-
}
5142
}
5243

5344
/**
@@ -64,12 +55,12 @@ protected void onDraw(Canvas canvas) {
6455
for(int i=1; i<8; i++){
6556
rect.set(barWidth*(2*i-1),topMargin,barWidth*2*i,mViewHeight);
6657
canvas.drawRect(rect,bgPaint);
67-
if(dataList != null || !dataList.isEmpty()){
58+
if(dataList != null && !dataList.isEmpty()){
6859
rect.set(barWidth*(2*i-1),
6960
topMargin+(mViewHeight-topMargin)*dataList.get(i-1)/100,
7061
barWidth*2*i,
7162
mViewHeight);
72-
canvas.drawRect(rect,redPaint);
63+
canvas.drawRect(rect,fgPaint);
7364
}
7465
}
7566
}
@@ -94,16 +85,13 @@ private int measureHeight(int measureSpec){
9485

9586
private int getMeasurement(int measureSpec, int preferred){
9687
int specSize = MeasureSpec.getSize(measureSpec);
97-
int measurement = 0;
88+
int measurement;
9889

9990
switch(MeasureSpec.getMode(measureSpec)){
10091
case MeasureSpec.EXACTLY:
101-
// This means the width of this view has been given.
10292
measurement = specSize;
10393
break;
10494
case MeasureSpec.AT_MOST:
105-
// Take the minimum of the preferred size and what
106-
// we were told to be.
10795
measurement = Math.min(preferred, specSize);
10896
break;
10997
default:

‎AndroidCharts/src/main/java/com/dacer/androidcharts/LineView.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class LineView extends View {
6262
//Constants
6363
private final int DOT_INNER_CIR_RADIUS;
6464
private final int DOT_OUTER_CIR_RADIUS;
65-
65+
private final int MIN_TOP_LINE_LENGTH;
6666
private final int MIN_VERTICAL_GRID_NUM = 4;
6767
private final int MIN_HORIZONTAL_GRID_NUM = 1;
6868
private final int BACKGROUND_LINE_COLOR = Color.parseColor("#EEEEEE");
@@ -84,6 +84,7 @@ public LineView(Context context, AttributeSet attrs){
8484
topLineLength = MyUtils.dip2px(mContext, 12);
8585
DOT_INNER_CIR_RADIUS = MyUtils.dip2px(mContext, 2);
8686
DOT_OUTER_CIR_RADIUS = MyUtils.dip2px(mContext,5);
87+
MIN_TOP_LINE_LENGTH = MyUtils.dip2px(mContext,12);
8788

8889
popupTextPaint.setAntiAlias(true);
8990
popupTextPaint.setColor(Color.WHITE);
@@ -163,7 +164,7 @@ private void refreshView(){
163164
(verticalGridNum+2)<getPopupHeight()){
164165
topLineLength = getPopupHeight()+DOT_OUTER_CIR_RADIUS+DOT_INNER_CIR_RADIUS+2;
165166
}else{
166-
topLineLength = MyUtils.dip2px(mContext,12);
167+
topLineLength = MIN_TOP_LINE_LENGTH;
167168
}
168169
xCoordinateList.clear();
169170
yCoordinateList.clear();

‎AndroidCharts/src/main/java/com/dacer/androidcharts/PieHelper.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ public PieHelper(float startDegree, float endDegree){
1414
}
1515

1616
public PieHelper(int startHour,int startMin,int startSec,int endHour,int endMin,int endSec){
17-
// if(startHour>24 || endHour>24 || startMin>60 || endMin>60 || startSec>60 || endSec>60){
18-
// throw new RuntimeException("hour should <= 24 and min should <=60 ! ");
19-
// }
2017
start = 270+startHour*15+startMin*15/60+startSec*15/3600;
2118
end = 270+endHour*15+endMin*15/60+endSec*15/3600;
2219
while(end<start){

‎AndroidCharts/src/main/java/com/dacer/androidcharts/SimpleDate.java‎

Lines changed: 0 additions & 140 deletions
This file was deleted.

‎AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/BarFragment.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.LayoutInflater;
66
import android.view.View;
77
import android.view.ViewGroup;
8+
import android.widget.Button;
89

910
import com.dacer.androidcharts.BarView;
1011
import com.dacer.androidcharts.LineView;
@@ -18,12 +19,22 @@ public class BarFragment extends Fragment {
1819
@Override
1920
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2021
View rootView = inflater.inflate(R.layout.fragment_bar, container, false);
21-
BarView barView = (BarView)rootView.findViewById(R.id.bar_view);
22+
final BarView barView = (BarView)rootView.findViewById(R.id.bar_view);
23+
Button button = (Button)rootView.findViewById(R.id.bar_button);
24+
button.setOnClickListener(new View.OnClickListener() {
25+
@Override
26+
public void onClick(View view) {
27+
randomSet(barView);
28+
}
29+
});
30+
return rootView;
31+
}
32+
33+
private void randomSet(BarView barView){
2234
ArrayList<Integer> barDataList = new ArrayList<Integer>();
2335
for(int i=0; i<7; i++){
2436
barDataList.add((int)(Math.random() * 100));
2537
}
2638
barView.setDataList(barDataList);
27-
return rootView;
2839
}
2940
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.dacer.androidchartsexample;
2+
3+
import android.app.Fragment;
4+
import android.os.Bundle;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.Button;
9+
10+
import com.dacer.androidcharts.BarView;
11+
import com.dacer.androidcharts.PieHelper;
12+
import com.dacer.androidcharts.PieView;
13+
14+
import java.util.ArrayList;
15+
16+
/**
17+
* Created by Dacer on 11/16/13.
18+
*/
19+
public class ClockPieFragment extends Fragment {
20+
@Override
21+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
22+
View rootView = inflater.inflate(R.layout.fragment_pie, container, false);
23+
final PieView pieView = (PieView)rootView.findViewById(R.id.pie_view);
24+
Button button = (Button)rootView.findViewById(R.id.pie_button);
25+
button.setOnClickListener(new View.OnClickListener() {
26+
@Override
27+
public void onClick(View view) {
28+
randomSet(pieView);
29+
}
30+
});
31+
return rootView;
32+
}
33+
34+
private void randomSet(PieView pieView){
35+
ArrayList<PieHelper> pieHelperArrayList = new ArrayList<PieHelper>();
36+
for(int i=0; i<20; i++){
37+
int startHour = (int)(24*Math.random());
38+
int startMin = (int)(60*Math.random());
39+
pieHelperArrayList.add(new PieHelper(startHour,startMin,0,startHour,startMin+25,0));
40+
}
41+
pieView.setDate(pieHelperArrayList);
42+
}
43+
}

‎AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/LineFragment.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
import android.widget.Button;
99

1010
import com.dacer.androidcharts.LineView;
11-
import com.dacer.androidcharts.SimpleDate;
12-
import com.dacer.androidcharts.TempLog;
1311

1412
import java.util.ArrayList;
15-
import java.util.Calendar;
1613

1714
/**
1815
* Created by Dacer on 11/15/13.

‎AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/MainActivity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void onNavigationDrawerItemSelected(int position) {
6363
break;
6464
case 2:
6565
fragmentManager.beginTransaction()
66-
.replace(R.id.container, new LineFragment())
66+
.replace(R.id.container, new ClockPieFragment())
6767
.commit();
6868
break;
6969
}

‎AndroidChartsExample/src/main/res/layout/fragment_bar.xml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22

33
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
4-
android:layout_width="match_parent" android:layout_height="match_parent">
4+
android:layout_width="match_parent" android:layout_height="match_parent"
5+
android:background="#ffffff">
56

67
<view
78
android:layout_width="fill_parent"
@@ -20,4 +21,12 @@
2021
android:layout_centerHorizontal="true"
2122
android:layout_alignLeft="@+id/bar_view"
2223
android:layout_alignRight="@+id/bar_view" />
24+
25+
<Button
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:text="Random"
29+
android:id="@+id/bar_button"
30+
android:layout_alignParentBottom="true"
31+
android:layout_centerHorizontal="true" />
2332
</RelativeLayout>

‎AndroidChartsExample/src/main/res/layout/fragment_line.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<Button
2222
android:layout_width="wrap_content"
2323
android:layout_height="wrap_content"
24-
android:text="New Button"
24+
android:text="Random"
2525
android:id="@+id/line_button"
2626
android:layout_alignParentBottom="true"
2727
android:layout_centerHorizontal="true" />

0 commit comments

Comments
 (0)