Skip to content

Commit

Permalink
Reformat Code
Browse files Browse the repository at this point in the history
  • Loading branch information
dacer committed Aug 25, 2017
1 parent 532d1f9 commit 99ea739
Show file tree
Hide file tree
Showing 23 changed files with 763 additions and 822 deletions.
2 changes: 1 addition & 1 deletion AndroidCharts/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<manifest
package="im.dacer.androidcharts">

<application>
Expand Down
161 changes: 78 additions & 83 deletions AndroidCharts/src/main/java/im/dacer/androidcharts/BarView.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,57 @@
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;

/**
* Created by Dacer on 11/11/13.
*/
public class BarView extends View {
private final int MINI_BAR_WIDTH;
private final int BAR_SIDE_MARGIN;
private final int TEXT_TOP_MARGIN;
private final int TEXT_COLOR = Color.parseColor("#9B9A9B");
private final int BACKGROUND_COLOR = Color.parseColor("#F6F6F6");
private final int FOREGROUND_COLOR = Color.parseColor("#FC496D");
private ArrayList<Float> percentList;
private ArrayList<Float> targetPercentList;
private Paint textPaint;
private Paint bgPaint;
private Paint fgPaint;
private Rect rect;
private int barWidth;
// private boolean showSideMargin = true;
private int bottomTextDescent;
private boolean autoSetWidth = true;
private int topMargin;
private int bottomTextHeight;
private ArrayList<String> bottomTextList = new ArrayList<String>();
private final int MINI_BAR_WIDTH;
private final int BAR_SIDE_MARGIN;
private final int TEXT_TOP_MARGIN;
private final int TEXT_COLOR = Color.parseColor("#9B9A9B");
private final int BACKGROUND_COLOR = Color.parseColor("#F6F6F6");
private final int FOREGROUND_COLOR = Color.parseColor("#FC496D");

private Runnable animator = new Runnable() {
@Override
public void run() {
boolean needNewFrame = false;
for (int i=0; i<targetPercentList.size();i++) {
if (percentList.get(i) < targetPercentList.get(i)) {
percentList.set(i,percentList.get(i)+0.02f);
needNewFrame = true;
} else if (percentList.get(i) > targetPercentList.get(i)){
percentList.set(i,percentList.get(i)-0.02f);
needNewFrame = true;
}
if(Math.abs(targetPercentList.get(i)-percentList.get(i))<0.02f){
percentList.set(i,targetPercentList.get(i));
}
@Override public void run() {
boolean needNewFrame = false;
for (int i = 0; i < targetPercentList.size(); i++) {
if (percentList.get(i) < targetPercentList.get(i)) {
percentList.set(i, percentList.get(i) + 0.02f);
needNewFrame = true;
} else if (percentList.get(i) > targetPercentList.get(i)) {
percentList.set(i, percentList.get(i) - 0.02f);
needNewFrame = true;
}
if (needNewFrame) {
postDelayed(this, 20);
if (Math.abs(targetPercentList.get(i) - percentList.get(i)) < 0.02f) {
percentList.set(i, targetPercentList.get(i));
}
invalidate();
}
if (needNewFrame) {
postDelayed(this, 20);
}
invalidate();
}
};

public BarView(Context context){
this(context,null);
public BarView(Context context) {
this(context, null);
}
public BarView(Context context, AttributeSet attrs){

public BarView(Context context, AttributeSet attrs) {
super(context, attrs);
bgPaint = new Paint();
bgPaint.setAntiAlias(true);
Expand All @@ -70,9 +67,9 @@ public BarView(Context context, AttributeSet attrs){
rect = new Rect();
topMargin = MyUtils.dip2px(context, 5);
int textSize = MyUtils.sp2px(context, 15);
barWidth = MyUtils.dip2px(context,22);
MINI_BAR_WIDTH = MyUtils.dip2px(context,22);
BAR_SIDE_MARGIN = MyUtils.dip2px(context,22);
barWidth = MyUtils.dip2px(context, 22);
MINI_BAR_WIDTH = MyUtils.dip2px(context, 22);
BAR_SIDE_MARGIN = MyUtils.dip2px(context, 22);
TEXT_TOP_MARGIN = MyUtils.dip2px(context, 5);
textPaint = new Paint();
textPaint.setAntiAlias(true);
Expand All @@ -84,23 +81,24 @@ public BarView(Context context, AttributeSet attrs){

/**
* dataList will be reset when called is method.
*
* @param bottomStringList The String ArrayList in the bottom.
*/
public void setBottomTextList(ArrayList<String> bottomStringList){
// this.dataList = null;
public void setBottomTextList(ArrayList<String> bottomStringList) {
// this.dataList = null;
this.bottomTextList = bottomStringList;
Rect r = new Rect();
bottomTextDescent = 0;
barWidth = MINI_BAR_WIDTH;
for(String s:bottomTextList){
textPaint.getTextBounds(s,0,s.length(),r);
if(bottomTextHeight<r.height()){
for (String s : bottomTextList) {
textPaint.getTextBounds(s, 0, s.length(), r);
if (bottomTextHeight < r.height()) {
bottomTextHeight = r.height();
}
if(autoSetWidth&&(barWidth<r.width())){
if (autoSetWidth && (barWidth < r.width())) {
barWidth = r.width();
}
if(bottomTextDescent<(Math.abs(r.bottom))){
if (bottomTextDescent < (Math.abs(r.bottom))) {
bottomTextDescent = Math.abs(r.bottom);
}
}
Expand All @@ -109,95 +107,93 @@ public void setBottomTextList(ArrayList<String> bottomStringList){
}

/**
*
* @param list The ArrayList of Integer with the range of [0-max].
*/
public void setDataList(ArrayList<Integer> list, int max){
public void setDataList(ArrayList<Integer> list, int max) {
targetPercentList = new ArrayList<Float>();
if(max == 0) max = 1;
if (max == 0) max = 1;

for(Integer integer : list){
targetPercentList.add(1-(float)integer/(float)max);
for (Integer integer : list) {
targetPercentList.add(1 - (float) integer / (float) max);
}

// Make sure percentList.size() == targetPercentList.size()
if(percentList.isEmpty() || percentList.size()<targetPercentList.size()){
int temp = targetPercentList.size()-percentList.size();
for(int i=0; i<temp;i++){
if (percentList.isEmpty() || percentList.size() < targetPercentList.size()) {
int temp = targetPercentList.size() - percentList.size();
for (int i = 0; i < temp; i++) {
percentList.add(1f);
}
} else if (percentList.size()>targetPercentList.size()){
int temp = percentList.size()-targetPercentList.size();
for(int i=0; i<temp;i++){
percentList.remove(percentList.size()-1);
} else if (percentList.size() > targetPercentList.size()) {
int temp = percentList.size() - targetPercentList.size();
for (int i = 0; i < temp; i++) {
percentList.remove(percentList.size() - 1);
}
}
setMinimumWidth(2);
removeCallbacks(animator);
post(animator);
}

@Override
protected void onDraw(Canvas canvas) {
@Override protected void onDraw(Canvas canvas) {
int i = 1;
if(percentList != null && !percentList.isEmpty()){
for(Float f:percentList){
rect.set(BAR_SIDE_MARGIN*i+barWidth*(i-1),
topMargin,
(BAR_SIDE_MARGIN+barWidth)* i,
getHeight()-bottomTextHeight-TEXT_TOP_MARGIN);
canvas.drawRect(rect,bgPaint);
if (percentList != null && !percentList.isEmpty()) {
for (Float f : percentList) {
rect.set(BAR_SIDE_MARGIN * i + barWidth * (i - 1), topMargin,
(BAR_SIDE_MARGIN + barWidth) * i,
getHeight() - bottomTextHeight - TEXT_TOP_MARGIN);
canvas.drawRect(rect, bgPaint);
/*rect.set(BAR_SIDE_MARGIN*i+barWidth*(i-1),
topMargin+(int)((getHeight()-topMargin)*percentList.get(i-1)),
(BAR_SIDE_MARGIN+barWidth)* i,
getHeight()-bottomTextHeight-TEXT_TOP_MARGIN);*/
/**
* The correct total height is "getHeight()-topMargin-bottomTextHeight-TEXT_TOP_MARGIN",not "getHeight()-topMargin".
* fix by [email protected] on 11/11/13.
*/
rect.set(BAR_SIDE_MARGIN*i+barWidth*(i-1),
topMargin+(int)((getHeight()-topMargin-bottomTextHeight-TEXT_TOP_MARGIN)*percentList.get(i-1)),
(BAR_SIDE_MARGIN+barWidth)* i,
getHeight()-bottomTextHeight-TEXT_TOP_MARGIN);
canvas.drawRect(rect,fgPaint);
/**
* The correct total height is "getHeight()-topMargin-bottomTextHeight-TEXT_TOP_MARGIN",not "getHeight()-topMargin".
* fix by [email protected] on 11/11/13.
*/
rect.set(BAR_SIDE_MARGIN * i + barWidth * (i - 1), topMargin + (int) ((getHeight()
- topMargin
- bottomTextHeight
- TEXT_TOP_MARGIN) * percentList.get(i - 1)),
(BAR_SIDE_MARGIN + barWidth) * i,
getHeight() - bottomTextHeight - TEXT_TOP_MARGIN);
canvas.drawRect(rect, fgPaint);
i++;
}
}

if(bottomTextList != null && !bottomTextList.isEmpty()){
if (bottomTextList != null && !bottomTextList.isEmpty()) {
i = 1;
for(String s:bottomTextList){
canvas.drawText(s,BAR_SIDE_MARGIN*i+barWidth*(i-1)+barWidth/2,
getHeight()-bottomTextDescent,textPaint);
for (String s : bottomTextList) {
canvas.drawText(s, BAR_SIDE_MARGIN * i + barWidth * (i - 1) + barWidth / 2,
getHeight() - bottomTextDescent, textPaint);
i++;
}
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mViewWidth = measureWidth(widthMeasureSpec);
int mViewHeight = measureHeight(heightMeasureSpec);
setMeasuredDimension(mViewWidth,mViewHeight);
setMeasuredDimension(mViewWidth, mViewHeight);
}

private int measureWidth(int measureSpec){
private int measureWidth(int measureSpec) {
int preferred = 0;
if(bottomTextList != null){
preferred = bottomTextList.size()*(barWidth+BAR_SIDE_MARGIN);
if (bottomTextList != null) {
preferred = bottomTextList.size() * (barWidth + BAR_SIDE_MARGIN);
}
return getMeasurement(measureSpec, preferred);
}

private int measureHeight(int measureSpec){
private int measureHeight(int measureSpec) {
int preferred = 222;
return getMeasurement(measureSpec, preferred);
}

private int getMeasurement(int measureSpec, int preferred){
private int getMeasurement(int measureSpec, int preferred) {
int specSize = MeasureSpec.getSize(measureSpec);
int measurement;
switch(MeasureSpec.getMode(measureSpec)){
switch (MeasureSpec.getMode(measureSpec)) {
case MeasureSpec.EXACTLY:
measurement = specSize;
break;
Expand All @@ -210,5 +206,4 @@ private int getMeasurement(int measureSpec, int preferred){
}
return measurement;
}

}
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
package im.dacer.androidcharts;


/**
* Created by Dacer on 11/14/13.
*/
public class ClockPieHelper {

int velocity = 5;
private float start;
private float end;
private float targetStart;
private float targetEnd;
int velocity = 5;

ClockPieHelper(float startDegree, float endDegree, ClockPieHelper targetPie){
ClockPieHelper(float startDegree, float endDegree, ClockPieHelper targetPie) {
start = startDegree;
end = endDegree;
targetStart = targetPie.getStart();
targetEnd = targetPie.getEnd();
}

public ClockPieHelper(int startHour, int startMin, int endHour, int endMin){
start = 270+startHour*15+startMin*15/60;
end = 270+endHour*15+endMin*15/60;
while(end<start){
end+=360;
public ClockPieHelper(int startHour, int startMin, int endHour, int endMin) {
start = 270 + startHour * 15 + startMin * 15 / 60;
end = 270 + endHour * 15 + endMin * 15 / 60;
while (end < start) {
end += 360;
}
}

public ClockPieHelper(int startHour, int startMin, int startSec, int endHour, int endMin, int endSec){
start = 270+startHour*15+startMin*15/60+startSec*15/3600;
end = 270+endHour*15+endMin*15/60+endSec*15/3600;
while(end<start){
end+=360;
public ClockPieHelper(int startHour, int startMin, int startSec, int endHour, int endMin,
int endSec) {
start = 270 + startHour * 15 + startMin * 15 / 60 + startSec * 15 / 3600;
end = 270 + endHour * 15 + endMin * 15 / 60 + endSec * 15 / 3600;
while (end < start) {
end += 360;
}
}

ClockPieHelper setTarget(float targetStart,float targetEnd){
ClockPieHelper setTarget(float targetStart, float targetEnd) {
this.targetStart = targetStart;
this.targetEnd = targetEnd;
return this;
}

ClockPieHelper setTarget(ClockPieHelper targetPie){
ClockPieHelper setTarget(ClockPieHelper targetPie) {
targetStart = targetPie.getStart();
targetEnd = targetPie.getEnd();
return this;
}

boolean isAtRest(){
return (start==targetStart)&&(end==targetEnd);
boolean isAtRest() {
return (start == targetStart) && (end == targetEnd);
}

void update(){
void update() {
start = updateSelf(start, targetStart, velocity);
end = updateSelf(end, targetEnd, velocity);
}

public float getSweep(){
return end-start;
public float getSweep() {
return end - start;
}

public float getStart(){
public float getStart() {
return start;
}

public float getEnd(){
public float getEnd() {
return end;
}

private float updateSelf(float origin, float target, int velocity){
private float updateSelf(float origin, float target, int velocity) {
if (origin < target) {
origin += velocity;
} else if (origin > target){
origin-= velocity;
} else if (origin > target) {
origin -= velocity;
}
if(Math.abs(target-origin)<velocity){
if (Math.abs(target - origin) < velocity) {
origin = target;
}
return origin;
Expand Down
Loading

0 comments on commit 99ea739

Please sign in to comment.