PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.54k stars 9.01k forks source link

Line chart grid line count miss number when ever reload new data #4939

Open arminsadreddin opened 4 years ago

arminsadreddin commented 4 years ago

Summary

Expected Behavior

Possible Solution

Device (please complete the following information):

Additional Context

arminsadreddin commented 4 years ago

I am using line chart to draw data in my app, my data represent usage of a device and it can be sorted in day,week and month, the problem is that when ever I switch between these filters, the grid line get in wrong number. for example the device day usage is has 20 different labels and in month is only has 3 label but the grids are still 20. Screenshot_1594543367 Screenshot_1594543373 and this is my code: LineChart lineChart = new LineChart(getApplicationContext()); lineChart = findViewById(R.id.LineChart);

    lineDataSet = new LineDataSet(lineEntries, "Usage");

    final Typeface face = ResourcesCompat.getFont(getApplicationContext(), R.font.iransansmobile_num_medium);
    lineDataSet.setValueTypeface(face);
    //barDataSet.setValueTypeface(face);

    lineData = new LineData(lineDataSet);

    lineChart.setData(lineData);

    lineChart.getAxisLeft().setTextColor(ColorTemplate.rgb("#FFFFFF")); // left y-axis
    lineChart.getXAxis().setTextColor(ColorTemplate.rgb("#FFFFFF"));
    lineChart.getLegend().setTextColor(ColorTemplate.rgb("#FFFFFF"));

    //Log.d("chart sizeeee", String.valueOf(usageList.size()));
    int viewable_size = 6;
    lineChart.setVisibleXRangeMaximum(viewable_size);

    lineChart.getXAxis().setLabelCount(viewable_size + 1, true);

    lineChart.getDescription().setEnabled(false);
    lineChart.getLegend().setEnabled(false);
    lineChart.getAxisLeft().setEnabled(false);
    lineChart.getAxisRight().setEnabled(false);
    lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    lineChart.getXAxis().setDrawAxisLine(false);
    lineChart.getXAxis().setCenterAxisLabels(false);

    lineChart.getXAxis().setDrawLabels(true);

    lineChart.getXAxis().setGridLineWidth(1);

    XAxis xAxis = lineChart.getXAxis();
    xAxis.setValueFormatter(new IndexAxisValueFormatter(){
        @Override
        public String getFormattedValue(float value) {
            Log.d("LABLE SIZE", String.valueOf(labels.size()));
            if((int)value >= labels.size()){
                return null;
            }
            return labels.get((int) value);
        }
    });

    lineChart.setOnChartGestureListener(new OnChartGestureListener() {

        @Override
        public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {

        }

        @Override
        public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {

        }

        @Override
        public void onChartLongPressed(MotionEvent me) {

        }

        @Override
        public void onChartDoubleTapped(MotionEvent me) {

        }

        @Override
        public void onChartSingleTapped(MotionEvent me) {

        }

        @Override
        public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {

        }

        @Override
        public void onChartScale(MotionEvent me, float scaleX, float scaleY) {

        }

        @Override
        public void onChartTranslate(MotionEvent me, float dX, float dY) {

        }
    });

    lineDataSet.setColor(ColorTemplate.rgb("FF6900"));
    lineDataSet.setValueTextColor(ColorTemplate.rgb("#FF6900"));
    lineDataSet.setDrawFilled(true);
    lineDataSet.setFillColor(ColorTemplate.rgb("#FF6900"));
    //lineDataSet.setCircleRadius(5);
    //lineDataSet.setCircleColor(ColorTemplate.rgb("FF3F51B5"));
    lineDataSet.setValueTextSize(12f);

    lineChart.invalidate();