MPAndroidChart - 为什么线不显示? [英] MPAndroidChart - why line not showing?
问题描述
我想创建一个带有时间表的小应用程序.但是由于某种原因,当我在图表中添加很多点时,它根本没有出现,尽管使用少量点一切正常.
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();
}
但与:
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();
}
推荐答案
以下代码创建问题:
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;
}
首先,您需要将 int 转换为 float 更改为以下行,因为此库将 float 值作为输入:
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));
其次:values.add(new Entry(100, 6));
这一行作为 values.add(new Entry(x, y));
因此您需要为图表提供适当的 x 值和相应的 y 值以生成图表.如果 n 处的 x 值(此处 n 是条目索引)在任何时候等于或小于 n-1 处的 x 值,则 x 值应按正确顺序创建图表,那么您将无法生成图表图表.因此,按照我描述的方式修复上面的 for 循环代码将可以正常工作.祝你好运:)
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 :)
这篇关于MPAndroidChart - 为什么线不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!