Skip to content

Commit 647907e

Browse files
author
xiaowh
committed
优化图表,竖屏也可以滑动图表,显示高亮十字线
1 parent 702fcc5 commit 647907e

10 files changed

Lines changed: 58 additions & 71 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
public class BaseChart extends LinearLayout {
2424

25-
public boolean landscape = false;//是否横屏模式
25+
public boolean landscape = false;//横屏还是竖屏
2626
public int precision = 3;//小数精度
2727
public Paint mPaint;
2828

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private void setShowLabels(boolean isShow) {
266266
*/
267267
public void setDataToChart(TimeDataManage mData) {
268268
this.mData = mData;
269-
cirCleView.setVisibility(landscape?View.VISIBLE:View.GONE);
269+
// cirCleView.setVisibility(landscape?View.VISIBLE:View.GONE);
270270
if (mData.getDatas().size() == 0) {
271271
cirCleView.setVisibility(View.GONE);
272272
lineChart.setNoDataText(getResources().getString(R.string.no_data));
@@ -364,7 +364,7 @@ public void setDataToChart(TimeDataManage mData) {
364364
Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.fade_fill_color);
365365
d1.setFillDrawable(drawable);
366366
d1.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
367-
d1.setHighlightEnabled(landscape);
367+
d1.setHighlightEnabled(true);
368368
d2.setHighlightEnabled(false);
369369
d1.setDrawCircles(false);
370370
d2.setDrawCircles(false);
@@ -381,7 +381,7 @@ public void setDataToChart(TimeDataManage mData) {
381381
barDataSet = new BarDataSet(barEntries, "成交量");
382382
barDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
383383
barDataSet.setDrawValues(false);
384-
barDataSet.setHighlightEnabled(landscape);
384+
barDataSet.setHighlightEnabled(true);
385385
barDataSet.setNeutralColor(ContextCompat.getColor(mContext, R.color.equal_color));
386386
barDataSet.setIncreasingColor(ContextCompat.getColor(mContext, R.color.up_color));
387387
barDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));
@@ -401,7 +401,7 @@ public void setDataToChart(TimeDataManage mData) {
401401
barChart.setViewPortOffsets(left, 0, right, CommonUtil.dip2px(mContext, 15));
402402
} else {
403403
lineChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 15));
404-
barChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), 0, CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5));
404+
barChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), 0, CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 15));
405405
}
406406

