AChartEngine-Combined TimeChart和ScatterChart,X轴日期

时间:2014-12-15 12:40:00

标签: android achartengine

我创建了一个图表,其中tamperatures显示在timeChart中。我需要用不同的颜色标记几个温度,所以我使用了XYCombinedChart并添加了一个ScatterChar,其中包含我想要标记的温度。一切都运作良好。唯一的问题是,为了使其工作,我在ScatterCharts的XYSeries中使用了毫秒,如下所示:

XYSeries pointSeries = new XYSeries("NotFromSensor");    
dataset.addSeries(pointSeries);    
int t = 0;    
for (t = 0; t < MainActivity.temps.size(); t++){    
    pointSeries.add(dt[t].getTime(), MainActivity.temps.get(t));    
}

创建图表的整个代码如下所示:

public class DeviceGraphActivity extends Activity {
private XYMultipleSeriesDataset dataset, pointDataset ;
private XYMultipleSeriesRenderer mRenderer;
private XYSeriesRenderer renderer1, pointRenderer;

public GraphicalView getDeviceView(Context context, String title){
    dataset = new XYMultipleSeriesDataset();
    clearDataset();// a set with all the data that will be displayed on the graph
    mRenderer = new XYMultipleSeriesRenderer();//determines the variables of the overall graph        
    int i;
    System.out.println(ShowGraphActivity.flag);
    if(DeviceFilterActivity.checkedDevices.isEmpty()){
        if(ShowGraphActivity.flag == true){

            MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE Time BETWEEN " +
                    "'"+(MainActivity.yy1)+"-"+((MainActivity.mm1))+"-"+(MainActivity.dd1)+"' " +
                            "AND '"+(MainActivity.yy2)+"-"+((MainActivity.mm2))+"-"+((MainActivity.dd2))+"'");
            setData(false);
            MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE FromSensor = 0 AND Time BETWEEN " +
                    "'"+(MainActivity.yy1)+"-"+((MainActivity.mm1))+"-"+(MainActivity.dd1)+"' " +
                            "AND '"+(MainActivity.yy2)+"-"+((MainActivity.mm2))+"-"+((MainActivity.dd2))+"'");
            setData(true);

        }
        else{
            MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures");
            setData(false);
            MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE FromSensor = 0");
            setData(true);
        }
        renderer1 = new XYSeriesRenderer();
        renderer1.setLineWidth(3);//sets graph line's width
        renderer1.setChartValuesTextSize(20);//sets the size of the text on the char
        renderer1.setColor(Color.BLUE);//sets the color of the graph
        renderer1.setDisplayBoundingPoints(true);
        mRenderer.addSeriesRenderer(renderer1);//adds the renderer(1 graph) to the overall graph

        pointRenderer = new XYSeriesRenderer();
        pointRenderer.setLineWidth(3);//sets graph line's width
        pointRenderer.setChartValuesTextSize(20);//sets the size of the text on the char
        pointRenderer.setPointStyle(PointStyle.CIRCLE);
        pointRenderer.setColor(Color.RED);//sets the color of the graph
        pointRenderer.setDisplayBoundingPoints(true);
        mRenderer.addSeriesRenderer(pointRenderer);
    }
    else{
        for(i = 0; i < DeviceFilterActivity.checkedDevices.size(); i++){
            if(ShowGraphActivity.flag){
                MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE DeviceID = "+DeviceFilterActivity.checkedDevices.get(i).getId()+" AND Time BETWEEN " +
                    "'"+(MainActivity.yy1)+"-"+((MainActivity.mm1))+"-"+(MainActivity.dd1)+"' " +
                            "AND '"+(MainActivity.yy2)+"-"+((MainActivity.mm2))+"-"+((MainActivity.dd2))+"'");
                setData(i, false);
                MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE FromSensor = 0 AND DeviceID = "+DeviceFilterActivity.checkedDevices.get(i).getId()+" AND Time BETWEEN " +
                        "'"+(MainActivity.yy1)+"-"+((MainActivity.mm1))+"-"+(MainActivity.dd1)+"' " +
                                "AND '"+(MainActivity.yy2)+"-"+((MainActivity.mm2))+"-"+((MainActivity.dd2))+"'");
                setData(i,true);
            }
            else{
                MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE DeviceID = "+DeviceFilterActivity.checkedDevices.get(i).getId());
                setData(i, false);
                MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE FromSensor = 0 AND DeviceID = "+DeviceFilterActivity.checkedDevices.get(i).getId());
                setData(i, true);
            }
            renderer1 = new XYSeriesRenderer();
            renderer1.setLineWidth(3);//sets graph line's width
            renderer1.setChartValuesTextSize(20);//sets the size of the text on the chart
            renderer1.setColor(DeviceFilterActivity.checkedColors.get(i));//sets the color of the graph
            renderer1.setDisplayBoundingPoints(true);
            mRenderer.addSeriesRenderer(renderer1);//adds the renderer(1 graph) to the overall graph 

            pointRenderer = new XYSeriesRenderer();
            pointRenderer.setLineWidth(3);//sets graph line's width
            pointRenderer.setChartValuesTextSize(20);//sets the size of the text on the chart
            pointRenderer.setColor(Color.RED);//sets the color of the graph
            pointRenderer.setDisplayBoundingPoints(true);
            mRenderer.addSeriesRenderer(pointRenderer);
        }
    }


    mRenderer.setLabelsTextSize(20);
    mRenderer.setPanEnabled(true);
    mRenderer.setZoomEnabled(true);
    mRenderer.setZoomButtonsVisible(true);
    mRenderer.setYAxisMax(50);
    mRenderer.setYAxisMin(0);
    mRenderer.setShowGrid(true);
    mRenderer.setMargins(new int[] { 50, 50, 25, 22 });
    mRenderer.setLegendTextSize(25);
    // we show the grid
    mRenderer.setInScroll(true);
    mRenderer.setChartTitle(title);
    mRenderer.setChartTitleTextSize(30);

    //------------------------

    mRenderer.setClickEnabled(false);//the graph can't be clicked so that it can be refreshed as well as be zoomed in or out 
    mRenderer.setSelectableBuffer(10);

    //---------------------------

    String[] types = new String[]{ new String(TimeChart.TYPE), new String(ScatterChart.TYPE)};
    //return ChartFactory.getTimeChartView(context, dataset, mRenderer, "YYYY-MM-DD");
    return ChartFactory.getCombinedXYChartView(context, dataset, mRenderer, types);//return a graph depending on the above declarations, that will be used as a view in the previous activity
}

