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);
csc.setTouchEnabled(true);
csc.setDragEnabled(true);
csc.setScaleEnabled(false);
csc.setScaleXEnabled(true);
csc.setScaleYEnabled(true);
csc.setPinchZoom(true);
csc.setDoubleTapToZoomEnabled(true);
csc.setHighlightPerDragEnabled(true);
csc.setDragDecelerationEnabled(true);
csc.setDragDecelerationFrictionCoef(0.99f);
XAxis xAxis = csc.getXAxis();
xAxis.setAxisMinimum(0);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.GRAY);
xAxis.setTextSize(14);
xAxis.setDrawGridLines(false);
YAxis yAxisLeft = csc.getAxisLeft();
yAxisLeft.setAxisMinimum(0);
yAxisLeft.setTextSize(14);
YAxis yAxisRight = csc.getAxisRight();
yAxisRight.setEnabled(false);
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});
}
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);
candleDataSet.setDrawValues(false);
CandleData data = new CandleData(candleDataSet);
csc.setData(data);
}
}
List<CandleEntry> candleEntry = new ArrayList<>();
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);