407407
axisLeftLine.setAxisMinimum(mData.getMin());

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ public void initChart(boolean landscape) {
9797
candleChart.setDrawBorders(true);
9898
candleChart.setBorderWidth(0.7f);
9999
candleChart.setBorderColor(ContextCompat.getColor(mContext, R.color.border_color));
100-
candleChart.setDragEnabled(landscape);
101-
candleChart.setScaleXEnabled(landscape);
102-
candleChart.setScaleYEnabled(false);
100+
candleChart.setDragEnabled(true);//是否可拖动
101+
candleChart.setScaleXEnabled(true);//x轴方向是否可放大缩小
102+
candleChart.setScaleYEnabled(false);//Y轴方向是否可放大缩小
103103
candleChart.setHardwareAccelerationEnabled(true);
104104
Legend mChartKlineLegend = candleChart.getLegend();
105105
mChartKlineLegend.setEnabled(false);
106+
//k线滚动系数设置,控制滚动惯性
106107
candleChart.setDragDecelerationEnabled(true);
107108
candleChart.setDragDecelerationFrictionCoef(0.6f);//0.92持续滚动时的速度快慢,[0,1) 0代表立即停止。
108109
candleChart.setDoubleTapToZoomEnabled(false);
@@ -112,8 +113,8 @@ public void initChart(boolean landscape) {
112113
barChart.setDrawBorders(true);
113114
barChart.setBorderWidth(0.7f);
114115
barChart.setBorderColor(ContextCompat.getColor(mContext, R.color.border_color));
115-
barChart.setDragEnabled(landscape);
116-
barChart.setScaleXEnabled(landscape);
116+
barChart.setDragEnabled(true);
117+
barChart.setScaleXEnabled(true);
117118
barChart.setScaleYEnabled(false);
118119
barChart.setHardwareAccelerationEnabled(true);
119120
Legend mChartChartsLegend = barChart.getLegend();
@@ -266,12 +267,6 @@ public void setDataToChart(KLineDataManage data) {
266267

267268
if (data.getAssetId().endsWith(".HK") && !data.getAssetId().contains("IDX")) {
268269
setPrecision(3);
269-
} else if (data.getAssetId().endsWith(".US")) {
270-
if (Math.abs(data.getPreClosePrice()) < 1) {
271-
setPrecision(4);
272-
} else {
273-
setPrecision(2);
274-
}
275270
} else {
276271
setPrecision(2);
277272
}

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public OneDayChart(Context context, @Nullable AttributeSet attrs) {
105105
public void initChart(boolean landscape) {
106106
this.landscape = landscape;
107107
//主图
108-
lineChart.setScaleEnabled(false);
109-
lineChart.setDrawBorders(true);
108+
lineChart.setScaleEnabled(false);//是否可以缩放
109+
lineChart.setDrawBorders(true);//是否画外框线
110110
lineChart.setBorderColor(ContextCompat.getColor(mContext, R.color.border_color));
111111
lineChart.setBorderWidth(0.7f);
112112
lineChart.setNoDataText(getResources().getString(R.string.loading));
@@ -127,18 +127,19 @@ public void initChart(boolean landscape) {
127127
xAxisLine = (TimeXAxis) lineChart.getXAxis();
128128
xAxisLine.setDrawAxisLine(false);
129129
xAxisLine.setTextColor(ContextCompat.getColor(mContext, R.color.label_text));
130-
xAxisLine.setPosition(XAxis.XAxisPosition.BOTTOM);
130+
xAxisLine.setPosition(XAxis.XAxisPosition.BOTTOM);//x轴刻度值显示在底部
131131
xAxisLine.setAvoidFirstLastClipping(true);
132132
xAxisLine.setGridColor(ContextCompat.getColor(mContext, R.color.grid_color));
133133
xAxisLine.setGridLineWidth(0.7f);
134134

135135
//主图左Y轴
136136
axisLeftLine = lineChart.getAxisLeft();
137-
axisLeftLine.setLabelCount(5, true);
137+
axisLeftLine.setLabelCount(5, true);//Y轴左边分多少个刻度
138138
axisLeftLine.setDrawGridLines(false);
139139
axisLeftLine.setValueLineInside(true);
140140
axisLeftLine.setDrawTopBottomGridLine(false);
141141
axisLeftLine.setDrawAxisLine(false);
142+
//刻度值在图形里面还是外面
142143
axisLeftLine.setPosition(landscape ? YAxis.YAxisLabelPosition.OUTSIDE_CHART : YAxis.YAxisLabelPosition.INSIDE_CHART);
143144
axisLeftLine.setTextColor(ContextCompat.getColor(mContext, R.color.axis_text));
144145
axisLeftLine.setValueFormatter(new ValueFormatter() {
@@ -150,10 +151,11 @@ public String getAxisLabel(float value, AxisBase axis) {
150151

151152
//主图右Y轴
152153
axisRightLine = lineChart.getAxisRight();
153-
axisRightLine.setLabelCount(5, true);
154+
axisRightLine.setLabelCount(5, true);//Y轴右边分多少个刻度
154155
axisRightLine.setDrawTopBottomGridLine(false);
155156
axisRightLine.setDrawGridLines(true);
156157
axisRightLine.setGridLineWidth(0.7f);
158+
//虚线
157159
axisRightLine.enableGridDashedLine(CommonUtil.dip2px(mContext, 4), CommonUtil.dip2px(mContext, 3), 0);
158160
axisRightLine.setDrawAxisLine(false);
159161
axisRightLine.setValueLineInside(true);
@@ -265,7 +267,7 @@ private void setShowLabels(boolean isShow) {
265267
*/
266268
public void setDataToChart(TimeDataManage mData) {
267269
this.mData = mData;
268-
cirCleView.setVisibility(landscape ? View.VISIBLE : View.GONE);
270+
// cirCleView.setVisibility(landscape ? View.VISIBLE : View.GONE);
269271
if (mData.getDatas().size() == 0) {
270272
cirCleView.setVisibility(View.GONE);
271273
lineChart.setNoDataText(getResources().getString(R.string.no_data));
@@ -311,13 +313,6 @@ public void setDataToChart(TimeDataManage mData) {
311313
if (mData.getAssetId().endsWith(".HK")) {
312314
setPrecision(mData.getAssetId().contains("IDX") ? 2 : 3);
313315
setMaxCount(ChartType.HK_ONE_DAY.getPointNum());
314-
} else if (mData.getAssetId().endsWith(".US")) {
315-
if (Math.abs(mData.getMax()) < 1) {
316-
setPrecision(4);
317-
} else {
318-
setPrecision(2);
319-
}
320-
setMaxCount(ChartType.US_ONE_DAY.getPointNum());
321316
} else {
322317
setPrecision(2);
323318
setMaxCount(ChartType.ONE_DAY.getPointNum());
@@ -368,7 +363,7 @@ public void setDataToChart(TimeDataManage mData) {
368363
Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.fade_fill_color);
369364
d1.setFillDrawable(drawable);
370365
d1.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
371-
d1.setHighlightEnabled(landscape);
366+
d1.setHighlightEnabled(true);//是否显示高亮十字线
372367
d2.setHighlightEnabled(false);
373368
d1.setDrawCircles(false);
374369
d2.setDrawCircles(false);
@@ -384,7 +379,7 @@ public void setDataToChart(TimeDataManage mData) {
384379

385380
barDataSet = new BarDataSet(barEntries, "成交量");
386381
barDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
387-
barDataSet.setHighlightEnabled(landscape);
382+
barDataSet.setHighlightEnabled(true);//是否显示高亮十字线
388383
barDataSet.setDrawValues(false);
389384
barDataSet.setNeutralColor(ContextCompat.getColor(mContext, R.color.equal_color));
390385
barDataSet.setIncreasingColor(ContextCompat.getColor(mContext, R.color.up_color));
@@ -405,7 +400,7 @@ public void setDataToChart(TimeDataManage mData) {
405400
barChart.setViewPortOffsets(left, 0, right, CommonUtil.dip2px(mContext, 15));
406401
} else {
407402
lineChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 15));
408-
barChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), 0, CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5));
403+
barChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), 0, CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 15));
409404
}
410405

411406
axisLeftLine.setAxisMinimum(mData.getMin());

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class KLineDataManage {
3636
private Context mContext;
3737
private ArrayList<KLineDataModel> kDatas = new ArrayList<>();
3838
private float offSet = 0f;//K线图最右边偏移量
39-
private String assetId;
39+
private String assetId;//股票代号
4040
private boolean landscape = false;//横屏还是竖屏
4141

4242
//MA参数
@@ -236,8 +236,8 @@ public void initRSI() {
236236

237237
private CandleDataSet setACandle(ArrayList<CandleEntry> candleEntries) {
238238
CandleDataSet candleDataSet = new CandleDataSet(candleEntries, "蜡烛线");
239-
candleDataSet.setDrawHorizontalHighlightIndicator(landscape);
240-
candleDataSet.setHighlightEnabled(landscape);
239+
candleDataSet.setDrawHorizontalHighlightIndicator(true);
240+
candleDataSet.setHighlightEnabled(true);
241241
candleDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
242242
candleDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
243243
candleDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));
@@ -255,7 +255,7 @@ private CandleDataSet setACandle(ArrayList<CandleEntry> candleEntries) {
255255
private CandleDataSet setBOLLCandle(ArrayList<CandleEntry> candleEntries) {
256256
CandleDataSet candleDataSet = new CandleDataSet(candleEntries, "BOLL叠加蜡烛线");
257257
candleDataSet.setDrawHorizontalHighlightIndicator(false);
258-
candleDataSet.setHighlightEnabled(landscape);
258+
candleDataSet.setHighlightEnabled(true);
259259
candleDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
260260
candleDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
261261
candleDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));
@@ -283,13 +283,13 @@ private LineDataSet setALine(ColorType ma, ArrayList<Entry> lineEntries, String
283283
boolean highlightEnable = false;
284284
return setALine(ma, lineEntries, label, highlightEnable);
285285
}
286-
286+
//行情走势线属性设置
287287
private LineDataSet setALine(ColorType colorType, ArrayList<Entry> lineEntries, String label, boolean highlightEnable) {
288288
LineDataSet lineDataSetMa = new LineDataSet(lineEntries, label);
289289
lineDataSetMa.setDrawHorizontalHighlightIndicator(false);
290-
lineDataSetMa.setHighlightEnabled(landscape?highlightEnable:landscape);
291-
lineDataSetMa.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
292-
lineDataSetMa.setDrawValues(false);
290+
lineDataSetMa.setHighlightEnabled(highlightEnable);//是否画高亮十字线
291+
lineDataSetMa.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));//高亮十字线颜色
292+
lineDataSetMa.setDrawValues(false);//是否画出每个蜡烛线的数值
293293
if (colorType == ColorType.blue) {
294294
lineDataSetMa.setColor(ContextCompat.getColor(mContext, R.color.ma5));
295295
} else if (colorType == ColorType.yellow) {
@@ -308,16 +308,17 @@ private BarDataSet setABar(ArrayList<BarEntry> barEntries) {
308308
return setABar(barEntries, label);
309309
}
310310

311+
//蜡烛线属性设置
311312
private BarDataSet setABar(ArrayList<BarEntry> barEntries, String label) {
312313
BarDataSet barDataSet = new BarDataSet(barEntries, label);
313-
barDataSet.setHighlightEnabled(landscape);
314-
barDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
314+
barDataSet.setHighlightEnabled(true);//是否画高亮十字线
315+
barDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));//高亮十字线颜色
315316
barDataSet.setValueTextSize(10);
316-
barDataSet.setDrawValues(false);
317-
barDataSet.setNeutralColor(ContextCompat.getColor(mContext, R.color.equal_color));
318-
barDataSet.setIncreasingColor(ContextCompat.getColor(mContext, R.color.up_color));
319-
barDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));
320-
barDataSet.setIncreasingPaintStyle(Paint.Style.FILL);
317+
barDataSet.setDrawValues(false);//是否画出每个蜡烛线的数值
318+
barDataSet.setNeutralColor(ContextCompat.getColor(mContext, R.color.equal_color));//行情平势时蜡烛的标识颜色
319+
barDataSet.setIncreasingColor(ContextCompat.getColor(mContext, R.color.up_color));//行情涨势时蜡烛的标识颜色
320+
barDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));//行情跌势时蜡烛的标识颜色
321+
barDataSet.setIncreasingPaintStyle(Paint.Style.FILL);//蜡烛实心或空心
321322
barDataSet.setDecreasingPaintStyle(Paint.Style.FILL);
322323
return barDataSet;
323324
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public class TimeDataManage {
2828
private double perVolMaxTimeLine = 0;
2929
private SparseArray<String> fiveDayXLabels = new SparseArray<String>();//专用于五日分时横坐标轴刻度
3030
private List<Integer> fiveDayXLabelKey = new ArrayList<Integer>();//专用于五日分时横坐标轴刻度
31-
private String assetId;
32-
private SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
31+
private String assetId;//股票代号
3332
private double preClose;//昨收价
3433

3534
/**
@@ -155,6 +154,7 @@ public double getPreClose() {
155154
public SparseArray<String> getOneDayXLabels(boolean landscape) {
156155
SparseArray<String> xLabels = new SparseArray<String>();
157156
if (assetId.endsWith(".HK")) {
157+
//港股横坐标刻度
158158
if (landscape) {
159159
xLabels.put(0, "09:30");
160160
xLabels.put(60, "10:30");
@@ -170,12 +170,6 @@ public SparseArray<String> getOneDayXLabels(boolean landscape) {
170170
xLabels.put(240, "");
171171
xLabels.put(330, "16:00");
172172
}
173-
} else if (assetId.endsWith(".US")) {
174-
xLabels.put(0, "09:30");
175-
xLabels.put(120, "11:30");
176-
xLabels.put(210, "13:00");
177-
xLabels.put(300, "14:30");
178-
xLabels.put(390, "16:00");
179173
} else {
180174
xLabels.put(0, "09:30");
181175
xLabels.put(60, "10:30");
@@ -201,6 +195,7 @@ public SparseArray<String> getFiveDayXLabels() {
201195
private List<Integer> getFiveDayXLabelKey(String assetId) {
202196
fiveDayXLabelKey.clear();
203197
if (assetId.endsWith(".HK")) {
198+
//港股横坐标刻度
204199
fiveDayXLabelKey.add(0);
205200
fiveDayXLabelKey.add(82);
206201
fiveDayXLabelKey.add(165);

MPChartLib/src/main/java/com/github/mikephil/charting/stockChart/enums/ChartType.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
* 画图类型
55
*/
66
public enum ChartType {
7-
ONE_DAY(241),
8-
FIVE_DAY(305),
9-
HK_ONE_DAY(331),
10-
HK_FIVE_DAY(415),
11-
US_ONE_DAY(390),
12-
US_FIVE_DAY(488),
13-
K_DAY_SMALL(100),
14-
K_WEEK_SMALL(100),
15-
K_MONTH_SMALL(100),
16-
K_DAY_BIG(1000),
17-
K_WEEK_BIG(1000),
18-
K_MONTH_BIG(1000);
7+
ONE_DAY(241),//沪深股票当日分时线总点数
8+
FIVE_DAY(305),//沪深股票五日分时线总点数
9+
HK_ONE_DAY(331),//港股当日分时线总点数
10+
HK_FIVE_DAY(415),//港股五日分时线总点数
11+
US_ONE_DAY(390),//美股当日分时线总点数
12+
US_FIVE_DAY(488),//美股五日分时线总点数
13+
K_DAY_SMALL(100),//竖屏日K展示的总数据条数
14+
K_WEEK_SMALL(100),//竖屏周K展示的总数据条数
15+
K_MONTH_SMALL(100),//竖屏月K展示的总数据条数
16+
K_DAY_BIG(1000),//横屏日K展示的总数据条数
17+
K_WEEK_BIG(1000),//横屏周K展示的总数据条数
18+
K_MONTH_BIG(1000);//横屏月K展示的总数据条数
1919

2020

2121
private int pointNum = 0;

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android {
1010
minSdkVersion 15
1111
targetSdkVersion 28
1212
versionCode 1
13-
versionName "3.1.0"
13+
versionName "3.2.0"
1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
}
1616
buildTypes {

app/src/main/res/layout/activity_stock_detail.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
android:layout_height="wrap_content"
3333
android:layout_margin="20dp"
3434
android:lineSpacingExtra="8dp"
35-
android:text="说明:\n1,竖屏只做展示,不做滑动和放缩等,更多操作请前往横屏页面。\n
36-
2,单击竖屏图表可进入横屏页面,横屏支持缩放,滑动,长按出现高亮十字线,K线副图可以单击切换不同指标。\n
35+
android:text="说明:\n1,支持缩放,滑动,长按出现高亮十字线。\n
36+
2,单击竖屏图表可进入横屏页面。\n
37+
3,K线副图可以单击切换不同指标。\n
3738
3,支持夜间模式。"/>
3839
</LinearLayout>

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
dependencies {
1010
classpath 'com.android.tools.build:gradle:3.4.0'
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12-
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
12+
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'//发布到 jitpack
1313
// NOTE: Do not place your application dependencies here; they belong
1414
// in the individual module build.gradle files
1515
}

0 commit comments

Comments
 (0)