我正在使用MPAndroidChart 库。我在 ListView 中有多个散点图。每个图表包含 365 个 xvalues(一年中的每一天)。y 值从 1 到 5 不等。图表的高度为 150dp。我将图表集中在一年中的当天(xIndex)。
holder.chart.setTouchEnabled(true);
holder.chart.setDragEnabled(true);
holder.chart.setScaleEnabled(false);
holder.chart.setScaleMinima(3f, 1f);
holder.chart.centerViewPort(xIndex, 3);
当我在图表上向左拖动时,散点就消失了。例如:我看到大约 10 个 yValue=1 的散点图。当我向左拖动时,只要 yValue=1 的最右侧散点超出图表范围,所有其他具有相同 yValue 的散点也会消失。我怎样才能防止这种情况?
第一张图片,您会看到 3 个散点图。
在中间的图表上滑动后,您会看到出现 8 月 29 日,但绿色、蓝色和灰色的散点图消失了:
一些附加信息: listView 在片段内。该片段位于 ViewPager 中。ViewPager 位于 FragmentTabhost 的选项卡内
创建图表并将其添加到列表视图的代码:
for(int j = 0 ; j < rpya.size(); j++)
{
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, rpya.get(j).year);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DATE, 1);
Calendar caltarget = Calendar.getInstance();
caltarget.clear();
caltarget.set(Calendar.YEAR, rpya.get(j).year + 1);
caltarget.set(Calendar.MONTH, 0);
caltarget.set(Calendar.DATE, 1);
ArrayList<String> newxVals = new ArrayList<String>();
while (cal.before(caltarget)){
newxVals.add(new SimpleDateFormat(getString(R.string.day_month)).format(cal.getTime()).toString());
cal.add(Calendar.DAY_OF_YEAR, 1);
}
list.add(new ScatterChartItem(String.valueOf(rpya.get(j).year), generateScatterData(rpya.get(j).ratings, newxVals), getActivity().getApplicationContext()));
}
ChartDataAdapter cda = new ChartDataAdapter(getActivity().getApplicationContext(), list);
lv.setAdapter(cda);
添加数据、适配器和图表类型的代码:
protected ScatterData generateScatterData(ArrayList<SubjectNote> sn, ArrayList<String> xVals) {
ScatterChart.ScatterShape[] shapes = ScatterChart.getAllPossibleShapes();
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
ArrayList<Entry> yVals3 = new ArrayList<Entry>();
ArrayList<Entry> yVals5 = new ArrayList<Entry>();
ArrayList<Entry> yVals7 = new ArrayList<Entry>();
ArrayList<Entry> yVals9 = new ArrayList<Entry>();
ArrayList<Integer> pos1 = new ArrayList<Integer>();
ArrayList<Integer> pos3 = new ArrayList<Integer>();
ArrayList<Integer> pos5 = new ArrayList<Integer>();
ArrayList<Integer> pos7 = new ArrayList<Integer>();
ArrayList<Integer> pos9 = new ArrayList<Integer>();
int len = xVals.size();
for(int i = 0; i < sn.size(); i++) {
int pos = 0;
//determine position by xVals
pos = xVals.indexOf(new SimpleDateFormat(getString(R.string.day_month)).format(sn.get(i).date).toString());
Calendar cItem = Calendar.getInstance();
cItem.setTime(sn.get(i).date);
int year = sn.get(i).ranking;
switch (sn.get(i).ranking){
case 1:
if(pos1.indexOf(pos) < 0) {
yVals1.add(new Entry(year, pos));
pos1.add(pos);
}
break;
case 3:
if(pos3.indexOf(pos) < 0) {
yVals3.add(new Entry(year, pos));
pos3.add(pos);
}
break;
case 5:
if(pos5.indexOf(pos) < 0) {
yVals5.add(new Entry(year, pos));
pos5.add(pos);
}
break;
case 7:
if(pos7.indexOf(pos) < 0) {
yVals7.add(new Entry(year, pos));
pos7.add(pos);
}
break;
case 9:
if(pos9.indexOf(pos) < 0) {
yVals9.add(new Entry(year, pos));
pos9.add(pos);
}
break;
}
}
// create a dataset and give it a type
ScatterDataSet set1 = new ScatterDataSet(yVals1, "--");
set1.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set1.setColor(getResources().getColor(R.color.color1inner));
ScatterDataSet set3 = new ScatterDataSet(yVals3, "-");
set3.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set3.setColor(getResources().getColor(R.color.color3inner));
ScatterDataSet set5 = new ScatterDataSet(yVals5, "0");
set5.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set5.setColor(getResources().getColor(R.color.color5inner));
ScatterDataSet set7 = new ScatterDataSet(yVals7, "+");
set7.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set7.setColor(getResources().getColor(R.color.color7inner));
ScatterDataSet set9 = new ScatterDataSet(yVals9, "++");
set9.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set9.setColor(getResources().getColor(R.color.color9inner));
set1.setScatterShapeSize(10f);
set3.setScatterShapeSize(10f);
set5.setScatterShapeSize(10f);
set7.setScatterShapeSize(10f);
set9.setScatterShapeSize(10f);
ArrayList<ScatterDataSet> dataSets = new ArrayList<ScatterDataSet>();
dataSets.add(set1); // add the datasets
dataSets.add(set3);
dataSets.add(set5);
dataSets.add(set7);
dataSets.add(set9);
// create a data object with the datasets
ScatterData d = new ScatterData(xVals, dataSets);
return d;
}
private class ChartDataAdapter extends ArrayAdapter<ChartItem> {
public ChartDataAdapter(Context context, List<ChartItem> objects) {
super(context, 0, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getItem(position).getView(position, convertView, getContext());
}
@Override
public int getItemViewType(int position) {
// return the views type
return getItem(position).getItemType();
}
@Override
public int getViewTypeCount() {
return 4; // we have 4 different item-types
}
}
图表项:
public abstract class ChartItem {
protected static final int TYPE_BARCHART = 0;
protected static final int TYPE_LINECHART = 1;
protected static final int TYPE_PIECHART = 2;
protected static final int TYPE_SCATTERCHART = 3;
protected ChartData mChartData;
protected String mDescription;
protected Context mContext;
public ChartItem(String description, ChartData cd, Context c){
this.mChartData = cd;
this.mDescription = description;
this.mContext = c;
}
public abstract int getItemType();
public abstract View getView(int position, View convertView, Context c);
}
散点图本身:
public class ScatterChartItem extends ChartItem {
public ScatterChartItem(String Description, ChartData cd, Context c) {
super(Description, cd, c);
}
@Override
public int getItemType() {
return TYPE_SCATTERCHART;
}
@Override
public View getView(int position, View convertView, Context c) {
ViewHolder holder = null;
Calendar cal = Calendar.getInstance();
int xIndex = cal.get(Calendar.DAY_OF_YEAR);
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(c).inflate(
R.layout.list_item_scatterchart, null);
holder.chart = (ScatterChart) convertView.findViewById(R.id.chart);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Typeface mTf = Typeface.createFromAsset(c.getAssets(),"OpenSans-Light.ttf");
final ViewHolder mHolder = holder;
// apply styling
//holder.chart.setValueTypeface(mTf);
holder.chart.setDescription(mDescription);
holder.chart.setDrawVerticalGrid(false);
holder.chart.setDrawGridBackground(false);
holder.chart.setDrawYValues(false);
holder.chart.setHighlightIndicatorEnabled(false);
holder.chart.setDrawLegend(false);
holder.chart.setDrawYLabels(true);
holder.chart.setDrawHorizontalGrid(false);
holder.chart.setMaxVisibleValueCount(5);
holder.chart.setPinchZoom(true);
holder.chart.setTouchEnabled(true);
holder.chart.setDragEnabled(true);
holder.chart.setScaleEnabled(true);
holder.chart.setScaleMinima(3f, 1f);
holder.chart.centerViewPort(xIndex, 3);
holder.chart.setDrawingCacheEnabled(true);
XLabels xl = holder.chart.getXLabels();
xl.setCenterXLabelText(false);
xl.setPosition(XLabels.XLabelPosition.BOTTOM);
//xl.setTypeface(mTf);
YLabels yl = holder.chart.getYLabels();
yl.setTextColor(Color.TRANSPARENT);
//yl.setTypeface(mTf);
yl.setLabelCount(0);
// set data
holder.chart.setData((ScatterData) mChartData);
// do not forget to refresh the chart
//holder.chart.invalidate();
holder.chart.animateY(700);
return convertView;
}
private static class ViewHolder {
ScatterChart chart;
}
}