2

我正在使用 achartengine 绘制折线图,​​以便在日期明智地显示血糖。我成功绘制了折线图。但是,我在这里挣扎,有一段时间我每天有不止一个血糖检查值。我不知道如何在同一日期在折线图中显示多个值。

Thanks Advance..

 Note: I need to show the line graph for multiple values each day. Normally Blood 
 sugar possible for checking for minimum 3 times per day.
 that's my problem.  



My Code : my code is very usefull for normal line graph generating..

    public class GraphActivity extends Activity {

private ArrayList<String> mMonthArrayList = new ArrayList<String>() ;

private GraphicalView mChart ;

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

    Toast.makeText(getApplicationContext(), "SecondActivity", 20).show();

    mMonthArrayList.add("01/01/2013");
    mMonthArrayList.add("02/01/2013");
    mMonthArrayList.add("03/01/2013");
    mMonthArrayList.add("04/01/2013");
    mMonthArrayList.add("05/01/2013");
    mMonthArrayList.add("06/01/2013");
    mMonthArrayList.add("07/01/2013");
    mMonthArrayList.add("08/01/2013");
    mMonthArrayList.add("09/01/2013");
    mMonthArrayList.add("10/01/2013");
    mMonthArrayList.add("11/01/2013");
    mMonthArrayList.add("12/01/2013");


    openChart();
}

private void openChart() 
{

    int xsideData[] = {0,2,3,4,6};
    int ysideData[] = {200,300,500,600,400};

    /*ArrayList<Integer> xsideData = new ArrayList<Integer>() ;
    xsideData.add(0);
    xsideData.add(1);
    xsideData.add(2);
    xsideData.add(3);
    xsideData.add(4);

    ArrayList<Integer> ysideData = new ArrayList<Integer>() ;
    ysideData.add(200);
    ysideData.add(300);
    ysideData.add(400);
    ysideData.add(600);
    ysideData.add(800);*/

    XYSeries xyseriesData = new XYSeries("XYSERIES TITLE");
    XYSeriesRenderer xyRenderer = new XYSeriesRenderer() ;
    XYMultipleSeriesDataset xymultipleDataset = new XYMultipleSeriesDataset();

    // Adding data to xside and yside Series
    for(int i = 0 ; i < xsideData.length; i++ ) {
        xyseriesData.add(xsideData[i], ysideData[i]);
    }

    xymultipleDataset.addSeries(xyseriesData);

    xyRenderer.setColor(Color.WHITE);
    xyRenderer.setPointStyle(PointStyle.CIRCLE);
    xyRenderer.setFillPoints(true);
    xyRenderer.setLineWidth(1);
    xyRenderer.setDisplayChartValues(true);

    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setXLabels(0);
    multiRenderer.setXTitle("Year 2012");
    //multiRenderer.setZoomButtonsVisible(true);   
    multiRenderer.setGridColor(Color.WHITE);
    multiRenderer.setShowGrid(true);

    multiRenderer.setLabelsTextSize(15);
    multiRenderer.setLegendTextSize(15);
    multiRenderer.setMargins(new int[] { 20, 30, 15, 0 });

    multiRenderer.setXLabels(7);
    multiRenderer.setYLabels(5);
    multiRenderer.setDisplayChartValues(false);
    //multiRenderer.setShowGrid(false);

    for(int i=0;i<xsideData.length;i++)
    {
        multiRenderer.addXTextLabel(i, mMonthArrayList.get(i));     
    }       
    multiRenderer.addSeriesRenderer(xyRenderer);

    LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
    // Creating a Line Chart
mChart = (GraphicalView) ChartFactory.
    getLineChartView(getBaseContext(), xymultipleDataset, multiRenderer);       
    // Adding the Line Chart to the LinearLayout
    chartContainer.addView(mChart);    
}

    }

      My output image : 
     ![enter image description here][1]
4

1 回答 1

3

看看这段代码......它工作正常

公共类 NewGraph 扩展 Activity {

private XYMultipleSeriesDataset mDataset;
private XYMultipleSeriesRenderer mRenderer;
List<double[]> values = new ArrayList<double[]>();
private GraphicalView mChartView;
private TimeSeries time_series;

// chart container
private LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    layout = (LinearLayout) findViewById(R.id.chart);

    // create dataset and renderer
    mDataset = new XYMultipleSeriesDataset();
    mRenderer = new XYMultipleSeriesRenderer();
    mRenderer.setAxisTitleTextSize(16);
    mRenderer.setChartTitleTextSize(20);
    mRenderer.setLabelsTextSize(15);
    mRenderer.setLegendTextSize(15);
    mRenderer.setPointSize(3f);

    XYSeriesRenderer r = new XYSeriesRenderer();
    r.setColor(Color.GREEN);
    r.setPointStyle(PointStyle.CIRCLE);
    r.setFillPoints(true);
    mRenderer.addSeriesRenderer(r);
    mRenderer.setClickEnabled(true);
    mRenderer.setSelectableBuffer(20);
    mRenderer.setPanEnabled(true);

    time_series = new TimeSeries("test");


    mDataset.addSeries(time_series);

    fillData();
    int pos=time_series.getItemCount();

    Log.v("newgraph", "pos"+pos);
    mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,
            "dd-MMM-yyyy");

    layout.addView(mChartView);
}

private void fillData() 
{
  //  long value = new Date().getTime() - 3 * TimeChart.DAY;
    final Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, 10);
    long mytimestamp1=c.getTimeInMillis();
    c.set(Calendar.HOUR_OF_DAY, 11);
    long mytimestamp2=c.getTimeInMillis();
    c.set(Calendar.HOUR_OF_DAY, 12);
    long mytimestamp3=c.getTimeInMillis();
    c.set(Calendar.HOUR_OF_DAY, 13);
    long mytimestamp4=c.getTimeInMillis();
    c.set(Calendar.HOUR_OF_DAY, 14);
    long mytimestamp5=c.getTimeInMillis();
    c.set(Calendar.HOUR_OF_DAY, 15);
    long mytimestamp6=c.getTimeInMillis();
    c.set(Calendar.HOUR_OF_DAY, 16);

    c.add(Calendar.DATE, 1);
    c.set(Calendar.HOUR_OF_DAY,12);
    long mytimestamp7=c.getTimeInMillis();

    time_series.add(mytimestamp1, 10);
    time_series.add(mytimestamp2, 20);
    time_series.add(mytimestamp3, 30);
    time_series.add(mytimestamp4, 40);
    time_series.add(mytimestamp5, 50);
    time_series.add(mytimestamp6, 60);
    time_series.add(mytimestamp7,30);
    /*time_series.add(new Date(mytimestamp1), 10);
    time_series.add(new Date(mytimestamp2), 20);
    time_series.add(new Date(mytimestamp3), 30);
    time_series.add(new Date(mytimestamp4), 40);
    time_series.add(new Date(mytimestamp5), 50);
    time_series.add(new Date(mytimestamp6), 60);
    */

}
于 2013-02-15T09:26:57.487 回答