2013-12-23 1 views
0

나는 Bluetooth Low Energy를 통해 외부 센서에 연결되는 어플리케이션을 가지고 있습니다. 나는 3 개의 문자열로 분할 된 동적 문자열로 들어오는 데이터를 가져온 다음 두 개로 변환 된 문자열을 가지고 있습니다. 나는 그런ChartEngine으로 센서 데이터의 동적 그래프 - Android

ChartDroid 등 다양한 차트 라이브러리와 지금 주 동안 동적으로 복식을 그래프로 노력 해왔다

나는 aChartEngine와 함께 가기로 결정 주로 임의의 데이터를 그래프로 그려주는 dynamic sample app을 발견했기 때문입니다. 내 샘플 앱을 구현하려고 노력 해왔다. 그런 다음 센서 데이터 doubles을 그래프로 적용하기를 원합니다. 여기

나는 순간에 구현하기 위해 노력하고 코드입니다 :

public class XYChartBuilder extends Activity { 
public static final String TYPE = "type"; 

private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset(); 

private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); 

private XYSeries mCurrentSeries; 

private XYSeriesRenderer mCurrentRenderer; 

private String mDateFormat; 

private Button mNewSeries; 

private Button mAdd; 

private GraphicalView mChartView; 

private int index = 0; 

static double x = 0; 
static double y = 0; 

protected Update mUpdateTask; 

@Override 
protected void onRestoreInstanceState(Bundle savedState) { 
    super.onRestoreInstanceState(savedState); 
    mDataset = (XYMultipleSeriesDataset) savedState 
      .getSerializable("dataset"); 
    mRenderer = (XYMultipleSeriesRenderer) savedState 
      .getSerializable("renderer"); 
    mCurrentSeries = (XYSeries) savedState 
      .getSerializable("current_series"); 
    mCurrentRenderer = (XYSeriesRenderer) savedState 
      .getSerializable("current_renderer"); 
    mDateFormat = savedState.getString("date_format"); 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putSerializable("dataset", mDataset); 
    outState.putSerializable("renderer", mRenderer); 
    outState.putSerializable("current_series", mCurrentSeries); 
    outState.putSerializable("current_renderer", mCurrentRenderer); 
    outState.putString("date_format", mDateFormat); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.demo_opengl_acc); 

    mRenderer.setApplyBackgroundColor(true); 
    mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50)); 
    mRenderer.setAxisTitleTextSize(16); 
    mRenderer.setChartTitleTextSize(20); 
    mRenderer.setLabelsTextSize(15); 
    mRenderer.setLegendTextSize(15); 
    mRenderer.setMargins(new int[] { 20, 30, 15, 0 }); 
    mRenderer.setZoomButtonsVisible(true); 
    mRenderer.setPointSize(10); 
    mRenderer.setXTitle("TIME"); 
    mRenderer.setYTitle("y"); 
    mRenderer.setShowGrid(true); 

    String seriesTitle = "Series " + (mDataset.getSeriesCount() + 1); 
    XYSeries series = new XYSeries(seriesTitle); 
    mDataset.addSeries(series); 
    mCurrentSeries = series; 
    XYSeriesRenderer renderer = new XYSeriesRenderer(); 
    mRenderer.addSeriesRenderer(renderer); 
    renderer.setPointStyle(PointStyle.CIRCLE); 
    renderer.setFillPoints(true); 
    mCurrentRenderer = renderer; 

    mUpdateTask = new Update(); 
    mUpdateTask.execute(this); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if (mChartView == null) { 
     LinearLayout layout = (LinearLayout) findViewById(R.id.chart); 
     mChartView = ChartFactory.getLineChartView(this, mDataset, 
       mRenderer); 
     mRenderer.setClickEnabled(true); 
     mRenderer.setSelectableBuffer(100); 
     mChartView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       SeriesSelection seriesSelection = mChartView 
         .getCurrentSeriesAndPoint(); 
       double[] xy = mChartView.toRealPoint(0); 
       if (seriesSelection == null) { 
        Toast.makeText(XYChartBuilder.this, 
          "No chart element was clicked", 
          Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(
          XYChartBuilder.this, 
          "Chart element in series index " 
            + seriesSelection.getSeriesIndex() 
            + " data point index " 
            + seriesSelection.getPointIndex() 
            + " was clicked" 
            + " closest point value X=" 
            + seriesSelection.getXValue() + ", Y=" 
            + seriesSelection.getValue() 
            + " clicked point value X=" 
            + (float) xy[0] + ", Y=" 
            + (float) xy[1], Toast.LENGTH_SHORT) 
          .show(); 
       } 
      } 
     }); 
     mChartView.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       SeriesSelection seriesSelection = mChartView 
         .getCurrentSeriesAndPoint(); 
       if (seriesSelection == null) { 
        Toast.makeText(XYChartBuilder.this, 
          "No chart element was long pressed", 
          Toast.LENGTH_SHORT); 
        return false; // no chart element was long pressed, so 
            // let something 
        // else handle the event 
       } else { 
        Toast.makeText(XYChartBuilder.this, 
          "Chart element in series index " 
            + seriesSelection.getSeriesIndex() 
            + " data point index " 
            + seriesSelection.getPointIndex() 
            + " was long pressed", 
          Toast.LENGTH_SHORT); 
        return true; // the element was long pressed - the event 
            // has been 
        // handled 
       } 
      } 
     }); 
     mChartView.addZoomListener(new ZoomListener() { 
      public void zoomApplied(ZoomEvent e) { 
       String type = "out"; 
       if (e.isZoomIn()) { 
        type = "in"; 
       } 
       System.out.println("Zoom " + type + " rate " 
         + e.getZoomRate()); 
      } 

      public void zoomReset() { 
       System.out.println("Reset"); 
      } 
     }, true, true); 
     mChartView.addPanListener(new PanListener() { 
      public void panApplied() { 
       System.out.println("New X range=[" 
         + mRenderer.getXAxisMin() + ", " 
         + mRenderer.getXAxisMax() + "], Y range=[" 
         + mRenderer.getYAxisMax() + ", " 
         + mRenderer.getYAxisMax() + "]"); 
      } 
     }); 
     layout.addView(mChartView, new LayoutParams(
       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     boolean enabled = mDataset.getSeriesCount() > 0; 
    } else { 
     mChartView.repaint(); 
    } 
} 

