问题
I want to create a small application with a schedule. But for some reason, when I add a lot of points to the chart, it simply does not appear, although with a small number of points everything works.
void f() {
ArrayList<Entry> values = new ArrayList<>();
for (int i = x1, index = 0, j = y; i < x2; ++i, ++index)
{
float waveLength = (float) calib_a * i + (float) calib_b;
int pixel = rotated.getPixel(i, j);
float I = (Color.red(pixel) + Color.blue(pixel) + Color.green(pixel)) / 765.0f;
values.add(new Entry((int) waveLength, I));
Log.d("[SPECTRAl]", " WAVE: " +waveLength + " I: " + I);
// i += 4;
}
LineDataSet lineValues = new LineDataSet(values, "");
lineValues.setColor(Color.BLACK);
lineValues.setLineWidth(2f);
LineData line = new LineData(lineValues);
chart.getXAxis().setGranularity(100f);
chart.setData(line);
chart.invalidate();
}
But work with:
void f() {
ArrayList<Entry> values = new ArrayList<>();
values.add(new Entry(100, 6));
values.add(new Entry(200, 3));
values.add(new Entry(300, 2));
values.add(new Entry(400, 4));
LineDataSet lineValues = new LineDataSet(values, "");
lineValues.setColor(Color.BLACK);
lineValues.setLineWidth(2f);
LineData line = new LineData(lineValues);
chart.getXAxis().setGranularity(100f);
chart.setData(line);
chart.invalidate();
}
回答1:
Following code creating issues:
for (int i = x1, index = 0, j = y; i < x2; ++i, ++index)
{
float waveLength = (float) calib_a * i + (float) calib_b;
int pixel = rotated.getPixel(i, j);
float I = (Color.red(pixel) + Color.blue(pixel) + Color.green(pixel)) / 765.0f;
values.add(new Entry((int) waveLength, I));
Log.d("[SPECTRAl]", " WAVE: " +waveLength + " I: " + I);
// i += 4;
}
First you need to change casting of int into float into following line as this library get float values as input:
values.add(new Entry((float) waveLength, I));
Instead you can remove casting as your wavelength is already a float:
values.add(new Entry(waveLength, I));
Secondly:values.add(new Entry(100, 6));
this line work as values.add(new Entry(x, y));
So you need to give chart proper x values and corresponding y values to generate a graph. x values should be in proper sequence to create a chart if the value of x at n (here n is index of entry) is equals or less than value of x at n-1 at any point then you will not be able to generate a chart. So fixing the code of for loop above as I described your chart will work properly. Best of luck :)
来源:https://stackoverflow.com/questions/58173935/mpandroidchart-why-line-not-showing