MP Android Chart - 如何修复折线图中X轴值的数量?

Sam*_*wal 5 java charts android linechart mpandroidchart

private void plotchart(String s[], float[] f1) {
    chart.setBackgroundColor(Color.rgb(63, 81, 181));
    chart.setDescription("");
    // enable touch gestures
    chart.setTouchEnabled(true);
    chart.setScaleEnabled(false);
    chart.setPinchZoom(false);
    chart.setGridBackgroundColor(Color.rgb(30, 46, 141));
    chart.setDrawGridBackground(false);
    chart.getAxisRight().setEnabled(false);
    chart.setDrawMarkerViews(true);
    chart.setDragEnabled(true);
    chart.setViewPortOffsets(0f, 0f, 0f, 0f);

   // chart.setVisibleXRangeMaximum(4);


    chart.getLegend().setEnabled(false);

    XAxis x = chart.getXAxis();
    x.setEnabled(true);
    x.setDrawGridLines(false);
    x.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
    x.setTextColor(Color.rgb(128, 128, 255));
    x.isDrawLabelsEnabled();
    x.setAxisLineColor(Color.BLACK);
    x.setSpaceBetweenLabels(3);
    x.setAvoidFirstLastClipping(true);


    YAxis y = chart.getAxisLeft();
    y.setAxisLineColor(Color.BLACK);
    y.setTextColor(Color.BLACK);
    y.setEnabled(true);
    y.setAxisLineColor(Color.rgb(128, 128, 255));
    y.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
    y.setDrawGridLines(false);
    y.setLabelCount(5,true);
    y.setGridColor(Color.rgb(128, 128, 255));
    y.setDrawZeroLine(false);
    y.setDrawLimitLinesBehindData(true);
    y.setDrawGridLines(true);
    y.setDrawLimitLinesBehindData(true);
    y.setTextColor(Color.rgb(128, 128, 255));
//    chart.setExtraOffsets(20f,2f,20f,2f);

    ArrayList<com.github.mikephil.charting.data.Entry> entries= new ArrayList<>();
    for (int i = 0; i < f1.length; i++) {
        // Log.i("f1",f1[i]+"");
         entries.add(new com.github.mikephil.charting.data.Entry(f1[i], i));
    }

    MymarkerView mv =new MymarkerView(this,R.layout.custom_marker_view);
    chart.setMarkerView(mv);

    LineDataSet dataset = new LineDataSet(entries, "");
    dataset.isDrawCirclesEnabled();
    dataset.setCircleRadius(0f);
    dataset.setDrawFilled(true);
    dataset.setFillColor(Color.rgb(0, 0, 0));
    dataset.setLineWidth(0.2f);
    dataset.setValueTextSize(0f);

    dataset.setColor(Color.rgb(0, 0, 0));

  //  chart.setVisibleXRange(1, 5);
   // chart.setVisibleYRangeMaximum(5, YAxis.AxisDependency.LEFT);
  //  chart.setClickable(true);
    chart.invalidate();


    ArrayList<String> time = new ArrayList<String>();
    for (int i = 0; i < s.length; i++) {
        time.add(i, s[i]);
    }

    LineData data = new LineData(time, dataset);
    chart.setData(data);


}
Run Code Online (Sandbox Code Playgroud)

我已经使用setLabelCount()命令修复了Y轴值.但是如何修复我的图表中的X轴值的数量.对于上面的代码,我形成了下面的图表.Chart1 Chart2 ..这些是为plotchart(String [],float [])方法给出的不同参数形成的两个图表.在所有这些图表中,我面临的问题是它没有显示固定数量的x轴值在某个地方,它显示了9个值,某处显示了7个值.我面临的另一个问题是我的第一个和最后一个Y轴值隐藏在我的显示中.

rem*_*til 6

xAxis 也有 setLabelCount() 属性。

 XAxis xAxis = lineChart.getXAxis();
 xAxis.setLabelCount(5);
Run Code Online (Sandbox Code Playgroud)


Cha*_*ois 3

如果我很好地理解你的问题,那么你似乎想使用setVisibleXRangeMaximum方法。

限制可见的内容

setVisibleXRangeMaximum(float maxXRange):设置一次最大可见的区域大小(x 轴上的范围)。例如,如果将其设置为 10,则无需滚动即可同时查看 x 轴上的值不超过 10 个。

此方法记录在 MPAndroidChart 教程的视口部分中,该教程的链接位于此处

当然,我认为如果您想修复 X ​​值数量的上限下限,则必须使用setVisibleXRangeMinimum 。