Skip to content

Commit 224c9e5

Browse files
author
xiaowh
committed
修复分时图下面的成交量柱状图颜色错误问题
1 parent 636f362 commit 224c9e5

5 files changed

Lines changed: 38 additions & 42 deletions

File tree

MPChartLib/src/main/java/com/github/mikephil/charting/stockChart/FiveDayChart.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,23 @@ public void setDataToChart(TimeDataManage mData) {
279279
ArrayList<Entry> lineCJEntries = new ArrayList<>();
280280
ArrayList<Entry> lineJJEntries = new ArrayList<>();
281281
ArrayList<BarEntry> barEntries = new ArrayList<>();
282+
float color;//成交量柱状图的颜色
282283
for (int i = 0, j = 0; i < mData.getDatas().size(); i++, j++) {
283284
TimeDataModel t = mData.getDatas().get(j);
284285
if (t == null) {
285286
lineCJEntries.add(new Entry(i, Float.NaN));
286287
lineJJEntries.add(new Entry(i, Float.NaN));
287-
barEntries.add(new BarEntry(i, Float.NaN));
288+
barEntries.add(new BarEntry(i, Float.NaN,0f));
288289
continue;
289290
}
290291
lineCJEntries.add(new Entry(i, (float) mData.getDatas().get(i).getNowPrice()));
291292
lineJJEntries.add(new Entry(i, (float) mData.getDatas().get(i).getAveragePrice()));
292-
barEntries.add(new BarEntry(i, mData.getDatas().get(i).getVolume()));
293+
if (i == 0) {
294+
color = mData.getDatas().get(i).getNowPrice() == mData.getPreClose() ? 0f : mData.getDatas().get(i).getNowPrice() > mData.getPreClose() ? 1f : -1f;
295+
} else {
296+
color = mData.getDatas().get(i).getNowPrice() == mData.getDatas().get(i - 1).getNowPrice() ? 0f : mData.getDatas().get(i).getNowPrice() > mData.getDatas().get(i - 1).getNowPrice() ? 1f : -1f;
297+
}
298+
barEntries.add(new BarEntry(i, mData.getDatas().get(i).getVolume(),color));
293299
}
294300
if(lineChart.getData() != null &&lineChart.getData().getDataSetCount() > 0
295301
&&barChart.getData() != null &&barChart.getData().getDataSetCount() > 0){
@@ -435,7 +441,8 @@ public void dynamicsAddOne(TimeDataModel timeDatamodel, int length) {
435441

436442
BarData barData = barChart.getData();
437443
IBarDataSet barDataSet = barData.getDataSetByIndex(0);
438-
barDataSet.addEntry(new BarEntry( index, timeDatamodel.getVolume()));
444+
float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
445+
barDataSet.addEntry(new BarEntry( index, timeDatamodel.getVolume(),color));
439446
lineData.notifyDataChanged();
440447
lineChart.notifyDataSetChanged();
441448
barData.notifyDataChanged();
@@ -468,7 +475,8 @@ public void dynamicsUpdateOne(TimeDataModel timeDatamodel, int length) {
468475
BarData barData = barChart.getData();
469476
IBarDataSet barDataSet = barData.getDataSetByIndex(0);
470477
barDataSet.removeEntry(index);
471-
barDataSet.addEntry(new BarEntry( index, timeDatamodel.getVolume()));
478+
float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
479+
barDataSet.addEntry(new BarEntry( index, timeDatamodel.getVolume(),color));
472480

473481
lineData.notifyDataChanged();
474482
lineChart.notifyDataSetChanged();

MPChartLib/src/main/java/com/github/mikephil/charting/stockChart/KLineChart.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ public void dynamicsAddOne(KLineDataManage kLineData) {
552552
if (barDataSet == null) {//当没有数据时
553553
return;
554554
}
555-
float color = kLineData.getKLineDatas().get(i).getOpen() > kLineData.getKLineDatas().get(i).getClose() ? 0f : 1f;
555+
float color = kLineData.getKLineDatas().get(i).getOpen() == kLineData.getKLineDatas().get(i).getClose()?0f:kLineData.getKLineDatas().get(i).getOpen() > kLineData.getKLineDatas().get(i).getClose() ? -1f : 1f;
556556
BarEntry barEntry = new BarEntry(i + kLineData.getOffSet(), (float) kLineData.getKLineDatas().get(i).getVolume(), color);
557557

558558
barDataSet.addEntry(barEntry);
@@ -591,7 +591,7 @@ public void dynamicsUpdateOne(KLineDataManage kLineData) {
591591
CombinedData barChartData = barChart.getData();
592592
IBarDataSet barDataSet = barChartData.getBarData().getDataSetByIndex(0);
593593
barDataSet.removeEntry(i);
594-
float color = kLineData.getKLineDatas().get(i).getOpen() > kLineData.getKLineDatas().get(i).getClose() ? 0f : 1f;
594+
float color = kLineData.getKLineDatas().get(i).getOpen() == kLineData.getKLineDatas().get(i).getClose()?0f:kLineData.getKLineDatas().get(i).getOpen() > kLineData.getKLineDatas().get(i).getClose() ? -1f : 1f;
595595
BarEntry barEntry = new BarEntry(i + kLineData.getOffSet(), (float) kLineData.getKLineDatas().get(i).getVolume(), color);
596596
barDataSet.addEntry(barEntry);
597597
} else {//副图是其他技术指标

MPChartLib/src/main/java/com/github/mikephil/charting/stockChart/OneDayChart.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,23 @@ public void setDataToChart(TimeDataManage mData) {
280280
ArrayList<Entry> lineCJEntries = new ArrayList<>();
281281
ArrayList<Entry> lineJJEntries = new ArrayList<>();
282282
ArrayList<BarEntry> barEntries = new ArrayList<>();
283+
float color;//成交量柱状图的颜色
283284
for (int i = 0, j = 0; i < mData.getDatas().size(); i++, j++) {
284285
TimeDataModel t = mData.getDatas().get(j);
285286
if (t == null) {
286287
lineCJEntries.add(new Entry( i, Float.NaN));
287288
lineJJEntries.add(new Entry( i, Float.NaN));
288-
barEntries.add(new BarEntry( i, Float.NaN));
289+
barEntries.add(new BarEntry( i, Float.NaN,0f));
289290
continue;
290291
}
291292
lineCJEntries.add(new Entry( i, (float) mData.getDatas().get(i).getNowPrice()));
292293
lineJJEntries.add(new Entry( i, (float) mData.getDatas().get(i).getAveragePrice()));
293-
barEntries.add(new BarEntry( i, mData.getDatas().get(i).getVolume()));
294+
if (i == 0) {
295+
color = mData.getDatas().get(i).getNowPrice() == mData.getPreClose() ? 0f : mData.getDatas().get(i).getNowPrice() > mData.getPreClose() ? 1f : -1f;
296+
} else {
297+
color = mData.getDatas().get(i).getNowPrice() == mData.getDatas().get(i - 1).getNowPrice() ? 0f : mData.getDatas().get(i).getNowPrice() > mData.getDatas().get(i - 1).getNowPrice() ? 1f : -1f;
298+
}
299+
barEntries.add(new BarEntry(i, mData.getDatas().get(i).getVolume(),color));
294300
}
295301

296302
if(lineChart.getData() != null &&lineChart.getData().getDataSetCount() > 0
@@ -433,7 +439,8 @@ public void dynamicsAddOne(TimeDataModel timeDatamodel, int length) {
433439

434440
BarData barData = barChart.getData();
435441
IBarDataSet barDataSet = barData.getDataSetByIndex(0);
436-
barDataSet.addEntry(new BarEntry(index, timeDatamodel.getVolume()));
442+
float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
443+
barDataSet.addEntry(new BarEntry(index, timeDatamodel.getVolume(),color));
437444
lineData.notifyDataChanged();
438445
lineChart.notifyDataSetChanged();
439446
barData.notifyDataChanged();
@@ -466,7 +473,8 @@ public void dynamicsUpdateOne(TimeDataModel timeDatamodel, int length) {
466473
BarData barData = barChart.getData();
467474
IBarDataSet barDataSet = barData.getDataSetByIndex(0);
468475
barDataSet.removeEntry(index);
469-
barDataSet.addEntry(new BarEntry(index, timeDatamodel.getVolume()));
476+
float color = timeDatamodel.getNowPrice() == d1.getEntryForIndex(index - 1).getY() ? 0f : timeDatamodel.getNowPrice() > d1.getEntryForIndex(index - 1).getY() ? 1f : -1f;
477+
barDataSet.addEntry(new BarEntry(index, timeDatamodel.getVolume(),color));
470478

471479
lineData.notifyDataChanged();
472480
lineChart.notifyDataSetChanged();

MPChartLib/src/main/java/com/github/mikephil/charting/stockChart/dataManage/KLineDataManage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void parseKlineData(JSONObject object, String assetId,boolean landscape)
140140
xVal.add(DataTimeUtil.secToDate(getKLineDatas().get(i).getDateMills()));
141141
candleEntries.add(new CandleEntry(i + offSet, (float) getKLineDatas().get(i).getHigh(), (float) getKLineDatas().get(i).getLow(), (float) getKLineDatas().get(i).getOpen(), (float) getKLineDatas().get(i).getClose()));
142142

143-
float color = getKLineDatas().get(i).getOpen() > getKLineDatas().get(i).getClose() ? 0f : 1f;
143+
float color = getKLineDatas().get(i).getOpen() == getKLineDatas().get(i).getClose() ? 0f : getKLineDatas().get(i).getOpen() > getKLineDatas().get(i).getClose() ? -1f : 1f;
144144
barEntries.add(new BarEntry(i + offSet, (float) getKLineDatas().get(i).getVolume(), color));
145145

146146
line5Entries.add(new Entry(i + offSet, (float) getKLineDatas().get(i).getMa5()));
@@ -211,7 +211,7 @@ public void initBOLL() {
211211
}
212212
lineDataBOLL.add(setALine(ColorType.blue, bollDataUP, false));
213213
lineDataBOLL.add(setALine(ColorType.yellow, bollDataMB, false));
214-
lineDataBOLL.add(setALine(ColorType.purple, bollDataDN, false));
214+
lineDataBOLL.add(setALine(ColorType.purple, bollDataDN, true));
215215
}
216216
/**
217217
* 初始化自己计算RSI
@@ -229,9 +229,9 @@ public void initRSI() {
229229
rsiData12.add(new Entry(i + offSet, rsiEntity12.getRSIs().get(i)));
230230
rsiData24.add(new Entry(i + offSet, rsiEntity24.getRSIs().get(i)));
231231
}
232-
lineDataRSI.add(setALine(ColorType.blue, rsiData6, "RSI" + RSIN1, true));
232+
lineDataRSI.add(setALine(ColorType.blue, rsiData6, "RSI" + RSIN1, false));
233233
lineDataRSI.add(setALine(ColorType.yellow, rsiData12, "RSI" + RSIN2, false));
234-
lineDataRSI.add(setALine(ColorType.purple, rsiData24, "RSI" + RSIN3, false));
234+
lineDataRSI.add(setALine(ColorType.purple, rsiData24, "RSI" + RSIN3, true));
235235
}
236236

237237
private CandleDataSet setACandle(ArrayList<CandleEntry> candleEntries) {

MPChartLib/src/main/java/com/github/mikephil/charting/stockChart/renderer/TimeBarChartRenderer.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,46 +105,26 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
105105

106106

107107
int i = j / 4;
108-
// if (i > 0) {
108+
109109
Object openClose = dataSet.getEntryForIndex(i).getData();
110-
if (openClose == null) {
111-
if (i > 0) {
112-
if (dataSet.getEntryForIndex(i).getY() > dataSet.getEntryForIndex(i - 1).getY()) {
113-
mRenderPaint.setColor(dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ?
114-
dataSet.getColor(j) :
115-
dataSet.getIncreasingColor());
116-
mRenderPaint.setStyle(dataSet.getIncreasingPaintStyle());
117-
} else {
118-
mRenderPaint.setColor(dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE ?
119-
dataSet.getColor(j) :
120-
dataSet.getDecreasingColor());
121-
mRenderPaint.setStyle(dataSet.getDecreasingPaintStyle());
122-
}
123-
} else {
124-
mRenderPaint.setColor(dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ?
125-
dataSet.getColor(j) :
126-
dataSet.getIncreasingColor());
127-
mRenderPaint.setStyle(dataSet.getIncreasingPaintStyle());
128-
}
129-
} else {//根据开平判断柱状图的颜色填充
110+
if (openClose != null) {//根据开平判断柱状图的颜色填充
130111
float value = (Float) openClose;
131112
if (value > 0) {//表示增加
132113
mRenderPaint.setColor(dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ?
133114
dataSet.getColor(j) :
134115
dataSet.getIncreasingColor());
135116
mRenderPaint.setStyle(dataSet.getIncreasingPaintStyle());
136-
} else if (value <= 0) {
117+
} else if (value < 0) {
137118
mRenderPaint.setColor(dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE ?
138119
dataSet.getColor(j) :
139120
dataSet.getDecreasingColor());
140121
mRenderPaint.setStyle(dataSet.getDecreasingPaintStyle());
122+
}else if (value == 0) {
123+
mRenderPaint.setColor(dataSet.getNeutralColor() == ColorTemplate.COLOR_NONE ?
124+
dataSet.getColor(j) :
125+
dataSet.getNeutralColor());
126+
mRenderPaint.setStyle(dataSet.getDecreasingPaintStyle());
141127
}
142-
// else if (value == 0) {
143-
// mRenderPaint.setColor(dataSet.getNeutralColor() == ColorTemplate.COLOR_NONE ?
144-
// dataSet.getColor(j) :
145-
// dataSet.getNeutralColor());
146-
// mRenderPaint.setStyle(dataSet.getDecreasingPaintStyle());
147-
// }
148128
}
149129

150130

0 commit comments

Comments
 (0)