private ArrayList<IBarDataSet> getiBarDataSets( List<BarEntry> dataListExpense, List<BarEntry> dataListIncome) { BarDataSet dataSetIncome = new BarDataSet(dataListIncome, mContext.getString(R.string.income)); dataSetIncome.setColor(ContextCompat.getColor(mContext, R.color.colorPrimaryGreen)); dataSetIncome.setDrawValues(false); BarDataSet dataSetExpense = new BarDataSet(dataListExpense, mContext.getString(R.string.expense)); dataSetExpense.setColor(ContextCompat.getColor(mContext, R.color.colorRed)); dataSetExpense.setDrawValues(false); ArrayList<IBarDataSet> dataSetList = new ArrayList<>(); switch (mSelectionType) { case IncomeExpenseAllType.INCOME: dataSetList.add(dataSetIncome); break; case IncomeExpenseAllType.EXPENSE: dataSetList.add(dataSetExpense); break; case IncomeExpenseAllType.ALL: dataSetList.add(dataSetIncome); dataSetList.add(dataSetExpense); break; } return dataSetList; }
private void setData(DataParse mData) { kLineDatas = mData.getKLineDatas(); // axisLeftBar.setAxisMaxValue(mData.getVolmax()); String unit = MyUtils.getVolUnit(mData.getVolmax()); int u = 1; if ("万手".equals(unit)) { u = 4; } else if ("亿手".equals(unit)) { u = 8; } axisLeftBar.setValueFormatter(new VolFormatter((int) Math.pow(10, u))); // axisRightBar.setAxisMaxValue(mData.getVolmax()); Log.e("@@@", mData.getVolmax() + "da"); ArrayList<String> xVals = new ArrayList<>(); ArrayList<BarEntry> barEntries = new ArrayList<>(); ArrayList<CandleEntry> candleEntries = new ArrayList<>(); ArrayList<Entry> line5Entries = new ArrayList<>(); ArrayList<Entry> line10Entries = new ArrayList<>(); ArrayList<Entry> line30Entries = new ArrayList<>(); for (int i = 0, j = 0; i < mData.getKLineDatas().size(); i++, j++) { xVals.add(mData.getKLineDatas().get(i).date + ""); barEntries.add(new BarEntry(mData.getKLineDatas().get(i).vol, i)); candleEntries.add( new CandleEntry( i, mData.getKLineDatas().get(i).high, mData.getKLineDatas().get(i).low, mData.getKLineDatas().get(i).open, mData.getKLineDatas().get(i).close)); if (i >= 4) { sum = 0; line5Entries.add(new Entry(getSum(i - 4, i) / 5, i)); } if (i >= 9) { sum = 0; line10Entries.add(new Entry(getSum(i - 9, i) / 10, i)); } if (i >= 29) { sum = 0; line30Entries.add(new Entry(getSum(i - 29, i) / 30, i)); } } barDataSet = new BarDataSet(barEntries, "成交量"); barDataSet.setBarSpacePercent(50); // bar空隙 barDataSet.setHighlightEnabled(true); barDataSet.setHighLightAlpha(255); barDataSet.setHighLightColor(Color.WHITE); barDataSet.setDrawValues(false); barDataSet.setColor(Color.RED); BarData barData = new BarData(xVals, barDataSet); barChart.setData(barData); final ViewPortHandler viewPortHandlerBar = barChart.getViewPortHandler(); viewPortHandlerBar.setMaximumScaleX(culcMaxscale(xVals.size())); Matrix touchmatrix = viewPortHandlerBar.getMatrixTouch(); final float xscale = 3; touchmatrix.postScale(xscale, 1f); CandleDataSet candleDataSet = new CandleDataSet(candleEntries, "KLine"); candleDataSet.setDrawHorizontalHighlightIndicator(false); candleDataSet.setHighlightEnabled(true); candleDataSet.setHighLightColor(Color.WHITE); candleDataSet.setValueTextSize(10f); candleDataSet.setDrawValues(false); candleDataSet.setColor(Color.RED); candleDataSet.setShadowWidth(1f); candleDataSet.setAxisDependency(YAxis.AxisDependency.LEFT); CandleData candleData = new CandleData(xVals, candleDataSet); ArrayList<ILineDataSet> sets = new ArrayList<>(); sets.add(setMaLine(5, xVals, line5Entries)); sets.add(setMaLine(10, xVals, line10Entries)); sets.add(setMaLine(30, xVals, line30Entries)); CombinedData combinedData = new CombinedData(xVals); LineData lineData = new LineData(xVals, sets); combinedData.setData(candleData); combinedData.setData(lineData); combinedchart.setData(combinedData); combinedchart.moveViewToX(mData.getKLineDatas().size() - 1); final ViewPortHandler viewPortHandlerCombin = combinedchart.getViewPortHandler(); viewPortHandlerCombin.setMaximumScaleX(culcMaxscale(xVals.size())); Matrix matrixCombin = viewPortHandlerCombin.getMatrixTouch(); final float xscaleCombin = 3; matrixCombin.postScale(xscaleCombin, 1f); combinedchart.moveViewToX(mData.getKLineDatas().size() - 1); barChart.moveViewToX(mData.getKLineDatas().size() - 1); setOffset(); /** * ************************************************************************************** * 此处解决方法来源于CombinedChartDemo,k线图y轴显示问题,图表滑动后才能对齐的bug,希望有人给出解决方法 (注:此bug现已修复,感谢和chenguang79一起研究) * ************************************************************************************** */ handler.sendEmptyMessageDelayed(0, 300); }
private void setChart(BarChart barChart, double[][] values, String[] labels) { int[] colors = new int[5]; colors[0] = Color.argb(255, 11, 55, 144); colors[1] = Color.argb(255, 48, 120, 254); colors[2] = Color.argb(255, 123, 179, 254); colors[3] = Color.argb(255, 230, 230, 230); colors[4] = Color.argb(0, 255, 255, 255); ArrayList<BarEntry> dataEntries = new ArrayList<BarEntry>(); for (int i = 0; i < values.length; ++i) { float[] fList = new float[values[i].length]; for (int j = 0; j < values[i].length; ++j) { fList[j] = (float) values[i][j]; } dataEntries.add(new BarEntry(fList, i)); } BarDataSet dataSet = new BarDataSet(dataEntries, ""); dataSet.setBarSpacePercent(2f); dataSet.setStackLabels(new String[] {"浅睡眠", "深睡眠", "活动"}); dataSet.setColors(new int[] {colors[0], colors[1], colors[2]}); dataSet.setDrawValues(false); ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); dataSets.add(dataSet); BarData barData = new BarData(labels, dataSets); barChart.setData(barData); YAxis leftAxis = barChart.getAxisLeft(); leftAxis.setDrawGridLines(true); leftAxis.setDrawLabels(false); leftAxis.setGridColor(colors[3]); leftAxis.setDrawAxisLine(true); leftAxis.setAxisLineColor(colors[3]); leftAxis.setAxisLineWidth(0.5f); YAxis rightAxis = barChart.getAxisRight(); rightAxis.setEnabled(true); rightAxis.setDrawLabels(false); rightAxis.setDrawGridLines(false); rightAxis.setAxisLineColor(colors[3]); rightAxis.setAxisLineWidth(0.5f); XAxis xAxis = barChart.getXAxis(); xAxis.setPosition(XAxisPosition.BOTTOM); xAxis.setDrawLabels(false); xAxis.setDrawGridLines(true); xAxis.setGridColor(colors[3]); xAxis.setAxisLineWidth(0.5f); xAxis.setAxisLineColor(colors[3]); barChart.setBackgroundColor(colors[4]); barChart.setGridBackgroundColor(colors[4]); barChart.getLegend().setEnabled(false); barChart.setScaleEnabled(false); barChart.setDescription(""); barChart.animateXY(2000, 2000); barChart.notifyDataSetChanged(); }