MpChart使用

下载MpChartLib

package com.smarttrust.gupiao;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.google.gson.Gson;
import com.smarttrust.gupiao.CallBack.HangqingCallBack;
import com.smarttrust.gupiao.Model.HangqingModel;
import com.smarttrust.gupiao.bean.KLineBean;
import com.smarttrust.gupiao.bean.KLineMa1Bean;
import com.smarttrust.gupiao.bean.KLineMa2Bean;
import com.smarttrust.gupiao.utils.Constant;
import com.smarttrust.gupiao.utils.LogUtils;
import com.smarttrust.gupiao.utils.NetWorkUtils;
import com.smarttrust.gupiao.utils.StringUtils;
import com.smarttrust.gupiao.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Response;

public class HangqingZongxinLandscapeActivity extends JichuActivity implements View.OnClickListener {
    private CombinedChart combinedchart;
    private BarChart barChart;
    XAxis xAxisBar, xAxisK;
    YAxis axisLeftBar, axisLeftK;
    YAxis axisRightBar, axisRightK;
    BarDataSet barDataSet;
    BarDataSet barDataSet1;
    private TextView one, two, three, four, five;
    private TextView bank_name, unit_tv, bank_num, shoupanprice, high, open, low, lang;
    private ImageView imagezhangdie, fanhui;
    private ArrayList<KLineBean> kDatas = new ArrayList<>();
    private ArrayList<KLineMa1Bean> kLineMa1data = new ArrayList<>();
    private ArrayList<KLineMa2Bean> kLineMa2data = new ArrayList<>();
    private ArrayList<KLineMa2Bean> zhureddata = new ArrayList<>();
    private ArrayList<KLineMa2Bean> zhublackdata = new ArrayList<>();
    private ArrayList<KLineMa2Bean> zhualldata = new ArrayList<>();
    private String dataurl = Constant.Url + "info/getStockInfo.do";
    private String symbol_code = "999999";
    private int stock_type = 1;
    private float volmax;
    private SparseArray<String> xValuesLabel = new SparseArray<>();
    private int first = 0;
    private float firstbar = 0;
    private float firstline = 0;
    private Handler handlertime = new Handler();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            barChart.setAutoScaleMinMaxEnabled(true);
            combinedchart.setAutoScaleMinMaxEnabled(true);

            combinedchart.notifyDataSetChanged();
            barChart.notifyDataSetChanged();

