使用MpAndroidChart库自动滚动图

时间:2015-01-04 05:55:21

标签: android graph mpandroidchart

我正在使用MpAndroidChart库和DynamicalAdding活动来获取实时graph.my实时数据来自handler.I我将此数据传递给addEntry()函数。现在正确调用此函数并绘制一些值但是当图表需要时scrren上的所有可用空间都不会自动滚动。请给我修改解决方案代码。下面是我的代码:

public class DynamicalAddingActivity extends Activity implements OnChartValueSelectedListener {

BluetoothSerialService mSerialService = null;
private LineChart mChart;

Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        switch(msg.what){
        case BluetoothSerialService.SUCCESS_CONNECT:
            /*BluetoothSerialService.connectedThread = new BluetoothSerialService.ConnectedThread((BluetoothSocket)msg.obj);
            Toast.makeText(getApplicationContext(), "Connected!", 0).show();
            String s = "successfully connected";*/
            BluetoothSerialService.connectedThread.start();
            break;
        case BluetoothSerialService.MESSAGE_READ:

            byte[] readBuf = (byte[]) msg.obj;
            String strIncom;
            try {
                String mystr = new String(readBuf,"UTF-8");
                Log.d("strIncom", mystr);
                String[] params = mystr.split(" ");

                   for(int i=0;i<params.length;i++){

                   String temp = params[i];
                   temp=temp.trim();
                   String [] parts=temp.split(",");
                   int len=parts.length;
                   if(len==3){

                       //Accel-X
                       String x=parts[0];
                       x=x.replace("0","");
                       x=x.trim();
                       double value1;
                       int c1=x.indexOf("-,1");
                       int count1=StringUtils.countMatches(x, "-");


                       if( x.length()==1&& x.indexOf("-")==0){
                           value1=1.0;
                       }
                       else if(x==" "&&x.isEmpty()==true){
                           value1=1.0;
                       }
                       else if(x.length()==0){
                           value1=1.0;
                   }
                       else if(count1>1 &&c1!=-1){
                           value1=1.0;
                       }
                       else if(x.indexOf("-",1)!=-1){
                           value1=1.0;
                       }
                       else{
                           value1 = Double.parseDouble(x);
                       }
                       Log.d("Accel-X",String.valueOf(value1));
                       addEntry(value1);

            }}} 

            catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 



            break;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_linechart_noseekbar);
    BluetoothSerialService.gethandler(mHandler);
    mChart = (LineChart) findViewById(R.id.chart1);
    mChart.setOnChartValueSelectedListener(this);
    mChart.setDrawYValues(false);
    mChart.setDrawGridBackground(false);
    mChart.setDescription("");
    mChart.scrollBy(100, 0);
    addEmptyData();

    mChart.invalidate();
}

int[] mColors = ColorTemplate.VORDIPLOM_COLORS;

private void addEntry(double value1) {
    float v=(float) value1;
    LineData data = mChart.getData();

    if(data != null) {

        LineDataSet set = data.getDataSetByIndex(0);
        // set.addEntry(...);

        if (set == null) {
            set = createSet();
            data.addDataSet(set);
        }

        data.addEntry(new Entry( v, set.getEntryCount()), 0);

        // let the chart know it's data has changed
        mChart.notifyDataSetChanged();

        // redraw the chart
        mChart.invalidate();   
    }
}




private void removeDataSet() {

    LineData data = mChart.getData();

    if(data != null) {

        data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));

        mChart.notifyDataSetChanged();
        mChart.invalidate();   
    }
}

private void addEmptyData() {

    // create 30 x-vals
    String[] xVals = new String[30];

    for (int i = 0; i < 30; i++)
        xVals[i] = "" + i;

    // create a chartdata object that contains only the x-axis labels (no entries or datasets)
    LineData data = new LineData(xVals);

    mChart.setData(data);
    mChart.invalidate();
}

@Override
public void onValueSelected(Entry e, int dataSetIndex) {
    Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected() {

}


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));

    return set;
}
}

1 个答案:

答案 0 :(得分:0)

添加新条目时需要更新xVals。这将解决您的问题。

将您的ArrayList设为全局 ArrayList xVals = new ArrayList();

当您添加新的entre时,请更新列表(例如) xVals.add("val"+i);