如何避免LineChart MPAndroidChart上的x或y轴自动缩放

时间:2019-07-18 03:52:37

标签: android mpandroidchart linechart

我正在使用lineChart中的mpandroidchart

当我使用linedataaddDataSet添加addEntry时, x或y轴的比例会更改以匹配数据集中设置的点的最大值和最小值。

如何避免更改它们?我觉得将setAutoScaleMaxMinEnables(false)设置为linechart无效。

我使用initChart()初始化lineChart

  private void initChart() {
    mLineChart = findViewById(R.id.lineChart);
    mLineChart.setAutoScaleMinMaxEnabled(false);
    mLineChart.setHighlightPerTapEnabled(false);

    mLineChart.getDescription().setEnabled(true);
    mLineChart.getDescription().setTextColor(Color.BLACK);
    mLineChart.getDescription().setTextSize(10f);
    mLineChart.getDescription().setPosition(0, 0);

    mLineChart.setTouchEnabled(true);

    mLineChart.setScaleEnabled(true);

    mLineChart.setDragEnabled(true);

    mLineChart.setPinchZoom(false);

    mLineChart.setBackgroundColor(Color.WHITE);

    YAxis leftYAxis = mLineChart.getAxisLeft();

    mLineChart.getAxisRight().setEnabled(false);

    XAxis xAxis = mLineChart.getXAxis();
    LimitLine xLimitLine = new LimitLine(0f, "");
    xLimitLine.setLineColor(Color.BLACK);
    xLimitLine.setTextColor(Color.BLACK);
    leftAxis.addLimitLine(xLimitLine); //
    LimitLine yLimitLine = new LimitLine(0f, "");
    yLimitLine.setLineColor(Color.BLACK);
    yLimitLine.setTextColor(Color.BLACK);
    xAxis.addLimitLine(yLimitLine);
    }
    //(0,0) is added because the graph view is not displayed when the app starts if there is no data.
    ArrayList<Entry> origin = new ArrayList<>();
    origin.add(new Entry(0, 0));
    LineDataSet xLineDataSet = new LineDataSet(origin, "");
    xLineDataSet.setDrawCircles(false);
    xLineDataSet.setColor(Color.BLACK);
    ArrayList<ILineDataSet> dataSets = new ArrayList<>();
    dataSets.add(xLineDataSet);
    LineData lineData = new LineData(dataSets);
    mLineChart.setData(lineData);

}  

我在添加行数据时使用此代码。

LineData lineData = mLineChart.getData();
            if (lineData == null) {
                return;
            }
            LineDataSet lineDataSet = (LineDataSet) lineData.getDataSetByIndex(1);
            if (lineDataSet == null) {
                lineDataSet = new LineDataSet(null, String.valueOf(1));

                lineDataSet.setColor(Color.rgb(0xb9, 0x40, 0x47));

                lineDataSet.setDrawCircles(true);

                lineDataSet.setDrawValues(false);

                lineData.addDataSet(lineDataSet);
            }
            //x and y contain the data I want to add      
            lineData.addEntry(new Entry((float) x, (float) y), 1);

0 个答案:

没有答案