android - 使用 MPChart 库的折线图
问题描述
图表图像
我在 android 中使用折线图,使用 MPChart 库可以正常工作。我得到的只有一个问题是 x 轴标签的边距。我想在 x 轴标签中从左侧添加一些边距(在附图中按箭头显示)。我尝试了很多东西,但没有得到任何解决方案。
xAxis.setSpaceMax(20);
xAxis.setSpaceMin(20);
我也尝试了这个空间最小值和最大值,但使用它没有任何效果。我怎样才能做到这一点?
我的完整代码
ChartActivity 扩展了 Activity {
private LineChart lineChart;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_realm_wiki);
lineChart = (LineChart) findViewById(R.id.lineChart);
setup(lineChart);
// creating list of entry
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(2015, 50));
entries.add(new Entry(2016, 70));
entries.add(new Entry(2017, 75));
entries.add(new Entry(2018, 80));
entries.add(new Entry(2019, 90));
ArrayList<LineDataSet> lines = new ArrayList<LineDataSet>();
LineDataSet lDataSet1 = new LineDataSet(entries, "DataSet1");
lDataSet1.setColor(Color.BLACK);
lDataSet1.setCircleColor(Color.BLACK);
lines.add(lDataSet1);
lineChart.getXAxis().setXOffset(1000);
lineChart.setData(new LineData(lDataSet1));
}
protected void setup(Chart<?> chart) {
Typeface mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
// no description text
chart.getDescription().setEnabled(false);
// enable touch gestures
chart.setTouchEnabled(true);
LineChart lineChart = (LineChart) chart;
lineChart.setPinchZoom(false);
lineChart.setOnTouchListener(null);
if (chart instanceof BarLineChartBase) {
BarLineChartBase mChart = (BarLineChartBase) chart;
mChart.setDrawGridBackground(false);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.setDoubleTapToZoomEnabled(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
leftAxis.setTypeface(mTf);
leftAxis.setTextSize(8f);
leftAxis.setTextColor(Color.DKGRAY);
leftAxis.setValueFormatter(new PercentFormatter());
leftAxis.setLabelCount(8);
leftAxis.setAxisMinValue(50);
leftAxis.setAxisMaxValue(120);
leftAxis.setGranularity(1f);
leftAxis.setSpaceBottom(5);
leftAxis.setSpaceTop(5);
leftAxis.setDrawGridLines(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(mTf);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(8f);
xAxis.setTextColor(Color.DKGRAY);
xAxis.setDrawGridLines(false);
xAxis.setLabelCount(4, true);
xAxis.setAxisMinValue(2015);
xAxis.setAxisMaxValue(2018);
mChart.getAxisRight().setEnabled(false);
}
}
protected void styleData(ChartData data) {
data.setValueTextSize(8f);
data.setValueTextColor(Color.DKGRAY);
data.setValueFormatter(new PercentFormatter());
}
解决方案
推荐阅读
- delphi - 如何使用 DBLookupComboBox 在 Delphi 中的表单页面之间移动
- r - 错误 - cocor 中的变量必须是数字
- java - Grails Spring Security 中的 AbstractMethodError
- javascript - 如何在 node.js 中使用startsWith?
- c# - 序列化派生类时如何让xml数据合同序列化程序忽略基类属性
- c++ - mongo-cxx 驱动程序找不到 unique_ptr 的 polyfill
- r - 从浏览器的目录中查看保存的 LDAvis 图
- python - 尝试在 Python 中运行使用 Sage Math 的代码时出现 NameError 和 AttributeError
- html - element.style 和 element.setAttribute(attribute, value) 不一样?
- javascript - Javascript 是否可以生成 Cheerio 无法提取的 DOM html?