MPAndroidChart - setVisibleYRange 的问题,不适用于设置更高的值
MPAndroidChart - issues with setVisibleYRange, does not work for setting higher values
使用 MPandroiChart 我遇到了一个问题,我正在添加动态数据并试图让图表始终适合最高数据,当我需要缩小范围时使用 "setVisibleYRange()" 似乎工作正常视图,但当我需要视图在缩小视图后再次展开时似乎不起作用,基于 mpandroidChart 示例,我做了一个简单的测试 activity 来表明我的观点:
public class DynamicalAddingActivityTest extends Activity implements OnChartValueSelectedListener {
private LineChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linechart_noseekbar);
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
mChart.setDescription("");
// add an empty data object
mChart.setData(new LineData());
// mChart.getXAxis().setDrawLabels(false);
// mChart.getXAxis().setDrawGridLines(false);
mChart.invalidate();
for(int i = 0; i<10; i++){
addEntry();
}
mChart.setVisibleYRange(2, AxisDependency.LEFT);
mChart.invalidate();
findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addEntry();
}
});
findViewById(R.id.change).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText t = (EditText) findViewById(R.id.edit);
int vis = Integer.valueOf(t.getText().toString());
mChart.setVisibleYRange(vis, AxisDependency.LEFT);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
});
}
int[] mColors = ColorTemplate.VORDIPLOM_COLORS;
private void addEntry() {
LineData data = mChart.getData();
if(data != null) {
LineDataSet set = data.getDataSetByIndex(0);
// set.addEntry(...); // can be called as well
if (set == null) {
set = createSet();
data.addDataSet(set);
}
// add a new x-value first
data.addXValue(set.getEntryCount() + "");
// choose a random dataSet
int randomDataSetIndex = (int) (Math.random() * data.getDataSetCount());
data.addEntry(new Entry((float) (Math.random() * 10), set.getEntryCount()), randomDataSetIndex);
// let the chart know it's data has changed
mChart.notifyDataSetChanged();
// mChart.setVisibleXRange(6);
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
//
// // this automatically refreshes the chart (calls invalidate())
// mChart.moveViewTo(data.getXValCount()-7, 55f, AxisDependency.LEFT);
// redraw the chart
mChart.invalidate();
}
}
private void removeLastEntry() {
LineData data = mChart.getData();
if(data != null) {
LineDataSet set = data.getDataSetByIndex(0);
if (set != null) {
Entry e = set.getEntryForXIndex(set.getEntryCount() - 1);
data.removeEntry(e, 0);
// or remove by index
// mData.removeEntry(xIndex, dataSetIndex);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
}
private void addDataSet() {
LineData data = mChart.getData();
if(data != null) {
int count = (data.getDataSetCount() + 1);
// create 10 y-vals
ArrayList<Entry> yVals = new ArrayList<Entry>();
if(data.getXValCount() == 0) {
// add 10 x-entries
for (int i = 0; i < 10; i++) {
data.addXValue("" + (i+1));
}
}
for (int i = 0; i < data.getXValCount(); i++) {
yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));
}
LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);
int color = mColors[count % mColors.length];
set.setColor(color);
set.setCircleColor(color);
set.setHighLightColor(color);
set.setValueTextSize(10f);
set.setValueTextColor(color);
data.addDataSet(set);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
private void removeDataSet() {
LineData data = mChart.getData();
if(data != null) {
data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dynamical, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionAddEntry:
addEntry();
Toast.makeText(this, "Entry added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionRemoveEntry:
removeLastEntry();
Toast.makeText(this, "Entry removed!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionAddDataSet:
addDataSet();
Toast.makeText(this, "DataSet added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionRemoveDataSet:
removeDataSet();
Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionAddEmptyLineData:
mChart.setData(new LineData());
mChart.invalidate();
Toast.makeText(this, "Empty data added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionClear:
mChart.clear();
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private LineDataSet createSet() {
LineDataSet set = new LineDataSet(null, "DataSet 1");
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);
set.setColor(Color.rgb(240, 99, 99));
set.setCircleColor(Color.rgb(240, 99, 99));
set.setHighLightColor(Color.rgb(190, 190, 190));
set.setAxisDependency(AxisDependency.LEFT);
set.setValueTextSize(10f);
return set;
}
}
用这个 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change"
android:gravity="top|left"
android:id="@+id/change"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:gravity="top|center_horizontal"
android:id="@+id/add"
/>
<EditText
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|right"
android:id="@+id/edit"/>
</LinearLayout>
</RelativeLayout>
因此,当我尝试输入一个大于我之前设置的值时 "setVisibleYRange()" 似乎不起作用,但是如果设置的值低于之前设置的值,它确实有效。
关于这是为什么的任何想法?我错过了什么吗?
@PhilJay
是的,你错过了一些东西。
这不是 "zoom" 方法。它的唯一目的是限制在 y 轴上显示的最大可见值范围。
如果当前可见范围为 100,然后调用 setVisibleYRange(50)
,则可见范围将减少到 50,因为当前范围 100 不再允许。如果然后 setVisibleYRange(100)
被调用,则无需更改任何内容,因为当前可见范围 50 正好在允许范围内。如果您随后开始缩小,它将停在 100 范围内。
使用 MPandroiChart 我遇到了一个问题,我正在添加动态数据并试图让图表始终适合最高数据,当我需要缩小范围时使用 "setVisibleYRange()" 似乎工作正常视图,但当我需要视图在缩小视图后再次展开时似乎不起作用,基于 mpandroidChart 示例,我做了一个简单的测试 activity 来表明我的观点:
public class DynamicalAddingActivityTest extends Activity implements OnChartValueSelectedListener {
private LineChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linechart_noseekbar);
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
mChart.setDescription("");
// add an empty data object
mChart.setData(new LineData());
// mChart.getXAxis().setDrawLabels(false);
// mChart.getXAxis().setDrawGridLines(false);
mChart.invalidate();
for(int i = 0; i<10; i++){
addEntry();
}
mChart.setVisibleYRange(2, AxisDependency.LEFT);
mChart.invalidate();
findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addEntry();
}
});
findViewById(R.id.change).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText t = (EditText) findViewById(R.id.edit);
int vis = Integer.valueOf(t.getText().toString());
mChart.setVisibleYRange(vis, AxisDependency.LEFT);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
});
}
int[] mColors = ColorTemplate.VORDIPLOM_COLORS;
private void addEntry() {
LineData data = mChart.getData();
if(data != null) {
LineDataSet set = data.getDataSetByIndex(0);
// set.addEntry(...); // can be called as well
if (set == null) {
set = createSet();
data.addDataSet(set);
}
// add a new x-value first
data.addXValue(set.getEntryCount() + "");
// choose a random dataSet
int randomDataSetIndex = (int) (Math.random() * data.getDataSetCount());
data.addEntry(new Entry((float) (Math.random() * 10), set.getEntryCount()), randomDataSetIndex);
// let the chart know it's data has changed
mChart.notifyDataSetChanged();
// mChart.setVisibleXRange(6);
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
//
// // this automatically refreshes the chart (calls invalidate())
// mChart.moveViewTo(data.getXValCount()-7, 55f, AxisDependency.LEFT);
// redraw the chart
mChart.invalidate();
}
}
private void removeLastEntry() {
LineData data = mChart.getData();
if(data != null) {
LineDataSet set = data.getDataSetByIndex(0);
if (set != null) {
Entry e = set.getEntryForXIndex(set.getEntryCount() - 1);
data.removeEntry(e, 0);
// or remove by index
// mData.removeEntry(xIndex, dataSetIndex);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
}
private void addDataSet() {
LineData data = mChart.getData();
if(data != null) {
int count = (data.getDataSetCount() + 1);
// create 10 y-vals
ArrayList<Entry> yVals = new ArrayList<Entry>();
if(data.getXValCount() == 0) {
// add 10 x-entries
for (int i = 0; i < 10; i++) {
data.addXValue("" + (i+1));
}
}
for (int i = 0; i < data.getXValCount(); i++) {
yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));
}
LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);
int color = mColors[count % mColors.length];
set.setColor(color);
set.setCircleColor(color);
set.setHighLightColor(color);
set.setValueTextSize(10f);
set.setValueTextColor(color);
data.addDataSet(set);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
private void removeDataSet() {
LineData data = mChart.getData();
if(data != null) {
data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dynamical, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionAddEntry:
addEntry();
Toast.makeText(this, "Entry added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionRemoveEntry:
removeLastEntry();
Toast.makeText(this, "Entry removed!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionAddDataSet:
addDataSet();
Toast.makeText(this, "DataSet added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionRemoveDataSet:
removeDataSet();
Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionAddEmptyLineData:
mChart.setData(new LineData());
mChart.invalidate();
Toast.makeText(this, "Empty data added!", Toast.LENGTH_SHORT).show();
break;
case R.id.actionClear:
mChart.clear();
Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private LineDataSet createSet() {
LineDataSet set = new LineDataSet(null, "DataSet 1");
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);
set.setColor(Color.rgb(240, 99, 99));
set.setCircleColor(Color.rgb(240, 99, 99));
set.setHighLightColor(Color.rgb(190, 190, 190));
set.setAxisDependency(AxisDependency.LEFT);
set.setValueTextSize(10f);
return set;
}
}
用这个 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change"
android:gravity="top|left"
android:id="@+id/change"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:gravity="top|center_horizontal"
android:id="@+id/add"
/>
<EditText
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|right"
android:id="@+id/edit"/>
</LinearLayout>
</RelativeLayout>
因此,当我尝试输入一个大于我之前设置的值时 "setVisibleYRange()" 似乎不起作用,但是如果设置的值低于之前设置的值,它确实有效。
关于这是为什么的任何想法?我错过了什么吗? @PhilJay
是的,你错过了一些东西。
这不是 "zoom" 方法。它的唯一目的是限制在 y 轴上显示的最大可见值范围。
如果当前可见范围为 100,然后调用 setVisibleYRange(50)
,则可见范围将减少到 50,因为当前范围 100 不再允许。如果然后 setVisibleYRange(100)
被调用,则无需更改任何内容,因为当前可见范围 50 正好在允许范围内。如果您随后开始缩小,它将停在 100 范围内。