无法使用 MPAndroidChart 库优化折线图
Can't optimize line chart using MPAndroidChart library
我正在尝试制作一个好看的折线图 MPAndroidChart,但我遇到了一些问题。
这是我现在得到的
这是它的代码
public void createGraph() {
chart.setTouchEnabled(false);
chart.setDragEnabled(false);
chart.setHighlightEnabled(false);
chart.setDrawYLabels(true);
chart.setDrawXLabels(true);
chart.animateXY(2000, 2000);
chart.setDrawBorder(true);
chart.setPadding(30, 30, 30, 30);
setData();
}
private void setData() {
String[] stages = { "Stage1", "Stage2", "Stage3", "Stage4" };
ArrayList<Entry> yVals = new ArrayList<Entry>();
yVals.add(new Entry(0.0f, 0));
yVals.add(new Entry(Float.parseFloat(nicotineTwo), 1));
yVals.add(new Entry(Float.parseFloat(nicotineThree), 2));
yVals.add(new Entry(Float.parseFloat(nicotineFour), 3));
LineDataSet set1 = new LineDataSet(yVals, "");
set1.setColor(Color.BLACK);
set1.setCircleColor(Color.BLACK);
set1.setLineWidth(1f);
ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
dataSets.add(set1);
LineData data = new LineData(stages, dataSets);
chart.setData(data);
}
我需要的是:
- 去掉左下角那个黑框
- 使文本 "Nicotine Lowered" 更大一些且更易读
- 加点padding什么的,因为右上角的Stage4出屏了
- 使沿线的数字更大,如果可能的话使用百分比“25.0%”
- 隐藏行中的第一个值,因为它始终为 0
- 显示 Y 轴上的所有刻度值,从 0 到 100(现在没有 100)
请帮帮我,这个库让我很困惑
- "black-box"就是传说。
调用
chart.getLegend().setEnabled(false)
禁用它。
- 也就是"description-text"。调用
chart.setDescriptionTextSize(...)
自定义其大小。
有两种可能的解决方案。您可以通过 .xml 添加更多保证金,或调用
XAxis x = chart.getXAxis();
x.setAvoidFirstLastClipping(true);
- 要增加值的文本大小,请调用
data.setValueTextSize(...)
。对于图表内的自定义格式化值,请查看库提供的 ValueFormatter
界面。
- 也可以用
ValueFormatter
接口解决。例如if(value == 0) return "";
- 现在 y 轴上的值范围由图表自动确定。您可以通过调用
yAxis.setAxisMaxValue(...)
& yAxis.setAxisMinValue(...)
. 来设置固定范围的值
这里是 full documentation.
chart.setDescriptionTextSize(...)
的范围有限(7f 到 14f)。也许这就是您没有注意到任何变化的原因。
Javadoc
sets the size of the description text in pixels, min 7f, max 14f
我正在尝试制作一个好看的折线图 MPAndroidChart,但我遇到了一些问题。
这是我现在得到的
这是它的代码
public void createGraph() {
chart.setTouchEnabled(false);
chart.setDragEnabled(false);
chart.setHighlightEnabled(false);
chart.setDrawYLabels(true);
chart.setDrawXLabels(true);
chart.animateXY(2000, 2000);
chart.setDrawBorder(true);
chart.setPadding(30, 30, 30, 30);
setData();
}
private void setData() {
String[] stages = { "Stage1", "Stage2", "Stage3", "Stage4" };
ArrayList<Entry> yVals = new ArrayList<Entry>();
yVals.add(new Entry(0.0f, 0));
yVals.add(new Entry(Float.parseFloat(nicotineTwo), 1));
yVals.add(new Entry(Float.parseFloat(nicotineThree), 2));
yVals.add(new Entry(Float.parseFloat(nicotineFour), 3));
LineDataSet set1 = new LineDataSet(yVals, "");
set1.setColor(Color.BLACK);
set1.setCircleColor(Color.BLACK);
set1.setLineWidth(1f);
ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
dataSets.add(set1);
LineData data = new LineData(stages, dataSets);
chart.setData(data);
}
我需要的是:
- 去掉左下角那个黑框
- 使文本 "Nicotine Lowered" 更大一些且更易读
- 加点padding什么的,因为右上角的Stage4出屏了
- 使沿线的数字更大,如果可能的话使用百分比“25.0%”
- 隐藏行中的第一个值,因为它始终为 0
- 显示 Y 轴上的所有刻度值,从 0 到 100(现在没有 100)
请帮帮我,这个库让我很困惑
- "black-box"就是传说。
调用
chart.getLegend().setEnabled(false)
禁用它。 - 也就是"description-text"。调用
chart.setDescriptionTextSize(...)
自定义其大小。 有两种可能的解决方案。您可以通过 .xml 添加更多保证金,或调用
XAxis x = chart.getXAxis(); x.setAvoidFirstLastClipping(true);
- 要增加值的文本大小,请调用
data.setValueTextSize(...)
。对于图表内的自定义格式化值,请查看库提供的ValueFormatter
界面。 - 也可以用
ValueFormatter
接口解决。例如if(value == 0) return "";
- 现在 y 轴上的值范围由图表自动确定。您可以通过调用
yAxis.setAxisMaxValue(...)
&yAxis.setAxisMinValue(...)
. 来设置固定范围的值
这里是 full documentation.
chart.setDescriptionTextSize(...)
的范围有限(7f 到 14f)。也许这就是您没有注意到任何变化的原因。
Javadoc
sets the size of the description text in pixels, min 7f, max 14f