Android Studio 组合图表崩溃
Android Studio combined chart crashes
在图表上按下时出现错误和应用程序崩溃。使用单个 LineChart 或 BarChart 我可以按视图或缩放。
实施 com.github.PhilJay:MPAndroidChart:v3.0.2
Log: 12-12 22:01:50.108 3804-3804/com.example.jer.chart8 E/InputEventReceiver: Exception dispatching input event. 12-12
22:01:50.108 3804-3804/com.example.jer.chart8 E/MessageQueue-JNI:Exception in MessageQueue callback: handleReceiveCallback 12-12
22:01:50.108 3804-3804/com.example.jer.chart8 E/MessageQueue-JNI: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
package com.example.jer.chart9;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private CombinedChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart = (CombinedChart) findViewById(R.id.mp_Combine);
mChart.getDescription().setText("This is testing Description");
mChart.setBackgroundColor(Color.WHITE);
mChart.setDrawGridBackground(true);
mChart.setDrawBarShadow(true);
mChart.setHighlightFullBarEnabled(true);
// draw bars behind lines
mChart.setDrawOrder(new CombinedChart.DrawOrder[]{
CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE
});
Legend l = mChart.getLegend();
l.setWordWrapEnabled(true);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mMonths[(int) value % mMonths.length];
}
});
CombinedData data = new CombinedData();
data.setData( generateLineData());
data.setData(generateBarData());
xAxis.setAxisMaximum(data.getXMax() + 0.25f);
mChart.setData(data);
mChart.invalidate();
mChart.animateXY(2000, 2000);
}
protected String[] mMonths = new String[] {
"Jan", "Feb", "Mar", "Apr", "May", "June"
};
private LineData generateLineData() {
LineData d = new LineData();
ArrayList<Entry> entries = new ArrayList<Entry>();
entries = getLineEntriesData(entries);
LineDataSet set = new LineDataSet(entries, "Line");
//set.setColor(Color.rgb(240, 238, 70));
set.setColors(ColorTemplate.COLORFUL_COLORS);
set.setLineWidth(2.5f);
set.setCircleColor(Color.rgb(240, 238, 70));
set.setCircleRadius(5f);
set.setFillColor(Color.rgb(240, 238, 70));
set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set.setDrawValues(true);
set.setValueTextSize(10f);
set.setValueTextColor(Color.rgb(240, 238, 70));
set.setAxisDependency(YAxis.AxisDependency.LEFT);
d.addDataSet(set);
return d;
}
private ArrayList<Entry> getLineEntriesData(ArrayList<Entry> entries){
entries.add(new Entry(1, 20));
entries.add(new Entry(2, 10));
entries.add(new Entry(3, 8));
entries.add(new Entry(4, 40));
entries.add(new Entry(5, 37));
return entries;
}
private BarData generateBarData() {
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
entries = getBarEnteries(entries);
BarDataSet set1 = new BarDataSet(entries, "Bar");
//set1.setColor(Color.rgb(60, 220, 78));
set1.setColors(ColorTemplate.COLORFUL_COLORS);
set1.setValueTextColor(Color.rgb(60, 220, 78));
set1.setValueTextSize(10f);
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
float barWidth = 0.45f; // x2 dataset
BarData d = new BarData(set1);
d.setBarWidth(barWidth);
return d;
}
private ArrayList<BarEntry> getBarEnteries(ArrayList<BarEntry> entries){
entries.add(new BarEntry(1, 25));
entries.add(new BarEntry(2, 30));
entries.add(new BarEntry(3, 38));
entries.add(new BarEntry(4, 10));
entries.add(new BarEntry(5, 15));
return entries;
}
}
1:
1:
您需要在点击条形图时添加mChart.setHighlightFullBarEnabled(false);
或设置自己的突出显示,有关详细信息,请查看this
在图表上按下时出现错误和应用程序崩溃。使用单个 LineChart 或 BarChart 我可以按视图或缩放。
实施 com.github.PhilJay:MPAndroidChart:v3.0.2
Log: 12-12 22:01:50.108 3804-3804/com.example.jer.chart8 E/InputEventReceiver: Exception dispatching input event. 12-12
22:01:50.108 3804-3804/com.example.jer.chart8 E/MessageQueue-JNI:Exception in MessageQueue callback: handleReceiveCallback 12-12
22:01:50.108 3804-3804/com.example.jer.chart8 E/MessageQueue-JNI: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
package com.example.jer.chart9;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private CombinedChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart = (CombinedChart) findViewById(R.id.mp_Combine);
mChart.getDescription().setText("This is testing Description");
mChart.setBackgroundColor(Color.WHITE);
mChart.setDrawGridBackground(true);
mChart.setDrawBarShadow(true);
mChart.setHighlightFullBarEnabled(true);
// draw bars behind lines
mChart.setDrawOrder(new CombinedChart.DrawOrder[]{
CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE
});
Legend l = mChart.getLegend();
l.setWordWrapEnabled(true);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mMonths[(int) value % mMonths.length];
}
});
CombinedData data = new CombinedData();
data.setData( generateLineData());
data.setData(generateBarData());
xAxis.setAxisMaximum(data.getXMax() + 0.25f);
mChart.setData(data);
mChart.invalidate();
mChart.animateXY(2000, 2000);
}
protected String[] mMonths = new String[] {
"Jan", "Feb", "Mar", "Apr", "May", "June"
};
private LineData generateLineData() {
LineData d = new LineData();
ArrayList<Entry> entries = new ArrayList<Entry>();
entries = getLineEntriesData(entries);
LineDataSet set = new LineDataSet(entries, "Line");
//set.setColor(Color.rgb(240, 238, 70));
set.setColors(ColorTemplate.COLORFUL_COLORS);
set.setLineWidth(2.5f);
set.setCircleColor(Color.rgb(240, 238, 70));
set.setCircleRadius(5f);
set.setFillColor(Color.rgb(240, 238, 70));
set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set.setDrawValues(true);
set.setValueTextSize(10f);
set.setValueTextColor(Color.rgb(240, 238, 70));
set.setAxisDependency(YAxis.AxisDependency.LEFT);
d.addDataSet(set);
return d;
}
private ArrayList<Entry> getLineEntriesData(ArrayList<Entry> entries){
entries.add(new Entry(1, 20));
entries.add(new Entry(2, 10));
entries.add(new Entry(3, 8));
entries.add(new Entry(4, 40));
entries.add(new Entry(5, 37));
return entries;
}
private BarData generateBarData() {
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
entries = getBarEnteries(entries);
BarDataSet set1 = new BarDataSet(entries, "Bar");
//set1.setColor(Color.rgb(60, 220, 78));
set1.setColors(ColorTemplate.COLORFUL_COLORS);
set1.setValueTextColor(Color.rgb(60, 220, 78));
set1.setValueTextSize(10f);
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
float barWidth = 0.45f; // x2 dataset
BarData d = new BarData(set1);
d.setBarWidth(barWidth);
return d;
}
private ArrayList<BarEntry> getBarEnteries(ArrayList<BarEntry> entries){
entries.add(new BarEntry(1, 25));
entries.add(new BarEntry(2, 30));
entries.add(new BarEntry(3, 38));
entries.add(new BarEntry(4, 10));
entries.add(new BarEntry(5, 15));
return entries;
}
}
1:
1:
您需要在点击条形图时添加mChart.setHighlightFullBarEnabled(false);
或设置自己的突出显示,有关详细信息,请查看this