2013-01-21 3 views
1

안드로이드에 scatter plot 그래프를 만들고 싶고 사용자가 클릭 한 포인트의 값에 접근하고 싶습니다.

아무도 도와주세요. 안드로이드에서 그래프를 만드는 방법에 대한 아이디어가 없습니다. Graph View.jar 파일을 사용하여 그래프를 만들었지 만 플롯 값에 액세스 할 수 없습니다.android scatter plot 그래프

package com.example.test; 
import java.text.FieldPosition; 
import java.text.Format; 
import java.text.ParsePosition; 
import java.util.Arrays; 

import android.app.Activity; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.PointF; 
import android.os.Bundle; 
import android.view.Menu; 

import com.androidplot.series.XYSeries; 
import com.androidplot.xy.LineAndPointFormatter; 
import com.androidplot.xy.PointFormatter; 
import com.androidplot.xy.SimpleXYSeries; 
import com.androidplot.xy.XYPlot; 
import com.androidplot.xy.XYStepMode; 

public class MainActivity extends Activity { 

    private XYPlot xyPlot; 

    final String[] mMonths = new String[] { 
     "Jan","Feb", "Mar","Apr", "May","Jun", 
     "Jul", "Aug","Sep","Oct", "Nov","Dec" 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // initialize our XYPlot reference: 
     xyPlot = (XYPlot) findViewById(R.id.xyplot); 

     Number[] income = {2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 }; 
    // Number[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 }; 

     // Converting the above income array into XYSeries 
     XYSeries incomeSeries = new SimpleXYSeries(
      Arrays.asList(income),     // array => list 
      SimpleXYSeries.ArrayFormat.Y_VALS_ONLY , // Y_VALS_ONLY means use the element index as the x value 
      "Income");         // Title of this series 

     // Converting the above expense array into XYSeries 
//  XYSeries expenseSeries = new SimpleXYSeries(
//   Arrays.asList(expense),     // array => list 
//   SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value 
//   "Expense");        // Title of this series 

     // Create a formatter to format Line and Point of income series 

     PointFormatter pformater = new PointFormatter() { 

      @Override 
      public void draw(Canvas arg0, Number arg1, PointF arg2) { 
       // TODO Auto-generated method stub 

      } 
     }; 
     LineAndPointFormatter incomeFormat = new LineAndPointFormatter(
      Color.rgb(0, 0, 0),     // line color 
      Color.rgb(200, 200, 200),    // point color 
      null);         // fill color (none) 

     // Create a formatter to format Line and Point of expense series 
//  LineAndPointFormatter expenseFormat = new LineAndPointFormatter(
//   Color.rgb(0, 0, 0),     // line color 
//   Color.rgb(200, 200, 200),    // point color 
//   null);         // fill color (none) 
// 
     // add expense series to the xyplot: 
     // xyPlot.addSeries(expenseSeries,expenseFormat); 

     // add income series to the xyplot: 
     xyPlot.addSeries(incomeSeries, incomeFormat); 

     // Formatting the Domain Values (X-Axis) 
     xyPlot.setDomainValueFormat(new Format() { 

      @Override 
      public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { 
       return new StringBuffer(mMonths[ ((Number)obj).intValue() ] ); 
      } 

      @Override 
      public Object parseObject(String source, ParsePosition pos) { 
       return null; 
      } 
     }); 

     xyPlot.setDomainLabel(""); 
     xyPlot.setRangeLabel("Amount in Dollars"); 

     // Increment X-Axis by 1 value 
     xyPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1); 

     xyPlot.getGraphWidget().setRangeLabelWidth(50); 

     // Reduce the number of range labels 
     xyPlot.setTicksPerRangeLabel(2); 

     // Reduce the number of domain labels 
     xyPlot.setTicksPerDomainLabel(2); 

     // Remove all the developer guides from the chart 
     xyPlot.disableAllMarkup(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

답변

0

스노 든은 산포도, 선 그래프, 영역 그래프, 막대 그래프, 막대 차트 및 열 맵을 갖춘 안드로이드에 대한 간단한, 빠른, 그래프 라이브러리입니다 :

여기 내 코드입니다.

나는 this이 당신이 찾고있는 제품이라고 생각합니다.

희망이 5 월 당신을 :) 도움이

+0

답장을 보내 주셔서 감사하지만 코드를 사용하여 내 scat의 플롯 값에 액세스 할 수 있습니다. ter 그래프 및 만약 내가 레이아웃을 기본적으로 해당 라이브러리에 의해 제공되기 때문에 내가 포인트를 액세스 할 수없는 모든 라이브러리를 사용하는 경우 – user1996604

1

Download the jar file here

사용 레이아웃 XML 파일

<com.jjoe64.graphview.GraphView 
     android:layout_width="match_parent" 
     android:layout_height="200dip" 
     android:title="Graph Title" 
     android:id="@+id/graph" 
     android:layout_below="@+id/pointtext"/> 

사용하여 활동 자바 파일에 다음 코드에 다음 코드

import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.Toast; 

import com.jjoe64.graphview.GraphView; 
import com.jjoe64.graphview.series.DataPoint; 
import com.jjoe64.graphview.series.DataPointInterface; 
import com.jjoe64.graphview.series.OnDataPointTapListener; 
import com.jjoe64.graphview.series.PointsGraphSeries; 
import com.jjoe64.graphview.series.Series; 

public class MainActivity extends AppCompatActivity { 

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

     GraphView point_graph = (GraphView) findViewById(R.id.graph); 

    PointsGraphSeries<DataPoint> point_series = new PointsGraphSeries<DataPoint>(new DataPoint[]{ 
      new DataPoint(0, 2000), 
      new DataPoint(1, 2500), 
      new DataPoint(2, 2700), 
      new DataPoint(3, 3000), 
      new DataPoint(4, 3500), 
      new DataPoint(5, 2800), 
      new DataPoint(6, 3700), 
      new DataPoint(7, 3800), 
      new DataPoint(8, 3500), 
    }); 
    point_graph.addSeries(point_series); 
    point_series.setShape(PointsGraphSeries.Shape.RECTANGLE); 
    point_series.setColor(Color.RED); 
    point_series.setSize(18); 

    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(point_graph); 
    staticLabelsFormatter.setHorizontalLabels(new String[]{"Jan", "Feb", "March", "Apr", "May", "june", "Aug", "Sept", "OCt", "Nov", "Dec"}); 
    point_graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter); 
    point_series.setOnDataPointTapListener(new OnDataPointTapListener() { 
     @Override 
     public void onTap(Series series, DataPointInterface dataPoint) { 
      Toast.makeText(MainActivity.this, "Series1: On Data Point clicked: " + dataPoint, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
관련 문제