MP Android Chart-如何在折线图中固定X轴值的数量?

5
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);


}

我已经使用setLabelCount()命令修复了Y轴的值。但是如何在我的图表中固定X轴值的数量呢?对于上面的代码,我形成了以下两个图表:Chart1 Chart2。在所有这些图表中,我遇到的问题是它没有显示固定数量的x轴值。有时它会显示9个值,有时它会显示7个值。我还遇到的另一个问题是我的显示中第一个和最后一个Y轴值被隐藏了。
5个回答

6
`setLabelCount()`属性对于x轴也同样适用。
 XAxis xAxis = lineChart.getXAxis();
 xAxis.setLabelCount(5);

3
如果我理解您的问题正确,似乎您想使用setVisibleXRangeMaximum方法。
“限制可见内容”: setVisibleXRangeMaximum(float maxXRange):设置应同时最大可见的区域(x轴范围)的大小。如果例如将其设置为10,则一次最多只能查看10个x轴值,否则需要滚动。
该方法在MPAndroidChart教程的Viewport部分中有介绍,链接在此:https://github.com/PhilJay/MPAndroidChart/wiki/Modifying-the-Viewport
当然,如果您想为X值的数量固定上限和下限,则需要使用setVisibleXRangeMinimum方法。

3

将X轴的值设置为固定的数字(例如10):

float minXRange = 10;
float maxXRange = 10;
chart.setVisibleXRange(minXRange, maxXRange); 

如果您在X轴上的值之间的间隔是恒定的,那么它也会固定X轴的比例。

要根据最大值和最小值自动设置Y轴的比例,并使其具有10%的间距:

YAxis y1 = chart.getAxisLeft();
float percent = 10;
y1.setSpaceTop(percent);
y1.setSpaceBottom(percent);

1
您可以使用库的 IndexAxisValueFormatter 方法。
ArrayList<String> xAxisLabel = new ArrayList<>();
        xAxisLabel.add("Mon");
        xAxisLabel.add("Tue");
        xAxisLabel.add("Wed");
        xAxisLabel.add("Thu");
        xAxisLabel.add("Fri");
        xAxisLabel.add("Sat");
        xAxisLabel.add("Sun");

chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(texts));

当前库版本为v3.0.3。

0
设置 X 轴数值
xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return (int) value + "";
        }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接