Skip to content

Getting crash when trying to change the dataset of barchart. #3940

@parthanjaria

Description

@parthanjaria

Initially i have an array-list of 73(at x-axis) set in the bar chart.

Then i change the array-list to 13(at x-axis) and that time i get crash. can you help me out with why it is happening.

Here is the written code :

`private void setUpBarChart(int type) {
barChartDataSet = new BarChartDataSet(type);

    BarDataSet dataSet = new BarDataSet(barChartDataSet.getBarEntries(), "Stats");
    dataSet.setColor(ContextCompat.getColor(getContext(), R.color.bright_sky_blue));
    dataSet.setValueTextColor(ContextCompat.getColor(getContext(), R.color.greyish_brown));

    IValueFormatter intValueFormatter = new IValueFormatter() {
        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex,
                                        ViewPortHandler viewPortHandler) {
            int val = Math.round(value);
            if (val > 0) {
                return UnitsManager.formatRupeeToMyCurrency(val);
            } else {
                return "";
            }
        }
    };

    dataSet.setValueFormatter(intValueFormatter);
    BarData data = new BarData(dataSet);

    barChart.setData(data);
    barChart.setDrawGridBackground(false);


    barChart.getAxisRight().setEnabled(false);
    barChart.getAxisRight().setDrawGridLines(false);
    Description desc = new Description();
    desc.setText("");
    barChart.setDescription(desc);
    barChart.getLegend().setEnabled(false);

    YAxis yAxis = barChart.getAxisLeft();

    yAxis.setSpaceBottom(4);
    yAxis.setLabelCount(3, true);

    yAxis.setAxisMinimum(0);
    yAxis.setGridColor(ContextCompat.getColor(getContext(), R.color.black_5));
    yAxis.setDrawAxisLine(false);
    yAxis.setGridLineWidth(1f);

    yAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            if (value > 10) {
                return UnitsManager.formatRupeeToMyCurrency(value);
            } else if (value > 0) {
                // Earlier only one decimal was being shown
                return UnitsManager.formatRupeeToMyCurrency(value);
            } else {
                return "";
            }
        }
    });

    XAxis xAxis = barChart.getXAxis();

    xAxis.setDrawGridLines(false);
    xAxis.setAxisLineColor(ContextCompat.getColor(getContext(), R.color.black_5));
    xAxis.setAxisLineWidth(1.0f);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    IAxisValueFormatter valueFormatter = new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            int index = (int) value;
            return barChartDataSet.getLabelForIndex(index); // getting crash at this line
        }
    };
    xAxis.setValueFormatter(valueFormatter);
    barChart.setDrawBorders(false);

    barChart.invalidate();
    barChart.setOnChartValueSelectedListener(this);
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            float width = layoutProfileStats.getWidth();
            int size = barChartDataSet.getNoOfValuesInXAxis()>=7?7: (int) barChartDataSet.getNoOfValuesInXAxis();
            float scale = ((width * barChartDataSet.getNoOfValuesInXAxis()) / size) / width;
            barChart.setScaleYEnabled(false);
            barChart.setScaleXEnabled(true);
            barChart.zoom(scale, 1, width * scale, 0);
            barChart.highlightValue(barChartDataSet.getBarEntries().size() - 1, 0);
        }
    };
    handler.postDelayed(null, 100);
}`

I call this method the number of times i wish to change the data, accordingly the data is changed. if i don't write this code : 'xAxis.setValueFormatter(valueFormatter);' then i don't get any error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions