Skip to content

Commit

Permalink
优化图表,竖屏也可以滑动图表,显示高亮十字线
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowh committed Apr 1, 2020
1 parent 702fcc5 commit 647907e
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class BaseChart extends LinearLayout {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private void setShowLabels(boolean isShow) {
*/
public void setDataToChart(TimeDataManage mData) {
this.mData = mData;
cirCleView.setVisibility(landscape?View.VISIBLE:View.GONE);
// cirCleView.setVisibility(landscape?View.VISIBLE:View.GONE);
if (mData.getDatas().size() == 0) {
cirCleView.setVisibility(View.GONE);
lineChart.setNoDataText(getResources().getString(R.string.no_data));
Expand Down Expand Up @@ -364,7 +364,7 @@ public void setDataToChart(TimeDataManage mData) {
Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.fade_fill_color);
d1.setFillDrawable(drawable);
d1.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
d1.setHighlightEnabled(landscape);
d1.setHighlightEnabled(true);
d2.setHighlightEnabled(false);
d1.setDrawCircles(false);
d2.setDrawCircles(false);
Expand All @@ -381,7 +381,7 @@ public void setDataToChart(TimeDataManage mData) {
barDataSet = new BarDataSet(barEntries, "成交量");
barDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
barDataSet.setDrawValues(false);
barDataSet.setHighlightEnabled(landscape);
barDataSet.setHighlightEnabled(true);
barDataSet.setNeutralColor(ContextCompat.getColor(mContext, R.color.equal_color));
barDataSet.setIncreasingColor(ContextCompat.getColor(mContext, R.color.up_color));
barDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));
Expand All @@ -401,7 +401,7 @@ public void setDataToChart(TimeDataManage mData) {
barChart.setViewPortOffsets(left, 0, right, CommonUtil.dip2px(mContext, 15));
} else {
lineChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 15));
barChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), 0, CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 5));
barChart.setViewPortOffsets(CommonUtil.dip2px(mContext, 5), 0, CommonUtil.dip2px(mContext, 5), CommonUtil.dip2px(mContext, 15));
}