private int generateRandomNum() { 
    Random randomGenerator = new Random(); 
    int randomInt = randomGenerator.nextInt(100); 
    return randomInt; 

} 

protected class Update extends AsyncTask<Context, Integer, String> { 
    @Override 
    protected String doInBackground(Context... params) { 

     int i = 0; 
     while (true) { 
      try { 
       Thread.sleep(50); 
       x = x + 5; 
       y = generateRandomNum(); 

       publishProgress(i); 
       i++; 
      } catch (Exception e) { 

      } 
     } 
     // return "COMPLETE!"; 
    } 

    // -- gets called just before thread begins 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 

     mCurrentSeries.add(x, y); 

     if (mChartView != null) { 
      mChartView.repaint(); 
     } 
     Bitmap bitmap = mChartView.toBitmap(); 
     try { 
      File file = new File(Environment.getExternalStorageDirectory(), 
        "test" + index++ + ".png"); 
      FileOutputStream output = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.PNG, 100, output); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    // -- called if the cancel button is pressed 
    @Override 
    protected void onCancelled() { 
     super.onCancelled(); 

    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
    } 
    } 
} 

와 나는 XML 레이아웃 응용 프로그램에 관련 페이지에 "그것을 합류"한 :

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".demo.DemoAccelerometerSensorActivity" > 

<sample.ble.sensortag.demo.DemoGLSurfaceView 
    android:id="@+id/gl" 
    android:layout_width="match_parent" 
    android:layout_height="93dp" /> 

<requestFocus /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="100dp" 
    android:text="Now Recording Data to file. Press button to add name to file" /> 

<TextView 
    android:id="@+id/AccValues" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Accelerometer" 
    android:textAppearance="?android:textAppearanceLarge" /> 

<Button 
    android:id="@+id/baddNametoFile" 
    android:layout_width="300dp" 
    android:layout_height="100dp" 
    android:layout_gravity="left" 
    android:layout_marginTop="250dp" 
    android:layout_weight="1" 
    android:background="#c0c0c0" 
    android:text="Add Name to File" /> 

