Skip to content

Commit

Permalink
1、修改一侧只有一个数据时,数据绘制不出来的问题;
Browse files Browse the repository at this point in the history
  • Loading branch information
banbury committed Sep 10, 2018
1 parent d736ed4 commit 9ca3d56
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ MPAndroidChart的辅助类,解决MPAndroidChart中的一些小细节,现已
# 使用方法:
## 1、依赖配置
- 在项目最外面的build.gradle文件中,allprojects节点下的repositories中添加:

`maven{ url "https://dl.bintray.com/teadoglibrary/MPAndroidChartFix" }`
- 再在app的build.gradle文件中,dependencies节点下添加,其中的版本建议根据最新版本修改:
`compile 'com.teaanddogdog:mpandroidchartutil:1.0.2'`

`compile 'com.teaanddogdog:mpandroidchartutil:1.0.3'`
- 本库需要依赖MPAndroidChart使用,所以app的build.gradle文件中,dependencies节点下还要添加MPAndroidChart库:

`compile 'com.github.PhilJay:MPAndroidChart:v3.0.3'`

## 2、代码中使用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
import com.github.mikephil.charting.formatter.LargeValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.formatter.StackedValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

Expand Down Expand Up @@ -68,12 +70,12 @@ private void setPieChart(PieChart pieChart, String[] alarm) {
// pieChart.setExtraRightOffset(30);//调整饼图右侧多30个dp,即向左移30dp

List<PieEntry> entryList = new ArrayList<>();
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[0]));
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[1]));
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[2]));
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[3]));
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[1]));
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[2]));
entryList.add(new PieEntry(100, alarm[3]));
entryList.add(new PieEntry(10, alarm[0]));
entryList.add(new PieEntry(10, alarm[1]));
entryList.add(new PieEntry(10, alarm[2]));
entryList.add(new PieEntry(10, alarm[1]));
entryList.add(new PieEntry(10, alarm[2]));
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[0]));
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[1]));
entryList.add(new PieEntry(mRandom.nextInt(200), alarm[2]));
Expand Down Expand Up @@ -142,8 +144,10 @@ private void setPieChart(PieChart pieChart, String[] alarm) {
valueFormatter.setAppendix("次");
PercentFormatter percentFormatter = new PercentFormatter();
mDataSet.setValueFormatter(percentFormatter);
// mDataSet.setValueFormatter(new MyPrecentFormatter());
mDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//值显示的位置


PieData data = new PieData(mDataSet);
pieChart.setData(data);
pieChart.setDrawEntryLabels(false);//是否在饼图上显示Lable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.teaanddogdog.mpandroidchartfix;

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

/**
* @author banbury
* @version v1.0
* @created 2018/8/3_17:50.
* @description
*/

public class MyPrecentFormatter extends PercentFormatter {


@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return super.getFormattedValue(value, entry, dataSetIndex, viewPortHandler) + (String) entry.getData();
// return super.getFormattedValue(value, entry, dataSetIndex, viewPortHandler) + "test";
}

}
4 changes: 2 additions & 2 deletions mpandroidchartutil/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def gitUrl = 'https://github.com/huzipiaopiao/MPAndroidChartFix.git' // Git仓
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

version = "1.0.2" //发布版本号
version = "1.0.3" //发布版本号
group = "com.teaanddogdog" //最终引用形式,如compile 'com.leon.lfilepicker:1.0.0',其中lfilepicker在后面配置

//配置参照的:https://www.jianshu.com/p/b5d3462407fc 命令:gradle bintrayUpload
Expand Down Expand Up @@ -60,7 +60,7 @@ bintray {
version{
name = project.getVersion()
vcsTag = "v${project.getVersion()}"
desc = 'remove some mode'//描述,自己定义
desc = 'fix a bug'//描述,自己定义
released = new Date()
attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public void drawValuesWithAVG(Canvas c) {
// get whole the radius
float radius = mChart.getRadius();
float rotationAngle = mChart.getRotationAngle();
float[] drawAngles = mChart.getDrawAngles();
float[] absoluteAngles = mChart.getAbsoluteAngles();
float[] drawAngles = mChart.getDrawAngles();//每一个数据占据的角度 15 15 15
float[] absoluteAngles = mChart.getAbsoluteAngles();//每一个数据累计后的总角度 15 30 45

float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
Expand Down Expand Up @@ -211,8 +211,8 @@ public void drawValuesWithAVG(Canvas c) {
xIndex = 0;


float rightSpace = radius * 2 / (rightCount - 1);
float leftSpace = radius * 2 / (leftCount - 1);
float rightSpace = (rightCount > 1) ? radius * 2 / (rightCount - 1) : radius / 2;
float leftSpace = (leftCount > 1) ? radius * 2 / (leftCount - 1) : radius / 2;


int tempRightIndex = 0;
Expand Down Expand Up @@ -303,7 +303,11 @@ public void drawValuesWithAVG(Canvas c) {
// tempLeftIndex++;
tempLeftToLeftIndex++;
} else {//没有转动时 和 有转动,且原本左侧上方的区域进入到右侧时
pt2y = (measuredHeight - topAndBottomSpace / 2) - leftSpace * (tempLeftIndex + leftToLeftCount);
if (leftCount > 1) {
pt2y = (measuredHeight - topAndBottomSpace / 2) - leftSpace * (tempLeftIndex + leftToLeftCount);
} else {
pt2y = pt1y;
}
tempLeftIndex++;
}

Expand All @@ -328,8 +332,13 @@ public void drawValuesWithAVG(Canvas c) {
tempRightIndex++;
tempRightToRightIndex++;
} else {//没有转动时 和 有转动,且原本右侧下方的区域进入到左侧时
pt2y = topAndBottomSpace / 2 + rightSpace * (tempRightIndex + leftToRightCount + rightToRightCount);
tempRightIndex++;
if (rightCount > 1) {
pt2y = topAndBottomSpace / 2 + rightSpace * (tempRightIndex + leftToRightCount + rightToRightCount);
tempRightIndex++;
} else {
pt2y = pt1y;
}

}

mValuePaint.setTextAlign(Paint.Align.LEFT);
Expand Down

0 comments on commit 9ca3d56

Please sign in to comment.