MPAndroidChart - 拖动后重置setViewPortOffsets(0,0,0,0)

时间:2017-05-30 13:23:39

标签: android mpandroidchart

我正在使用带水平条形图的MPAndroidChart。 我将视口设置为0,0,0,0但是一旦我拖动值reset。

我该怎么办?

似乎即使我删除了偏移量,MPAndroidChart在拖动后仍在调整窗口大小。

enter image description here

enter image description here

public class FutureFragment extends Fragment {

    private ProgressDialog progressDialog;
    private ArrayList<Double> futureValues;

    public FutureFragment() {

    }

    protected HorizontalBarChart mChart;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_four, container, false);

        mChart = (HorizontalBarChart) v.findViewById(R.id.chart1);
        mChart.getDescription().setEnabled(false);
        mChart.setPinchZoom(false);
        mChart.setDrawGridBackground(false);
        mChart.setDoubleTapToZoomEnabled(false);
        mChart.setHighlightPerTapEnabled(false);

        XAxis xl = mChart.getXAxis();
        xl.setDrawAxisLine(false);
        xl.setDrawGridLines(false);

        YAxis yl = mChart.getAxisLeft();
        yl.setDrawAxisLine(false);
        yl.setDrawGridLines(false);
        yl.setAxisMinimum(0f);

        YAxis yr = mChart.getAxisRight();
        yr.setDrawAxisLine(false);
        yr.setDrawGridLines(false);
        yr.setAxisMinimum(0f);

        if (savedInstanceState == null) {
            new PredictFuture().execute();
        }

        return  v;
    }

    private class PredictFuture extends AsyncTask<Void, Integer, Void> {
        //Before running code in separate thread
        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(getContext(), getString(R.string.future_asynctask_title), getString(R.string.future_asynctask_message), false, false);
        }

        //The code to be executed in a background thread.
        @Override
        protected Void doInBackground(Void... params) {
            setData();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();

            mChart.setDragEnabled(true);
            mChart.animateY(1000);
            mChart.getLegend().setEnabled(false);
            mChart.getAxisLeft().setDrawLabels(false);
            mChart.getAxisRight().setDrawLabels(false);

            final int pxHeight = mChart.getHeight();
            final int pxWidth = mChart.getWidth();

            mChart.getXAxis().setPosition(XAxis.XAxisPosition.TOP);
            mChart.getXAxis().setXOffset(((getDp(pxWidth)) * -1) + 8);

            // Set bar width to 50dp
            float rows  = getDp(pxHeight)/50;
            float percent  = futureValues.size()/rows;

            mChart.setScaleMinima(1, percent);
            mChart.getXAxis().setLabelCount((int)rows);
            mChart.getXAxis().setTextSize(18);
            mChart.getXAxis().setTypeface(Typeface.DEFAULT);
            mChart.getXAxis().setTextColor(1979711488);
            mChart.setViewPortOffsets ( 0f, 0f, 0f, 0f );
            mChart.invalidate ();
        }
    }

    private void setData() {

        float barWidth = 0.9f;
        ArrayList<BarEntry> yVals1 = new ArrayList<>();

        ArrayList<BillsArray> billValues = ((Bills)getActivity()).getBills();
        TopUpArray topUpValues = ((Bills)getActivity()).getTopUpValues();

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
        TimeZone timeZone = TimeZone.getTimeZone(preferences.getString(Splash.ZONE, "UTC"));
        Future future = new Future(topUpValues, billValues, timeZone);

        futureValues = future.getFutureValues();
        ArrayList<String> futureDates = future.getFutureDates();

        for (int i = 0; i < futureValues.size(); i++) {
            float val = (float) (futureValues.get(i).doubleValue());
            yVals1.add(new BarEntry(i, val, futureDates.get(i)));
        }

        BarDataSet set1;

        if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(yVals1, getString(R.string.future_graph_name));
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1);
            BarData data = new BarData(dataSets);
            data.setDrawValues(false);
            data.setBarWidth(barWidth);
            mChart.setData(data);
            mChart.getXAxis().setValueFormatter(new LabelValueFormatter(set1));

        }
    }

    public int getDp(int px) {
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }
}

1 个答案:

答案 0 :(得分:0)

我在scrollView中的经典BarChart上尝试了mChart.setExtraOffsets ( 0f, 0f, 0f, 0f );并且它可以正常工作。 我希望它在水平条形图中的工作方式相同。

我认为滚动时会出现问题,重新绘制图表并调用方法resetViewPortOffsets()