axisLeftLine.setAxisMinimum(mData.getMin());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ public void initChart(boolean landscape) {
candleChart.setDrawBorders(true);
candleChart.setBorderWidth(0.7f);
candleChart.setBorderColor(ContextCompat.getColor(mContext, R.color.border_color));
candleChart.setDragEnabled(landscape);
candleChart.setScaleXEnabled(landscape);
candleChart.setScaleYEnabled(false);
candleChart.setDragEnabled(true);//是否可拖动
candleChart.setScaleXEnabled(true);//x轴方向是否可放大缩小
candleChart.setScaleYEnabled(false);//Y轴方向是否可放大缩小
candleChart.setHardwareAccelerationEnabled(true);
Legend mChartKlineLegend = candleChart.getLegend();
mChartKlineLegend.setEnabled(false);
//k线滚动系数设置,控制滚动惯性
candleChart.setDragDecelerationEnabled(true);
candleChart.setDragDecelerationFrictionCoef(0.6f);//0.92持续滚动时的速度快慢,[0,1) 0代表立即停止。
candleChart.setDoubleTapToZoomEnabled(false);
Expand All @@ -112,8 +113,8 @@ public void initChart(boolean landscape) {
barChart.setDrawBorders(true);
barChart.setBorderWidth(0.7f);
barChart.setBorderColor(ContextCompat.getColor(mContext, R.color.border_color));
barChart.setDragEnabled(landscape);
barChart.setScaleXEnabled(landscape);
barChart.setDragEnabled(true);
barChart.setScaleXEnabled(true);
barChart.setScaleYEnabled(false);
barChart.setHardwareAccelerationEnabled(true);
Legend mChartChartsLegend = barChart.getLegend();
Expand Down Expand Up @@ -266,12 +267,6 @@ public void setDataToChart(KLineDataManage data) {

if (data.getAssetId().endsWith(".HK") && !data.getAssetId().contains("IDX")) {
setPrecision(3);
} else if (data.getAssetId().endsWith(".US")) {
if (Math.abs(data.getPreClosePrice()) < 1) {
setPrecision(4);
} else {
setPrecision(2);
}
} else {
setPrecision(2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public OneDayChart(Context context, @Nullable AttributeSet attrs) {
public void initChart(boolean landscape) {
this.landscape = landscape;
//主图
lineChart.setScaleEnabled(false);
lineChart.setDrawBorders(true);
lineChart.setScaleEnabled(false);//是否可以缩放
lineChart.setDrawBorders(true);//是否画外框线
lineChart.setBorderColor(ContextCompat.getColor(mContext, R.color.border_color));
lineChart.setBorderWidth(0.7f);
lineChart.setNoDataText(getResources().getString(R.string.loading));
Expand All @@ -127,18 +127,19 @@ public void initChart(boolean landscape) {
xAxisLine = (TimeXAxis) lineChart.getXAxis();
xAxisLine.setDrawAxisLine(false);
xAxisLine.setTextColor(ContextCompat.getColor(mContext, R.color.label_text));
xAxisLine.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxisLine.setPosition(XAxis.XAxisPosition.BOTTOM);//x轴刻度值显示在底部
xAxisLine.setAvoidFirstLastClipping(true);
xAxisLine.setGridColor(ContextCompat.getColor(mContext, R.color.grid_color));
xAxisLine.setGridLineWidth(0.7f);

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

//主图右Y轴
axisRightLine = lineChart.getAxisRight();
axisRightLine.setLabelCount(5, true);
axisRightLine.setLabelCount(5, true);//Y轴右边分多少个刻度
axisRightLine.setDrawTopBottomGridLine(false);
axisRightLine.setDrawGridLines(true);
axisRightLine.setGridLineWidth(0.7f);
//虚线
axisRightLine.enableGridDashedLine(CommonUtil.dip2px(mContext, 4), CommonUtil.dip2px(mContext, 3), 0);
axisRightLine.setDrawAxisLine(false);
axisRightLine.setValueLineInside(true);
Expand Down Expand Up @@ -265,7 +267,7 @@ private void setShowLabels(boolean isShow) {
*/
public void setDataToChart(TimeDataManage mData) {
this.mData = mData;
cirCleView.setVisibility(landscape ? View.VISIBLE : View.GONE);
// cirCleView.setVisibility(landscape ? View.VISIBLE : View.GONE);
if (mData.getDatas().size() == 0) {
cirCleView.setVisibility(View.GONE);
lineChart.setNoDataText(getResources().getString(R.string.no_data));
Expand Down Expand Up @@ -311,13 +313,6 @@ public void setDataToChart(TimeDataManage mData) {
if (mData.getAssetId().endsWith(".HK")) {
setPrecision(mData.getAssetId().contains("IDX") ? 2 : 3);
setMaxCount(ChartType.HK_ONE_DAY.getPointNum());
} else if (mData.getAssetId().endsWith(".US")) {
if (Math.abs(mData.getMax()) < 1) {
setPrecision(4);
} else {
setPrecision(2);
}
setMaxCount(ChartType.US_ONE_DAY.getPointNum());
} else {
setPrecision(2);
setMaxCount(ChartType.ONE_DAY.getPointNum());
Expand Down Expand Up @@ -368,7 +363,7 @@ public void setDataToChart(TimeDataManage mData) {
Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.fade_fill_color);
d1.setFillDrawable(drawable);
d1.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
d1.setHighlightEnabled(landscape);
d1.setHighlightEnabled(true);//是否显示高亮十字线
d2.setHighlightEnabled(false);
d1.setDrawCircles(false);
d2.setDrawCircles(false);
Expand All @@ -384,7 +379,7 @@ public void setDataToChart(TimeDataManage mData) {

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

axisLeftLine.setAxisMinimum(mData.getMin());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class KLineDataManage {
private Context mContext;
private ArrayList<KLineDataModel> kDatas = new ArrayList<>();
private float offSet = 0f;//K线图最右边偏移量
private String assetId;
private String assetId;//股票代号
private boolean landscape = false;//横屏还是竖屏

//MA参数
Expand Down Expand Up @@ -236,8 +236,8 @@ public void initRSI() {

private CandleDataSet setACandle(ArrayList<CandleEntry> candleEntries) {
CandleDataSet candleDataSet = new CandleDataSet(candleEntries, "蜡烛线");
candleDataSet.setDrawHorizontalHighlightIndicator(landscape);
candleDataSet.setHighlightEnabled(landscape);
candleDataSet.setDrawHorizontalHighlightIndicator(true);
candleDataSet.setHighlightEnabled(true);
candleDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
candleDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
candleDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));
Expand All @@ -255,7 +255,7 @@ private CandleDataSet setACandle(ArrayList<CandleEntry> candleEntries) {
private CandleDataSet setBOLLCandle(ArrayList<CandleEntry> candleEntries) {
CandleDataSet candleDataSet = new CandleDataSet(candleEntries, "BOLL叠加蜡烛线");
candleDataSet.setDrawHorizontalHighlightIndicator(false);
candleDataSet.setHighlightEnabled(landscape);
candleDataSet.setHighlightEnabled(true);
candleDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
candleDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
candleDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));
Expand Down Expand Up @@ -283,13 +283,13 @@ private LineDataSet setALine(ColorType ma, ArrayList<Entry> lineEntries, String
boolean highlightEnable = false;
return setALine(ma, lineEntries, label, highlightEnable);
}

//行情走势线属性设置
private LineDataSet setALine(ColorType colorType, ArrayList<Entry> lineEntries, String label, boolean highlightEnable) {
LineDataSet lineDataSetMa = new LineDataSet(lineEntries, label);
lineDataSetMa.setDrawHorizontalHighlightIndicator(false);
lineDataSetMa.setHighlightEnabled(landscape?highlightEnable:landscape);
lineDataSetMa.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
lineDataSetMa.setDrawValues(false);
lineDataSetMa.setHighlightEnabled(highlightEnable);//是否画高亮十字线
lineDataSetMa.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));//高亮十字线颜色
lineDataSetMa.setDrawValues(false);//是否画出每个蜡烛线的数值
if (colorType == ColorType.blue) {
lineDataSetMa.setColor(ContextCompat.getColor(mContext, R.color.ma5));
} else if (colorType == ColorType.yellow) {
Expand All @@ -308,16 +308,17 @@ private BarDataSet setABar(ArrayList<BarEntry> barEntries) {
return setABar(barEntries, label);
}

//蜡烛线属性设置
private BarDataSet setABar(ArrayList<BarEntry> barEntries, String label) {
BarDataSet barDataSet = new BarDataSet(barEntries, label);
barDataSet.setHighlightEnabled(landscape);
barDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));
barDataSet.setHighlightEnabled(true);//是否画高亮十字线
barDataSet.setHighLightColor(ContextCompat.getColor(mContext, R.color.highLight_Color));//高亮十字线颜色
barDataSet.setValueTextSize(10);
barDataSet.setDrawValues(false);
barDataSet.setNeutralColor(ContextCompat.getColor(mContext, R.color.equal_color));
barDataSet.setIncreasingColor(ContextCompat.getColor(mContext, R.color.up_color));
barDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));
barDataSet.setIncreasingPaintStyle(Paint.Style.FILL);
barDataSet.setDrawValues(false);//是否画出每个蜡烛线的数值
barDataSet.setNeutralColor(ContextCompat.getColor(mContext, R.color.equal_color));//行情平势时蜡烛的标识颜色
barDataSet.setIncreasingColor(ContextCompat.getColor(mContext, R.color.up_color));//行情涨势时蜡烛的标识颜色
barDataSet.setDecreasingColor(ContextCompat.getColor(mContext, R.color.down_color));//行情跌势时蜡烛的标识颜色
barDataSet.setIncreasingPaintStyle(Paint.Style.FILL);//蜡烛实心或空心
barDataSet.setDecreasingPaintStyle(Paint.Style.FILL);
return barDataSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class TimeDataManage {
private double perVolMaxTimeLine = 0;
private SparseArray<String> fiveDayXLabels = new SparseArray<String>();//专用于五日分时横坐标轴刻度
private List<Integer> fiveDayXLabelKey = new ArrayList<Integer>();//专用于五日分时横坐标轴刻度
private String assetId;
private SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
private String assetId;//股票代号
private double preClose;//昨收价

/**
Expand Down Expand Up @@ -155,6 +154,7 @@ public double getPreClose() {
public SparseArray<String> getOneDayXLabels(boolean landscape) {
SparseArray<String> xLabels = new SparseArray<String>();
if (assetId.endsWith(".HK")) {
//港股横坐标刻度
if (landscape) {
xLabels.put(0, "09:30");
xLabels.put(60, "10:30");
Expand All @@ -170,12 +170,6 @@ public SparseArray<String> getOneDayXLabels(boolean landscape) {
xLabels.put(240, "");
xLabels.put(330, "16:00");
}
} else if (assetId.endsWith(".US")) {
xLabels.put(0, "09:30");
xLabels.put(120, "11:30");
xLabels.put(210, "13:00");
xLabels.put(300, "14:30");
xLabels.put(390, "16:00");
} else {
xLabels.put(0, "09:30");
xLabels.put(60, "10:30");
Expand All @@ -201,6 +195,7 @@ public SparseArray<String> getFiveDayXLabels() {
private List<Integer> getFiveDayXLabelKey(String assetId) {
fiveDayXLabelKey.clear();
if (assetId.endsWith(".HK")) {
//港股横坐标刻度
fiveDayXLabelKey.add(0);
fiveDayXLabelKey.add(82);
fiveDayXLabelKey.add(165);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
* 画图类型
*/
public enum ChartType {
ONE_DAY(241),
FIVE_DAY(305),
HK_ONE_DAY(331),
HK_FIVE_DAY(415),
US_ONE_DAY(390),
US_FIVE_DAY(488),
K_DAY_SMALL(100),
K_WEEK_SMALL(100),
K_MONTH_SMALL(100),
K_DAY_BIG(1000),
K_WEEK_BIG(1000),
K_MONTH_BIG(1000);
ONE_DAY(241),//沪深股票当日分时线总点数
FIVE_DAY(305),//沪深股票五日分时线总点数
HK_ONE_DAY(331),//港股当日分时线总点数
HK_FIVE_DAY(415),//港股五日分时线总点数
US_ONE_DAY(390),//美股当日分时线总点数
US_FIVE_DAY(488),//美股五日分时线总点数
K_DAY_SMALL(100),//竖屏日K展示的总数据条数
K_WEEK_SMALL(100),//竖屏周K展示的总数据条数
K_MONTH_SMALL(100),//竖屏月K展示的总数据条数
K_DAY_BIG(1000),//横屏日K展示的总数据条数
K_WEEK_BIG(1000),//横屏周K展示的总数据条数
K_MONTH_BIG(1000);//横屏月K展示的总数据条数


private int pointNum = 0;
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "3.1.0"
versionName "3.2.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_stock_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:lineSpacingExtra="8dp"
android:text="说明:\n1,竖屏只做展示,不做滑动和放缩等,更多操作请前往横屏页面。\n
2,单击竖屏图表可进入横屏页面,横屏支持缩放,滑动,长按出现高亮十字线,K线副图可以单击切换不同指标。\n
android:text="说明:\n1,支持缩放,滑动,长按出现高亮十字线。\n
2,单击竖屏图表可进入横屏页面。\n
3,K线副图可以单击切换不同指标。\n
3,支持夜间模式。"/>
</LinearLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'//发布到 jitpack
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down

0 comments on commit 647907e

Please sign in to comment.