一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖一(概述)


目錄
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖一(概述)
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖二(自定義xy軸)
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖三(對(duì)齊圖表、自定義柱狀圖高亮)
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖四(高亮聯(lián)動(dòng))
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖五(自定義標(biāo)記)


demo更新地址https://github.com/AndroidJiang/StockChart


因?yàn)楣镜捻?xiàng)目需求,最近不得不研究下股票走勢(shì)圖,經(jīng)過(guò)自己的學(xué)習(xí)和篩選,考慮到自己自定義圖表庫(kù)水平有限,最后選擇MpAndroidChart,項(xiàng)目源碼將會(huì)在開(kāi)發(fā)完畢后放到github上,歡迎star,圖表庫(kù)來(lái)實(shí)現(xiàn)股票走勢(shì)圖,本項(xiàng)目用的是v2.2.5。股票走勢(shì)圖大致有兩種:分時(shí)圖,K線圖。下面先貼上最終要實(shí)現(xiàn)的效果圖:

來(lái)源于自選股app
來(lái)源于自選股app
這是最終效果,圖片來(lái)自于自選股,最終實(shí)現(xiàn)的效果可能有點(diǎn)出入,不過(guò)大體差不多。第一階段先從簡(jiǎn)單的分時(shí)圖開(kāi)始,下面貼出第一階段實(shí)現(xiàn)的效果圖:
這里寫(xiě)圖片描述
大致代碼如下:

  private void initChart() {
    lineChart.setScaleEnabled(false);
    lineChart.setDrawBorders(true);
    lineChart.setBorderWidth(1);
    lineChart.setBorderColor(getResources().getColor(R.color.grayLine));
    lineChart.setDescription("");
    Legend lineChartLegend = lineChart.getLegend();
    lineChartLegend.setEnabled(false);

    barChart.setScaleEnabled(false);
    barChart.setDrawBorders(false);
  /*  barChart.setBorderWidth(1);
    barChart.setBorderColor(getResources().getColor(R.color.grayLine));*/
    barChart.setDescription("");
    Legend barChartLegend = barChart.getLegend();
    barChartLegend.setEnabled(false);
    //x軸
    xAxis = lineChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setLabelsToSkip(59);



    //左邊y
    axisLeft = lineChart.getAxisLeft();
    axisLeft.setLabelCount(5, true);
    axisLeft.setDrawLabels(true);

    //bar x y軸
    xAxisBar = barChart.getXAxis();
    xAxisBar.setDrawLabels(false);
    xAxisBar.setDrawGridLines(false);

    axisLeftBar = barChart.getAxisLeft();
    axisLeftBar.setDrawGridLines(false);


    axisRightBar = barChart.getAxisRight();
   // axisRightBar.setDrawLabels(false);
    axisRightBar.setDrawGridLines(false);

    //y軸樣式
    this.axisLeft.setValueFormatter(new YAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, YAxis yAxis) {
            DecimalFormat mFormat = new DecimalFormat("#0.00");
            return mFormat.format(value);
        }
    });
    //右邊y
    this.axisRight = lineChart.getAxisRight();
    this.axisRight.setLabelCount(5, true);
    this.axisRight.setDrawLabels(true);
    this.axisRight.setValueFormatter(new YAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, YAxis yAxis) {
            DecimalFormat mFormat = new DecimalFormat("#0.00%");
            return mFormat.format(value);
        }
    });

    this.axisRight.setStartAtZero(false);
    this.axisRight.setDrawGridLines(false);
    this.axisRight.setDrawAxisLine(false);
    //背景線
    this.xAxis.setGridColor(getResources().getColor(R.color.grayLine));
    this.xAxis.setAxisLineColor(getResources().getColor(R.color.grayLine));
    this.axisLeft.setGridColor(getResources().getColor(R.color.grayLine));
    this.axisRight.setAxisLineColor(getResources().getColor(R.color.grayLine));

}

獲取數(shù)據(jù):

private void getMinutesData() {
    String code = "sz002081";
    subscriptionMinute = clientApi.getMinutes(code)
            .compose(SchedulersCompat.<ResponseBody>applyIoSchedulers())
            .subscribe(new Subscriber<ResponseBody>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    showToast("更新失敗" + e.toString());
                }

                @Override
                public void onNext(ResponseBody minutes) {
                    MData mData = new MData();
                    JSONObject object = null;
                    try {
                        object = new JSONObject(minutes.string());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mData.parseData(object);
                    setData(mData);

                }
            });
    mCompositeSubscription.add(subscriptionMinute);
}

塞入數(shù)據(jù):

