MPAndroidChart记录

地址

https://github.com/PhilJay/MPAndroidChart

初始化设置折线图

 public static void initLineChart(LineChart mLineChar) {

        //设置手势滑动事件
//        mLineChar.setOnChartGestureListener(this);
        //设置数值选择监听
//        mLineChar.setOnChartValueSelectedListener(this);
        //后台绘制
        mLineChar.setDrawGridBackground(false);
        //设置描述文本
        mLineChar.getDescription().setEnabled(false);
        //设置支持触控手势
        mLineChar.setTouchEnabled(true);
        //设置缩放
        mLineChar.setDragEnabled(true);
        //设置推动
        mLineChar.setScaleEnabled(true);
        //如果禁用,扩展可以在x轴和y轴分别完成
        mLineChar.setPinchZoom(true);
        // create marker to display box when values are selected
//        mv= new MyMarkerView(this, R.layout.custom_marker_view);
 //设置x轴laber为string
        lineCharSales.getXAxis().setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                int v = (int) value;
                return String.valueOf(v);
            }
        });
         lineCharSales.getAxisLeft().setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                float axisMaximum = lineCharSales.getAxisLeft().getAxisMaximum();
                if (axisMaximum > 10000) {
                    double l = value / 10000;
                    tvSaleLegend.setText("销售额(万元)");
                    tvProfitLegend.setText("毛利(万元)");
                    DecimalFormat df = new DecimalFormat("0.0");
                    return df.format(l);
                } else if (axisMaximum > 1000 && axisMaximum < 10000) {
                    double l = value / 1000;
                    tvSaleLegend.setText("销售额(千元)");
                    tvProfitLegend.setText("毛利(千元)");
                    DecimalFormat df = new DecimalFormat("0.0");
                    return df.format(l);
                } else {
                    tvSaleLegend.setText("销售额(元)");
                    tvProfitLegend.setText("毛利(元)");
                    DecimalFormat df = new DecimalFormat("0");
                    return df.format(value);
                }
            }
        });
         lineCharSales.setOnChartValueSelectedListener(saleListener);
        lineCharProfit.setOnChartValueSelectedListener(profitListener);
        // Set the marker to the chart
//        mv.setChartView(mLineChar);
//        mLineChar.setMarker(mv);
//        mLineChar.setDrawMarkerViews(false);
//        xAxis = mLineChar.getXAxis();
        //设置不显示,x轴y轴网格线
        mLineChar.getXAxis().setDrawGridLines(false);
        mLineChar.getAxisLeft().setDrawGridLines(true);
        mLineChar.getAxisLeft().setGridColor(Color.parseColor("#eeeeee"));
        mLineChar.getAxisRight().setDrawGridLines(false);
        mLineChar.getAxisLeft().enableGridDashedLine(20f, 10f, 0f);
        mLineChar.getAxisLeft().setGridLineWidth(1f);
        //保证Y轴从0开始,不然会上移一点
//        mLineChar.getAxisRight().setAxisMinimum(0f);
//        mLineChar.getAxisLeft().setAxisMinimum(0f);
        //设置y轴右边的线不显示
        mLineChar.getAxisRight().setDrawAxisLine(false);
        //设置不显示y轴右边的laber
        mLineChar.getAxisRight().setDrawLabels(false);
        //设置x轴laber显示在下边
        mLineChar.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
//        mLineChar.getAxisLeft().setLabelCount(3);
        mLineChar.getAxisRight().setDrawTopYLabelEntry(true);
       /* //设置Y轴坐标之间的最小间隔
        yAxis.setGranularity(1);
        //设置y轴的刻度数量
        //+2:最大值n就有n+1个刻度,在加上y轴多一个单位长度,为了好看,so+2
        yAxis.setLabelCount(Collections.max(list) + 2, false);*/
       mLineChar.getAxisLeft().setLabelCount(6);
        //图例:得到Lengend
        Legend legend = mLineChar.getLegend();
//        legend.setForm(Legend.LegendForm.valueOf(""));
//        隐藏Lengend
        legend.setEnabled(false);
        //隐藏x轴描述
        Description description = new Description();
        description.setText("x轴描述");
        description.setTextSize(16f);
        description.setEnabled(false);
        mLineChar.setDescription(description);
    }
     OnChartValueSelectedListener profitListener = new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, Highlight h) {
            showDetails(e,2);
        }

        @Override
        public void onNothingSelected() {

        }
    };
     private void showDetails(Entry entry,int type) {
        if (entry != null) {
            float x = entry.getX();
            SaleStatisticsListBean.ContentBean contentBean = content.get((int)x);
            line1Details.setVisibility(View.VISIBLE);
            tvSelectDate.setText(contentBean.getSaleDate() + ",");
            tvSelectSales.setText(UI.double2(contentBean.getTotalMoney()) + ",");
            tvSelectProfit.setText(UI.double2(contentBean.getGrossProfitMoney()));
            llyLinechartProfit.setVisibility(View.VISIBLE);
            tvSelectDateProfit.setText(contentBean.getSaleDate() + ",");
            tvSelectProfitProfit.setText(UI.double2(contentBean.getGrossProfitRate()) + "%");
            if (type == 1) {
                lineCharProfit.setOnChartValueSelectedListener(null);
                lineCharProfit.highlightValue(x,Float.parseFloat(String.valueOf(contentBean.getGrossProfitRate())),0);
                lineCharProfit.setOnChartValueSelectedListener(profitListener);
            } else {
                lineCharSales.setOnChartValueSelectedListener(null);
                lineCharSales.highlightValue(x,Float.parseFloat(String.valueOf(contentBean.getGrossProfitMoney())),0);
                lineCharSales.setOnChartValueSelectedListener(saleListener);
            }
        }
    }

需要注意的地方

  lineCharProfit.highlightValue(x,Float.parseFloat(String.valueOf(contentBean.getGrossProfitRate())),0);

此方法中的“0”表示的是你整个图表中的第几根线,默认值是-1
因为需求中中涉及到两张图的联动 所以当选中其中一张图的时候另一张图中对应日期的数据要选中

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值