<Button 
    android:id="@+id/Stop" 
    android:layout_width="300dp" 
    android:layout_height="100dp" 
    android:layout_gravity="right" 
    android:layout_marginTop="250dp" 
    android:layout_weight="1" 
    android:background="#FF0000" 
    android:text="Stop Recording" /> 

<EditText 
    android:id="@+id/ETName" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="190dp" 
    android:ems="10" 
    android:hint="Name Here" 
    android:inputType="textCapWords" 
    android:maxWidth="200dp" /> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="match_parent" 
    android:layout_height="81dp" 
    android:layout_margin="10dp" 
    android:textAppearance="?android:textAppearanceMedium" /> 


<LinearLayout 
    android:id="@+id/chart" 
    android:layout_width="600dp" 
    android:layout_height="match_parent" 
    android:layout_marginTop="430dp" 
    android:orientation="vertical" > 
</LinearLayout> 

프로젝트를 구현할 때의 문제는 응용 프로그램에 전혀 표시되지 않는다는 것입니다.

오류가 없습니다. 그냥 표시되지 않습니다. 나는 누군가가 도울 수 있는지 궁금해하고있다. 또는 다른 기본 동적 그래프 자습서를 알고 있다면 기존 프로젝트에 구현할 수 있습니다. 나는 15 (편집 - 지금은) 오전에만 지난 몇 개월 동안 자바를 시작했습니다. 나는 단지 기초에 아직도있다. 코드를 게시하면 설명해주십시오. 사전에

감사합니다, 다음과 같은

+1

코드는 잘 .I이이 문제가 레이아웃 또는 아무것도 else.post 전체 XML 레이아웃과 생각 바뀌고 동적 인 그래프의 표시 장치에서 확인할 수 있습니다. – keshav

+0

답장을 보내 주셔서 감사합니다. 전체 xml 코드를 포함하도록 제 질문을 수정했습니다. 예제가 제대로 작동한다는 것을 아는 것이 좋습니다. – Gurfuffle

+0

답변보기. 해결 된 것 같습니다. – keshav

답변

4
The problem is with your layout, it is not designed well. 

사용 뭔가. 몇 가지 사항을 조정해야 할 수도 있습니다. 작은 화면 장치에서 레이아웃이 제대로 표시되지 않습니다 .U 레이아웃을 편집했으며 작은 장치에서도 화면을 표시했습니다. ur 요구 사항에 따라 높이와 다른 사항을 조정하십시오.

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/AccValues" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="Accelerometer" 
      android:textAppearance="?android:textAppearanceLarge" /> 

     <View 
      android:id="@+id/gl" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" /> 

     <requestFocus /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Now Recording Data to file. Press button to add name to file" /> 

     <EditText 
      android:id="@+id/ETName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:hint="Name Here" 
      android:inputType="textCapWords" 
      android:maxWidth="200dp" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/baddNametoFile" 
       android:layout_width="match_parent" 
       android:layout_height="40dp" 
       android:layout_gravity="left" 
       android:layout_weight="1" 
       android:background="#c0c0c0" 
       android:text="Add Name to File" /> 

      <Button 
       android:id="@+id/Stop" 
       android:layout_width="match_parent" 
       android:layout_height="40dp" 
       android:layout_gravity="right" 
       android:layout_weight="1" 
       android:background="#FF0000" 
       android:text="Stop Recording" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="ABSGS " 
      android:textAppearance="?android:textAppearanceMedium" /> 

     <LinearLayout 
      android:id="@+id/chart" 
      android:layout_width="match_parent" 
      android:layout_height="164dp" 
      android:orientation="vertical" > 
     </LinearLayout> 
    </LinearLayout> 

</FrameLayout> 

enter image description here

+0

수정 사항을 확인하고 몇 가지 사항을 변경했습니다. – keshav

+0

UR 앱을 실행하는 위치는 어디입니까? – keshav

+0

응답 해 주셔서 감사합니다. 불행히도 여전히 나타나지 않습니다. 나는 태블릿에서 실행 중이며 Nexus 7 2013 – Gurfuffle