gpt4 book ai didi

android - 如何在 MPAndroidChart 中设置 xAxis 的字符串值?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:21 25 4
gpt4 key购买 nike

我想制作折线图,但我在 xAxis 中显示值字符串时遇到问题,我使用了来自 MPAndroidChart 的库 Github|到折线图。请帮助我如何添加字符串值以及实际上我想问的很多问题

private void drawLineChartLine(){

private float[] yDataL = {40, 60, 70, 80};
private String[] xDataL = {"Week 1", "Week 1" , "Week 3" , "Week 4"};


ArrayList<Entry> yEntrys = new ArrayList<>();

final ArrayList<String> xEntrys = new ArrayList<>();

for(int i = 0; i < yDataL.length; i++){
yEntrys.add(new Entry(yDataL[i] ,i));
}

for(int i = 1; i < xDataL.length; i++){
xEntrys.add(xDataL[i]);
}

//create the data set
LineDataSet lineDataset = new LineDataSet(yEntrys, "assa");

XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);


xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return xEntrys.get((int) value);
}
});

LineData lineData = new LineData(lineDataset);
lineChart.setData(lineData);
lineChart.invalidate();
}

我有错误

Invalid index 40, size is 6

在代码中

xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return xEntrys.get((int) value);
}
});

最佳答案

根据新更新 v3.1.0 IAxisValueFormatter 方法已弃用。

因此使用 com.github.mikephil.charting.formatter.IndexAxisValueFormatter我们可以在 x 轴上显示月份列表

private List<Entry> getIncomeEntries() {
ArrayList<Entry> incomeEntries = new ArrayList<>();

incomeEntries.add(new Entry(1, 11300));
incomeEntries.add(new Entry(2, 1390));
incomeEntries.add(new Entry(3, 1190));
incomeEntries.add(new Entry(4, 7200));
incomeEntries.add(new Entry(5, 4790));
incomeEntries.add(new Entry(6, 4500));
incomeEntries.add(new Entry(7, 8000));
incomeEntries.add(new Entry(8, 7034));
incomeEntries.add(new Entry(9, 4307));
incomeEntries.add(new Entry(10, 8762));
incomeEntries.add(new Entry(11, 4355));
incomeEntries.add(new Entry(12, 6000));

return incomeEntries.subList(0, 12);
}

在Oncreate方法中

ArrayList<ILineDataSet> dataSets = new ArrayList<>();
List<String> xAxisValues = new ArrayList<>(Arrays.asList("Jan", "Feb", "March", "April", "May", "June","July", "August", "September", "October", "November", "Decemeber"));
List<Entry> incomeEntries = getIncomeEntries();
dataSets = new ArrayList<>();
LineDataSet set1;

set1 = new LineDataSet(incomeEntries, "Income");
set1.setColor(Color.rgb(65, 168, 121));
set1.setValueTextColor(Color.rgb(55, 70, 73));
set1.setValueTextSize(10f);
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
dataSets.add(set1);

//customization
LineChart mLineGraph = findByViewId(R.id.line_chart);
mLineGraph.setTouchEnabled(true);
mLineGraph.setDragEnabled(true);
mLineGraph.setScaleEnabled(false);
mLineGraph.setPinchZoom(false);
mLineGraph.setDrawGridBackground(false);
mLineGraph.setExtraLeftOffset(15);
mLineGraph.setExtraRightOffset(15);
//to hide background lines
mLineGraph.getXAxis().setDrawGridLines(false);
mLineGraph.getAxisLeft().setDrawGridLines(false);
mLineGraph.getAxisRight().setDrawGridLines(false);

//to hide right Y and top X border
YAxis rightYAxis = mLineGraph.getAxisRight();
rightYAxis.setEnabled(false);
YAxis leftYAxis = mLineGraph.getAxisLeft();
leftYAxis.setEnabled(false);
XAxis topXAxis = mLineGraph.getXAxis();
topXAxis.setEnabled(false);


XAxis xAxis = mLineGraph.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setEnabled(true);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

set1.setLineWidth(4f);
set1.setCircleRadius(3f);
set1.setDrawValues(false);
set1.setCircleHoleColor(getResources().getColor(R.color.pie_color_4));
set1.setCircleColor(getResources().getColor(R.color.pie_color_4));

//String setter in x-Axis
mLineGraph.getXAxis().setValueFormatter(new com.github.mikephil.charting.formatter.IndexAxisValueFormatter(xAxisValues));

LineData data = new LineData(dataSets);
mLineGraph.setData(data);
mLineGraph.animateX(2000);
mLineGraph.invalidate();
mLineGraph.getLegend().setEnabled(false);
mLineGraph.getDescription().setEnabled(false);

关于android - 如何在 MPAndroidChart 中设置 xAxis 的字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45320457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com