MPandroidchart:标签不与水平条形图对齐

时间:2019-01-12 06:05:03

标签: android mpandroidchart

我在用MPandroid实现简单的水平条形图时遇到问题。关于水平条形图问题的各种类似问题都在图书馆的GithUb周围浮动,但尚未得到解答,作者明确表示他希望人们将“如何做”问题放在StackOverflow上。

当前结果是这样:

enter image description here

标签已添加到图表中,但未正确与条形对齐。 你能看出我逻辑上的缺陷吗?我将只在片段中发布构建图表的代码-我假设其他内容无关。谢谢!

private static FragmentCompareBinding oBinding;
    private SongViewModel oSongViewModel;
private List<ModelSongCounter> dataObjects;
    private HorizontalBarChart chart;
    private SeekBar seekBarX, seekBarY;
    private TextView tvX, tvY;

    public CompareFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //init Databinding
        oBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_compare, container, false);//.setContentView(getActivity(), R.layout.fragment_main);

        oSongViewModel = ViewModelProviders.of(getActivity()).get(SongViewModel.class);

        Single.fromCallable(() -> oSongViewModel.getNonLibeSongs())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        e -> InitChart(e)
                );


        return oBinding.getRoot();
    }

public void InitChart(List<ModelSongCounter> result){

        dataObjects = result;

        tvX = oBinding.tvXMax;
        tvY = oBinding.tvYMax;

        seekBarX = oBinding.seekBar1;
        seekBarY = oBinding.seekBar2;

        seekBarY.setOnSeekBarChangeListener(this);
        seekBarX.setOnSeekBarChangeListener(this);

        chart = oBinding.testchart;
        //chart.setOnChartValueSelectedListener(this);
        // chart.setHighlightEnabled(false);

        chart.setDrawBarShadow(false);

        //chart.setDrawValueAboveBar(true);

        //chart.getDescription().setEnabled(false);

        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        //chart.setMaxVisibleValueCount(60);

        // scaling can now only be done on x- and y-axis separately
        chart.setPinchZoom(false);

        // draw shadows for each bar that show the maximum value
        // chart.setDrawBarShadow(true);

        chart.setDrawGridBackground(false);

        XAxis xl = chart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);
        //xl.setTypeface(tfLight);
        xl.setDrawAxisLine(true);
        xl.setDrawGridLines(false);
        //xl.setGranularity(10f);
        xl.setEnabled(true);

        YAxis yl = chart.getAxisLeft();
        //yl.setTypeface(tfLight);
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(true);
        yl.setAxisMaximum(100f);
        yl.setAxisMinimum(0f);
        yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
//        yl.setInverted(true);

        YAxis yr = chart.getAxisRight();
        //yr.setTypeface(tfLight);
        yr.setDrawAxisLine(true);
        yr.setDrawGridLines(false);
        //yr.setAxisMinimum(0f); // this replaces setStartAtZero(true)
//        yr.setInverted(true);

        chart.setFitBars(true);
        chart.animateY(2500);

        // setting data
        seekBarY.setProgress(50);
        seekBarX.setProgress(12);
/*
        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setFormSize(8f);
        l.setXEntrySpace(4f);*/
    }

    private void setData(int count, float range) {

        float barWidth = 9f;

        List<BarEntry> entries = new ArrayList<BarEntry>();
        final List<String> labels = new ArrayList<String>();

        for (int i = 0; i < dataObjects.size(); i++) {
            // turn your data into Entry objects
            if (dataObjects.get(i) != null) {
                float val = (dataObjects.get(i).getCounter() * range);
                entries.add(new BarEntry(i,
                        val));
                labels.add(dataObjects.get(i).getName()+" "+dataObjects.get(i).counter);
            }
        }

        BarDataSet set1;

        chart.getXAxis().setLabelCount(labels.size());
        chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));

        if (chart.getData() != null &&
                chart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
            set1.setValues(entries);
            //set1.setStackLabels(labels.toArray(new String[labels.size()]));
            chart.getData().notifyDataChanged();
            chart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(entries, "DataSet 1");
            //set1.setStackLabels(labels.toArray(new String[labels.size()]));

            set1.setDrawIcons(false);

            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1);

            BarData data = new BarData(set1);
            //data.setValueTextSize(10f);
            data.setDrawValues(false);
            //data.setValueTypeface(tfLight);
            data.setBarWidth(barWidth);
            chart.setData(data);
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        tvX.setText(String.valueOf(seekBarX.getProgress()));
        tvY.setText(String.valueOf(seekBarY.getProgress()));

        setData(seekBarX.getProgress(), seekBarY.getProgress());
        chart.setFitBars(true);
        chart.invalidate();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {}

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {}

    private final RectF mOnValueSelectedRectF = new RectF();

0 个答案:

没有答案