public void setData(boolean arePoints){//sets the temperatures and dates data that will be used on the graph
    Date[] dt = new Date[MainActivity.dates.size()]; 
    for (int k = 0; k< MainActivity.dates.size(); k++){
        GregorianCalendar gc = new GregorianCalendar(MainActivity.dates.get(k).getYy(),
                MainActivity.dates.get(k).getMM()-1,MainActivity.dates.get(k).getDd(),
                MainActivity.dates.get(k).getHh(),MainActivity.dates.get(k).getMm(),
                MainActivity.dates.get(k).getSs());
        dt[k] = gc.getTime();
    }   
    if(!arePoints){
        TimeSeries series = new TimeSeries("Temperature");
        dataset.addSeries(series);
        int t = 0;
        for (t = 0; t < MainActivity.temps.size(); t++){
            series.add(dt[t] , MainActivity.temps.get(t));
        }
    }
    else{
        XYSeries pointSeries = new XYSeries("NotFromSensor");
        dataset.addSeries(pointSeries);
        int t = 0;
        for (t = 0; t < MainActivity.temps.size(); t++){
            pointSeries.add(dt[t].getTime(), MainActivity.temps.get(t));
        }
    }


}

public void setData(int i,boolean arePoints){//sets the temperatures and dates data that will be used on the graph
    Date[] dt = new Date[MainActivity.dates.size()]; 
    for (int k = 0; k< MainActivity.dates.size(); k++){
        GregorianCalendar gc = new GregorianCalendar(MainActivity.dates.get(k).getYy(),
                MainActivity.dates.get(k).getMM()-1,MainActivity.dates.get(k).getDd(),
                MainActivity.dates.get(k).getHh(),MainActivity.dates.get(k).getMm(),
                MainActivity.dates.get(k).getSs());
        dt[k] = gc.getTime();
    }
    if(!arePoints){
        TimeSeries series = new TimeSeries("device "+DeviceFilterActivity.checkedDevices.get(i).getName());
        dataset.addSeries(series);
        int t = 0;
        for (t = 0; t < MainActivity.temps.size(); t++){
            series.add(dt[t] , MainActivity.temps.get(t));
        }
    }
    else{
        XYSeries pointSeries = new XYSeries("NotFromSensor");
        dataset.addSeries(pointSeries);
        int t = 0;
        for (t = 0; t < MainActivity.temps.size(); t++){
            pointSeries.add(dt[t].getTime() , MainActivity.temps.get(t));
        }
    }
}



public void clearDataset(){
    this.dataset.clear();
}

}

显然,图表以毫秒为单位显示时间。如何使用TimeChart中的X轴并显示实际日期?

1 个答案:

答案 0 :(得分:0)

我刚刚找到了解决这个问题的方法。它是一个自定义库,类似于原始AChartEngine的扩展。它包含一个名为CombinedTimeChart的类。它与CombinedXYChart完全相同,但您可以使用正确的日期设置x轴,而无需手动设置其标签。我提供了获取图书馆的链接:

https://github.com/hdodenhof/achartengine

向创作者致信他的出色工作。我测试了它,效果很好。您唯一需要做的就是将整个项目导入您的工作区(如果使用Eclipse)并将其作为库导入项目中:

  • 在Package Explorer中右键单击您的项目名称。
  • 属性
  • 的Android
  • 添加...
  • 选择包含导入项目名称的文件夹(achartengine-master)
  • 应用

现在您已准备好使用该库,就像通常使用原始AChartEngine库一样。请记住使用方法ChartFactory.getCombinedTimeChartView(context, dataset, renderer, type)来获取带有选择图的图表。

我在论坛上看到很多相关的问题,答案是“使用CombinedXYChart与LineChart和ScatterChart”,这不是一个糟糕的解决方案,但在我看来,上面是一个更好,更简单的方法来创建这种图表。

希望它有所帮助。