            combinedchart.invalidate();
            barChart.invalidate();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hangqing_zhongxin_land);
        initview();
        initChart();
        getOffLineData(symbol_code);

    }

    @Override
    public void onResume() {
        super.onResume();
        handlertime.postDelayed(runnable, 60 * 1000); //每隔1s执行
    }

    @Override
    public void onPause() {
        super.onPause();
        handlertime.removeCallbacks(runnable);
    }

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            // handler自带方法实现定时器
            try {
                handlertime.postDelayed(this, 60 * 1000);
                getOffLineData(symbol_code);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    private void initview() {
        Intent intent = getIntent();
        if (intent != null) {
            symbol_code = intent.getStringExtra("symbol_code");
            stock_type = intent.getIntExtra("stock_type", 0);
        }
        one = (TextView) findViewById(R.id.one);
        two = (TextView) findViewById(R.id.two);
        three = (TextView) findViewById(R.id.three);
        four = (TextView) findViewById(R.id.four);
        five = (TextView) findViewById(R.id.five);
        bank_name = (TextView) findViewById(R.id.bank_name);
        unit_tv = (TextView) findViewById(R.id.unit_tv);
        shoupanprice = (TextView) findViewById(R.id.shoupanprice);
        bank_num = (TextView) findViewById(R.id.bank_num);
        high = (TextView) findViewById(R.id.high);
        open = (TextView) findViewById(R.id.open);
        low = (TextView) findViewById(R.id.low);
        lang = (TextView) findViewById(R.id.lang);
        imagezhangdie = (ImageView) findViewById(R.id.imagezhangdie);
        fanhui = (ImageView) findViewById(R.id.fanhui);
        combinedchart = (CombinedChart) findViewById(R.id.combinedchart);
        barChart = (BarChart) findViewById(R.id.barchart);

        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        fanhui.setOnClickListener(this);


        if (stock_type == 1) {
            one.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
            two.setBackgroundColor(getResources().getColor(R.color.white));
            three.setBackgroundColor(getResources().getColor(R.color.white));
            four.setBackgroundColor(getResources().getColor(R.color.white));
            five.setBackgroundColor(getResources().getColor(R.color.white));
        } else if (stock_type == 2) {
            two.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
            one.setBackgroundColor(getResources().getColor(R.color.white));
            three.setBackgroundColor(getResources().getColor(R.color.white));
            four.setBackgroundColor(getResources().getColor(R.color.white));
            five.setBackgroundColor(getResources().getColor(R.color.white));
        } else if (stock_type == 3) {
            three.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
            two.setBackgroundColor(getResources().getColor(R.color.white));
            one.setBackgroundColor(getResources().getColor(R.color.white));
            four.setBackgroundColor(getResources().getColor(R.color.white));
            five.setBackgroundColor(getResources().getColor(R.color.white));
        } else if (stock_type == 4) {
            four.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
            two.setBackgroundColor(getResources().getColor(R.color.white));
            three.setBackgroundColor(getResources().getColor(R.color.white));
            one.setBackgroundColor(getResources().getColor(R.color.white));
            five.setBackgroundColor(getResources().getColor(R.color.white));
            stock_type = 4;
        } else if (stock_type == 5) {
            five.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
            two.setBackgroundColor(getResources().getColor(R.color.white));
            three.setBackgroundColor(getResources().getColor(R.color.white));
            four.setBackgroundColor(getResources().getColor(R.color.white));
            one.setBackgroundColor(getResources().getColor(R.color.white));
        }

    }


    private void initChart() {
        barChart.setDrawBorders(true);
        barChart.setBorderWidth(1);
        barChart.setBorderColor(Color.rgb(94, 94, 94));
        Description d = new Description();
        d.setText("");
        barChart.setDescription(d);
        barChart.setDragEnabled(true);
        barChart.setScaleYEnabled(false);
        Legend barChartLegend = barChart.getLegend();
        barChartLegend.setEnabled(false);

        //BarYAxisFormatter  barYAxisFormatter=new BarYAxisFormatter();
        //bar x y        xAxisBar = barChart.getXAxis();
        xAxisBar.setDrawLabels(true);
        xAxisBar.setDrawGridLines(false);
        xAxisBar.setDrawAxisLine(false);
        xAxisBar.setTextColor(getResources().getColor(R.color.white));
        xAxisBar.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxisBar.setGridColor(getResources().getColor(R.color.minute_grayLine));

        axisLeftBar = barChart.getAxisLeft();
        axisLeftBar.setAxisMinValue(0);
        axisLeftBar.setDrawGridLines(false);
        axisLeftBar.setDrawAxisLine(false);
        axisLeftBar.setDrawLabels(false);
        axisLeftBar.setTextColor(getResources().getColor(R.color.minute_zhoutv));


        axisRightBar = barChart.getAxisRight();
        axisRightBar.setDrawLabels(true);
        axisRightBar.setTextSize(8);
        axisRightBar.setDrawGridLines(true);
        axisRightBar.setDrawAxisLine(true);
        axisRightBar.setGridColor(getResources().getColor(R.color.grayLine));//横线
        axisRightBar.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        /****************************************************************/
        combinedchart.setDrawBorders(true);
        combinedchart.setBorderWidth(1);
        combinedchart.setBorderColor(Color.rgb(94, 94, 94));
        combinedchart.setDescription(d);
        combinedchart.setDragEnabled(true);
        combinedchart.setScaleYEnabled(false);
        // draw bars behind lines
        combinedchart.setDrawOrder(new CombinedChart.DrawOrder[]{
                CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.BUBBLE, CombinedChart.DrawOrder.CANDLE, CombinedChart.DrawOrder.LINE, CombinedChart.DrawOrder.SCATTER
        });

        Legend combinedchartLegend = combinedchart.getLegend();
        combinedchartLegend.setEnabled(false);
        //bar x y        xAxisK = combinedchart.getXAxis();
        xAxisK.setDrawLabels(true);
        xAxisK.setDrawGridLines(false);
        xAxisK.setDrawAxisLine(false);
        xAxisK.setTextColor(getResources().getColor(R.color.white));
        xAxisK.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxisK.setGridColor(getResources().getColor(R.color.minute_grayLine));

        axisLeftK = combinedchart.getAxisLeft();
        axisLeftK.setDrawGridLines(false);
        axisLeftK.setDrawAxisLine(false);
        axisLeftK.setDrawLabels(false);
        axisLeftK.setTextColor(getResources().getColor(R.color.minute_zhoutv));
        axisLeftK.setGridColor(getResources().getColor(R.color.minute_grayLine));
        axisLeftK.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);

        axisRightK = combinedchart.getAxisRight();
        axisRightK.setDrawLabels(true);
        axisRightK.setDrawGridLines(true);
        axisRightK.setDrawAxisLine(true);
        axisRightK.setTextSize(8);
        axisRightK.setGridColor(getResources().getColor(R.color.grayLine));
        axisRightK.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);

        combinedchart.setDragDecelerationEnabled(true);
        barChart.setDragDecelerationEnabled(true);
        combinedchart.setDragDecelerationFrictionCoef(0.2f);
        barChart.setDragDecelerationFrictionCoef(0.2f);
    }


    private float culcMaxscale(float count) {
        float max = 1;
        max = count / 127 * 5;
        return max;
    }

    private void setData(ArrayList<KLineBean> kDatas) {
        ArrayList<String> xVals = new ArrayList<>();
        ArrayList<String> xValszhu = new ArrayList<>();
        ArrayList<BarEntry> barEntries = new ArrayList<>();
        ArrayList<BarEntry> barEntries2 = new ArrayList<>();
        ArrayList<CandleEntry> candleEntries = new ArrayList<>();
        ArrayList<Entry> line5Entries = new ArrayList<>();
        ArrayList<Entry> line10Entries = new ArrayList<>();

        if (xVals != null && xVals.size() > 0) {
            xVals.clear();
        }
        if (xValszhu != null && xValszhu.size() > 0) {
            xValszhu.clear();
        }
        if (barEntries != null && barEntries.size() > 0) {
            barEntries.clear();
        }
        if (barEntries2 != null && barEntries2.size() > 0) {
            barEntries2.clear();
        }
        if (candleEntries != null && candleEntries.size() > 0) {
            xVals.clear();
        }
        if (line5Entries != null && line5Entries.size() > 0) {
            xVals.clear();
        }
        if (line10Entries != null && line10Entries.size() > 0) {
            xVals.clear();
        }

        for (int i = 0; i < zhualldata.size(); i++) {
            xValszhu.add(zhualldata.get(i).data + "");
            if (zhualldata.get(i).color.equals("red")) {
                barEntries.add(new BarEntry(i, zhualldata.get(i).vol));
            } else {
                barEntries2.add(new BarEntry(i, zhualldata.get(i).vol));
            }
        }
        for (int i = 0; i < kDatas.size(); i++) {
            xVals.add(kDatas.get(i).date + "");
            candleEntries.add(new CandleEntry(i, kDatas.get(i).high, kDatas.get(i).low, kDatas.get(i).open, kDatas.get(i).close));
        }
        ArrayList<String> xVals1 = new ArrayList<>();
        ArrayList<String> xVals2 = new ArrayList<>();
        if (xVals1 != null && xVals1.size() > 0) {
            xVals1.clear();
        }
        if (xVals2 != null && xVals2.size() > 0) {
            xVals2.clear();
        }
        for (int i = 0; i < kLineMa1data.size(); i++) {
            xVals1.add(kLineMa1data.get(i).data);
            line5Entries.add(new Entry(i, kLineMa1data.get(i).vol));
        }
        for (int i = 0; i < kLineMa2data.size(); i++) {
            xVals2.add(kLineMa2data.get(i).data);
            line10Entries.add(new Entry(i, kLineMa2data.get(i).vol));
        }

        barDataSet = new BarDataSet(barEntries2, "set1");
        barDataSet.setColor(Color.BLACK);
        barDataSet.setDrawValues(false);
        barDataSet.setBarBorderWidth(0.5f);
        barDataSet.setBarBorderColor(Color.BLACK);

        barDataSet1 = new BarDataSet(barEntries, "set2");
        barDataSet1.setColor(Color.WHITE);
        barDataSet1.setDrawValues(false);
        barDataSet1.setBarBorderWidth(0.5f);
        barDataSet1.setBarBorderColor(Color.rgb(255, 78, 78));
        BarData barData = new BarData(barDataSet, barDataSet1);
        barData.setBarWidth(0.8f);
        barChart.setData(barData);
        if (first == 1) {
            final ViewPortHandler viewPortHandlerBar = barChart.getViewPortHandler();
            viewPortHandlerBar.setMaximumScaleX(culcMaxscale(xValszhu.size()));
            Matrix touchmatrix = viewPortHandlerBar.getMatrixTouch();
            final float xscale = 1;
            touchmatrix.postScale(xscale, 1f);
        }

        CandleDataSet candleDataSet = new CandleDataSet(candleEntries, "KLine");

        candleDataSet.setShadowColor(Color.DKGRAY);
        candleDataSet.setShadowWidth(0.7f);
        candleDataSet.setDecreasingColor(Color.BLACK);
        candleDataSet.setDecreasingPaintStyle(Paint.Style.FILL);
        candleDataSet.setIncreasingColor(Color.rgb(2, 0, 4));
        candleDataSet.setIncreasingPaintStyle(Paint.Style.STROKE);
        candleDataSet.setValueTextSize(10f);
        candleDataSet.setColor(Color.rgb(2, 0, 4));
        candleDataSet.setDrawValues(false);
        candleDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
        CandleData candleData = new CandleData(candleDataSet);

        ArrayList<ILineDataSet> sets = new ArrayList<>();
        sets.add(setMaLine(5, xVals1, line5Entries));
        sets.add(setMaLine(10, xVals2, line10Entries));


        CombinedData combinedData = new CombinedData();
        LineData lineData = new LineData(sets);
        combinedData.setData(candleData);// 带东西的线的数据
        combinedData.setData(lineData);//线的数据三条线
        combinedchart.setData(combinedData);
        combinedchart.moveViewToX(kDatas.size() - 1);
        if (first == 1) {
            final ViewPortHandler viewPortHandlerCombin = combinedchart.getViewPortHandler();
            viewPortHandlerCombin.setMaximumScaleX(culcMaxscale(xVals.size()));
            Matrix matrixCombin = viewPortHandlerCombin.getMatrixTouch();
            final float xscaleCombin = 1;
            matrixCombin.postScale(xscaleCombin, 1f);
        }


        combinedchart.moveViewToX(kDatas.size() - 1);
        barChart.moveViewToX(kDatas.size() - 1);
        setOffset();

/****************************************************************************************
 此处解决方法来源于CombinedChartDemok线图y轴显示问题,图表滑动后才能对齐的bug,希望有人给出解决方法
 (注:此bug现已修复,感谢和chenguang79一起研究)
 ****************************************************************************************/

        handler.sendEmptyMessageDelayed(0, 300);

    }

    @NonNull
    private LineDataSet setMaLine(int ma, ArrayList<String> xVals, ArrayList<Entry> lineEntries) {
        LineDataSet lineDataSetMa = new LineDataSet(lineEntries, "ma" + ma);
        if (ma == 5) {
            lineDataSetMa.setHighlightEnabled(true);
            lineDataSetMa.setDrawHorizontalHighlightIndicator(false);
            lineDataSetMa.setHighLightColor(Color.WHITE);
        } else {/*此处必须得写*/
            lineDataSetMa.setHighlightEnabled(false);
        }
        lineDataSetMa.setDrawValues(false);
        if (ma == 5) {//TODO 颜色值
            lineDataSetMa.setColor(Color.rgb(255, 78, 78));//红线

        } else if (ma == 10) {
            lineDataSetMa.setColor(Color.rgb(67, 208, 255));//蓝线
        }
//        else {
//            lineDataSetMa.setColor(Color.YELLOW);
//        }
        lineDataSetMa.setLineWidth(1f);
        lineDataSetMa.setDrawCircles(false);
        lineDataSetMa.setAxisDependency(YAxis.AxisDependency.RIGHT);
        return lineDataSetMa;
    }

    //    设置量表对齐
    private void setOffset() {
        if (first == 1) {
            float lineLeft = combinedchart.getViewPortHandler().offsetLeft();
            float barLeft = barChart.getViewPortHandler().offsetLeft();
            float lineRight = combinedchart.getViewPortHandler().offsetRight();
            float barRight = barChart.getViewPortHandler().offsetRight();
            float barBottom = barChart.getViewPortHandler().offsetBottom();
            float offsetLeft, offsetRight;
            float transLeft = 0, transRight = 0;
// 注:setExtraLeft...函数是针对图表相对位置计算,比如AoffLeftA=20dp,BoffLeftB=30dp,A.setExtraLeftOffset(10),并不是30,还有注意单位转换
            if (barLeft < lineLeft) {
                offsetLeft = Utils.convertPixelsToDp(lineLeft - barLeft);
                barChart.setExtraLeftOffset(offsetLeft);
                transLeft = lineLeft;
            } else {
                offsetLeft = Utils.convertPixelsToDp(barLeft - lineLeft);
                combinedchart.setExtraLeftOffset(offsetLeft);
                transLeft = barLeft;
            }
//  注:setExtraRight...函数是针对图表绝对位置计算,比如AoffRightA=20dp,BoffRightB=30dp,A.setExtraLeftOffset(30),并不是10,还有注意单位转换
            if (barRight < lineRight) {
                offsetRight = Utils.convertPixelsToDp(lineRight);
                barChart.setExtraRightOffset(offsetRight);
                transRight = lineRight;
            } else {
                offsetRight = Utils.convertPixelsToDp(barRight);
                combinedchart.setExtraRightOffset(offsetRight);
                transRight = barRight;
            }

            firstbar = transRight;
            firstline = transRight;
        }

        barChart.setViewPortOffsets(0, 5, firstbar, 0);
        combinedchart.setViewPortOffsets(0, 0, firstline, 0);
    }

    private void getOffLineData(final String symbol_code) {
        if (NetWorkUtils.isNetworkConnected(HangqingZongxinLandscapeActivity.this)) {
            OkHttpUtils
                    .get().addParams("symbol_code", symbol_code).addParams("stock_length", 120 + "").addParams("stock_type", stock_type + "")
                    .url(dataurl)
                    .build()
                    .execute(new HangqingCallBack() {
                        @Override
                        public HangqingModel parseNetworkResponse(Response response, int i) throws Exception {
                            String string = response.body().string();
                            LogUtils.LOGI("tag", string);
                            LogUtils.LOGI("tag==data==", response.toString());
                            HangqingModel model = new Gson().fromJson(string, HangqingModel.class);
                            return model;
                        }

                        @Override
                        public void onError(Call call, Exception e, int i) {
                            LogUtils.LOGE("error", e.toString());
                            if (!symbol_code.equals(""))
                                ToastUtils.shortToast(HangqingZongxinLandscapeActivity.this, "获取数据失败,请稍后重试");
                        }

                        @Override
                        public void onResponse(HangqingModel model, int i) {
                            first = first + 1;
                            if (zhualldata != null && zhualldata.size() > 0) {
                                zhualldata.clear();
                            }
                            if (kDatas != null && kDatas.size() > 0) {
                                kDatas.clear();
                            }
                            if (kLineMa1data != null && kLineMa1data.size() > 0) {
                                kLineMa1data.clear();
                            }
                            if (kLineMa2data != null && kLineMa2data.size() > 0) {
                                kLineMa2data.clear();
                            }
                            if (zhureddata != null && zhureddata.size() > 0) {
                                zhureddata.clear();
                            }
                            if (zhublackdata != null && zhublackdata.size() > 0) {
                                zhublackdata.clear();
                            }
                            if (model.status == 1 && model.data != null) {
                                if (StringUtils.isEmpty(model.data.unit)) {
                                    unit_tv.setText(model.data.unit);
                                }
                                if (model.data.stock != null) {
                                    bank_name.setText(model.data.stock.S_NAME);
                                    bank_num.setText(model.data.stock.S_CODE);
                                    DecimalFormat format = new DecimalFormat("#0.00");
                                    format.format(model.data.stock.CURRENT_CLOSE);
                                    shoupanprice.setText(format.format(model.data.stock.CURRENT_CLOSE));
                                    high.setText(format.format(model.data.stock.TODAY_MAX));
                                    low.setText(format.format(model.data.stock.TODAY_MIN));
                                    lang.setText(model.data.stock.TODAY_VOLUME + "");
                                    open.setText(format.format(model.data.stock.TODAY_OPEN));
                                    if (model.data.stock.LATEST_CHANGE_RATIO >= 0) {
                                        shoupanprice.setTextColor(getResources().getColor(R.color.red));
                                        high.setTextColor(getResources().getColor(R.color.red));
                                        low.setTextColor(getResources().getColor(R.color.red));
                                        open.setTextColor(getResources().getColor(R.color.red));
                                        lang.setTextColor(getResources().getColor(R.color.red));
                                        imagezhangdie.setBackgroundResource(R.drawable.zhinengxuangu_shang);
                                    } else {
                                        shoupanprice.setTextColor(getResources().getColor(R.color.green));
                                        high.setTextColor(getResources().getColor(R.color.green));
                                        low.setTextColor(getResources().getColor(R.color.green));
                                        open.setTextColor(getResources().getColor(R.color.green));
                                        lang.setTextColor(getResources().getColor(R.color.green));
                                        imagezhangdie.setBackgroundResource(R.drawable.zhinengxuangu_xia);
                                    }
                                }
                                if (model.data.draw != null) {
                                    if (model.data.draw.up != null) {
                                        if (model.data.draw.up.k != null) {
                                            ArrayList<ArrayList<Object>> kdata = model.data.draw.up.k;
                                            ArrayList<ArrayList<Object>> m1data = model.data.draw.up.ma1;
                                            ArrayList<ArrayList<Object>> m2data = model.data.draw.up.ma2;
                                            int count = model.data.draw.up.k.size();
                                            int countm1 = model.data.draw.up.ma1.size();
                                            int countm2 = model.data.draw.up.ma2.size();
                                            for (int n = 0; n < count; n++) {
                                                KLineBean kLineData = new KLineBean();
                                                kLineData.date = kdata.get(n).get(0) + "";
                                                kLineData.open = Float.parseFloat(kdata.get(n).get(1) + "");
                                                kLineData.close = Float.parseFloat(kdata.get(n).get(2) + "");
                                                kLineData.high = Float.parseFloat(kdata.get(n).get(3) + "");
                                                kLineData.low = Float.parseFloat(kdata.get(n).get(4) + "");
//                                            kLineData.vol = (float) kdata.get(n).get(1);
//                                            volmax = Math.max(kLineData.vol, volmax);
//                                            xValuesLabel.put(i, kLineData.date);
                                                kDatas.add(kLineData);
                                            }
                                            for (int n = 0; n < countm1; n++) {
                                                KLineMa1Bean kLineData = new KLineMa1Bean();
                                                kLineData.vol = Float.parseFloat(m1data.get(n).get(1) + "");
                                                kLineData.data = m1data.get(n).get(0) + "";
//                                            volmax = Math.max(kLineData.vol, volmax);
//                                            xValuesLabel.put(i, kLineData.date);
                                                kLineMa1data.add(kLineData);
                                            }
                                            for (int n = 0; n < countm2; n++) {
                                                KLineMa2Bean kLineData = new KLineMa2Bean();
                                                kLineData.vol = Float.parseFloat(m2data.get(n).get(1) + "");
                                                kLineData.data = m2data.get(n).get(0) + "";
//                                            volmax = Math.max(kLineData.vol, volmax);
//                                            xValuesLabel.put(i, kLineData.date);
                                                kLineMa2data.add(kLineData);
                                            }
                                        }
                                    }
                                    if (model.data.draw.down != null) {
                                        if (model.data.draw.down.v1 != null) {
                                            ArrayList<ArrayList<Object>> zhudata = model.data.draw.down.v1;
                                            for (int n = 0; n < zhudata.size(); n++) {
                                                KLineMa2Bean kLineData = new KLineMa2Bean();
                                                kLineData.vol = Float.parseFloat(zhudata.get(n).get(1) + "");
                                                kLineData.data = zhudata.get(n).get(0) + "";
                                                volmax = Math.max(kLineData.vol, volmax);
                                                xValuesLabel.put(n, kLineData.data);
                                                zhureddata.add(kLineData);
                                            }
                                        }
                                        if (model.data.draw.down.v2 != null) {
                                            ArrayList<ArrayList<Object>> zhudata = model.data.draw.down.v2;
                                            for (int n = 0; n < zhudata.size(); n++) {
                                                KLineMa2Bean kLineData = new KLineMa2Bean();
                                                kLineData.vol = Float.parseFloat(zhudata.get(n).get(1) + "");
                                                kLineData.data = zhudata.get(n).get(0) + "";
                                                volmax = Math.max(kLineData.vol, volmax);
                                                xValuesLabel.put(n, kLineData.data);
                                                zhublackdata.add(kLineData);
                                            }
                                        }

                                        if (model.data.draw.down.v != null) {
                                            ArrayList<ArrayList<Object>> zhudata = model.data.draw.down.v;
                                            for (int n = 0; n < zhudata.size(); n++) {
                                                KLineMa2Bean kLineData = new KLineMa2Bean();
                                                kLineData.vol = Float.parseFloat(zhudata.get(n).get(1) + "");
                                                kLineData.data = zhudata.get(n).get(0) + "";
                                                kLineData.color = zhudata.get(n).get(2) + "";
                                                volmax = Math.max(kLineData.vol, volmax);
                                                xValuesLabel.put(n, kLineData.data);
                                                zhualldata.add(kLineData);
                                            }
                                        }
                                    }
                                    setData(kDatas);
                                } else {
                                    ToastUtils.shortToast(HangqingZongxinLandscapeActivity.this, "暂时没有该只股票的数据");
                                }
                            }
                        }

                    });
        } else {
            Toast.makeText(HangqingZongxinLandscapeActivity.this, "当前网络不可用", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.one:
                one.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
                two.setBackgroundColor(getResources().getColor(R.color.white));
                three.setBackgroundColor(getResources().getColor(R.color.white));
                four.setBackgroundColor(getResources().getColor(R.color.white));
                five.setBackgroundColor(getResources().getColor(R.color.white));
                stock_type = 1;
                getOffLineData(symbol_code);
                break;
            case R.id.two:
                two.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
                one.setBackgroundColor(getResources().getColor(R.color.white));
                three.setBackgroundColor(getResources().getColor(R.color.white));
                four.setBackgroundColor(getResources().getColor(R.color.white));
                five.setBackgroundColor(getResources().getColor(R.color.white));
                stock_type = 2;
                getOffLineData(symbol_code);
                break;
            case R.id.three:
                three.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
                two.setBackgroundColor(getResources().getColor(R.color.white));
                one.setBackgroundColor(getResources().getColor(R.color.white));
                four.setBackgroundColor(getResources().getColor(R.color.white));
                five.setBackgroundColor(getResources().getColor(R.color.white));
                stock_type = 3;
                getOffLineData(symbol_code);
                break;
            case R.id.four:
                four.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
                two.setBackgroundColor(getResources().getColor(R.color.white));
                three.setBackgroundColor(getResources().getColor(R.color.white));
                one.setBackgroundColor(getResources().getColor(R.color.white));
                five.setBackgroundColor(getResources().getColor(R.color.white));
                stock_type = 4;
                getOffLineData(symbol_code);
                break;
            case R.id.five:
                five.setBackgroundColor(getResources().getColor(R.color.hangqingzhongxin));
                two.setBackgroundColor(getResources().getColor(R.color.white));
                three.setBackgroundColor(getResources().getColor(R.color.white));
                four.setBackgroundColor(getResources().getColor(R.color.white));
                one.setBackgroundColor(getResources().getColor(R.color.white));
                stock_type = 5;
                getOffLineData(symbol_code);
                break;
            case R.id.fanhui:
                finish();
                break;
        }
    }
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/fanhui"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:src="@drawable/hangqingfanhui" />

            <TextView
                android:id="@+id/bank_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/fanhui"
                android:text="招商银行"
                android:textSize="@dimen/dimen12" />

            <TextView
                android:id="@+id/bank_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_toRightOf="@+id/bank_name"
                android:text="62049"
                android:textSize="@dimen/dimen12" />


            <TextView
                android:id="@+id/shoupanprice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:layout_toLeftOf="@+id/imagezhangdie"
                android:text="30.89"
                android:textColor="@color/red"
                android:textSize="@dimen/dimen12" />

            <ImageView
                android:id="@+id/imagezhangdie"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="3dp"
                android:layout_toLeftOf="@+id/opentv"
                android:background="@drawable/zhinengxuangu_xia" />


            <TextView
                android:id="@+id/opentv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/open"
                android:text=""
                android:textSize="@dimen/dimen12" />

            <TextView
                android:id="@+id/open"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="15dp"
                android:layout_toLeftOf="@+id/highttv"
                android:text="31.09"
                android:textColor="@color/green"
                android:textSize="@dimen/dimen12" />


            <TextView
                android:id="@+id/highttv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/high"
                android:text=""
                android:textSize="@dimen/dimen12" />

            <TextView
                android:id="@+id/high"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="15dp"
                android:layout_toLeftOf="@+id/lowtv"
                android:text="31.09"
                android:textColor="@color/red"
                android:textSize="@dimen/dimen12" />
            <!-- <TextView
                 android:id="@+id/zhangdie"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="1.22"
                 android:textColor="@color/red"
                 android:textSize="@dimen/dimen12" />

             <TextView
                 android:id="@+id/zhangfu"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginLeft="5dp"
                 android:text="1.22"
                 android:layout_toLeftOf="@+id/lowtv"
                 android:textColor="@color/red"
                 android:textSize="@dimen/dimen12" />-->

            <TextView
                android:id="@+id/lowtv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/low"
                android:text=""
                android:textSize="@dimen/dimen12" />

            <TextView
                android:id="@+id/low"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="15dp"
                android:layout_toLeftOf="@+id/langtv"
                android:text="31.09"
                android:textColor="@color/red"
                android:textSize="@dimen/dimen12" />

            <TextView
                android:id="@+id/langtv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/lang"
                android:text=""
                android:textSize="@dimen/dimen12" />

            <TextView
                android:id="@+id/lang"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="15dp"
                android:text="31.09"
                android:textColor="@color/green"
                android:textSize="@dimen/dimen12" />
        </RelativeLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/zixun_caijingbeijing"
            android:paddingBottom="8dp"
            android:paddingTop="8dp">

            <com.github.mikephil.charting.charts.CombinedChart
                android:id="@+id/combinedchart"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></com.github.mikephil.charting.charts.CombinedChart>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/hangqingzhongxin_bj">

            <com.github.mikephil.charting.charts.BarChart
                android:id="@+id/barchart"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></com.github.mikephil.charting.charts.BarChart>
        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/unit_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end"
                android:text=""

                android:textSize="@dimen/dimen8" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/one"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@color/hangqingzhongxin"
                        android:text="05M"
                        android:textSize="@dimen/dimen12" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/two"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="15M"
                        android:textSize="@dimen/dimen12" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/three"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="30M"
                        android:textSize="@dimen/dimen12" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/four"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="01H"
                        android:textSize="@dimen/dimen12" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/five"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="01D"
                        android:textSize="@dimen/dimen12" />
                </LinearLayout>
            </LinearLayout>
        </FrameLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginTop="5dp"
            android:background="@color/hangqingzhongxin"></View>
    </LinearLayout>
</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值