MPAndroidChart ライブラリを使用しています。ListView に複数の散布図があります。各チャートには 365 個の xvalues (年間を通して毎日) が含まれています。yvalues は 1 から 5 まで変化します。グラフの高さは 150 dp です。チャートを今年の現在の日 (xIndex) に中央揃えします。
holder.chart.setTouchEnabled(true);
holder.chart.setDragEnabled(true);
holder.chart.setScaleEnabled(false);
holder.chart.setScaleMinima(3f, 1f);
holder.chart.centerViewPort(xIndex, 3);
チャートを左にドラッグすると、散布図が消えます。例: yValue=1 で約 10 個のスキャッターが表示されます。左にドラッグすると、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;
}
}