最近制作一個(gè)炒幣的app,類(lèi)似于炒股的那種,網(wǎng)上資料很多很雜,最后使用github上面的MPAndroidChart庫(kù)基本實(shí)現(xiàn)了功能物邑。
一、介紹
MPAndroidChart庫(kù):https://github.com/PhilJay/MPAndroidChart
image
1.1支持圖形
- Line Chart 折線(xiàn)圖
- Bar Chart 直方圖
- Pie Chart 餅圖
- Bubble Chart 氣泡圖
- Candle Stick Chart 蠟燭圖(用于展示金融數(shù)據(jù)時(shí)常稱(chēng)為K線(xiàn)圖)
- Radar Chart 雷達(dá)圖
- Cubic Line Chart 立方折線(xiàn)圖
- Stacked Bar Chart 堆積圖
1.2MPAndroid使用
- Project level build.gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- App level build.gradle
dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}
二、基礎(chǔ)實(shí)現(xiàn)效果圖
基礎(chǔ)實(shí)現(xiàn)效果圖
三、核心代碼
3.1那先、初始化表格
主要的一些屬性均已寫(xiě)注釋,部分注釋可能有所偏差,具體以MPAndroidChart源碼注釋為準(zhǔn)
private void initChart() {
//K線(xiàn)
ccKl.setNoDataTextColor(getResources().getColor(R.color.gray8B));//無(wú)數(shù)據(jù)時(shí)提示文字的顏色
ccKl.setDescription(null);//取消描述
ccKl.getLegend().setEnabled(false);//取消圖例
ccKl.setDragDecelerationEnabled(false);//不允許甩動(dòng)慣性滑動(dòng) 和moveView方法有沖突 設(shè)置為false
ccKl.setMinOffset(0);//設(shè)置外邊緣偏移量
ccKl.setExtraBottomOffset(6);//設(shè)置底部外邊緣偏移量 便于顯示X軸
ccKl.setScaleEnabled(false);//不可縮放
ccKl.setAutoScaleMinMaxEnabled(true);//自適應(yīng)最大最小值
ccKl.setDrawOrder(new CombinedChart.DrawOrder[]{CombinedChart.DrawOrder.CANDLE, CombinedChart.DrawOrder.LINE}); //繪制順序,先繪制條形再繪制條線(xiàn)
//K線(xiàn) x軸
XAxis xac = ccKl.getXAxis();
xac.setPosition(XAxis.XAxisPosition.BOTTOM);
xac.setGridColor(getResources().getColor(R.color.black3B));//網(wǎng)格線(xiàn)顏色
xac.setTextColor(getResources().getColor(R.color.gray8B));//標(biāo)簽顏色
xac.setTextSize(8);//標(biāo)簽字體大小
xac.setAxisLineColor(getResources().getColor(R.color.black3B));//軸線(xiàn)顏色
xac.disableAxisLineDashedLine();//取消軸線(xiàn)虛線(xiàn)設(shè)置
xac.setAvoidFirstLastClipping(true);//避免首尾端標(biāo)簽被裁剪
xac.setLabelCount(5, true);//強(qiáng)制顯示2個(gè)標(biāo)簽
//K線(xiàn) 左Y軸
YAxis axisLeft = ccKl.getAxisLeft();
axisLeft.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART); //標(biāo)簽顯示在內(nèi)側(cè);OUTSIDE_CHART外側(cè)
axisLeft.setGridColor(getResources().getColor(R.color.black3B)); //網(wǎng)格顏色
axisLeft.setTextColor(getResources().getColor(R.color.gray8B)); //文字顏色
axisLeft.setTextSize(8); //文字大小
axisLeft.setLabelCount(5, true); //label個(gè)數(shù),強(qiáng)制設(shè)置標(biāo)簽計(jì)數(shù)
axisLeft.enableGridDashedLine(5, 4, 0);//橫向網(wǎng)格線(xiàn)設(shè)置為虛線(xiàn)
//K線(xiàn) 右Y軸
YAxis axisRight = ccKl.getAxisRight();
//axisRight.setEnabled(false); //不繪制右軸
axisRight.setDrawLabels(false); //不繪制右軸標(biāo)簽
axisRight.setDrawGridLines(false); //不繪制網(wǎng)格
axisRight.setDrawAxisLine(false); //不繪制沿軸線(xiàn)
//蠟燭圖
candleSet = new CandleDataSet(new ArrayList<CandleEntry>(), "Kline");
candleSet.setAxisDependency(YAxis.AxisDependency.LEFT);
candleSet.setDrawHorizontalHighlightIndicator(false);
candleSet.setHighlightLineWidth(0.5f);
candleSet.setHighLightColor(getResources().getColor(R.color.brown));
candleSet.setShadowWidth(0.7f);
candleSet.setIncreasingColor(getResources().getColor(R.color.redEB)); //上漲設(shè)置為紅色
candleSet.setIncreasingPaintStyle(Paint.Style.FILL); //fill:實(shí)心填充 stroke:空心描邊 fill_and_stroke 填充描邊
candleSet.setDecreasingColor(getResources().getColor(R.color.green4C));//下跌設(shè)置為綠色
candleSet.setDecreasingPaintStyle(Paint.Style.FILL); //fill:實(shí)心填充 stroke:空心描邊 fill_and_stroke 填充描邊
candleSet.setNeutralColor(getResources().getColor(R.color.redEB));
candleSet.setShadowColorSameAsCandle(true);
candleSet.setDrawValues(false);
candleSet.setHighlightEnabled(false);
//5分均線(xiàn)
lineSet5 = new LineDataSet(new ArrayList<Entry>(), "MA5");
lineSet5.setAxisDependency(YAxis.AxisDependency.LEFT);
lineSet5.setColor(getResources().getColor(R.color.purple));
lineSet5.setDrawCircles(false);
lineSet5.setDrawValues(false);
lineSet5.setHighlightEnabled(false);
//10分均線(xiàn)
lineSet10 = new LineDataSet(new ArrayList<Entry>(), "MA10");
lineSet10.setAxisDependency(YAxis.AxisDependency.LEFT);
lineSet10.setColor(getResources().getColor(R.color.yellow));
lineSet10.setDrawCircles(false);
lineSet10.setDrawValues(false);
lineSet10.setHighlightEnabled(false);
//30分均線(xiàn)
lineSet30 = new LineDataSet(new ArrayList<Entry>(), "MA30");
lineSet30.setAxisDependency(YAxis.AxisDependency.LEFT);
lineSet30.setColor(getResources().getColor(R.color.white));
lineSet30.setDrawCircles(false);
lineSet30.setDrawValues(false);
lineSet30.setHighlightEnabled(false);
//分時(shí)線(xiàn)
lineSetMin = new LineDataSet(new ArrayList<Entry>(), "Minutes");
lineSetMin.setAxisDependency(YAxis.AxisDependency.LEFT);
lineSetMin.setColor(Color.WHITE);
lineSetMin.setDrawCircles(false);
lineSetMin.setDrawValues(false);
lineSetMin.setDrawFilled(true);
lineSetMin.setHighlightEnabled(false);
lineSetMin.setFillColor(getResources().getColor(R.color.gray8B));
lineSetMin.setFillAlpha(60);
//成交量
bcKl.setNoDataTextColor(getResources().getColor(R.color.gray8B));
bcKl.setDescription(null);
bcKl.getLegend().setEnabled(false);
bcKl.setDragDecelerationEnabled(false); //不允許甩動(dòng)慣性滑動(dòng)
bcKl.setMinOffset(0); //設(shè)置外邊緣偏移量
bcKl.setScaleEnabled(false);//不可縮放
bcKl.setAutoScaleMinMaxEnabled(true);//自適應(yīng)最大最小值
//自定義Y軸標(biāo)簽位置
bcKl.setRendererLeftYAxis(new InBoundYAxisRenderer(bcKl.getViewPortHandler(), bcKl.getAxisLeft(),
bcKl.getTransformer(YAxis.AxisDependency.LEFT)));
//設(shè)置渲染器控制顏色、偏移赡艰,以及高亮
bcKl.setRenderer(new OffsetBarRenderer(bcKl, bcKl.getAnimator(), bcKl.getViewPortHandler(), -0.5f)
.setHighlightWidthSize(0.5f, CommentUtil.sp2px(this, 8)));
//x軸
XAxis xAxis = bcKl.getXAxis();
xAxis.setEnabled(false);
//左Y軸
YAxis axisLeft1 = bcKl.getAxisLeft();
axisLeft1.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);//標(biāo)簽顯示在內(nèi)側(cè)
axisLeft1.setDrawAxisLine(false);
axisLeft1.setGridColor(getResources().getColor(R.color.black3B));
axisLeft1.setTextColor(getResources().getColor(R.color.gray8B));
axisLeft1.setTextSize(8);
axisLeft1.setLabelCount(2, true);
axisLeft1.setAxisMinimum(0);
axisLeft1.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return value == 0 ? "" : value + "";
}
});
//右Y軸
YAxis axisRight1 = bcKl.getAxisRight();
//axisRight1.setEnabled(false); //不繪制右軸
axisRight1.setDrawLabels(false); //不繪制右軸標(biāo)簽
axisRight1.setDrawGridLines(false); //不繪制網(wǎng)格
axisRight1.setDrawAxisLine(false); //不繪制沿軸線(xiàn)
//柱狀圖
barSet = new BarDataSet(new ArrayList<BarEntry>(), "VOL");
barSet.setHighLightColor(getResources().getColor(R.color.brown));
barSet.setColors(getResources().getColor(R.color.redEB), getResources().getColor(R.color.green4C));
barSet.setDrawValues(false);
barSet.setHighlightEnabled(false);
}
3.2售淡、數(shù)據(jù)來(lái)源以及設(shè)置數(shù)據(jù)
設(shè)置數(shù)據(jù)
private void configData() {
if (combinedData == null) {
combinedData = new CombinedData();
}
xValues.clear();
List<CandleEntry> candleValues = candleSet.getValues();
candleValues.clear();
List<Entry> ma5Values = lineSet5.getValues();
ma5Values.clear();
List<Entry> ma10Values = lineSet10.getValues();
ma10Values.clear();
List<Entry> ma30Values = lineSet30.getValues();
ma30Values.clear();
List<Entry> minValues = lineSetMin.getValues();
minValues.clear();
List<BarEntry> barValues = barSet.getValues();
barValues.clear();
for (int i = 0; i < dataList.size(); i++) {
List<String> k = dataList.get(i);
Date d = new Date(Long.parseLong(k.get(6)) * 1000); //6.毫秒
String x = sdf.format(d); //顯示日期
if (xValues.containsValue(x)) { //x重復(fù)
dataList.remove(i);
i--;
} else {
xValues.put(i, x);
float open = Float.parseFloat(k.get(4)); //4.open
float close = Float.parseFloat(k.get(1)); //1.close
candleValues.add(new CandleEntry(i, Float.parseFloat(k.get(2)), Float.parseFloat(k.get(3)), open, close, x)); //2.max 3.min
minValues.add(new Entry(i, close, x));
barValues.add(new BarEntry(i, Float.parseFloat(k.get(8)), close >= open ? 0 : 1)); //8.volume交易量
if (i >= 4) {
ma5Values.add(new Entry(i, getMA(i, 5)));
if (i >= 9) {
ma10Values.add(new Entry(i, getMA(i, 10)));
if (i >= 29) {
ma30Values.add(new Entry(i, getMA(i, 30)));
}
}
}
}
}
candleSet.setValues(candleValues);
lineSet5.setValues(ma5Values);
lineSet10.setValues(ma10Values);
lineSet30.setValues(ma30Values);
lineSetMin.setValues(minValues);
if (tlKl.getSelectedTabPosition() == 0) {
combinedData.removeDataSet(candleSet); //分時(shí)圖時(shí)移除蠟燭圖
combinedData.setData(new LineData(lineSetMin)); //分時(shí)線(xiàn)
} else {
combinedData.setData(new CandleData(candleSet)); //蠟燭圖
combinedData.setData(new LineData(lineSet5, lineSet10, lineSet30)); //5分線(xiàn) 10分線(xiàn) 30分線(xiàn)
}
ccKl.setData(combinedData);
float xMax = xValues.size() - 0.5F; //默認(rèn)X軸最大值是 xValues.size() - 1
ccKl.getXAxis().setAxisMaximum(xMax); //使最后一個(gè)顯示完整
barSet.setValues(barValues);
BarData barData = new BarData(barSet);
barData.setBarWidth(1 - candleSet.getBarSpace() * 2);//使Candle和Bar寬度一致
bcKl.setData(barData);
bcKl.getXAxis().setAxisMaximum(xMax + (-0.5f));//保持邊緣對(duì)齊
ccKl.setVisibleXRange(52, 52);//設(shè)置顯示X軸個(gè)數(shù)的上下限,豎屏固定52個(gè)
bcKl.setVisibleXRange(52, 52);
}
四、源碼下載