第三方资源库MPChart如何添加到项目中就不说了,不知道的网上搜一下,很多。
也可以参考:https://blog.youkuaiyun.com/lvxiaobo1994/article/details/82790187
本篇主要说明MPChart中BubbleChart(气泡图)是如何调用实现的。
一、布局文件
<com.github.mikephil.charting.charts.BubbleChart
android:id="@+id/bubbleChart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
二、BubbleChartActivity
我这里实现的是三种不同层次的气泡图,根据Y轴数据判断气泡的颜色。
public class BubbleChartActivity extends AppCompatActivity {
private BubbleChart bubbleChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bubble_chart);
setView();
setData();
}
private void setView() {
bubbleChart = findViewById(R.id.bubbleChart);
bubbleChart.getDescription().setEnabled(false);
bubbleChart.setOnChartValueSelectedListener(this);
bubbleChart.setDrawGridBackground(false);
bubbleChart.setTouchEnabled(true);
bubbleChart.setDragEnabled(true);
bubbleChart.setScaleEnabled(true);
bubbleChart.setMaxVisibleValueCount(200);
bubbleChart.setPinchZoom(true);
Legend l = bubbleChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
YAxis yl = bubbleChart.getAxisLeft();
yl.setSpaceTop(30f);
yl.setSpaceBottom(30f);
yl.setDrawZeroLine(false);
bubbleChart.getAxisRight().setEnabled(false);
XAxis xl = bubbleChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
bubbleChart.animateY(2000);
}
private void setData() {
int count = 10;
int range = 100;
ArrayList<BubbleEntry> yVals = new ArrayList<BubbleEntry>();
ArrayList<BubbleEntry> yVals_blue = new ArrayList<BubbleEntry>();
ArrayList<BubbleEntry> yVals_yellow = new ArrayList<BubbleEntry>();
ArrayList<BubbleEntry> yVals_red = new ArrayList<BubbleEntry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
yVals.add(new BubbleEntry(i + 1, val, val));
}
for (int i = 0; i < yVals.size(); i++) {
BubbleEntry bubbleEntry = yVals.get(i);
if (bubbleEntry.getY() <= 100 / 3) {
yVals_red.add(bubbleEntry);
} else if (bubbleEntry.getY() > 100 / 3 && bubbleEntry.getY() <= 200 / 3) {
yVals_yellow.add(bubbleEntry);
} else if (bubbleEntry.getY() > 200 / 3) {
yVals_blue.add(bubbleEntry);
}
}
BubbleDataSet set1 = new BubbleDataSet(yVals_blue, "优秀");
set1.setDrawIcons(false);
set1.setColor(Color.BLUE, 100);
set1.setDrawValues(true);
BubbleDataSet set2 = new BubbleDataSet(yVals_yellow, "良好");
set2.setDrawIcons(false);
set2.setColor(Color.YELLOW, 100);
set2.setDrawValues(true);
BubbleDataSet set3 = new BubbleDataSet(yVals_red, "很差");
set3.setDrawIcons(false);
set3.setColor(Color.RED, 100);
set3.setDrawValues(true);
ArrayList<IBubbleDataSet> dataSets = new ArrayList<IBubbleDataSet>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
BubbleData data = new BubbleData(dataSets);
data.setDrawValues(true);
data.setValueTextSize(14f);
data.setValueTextColor(Color.BLACK);
data.setHighlightCircleWidth(1.5f);
bubbleChart.setData(data);
bubbleChart.invalidate();
}
}
至于里面BubbleChart的属性方法是什么意思,大家可以自行百度。