private void setData(MData mData) {
    if (mData.getDatas().size() == 0) {
        lineChart.setNoDataText("暫無(wú)數(shù)據(jù)");
        return;
    }
    //設(shè)置y左右兩軸最大最小值
    axisLeft.setAxisMinValue(mData.getMin());
    axisLeft.setAxisMaxValue(mData.getMax());
    axisRight.setAxisMinValue(mData.getPercentMin());
    axisRight.setAxisMaxValue(mData.getPercentMax());


    axisLeftBar.setAxisMaxValue(mData.getVolmax());
    axisLeftBar.setAxisMinValue(0);//即使最小是不是0,也無(wú)礙
    axisLeftBar.setShowOnlyMinMax(true);
    axisRightBar.setAxisMaxValue(mData.getVolmax());
    axisRightBar.setAxisMinValue(0);//即使最小是不是0,也無(wú)礙
    axisRightBar.setShowOnlyMinMax(true);
    //基準(zhǔn)線
    LimitLine ll = new LimitLine(mData.getBaseValue());
    ll.setLineWidth(1f);
    ll.setLineColor(Color.RED);
    ll.enableDashedLine(10f, 10f, 0f);
    ll.setLineWidth(1);
    axisLeft.addLimitLine(ll);


    ArrayList<Entry> lineCJEntries = new ArrayList<Entry>();
    ArrayList<Entry> lineJJEntries = new ArrayList<Entry>();
    ArrayList<String> dateList = new ArrayList<String>();
    ArrayList<BarEntry> barEntries = new ArrayList<BarEntry>();
    ArrayList<String> xVals = new ArrayList<String>();

    for (int i = 0; i < mData.getDatas().size(); i++) {
        //避免數(shù)據(jù)重復(fù),skip也能正常顯示
        if(mData.getDatas().get(i).time.equals("13:30")){
            continue;
        }
        lineCJEntries.add(new Entry(mData.getDatas().get(i).chengjiaojia, i));

        lineJJEntries.add(new Entry(mData.getDatas().get(i).junjia, i));
        barEntries.add(new BarEntry(mData.getDatas().get(i).chengjiaoliang, i));
        dateList.add(mData.getDatas().get(i).time);
    }
    d1 = new LineDataSet(lineCJEntries, "成交價(jià)");
    d2 = new LineDataSet(lineJJEntries, "均價(jià)");
    barDataSet = new BarDataSet(barEntries, "成交量");

    d1.setCircleRadius(0);
    d2.setCircleRadius(0);
    d1.setColor(Color.BLUE);
    d2.setColor(Color.RED);
    d1.setHighLightColor(Color.BLACK);
    d2.setHighlightEnabled(false);
    d1.setDrawFilled(true);

    barDataSet.setBarSpacePercent(0); //bar空隙
    barDataSet.setHighLightColor(Color.BLACK);
    barDataSet.setHighLightAlpha(255);
    barDataSet.setDrawValues(false);
    //誰(shuí)為基準(zhǔn)
    d1.setAxisDependency(YAxis.AxisDependency.LEFT);
   // d2.setAxisDependency(YAxis.AxisDependency.RIGHT);
    ArrayList<ILineDataSet> sets = new ArrayList<ILineDataSet>();
    sets.add(d1);
    sets.add(d2);
    LineData cd = new LineData(dateList, sets);
    lineChart.setData(cd);

    BarData barData=new BarData(dateList,barDataSet);
    barChart.setData(barData);
    lineChart.invalidate();//刷新圖
    barChart.invalidate();
}

總結(jié)一下有幾個(gè)問(wèn)題:

  • 當(dāng)x軸沒(méi)有到當(dāng)天的15:00時(shí),仍然充滿整個(gè)x軸,需求是x軸數(shù)據(jù)時(shí)不變的,更新到幾點(diǎn),表就畫(huà)到幾點(diǎn) - 下面柱狀圖高亮效果不是十字架,差評(píng)
  • 兩個(gè)表之間沒(méi)有聯(lián)動(dòng) - 高亮地方應(yīng)該展示對(duì)應(yīng)軸的數(shù)據(jù) 目前來(lái)說(shuō),有以上四個(gè)問(wèn)題需要解決,本篇到此為止。

目錄
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖一(概述)
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖二(自定義xy軸)
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖三(對(duì)齊圖表、自定義柱狀圖高亮)
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖四(高亮聯(lián)動(dòng))
一步一步教你寫(xiě)股票走勢(shì)圖——分時(shí)圖五(自定義標(biāo)記)


demo更新地址https://github.com/AndroidJiang/StockChart


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容