java MP Android Chart-如何修复折线图中X轴值的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38326306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MP Android Chart-How to fix the number of X-Axis values in the Line Chart?
提问by Samarth Kejriwal
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);
}
I Have fixed the Y-Axis values by using setLabelCount() command.But how to fix the number of X-Axis values in my chart..For the above code i have the following chart formed.Chart1Chart2..These are the two charts formed for different arguments given to the plotchart(String [],float[]) method.In all these charts I am facing the problem that it is not showing the fixed number of x-axis values.Somewhere it is showing 9 values and somewhere it is showing 7 values..And one more issue I am facing is that my first and the last Y-Axis values are getting hidden in my display.
我已经使用 setLabelCount() 命令修复了 Y 轴值。但是如何修复图表中 X 轴值的数量..对于上面的代码,我形成了以下图表。Chart1 Chart2 ..这些是为 plotchart(String [],float[]) 方法的不同参数形成的两个图表。在所有这些图表中,我面临的问题是它没有显示固定数量的 x 轴值.某处显示 9 个值,某处显示 7 个值..我面临的另一个问题是我的第一个和最后一个 Y 轴值被隐藏在我的显示器中。
回答by Challensois
If I understood your question well, it seems that you want to use the setVisibleXRangeMaximummethod.
如果我很好地理解了您的问题,那么您似乎想使用setVisibleXRangeMaximum方法。
Restraining what's visible
setVisibleXRangeMaximum(float maxXRange):Sets the size of the area (range on the x-axis) that should be maximum visible at once. If this is e.g. set to 10, no more than 10 values on the x-axis can be viewed at once without scrolling.
限制可见的东西
setVisibleXRangeMaximum(float maxXRange):设置一次最大可见区域的大小(x轴上的范围)。例如,如果将其设置为 10,则无需滚动即可同时查看 x 轴上的不超过 10 个值。
This method is documented in the Viewport part of the MPAndroidChart tutorial, which is linked here.
该方法记录在 MPAndroidChart 教程的视口部分,链接在这里。
Of course, I think you will have to use the setVisibleXRangeMinimumif you want to fix an upper anda lower bounds for the number of X values.
当然,我认为如果要修复X 值数量的上限和下限,则必须使用setVisibleXRangeMinimum。
回答by remya thekkuvettil
setLabelCount() property is there for xAxis too.
xAxis 也有 setLabelCount() 属性。
XAxis xAxis = lineChart.getXAxis();
xAxis.setLabelCount(5);
回答by AW5
To set the X-axis values to a fixed number (10 for example):
要将 X 轴值设置为固定数字(例如 10):
float minXRange = 10;
float maxXRange = 10;
chart.setVisibleXRange(minXRange, maxXRange);
If the gap between your values on the X-axis is constant, it will also fix the scale for the X-axis.
如果 X 轴上的值之间的差距是恒定的,它也会固定 X 轴的比例。
To auto set the scale on the Y-Axis with 10% spacing according to your maximum and minimum values:
要根据您的最大值和最小值以 10% 的间距自动设置 Y 轴上的比例:
YAxis y1 = chart.getAxisLeft();
float percent = 10;
y1.setSpaceTop(percent);
y1.setSpaceBottom(percent);
回答by ysfcyln
You can use IndexAxisValueFormatter
method of library
您可以使用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));
Current library version v3.0.3
当前库版本 v3.0.3
回答by MinnuKaAnae
Set X-Axis values
设置 X 轴值
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return (int) value + "";
}
});