无法使用achartengine显示图形。

时间:2014-06-19 10:17:53

标签: achartengine

我正在从我的数据库中检索数据并存储在2个数组中。 1个数组由温度值组成,另一个包含日期。我正在使用achartengine的TimeChart,但仍然无法显示图表。当我使用x轴的随机值并使用LineChart时,它运行良好。 x轴标签是1970-01-01 5:30:00。似乎没有为图表采用我的数据库值。有人可以帮忙吗?

package com.ti.sensortag;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import DB.DBHandler;
import DB.Temperature;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.util.Log;
import android.view.Menu;
import android.widget.LinearLayout;

public class Temperature_Graph extends Activity {
DBHandler db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temperature__graph);

        GraphicalView mChart;
        db=new DBHandler(getApplicationContext());

        //reading data from database

        List<Temperature> temp=db.getAllTempReadings(); 
        int arraySize=temp.size();
        double TArray[]=new double[arraySize]; //array to hold temperature values
        String TSArray[]=new String[arraySize]; // array to hold timestamp values
        Date dateArray[]=new Date[TSArray.length]; // array to hold timestamp (String) converted to (date)

        for(int i=0;i<arraySize;i++){
            TArray[i]=Double.parseDouble(temp.get(i).getTemperature());
            TSArray[i]=temp.get(i).getTimestamp();

            //converting string to date
            SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
            try {

                Date result=sdf.parse(TSArray[i]);
                 dateArray[i]= result;
                 Log.d("dateArray",String.valueOf(dateArray[i]));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        //TimeSeries

        TimeSeries tSeries=new TimeSeries("Temperature");
        TimeSeries hSeries=new TimeSeries("35");       // hypothermia
        TimeSeries fSeries=new TimeSeries("37.5");     // fever
        TimeSeries nSeries=new TimeSeries("Normal-36.4");

        double hArray[]=new double[arraySize];
        double fArray[]=new double[arraySize];
        double nArray[]=new double[arraySize];
        double numbers[]=new double[arraySize];


        for(int i=0;i<TSArray.length;i++){
            hArray[i]=35;
            fArray[i]=37.5;
            nArray[i]=36.4;

            //adding values to the timeseries

            tSeries.add(dateArray[i], TArray[i]);
            hSeries.add(dateArray[i], hArray[i]);
            nSeries.add(dateArray[i], nArray[i]);
            fSeries.add(dateArray[i], fArray[i]); 
            }


        //Dataset
        XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();

        //adding series to the dataset
        dataset.addSeries(tSeries);
        dataset.addSeries(hSeries);
        dataset.addSeries(nSeries);
        dataset.addSeries(fSeries);

        //properties of temperature series
         XYSeriesRenderer tRenderer=new XYSeriesRenderer();
         tRenderer.setColor(Color.YELLOW);
         tRenderer.setPointStyle(PointStyle.DIAMOND);
         tRenderer.setFillPoints(true);
         final NumberFormat tempValueFormat=new DecimalFormat("##.##");
         tRenderer.setChartValuesFormat(tempValueFormat);
         tRenderer.setChartValuesTextSize(20);
         tRenderer.setDisplayChartValues(true);
         tRenderer.setChartValuesSpacing((float)0.5);
         tRenderer.setChartValuesTextSize(20);

        //properties of hSeries
        XYSeriesRenderer hRenderer=new XYSeriesRenderer();
         hRenderer.setColor(Color.MAGENTA);
         hRenderer.setLineWidth(3);

        //properties of fSeries
        XYSeriesRenderer fRenderer=new XYSeriesRenderer();
         fRenderer.setColor(Color.RED);
         fRenderer.setLineWidth(3);

        //properties of nSeries
        XYSeriesRenderer nRenderer=new XYSeriesRenderer();
         nRenderer.setColor(Color.CYAN);
         nRenderer.setLineWidth(3);
         fRenderer.setLineWidth(3);

         //properties of the chart

         XYMultipleSeriesRenderer mRenderer=new XYMultipleSeriesRenderer();
         mRenderer.setBackgroundColor(Color.BLACK);
         mRenderer.setPointSize(5);
         mRenderer.setLabelsColor(Color.GREEN);
         mRenderer.setChartTitle("TEMPERATURE LINE GRAPH");
         mRenderer.setChartTitleTextSize(30);
         mRenderer.setAxesColor(Color.WHITE);
         mRenderer.setApplyBackgroundColor(true);
         mRenderer.setAxisTitleTextSize(15);
         mRenderer.setXTitle("TIME");
         mRenderer.setYTitle("TEMPERATURE");
        // mRenderer.setZoomButtonsVisible(true);
         mRenderer.setXLabelsAngle(10);
         mRenderer.setYLabelsAlign(Align.LEFT);
         mRenderer.setYLabels(15);
         mRenderer.setXLabels(10);
         mRenderer.setXLabelsAlign(Align.RIGHT);
         mRenderer.setYLabelsAngle(10);
         mRenderer.setXAxisMin(0);
            mRenderer.setXAxisMax(30);
            mRenderer.setShowGridX(true);
             mRenderer.setShowGridY(true);
          mRenderer.setXLabelsAlign(Align.CENTER);
          mRenderer.setLabelsTextSize(20);
          //mRenderer.setLegendTextSize(20);
         // mRenderer.setLegendHeight(50);

          //Adding series to renderer
          mRenderer.addSeriesRenderer(tRenderer);
          mRenderer.addSeriesRenderer(hRenderer);
          mRenderer.addSeriesRenderer(nRenderer);
          mRenderer.addSeriesRenderer(fRenderer);

         LinearLayout layout=(LinearLayout)findViewById(R.id.graph);
         mChart=(GraphicalView)ChartFactory.getTimeChartView(getBaseContext(), dataset, mRenderer, "yyyy-MM-dd hh:mm:ss");
         layout.addView(mChart);




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.temperature__graph, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

TimeChart似乎不支持一次显示最少数量的点数。当我删除时 mRenderer.setXAxisMin(0); mRenderer.setXAxisMax(30); 图表显示出来。有没有办法使用TimeChart一次显示一定数量的点?