先來個截圖吧,補上以前的折線的截圖和下面的K線的截圖java
和以前的同樣,註釋都在代碼中,其實 基本上的設置,這幾個圖形都大同小異字體
public class CandleStickChartUtils { private CandleStickChart csc; public CandleStickChartUtils(CandleStickChart csc){ this.csc = csc; initSetting(); } /** * 經常使用設置 */ private void initSetting() { csc.getDescription().setText(""); csc.getDescription().setTextColor(Color.RED); csc.getDescription().setTextSize(16);//設置描述的文字 ,顏色 大小 csc.setNoDataText("無數據噢"); //沒數據的時候顯示 csc.setDrawBorders(false);//是否顯示邊框 csc.animateX(500);//x軸動畫 csc.setTouchEnabled(true); // 設置是否能夠觸摸 csc.setDragEnabled(true);// 是否能夠拖拽 csc.setScaleEnabled(false);// 是否能夠縮放 x和y軸, 默認是true csc.setScaleXEnabled(true); //是否能夠縮放 僅x軸 csc.setScaleYEnabled(true); //是否能夠縮放 僅y軸 csc.setPinchZoom(true); //設置x軸和y軸可否同時縮放。默認是否 csc.setDoubleTapToZoomEnabled(true);//設置是否能夠經過雙擊屏幕放大圖表。默認是true csc.setHighlightPerDragEnabled(true);//可否拖拽高亮線(數據點與座標的提示線),默認是true csc.setDragDecelerationEnabled(true);//拖拽滾動時,手放開是否會持續滾動,默認是true(false是拖到哪是哪,true拖拽以後還會有緩衝) csc.setDragDecelerationFrictionCoef(0.99f);//與上面那個屬性配合,持續滾動時的速度快慢,[0,1) 0表明當即中止 //x軸的設置 XAxis xAxis = csc.getXAxis();//獲取x軸 xAxis.setAxisMinimum(0); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//設置x軸位置 xAxis.setTextColor(Color.GRAY);//設置x軸字體顏色 xAxis.setTextSize(14);//設置x軸文字字體大小 xAxis.setDrawGridLines(false);//設置豎向線 網格線 //y軸的設置 YAxis yAxisLeft = csc.getAxisLeft(); yAxisLeft.setAxisMinimum(0);//設置左側y軸 yAxisLeft.setTextSize(14);//左側y軸文字字體大小 YAxis yAxisRight = csc.getAxisRight(); //設置右側y軸 yAxisRight.setEnabled(false);//設置右側y軸是否可用 /** * 設置圖例 */ Legend legend = csc.getLegend(); legend.setEnabled(true);// legend.setForm(Legend.LegendForm.SQUARE); //設置比例圖樣式 方 legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//設置橫向 legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);//設置位置 legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); String[] lables = new String[]{"漲","跌"}; int[] colors = new int[]{Color.RED,Color.GREEN}; LegendEntry incre = new LegendEntry("漲", Legend.LegendForm.SQUARE, 10f, 1f, null, colors[0]); LegendEntry decre = new LegendEntry("跌", Legend.LegendForm.SQUARE, 10f, 1f, null, colors[1]); legend.setCustom(new LegendEntry[]{incre,decre}); } /** * 設置數據 * @param yVals */ public void setCandleStickData(List<CandleEntry> yVals){ CandleDataSet candleDataSet = new CandleDataSet(yVals,""); candleDataSet.setValueTextColor(Color.BLACK); candleDataSet.setValueTextSize(14); candleDataSet.setShadowColor(Color.DKGRAY);//設置影線的顏色 candleDataSet.setShadowWidth(0.5f);//設置影線的寬度 candleDataSet.setShadowColorSameAsCandle(true);//設置影線和蠟燭圖的顏色同樣 candleDataSet.setDecreasingColor(Color.GREEN);//設置減小色 candleDataSet.setDecreasingPaintStyle(Paint.Style.STROKE);//綠跌,空心描邊 candleDataSet.setIncreasingColor(Color.RED);//設置增加色 candleDataSet.setIncreasingPaintStyle(Paint.Style.FILL);//設置增加紅 實心 candleDataSet.setNeutralColor(Color.RED);//當天價格不漲不跌(一字線)顏色 candleDataSet.setHighlightEnabled(true);//設置定位線是否可用 candleDataSet.setHighLightColor(Color.BLACK); //設置定位線的顏色 candleDataSet.setHighlightLineWidth(0.5f);//設置定位線的線寬 candleDataSet.setBarSpace(0.9f);//0 至1 之間,越小蠟燭圖的寬度越寬 candleDataSet.setDrawValues(false);//設置是否顯示蠟燭圖上的文字 CandleData data = new CandleData(candleDataSet); csc.setData(data); } }
Activity中用的時候動畫
List<CandleEntry> candleEntry = new ArrayList<>(); /** * shadowH 當天的最高價 * shadowL 當天的最低價 * open 開盤價 * close 收盤價 */ candleEntry.add(new CandleEntry(2,8,1,8f,2f)); candleEntry.add(new CandleEntry(3,5,1,4f,4f)); candleEntry.add(new CandleEntry(4,8,2,4f,6f)); cscUtils.setCandleStickData(candleEntry);
惟一的一點是,使用CandleStickChart只能繪製出K線圖,在股票中還有MA5(Movig Acerage )等幾日的均線圖,使用這個就繪製不了了,或者說很差實現了,這就有另外一個API了,明日繼續this