我正在使用MPAndroidChart library。我在ListView中有多个散点图。每个图表包含365个x值(一年中的每一天)。 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;
}
}
答案 0 :(得分:0)
好的,通过阅读代码我可以看到以下可能导致您的问题。第一个与图表库有关,其他是与ListView
实施有关的问题。
setScaleMinima(...)
后致电centerViewPort(...)
和setData(...)
。ScatterChart
的项目),因此不需要方法getItemType()
。getItemViewType(...)
方法和getViewTypeCount(...)
方法。只需删除那些。如果您想在ListView
。让我知道这是否有效。