X 轴标签未显示,使用 MPAndroidChart
X Axis Labels not showing up, using MPAndroidChart
我想在折线图上添加 x 轴标签(使用 MPAndroidChart),但无论我做什么,都无法显示它们。我有一个名为 setupChart 的方法,它处理该图表的所有内容,这就是它的样子:
private void setupChart(LineChart chart, LineData data, int color) {
((LineDataSet) data.getDataSetByIndex(0)).setCircleColorHole(Color.WHITE);
((LineDataSet) data.getDataSetByIndex(0)).setCircleColor(color);
((LineDataSet) data.getDataSetByIndex(0)).setColors(dataColors);
data.getDataSetByIndex(0).setAxisDependency(YAxis.AxisDependency.LEFT);
((LineDataSet) data.getDataSetByIndex(0)).setDrawCircles(false);
// no description text
chart.getDescription().setEnabled(false);
// mChart.setDrawHorizontalGrid(false);
//
// enable / disable grid background
chart.setDrawGridBackground(false);
// enable touch gestures
chart.setTouchEnabled(true);
// disable scaling and dragging
chart.setDragEnabled(false);
chart.setScaleEnabled(false);
chart.setAutoScaleMinMaxEnabled(false);
// if disabled, scaling can be done on x- and y-axis separately
chart.setPinchZoom(false);
chart.setBackgroundColor(Color.parseColor("#dddddd"));
// set custom chart offsets (automatic offset calculation is hereby disabled)
chart.setViewPortOffsets(10, 0, 10, 0);
// add data
chart.setData(data);
// get the legend (only possible after setting data)
Legend l = chart.getLegend();
l.setEnabled(false);
chart.getAxisLeft().setEnabled(false);
chart.getAxisRight().setEnabled(false);
chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
chart.getXAxis().setEnabled(false);
chart.getXAxis().setDrawLabels(true);
chart.getXAxis().setTextColor(context.getResources().getColor(R.color.colorPrimary));
chart.getXAxis().setTypeface(Typeface.SANS_SERIF);
YAxis yAxis = chart.getAxisLeft();
yAxis.setAxisMinimum(0f);
yAxis.setAxisMaximum(100f);
final String[] weeks = new String[52];
for(int i = 0; i < weeks.length; i++) weeks[i] = "Week " + (i+1);
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return weeks[(int) value];
}
};
XAxis xAxis = chart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setValueFormatter(formatter);
// animate calls invalidate()...
chart.animateXY(2000,2500);
}
我不确定我做错了什么。
我通过增加偏移量解决了这个问题:
chart.setViewPortOffsets(60, 0, 50, 60);
我的图表没有显示 x-axis 和 y-axis。我删除了 x-axis 和 y-axis 出现的这一行。
当这条线存在时lineChart.setViewPortOffsets(0,0,0,0)
图表看起来像这样
当我删除这条线时 lineChart.setViewPortOffsets(0,0,0,0)
图表看起来像这样
linechart.setExtraBottomOffset(ff)
修改偏移量也可以帮助您解决这个问题。
调整 (ff) 浮点值以获得所需的值 space。
我想在折线图上添加 x 轴标签(使用 MPAndroidChart),但无论我做什么,都无法显示它们。我有一个名为 setupChart 的方法,它处理该图表的所有内容,这就是它的样子:
private void setupChart(LineChart chart, LineData data, int color) {
((LineDataSet) data.getDataSetByIndex(0)).setCircleColorHole(Color.WHITE);
((LineDataSet) data.getDataSetByIndex(0)).setCircleColor(color);
((LineDataSet) data.getDataSetByIndex(0)).setColors(dataColors);
data.getDataSetByIndex(0).setAxisDependency(YAxis.AxisDependency.LEFT);
((LineDataSet) data.getDataSetByIndex(0)).setDrawCircles(false);
// no description text
chart.getDescription().setEnabled(false);
// mChart.setDrawHorizontalGrid(false);
//
// enable / disable grid background
chart.setDrawGridBackground(false);
// enable touch gestures
chart.setTouchEnabled(true);
// disable scaling and dragging
chart.setDragEnabled(false);
chart.setScaleEnabled(false);
chart.setAutoScaleMinMaxEnabled(false);
// if disabled, scaling can be done on x- and y-axis separately
chart.setPinchZoom(false);
chart.setBackgroundColor(Color.parseColor("#dddddd"));
// set custom chart offsets (automatic offset calculation is hereby disabled)
chart.setViewPortOffsets(10, 0, 10, 0);
// add data
chart.setData(data);
// get the legend (only possible after setting data)
Legend l = chart.getLegend();
l.setEnabled(false);
chart.getAxisLeft().setEnabled(false);
chart.getAxisRight().setEnabled(false);
chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
chart.getXAxis().setEnabled(false);
chart.getXAxis().setDrawLabels(true);
chart.getXAxis().setTextColor(context.getResources().getColor(R.color.colorPrimary));
chart.getXAxis().setTypeface(Typeface.SANS_SERIF);
YAxis yAxis = chart.getAxisLeft();
yAxis.setAxisMinimum(0f);
yAxis.setAxisMaximum(100f);
final String[] weeks = new String[52];
for(int i = 0; i < weeks.length; i++) weeks[i] = "Week " + (i+1);
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return weeks[(int) value];
}
};
XAxis xAxis = chart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setValueFormatter(formatter);
// animate calls invalidate()...
chart.animateXY(2000,2500);
}
我不确定我做错了什么。
我通过增加偏移量解决了这个问题:
chart.setViewPortOffsets(60, 0, 50, 60);
我的图表没有显示 x-axis 和 y-axis。我删除了 x-axis 和 y-axis 出现的这一行。
当这条线存在时lineChart.setViewPortOffsets(0,0,0,0)
图表看起来像这样
当我删除这条线时 lineChart.setViewPortOffsets(0,0,0,0)
图表看起来像这样
linechart.setExtraBottomOffset(ff)
修改偏移量也可以帮助您解决这个问题。
调整 (ff) 浮点值以获得所需的值 space。