AChartEngine组合的TimeChart和ScatterChart,X轴日期

尼克·瓦拉诺斯(Nick Valanos)

我创建了一个图表,其中在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轴显示真实日期?

尼克·瓦拉诺斯(Nick Valanos)

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

https://github.com/hdodenhof/achartengine

归功于创作者的出色表现。我测试了它,效果很好。唯一要做的就是将整个项目导入到您的工作空间中(如果使用Eclipse),并将其作为库导入到您的项目中:

  • 在“程序包资源管理器”中右键单击您的项目名称。
  • 特性
  • 安卓
  • 添加...
  • 选择带有导入项目名称的文件夹(achartengine-master)
  • 申请
  • 好的

现在,您可以像通常使用原始AChartEngine库那样使用该库了。记住要使用该方法ChartFactory.getCombinedTimeChartView(context, dataset, renderer, type)来获得带有所选图形的图表。

我在论坛上看到了许多相关问题,答案为“使用CombinedXYChart与LineChart和ScatterChart结合使用”,这不是一个不好的解决方案,但在我看来,以上是创建此类图表的更好,更轻松的方法。

希望能帮助到你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

高图日期/时间和X轴

来自分类Dev

X轴上的Gnuplot日期和时间

来自分类Dev

高图日期/时间和X轴

来自分类Dev

X和Y轴绘图日期

来自分类Dev

以辅助 y 轴和 x 轴作为日期的图表

来自分类Dev

将线性和日期时间x轴与Highcharts混合

来自分类Dev

MPAndroidChart:x 轴上的日期 - 粘性月份和年份

来自分类Dev

在 x 轴上带有日期和时间的烛台图

来自分类Dev

SSRS - 更改 x 轴以显示当天的日期和小时 - 问题

来自分类Dev

Achartengine,使用TimeChartView 2 x轴标签值

来自分类Dev

与aChartEngine中的X轴计数无关的绘图点

来自分类Dev

achartengine轴标签问题

来自分类Dev

AChartEngine-轴的线宽

来自分类Dev

如何在JavaFX ScatterChart中从点到X轴绘制一条线?

来自分类Dev

Amchart X轴日期格式

来自分类Dev

x轴刻度作为日期

来自分类Dev

散景:X轴日期格式和烛台图悬停日期问题

来自分类Dev

如何在带有日期的x轴上看到月份和年份?和y轴上的值?与Chartkick

来自分类Dev

SSRS 根据一个 X 轴 LABEL 上的两个日期组合两个值

来自分类Dev

如何使用achartengine使用TimeChartView在轴上显示特定日期周期

来自分类Dev

R:组合barplot和lineplot;x轴上的线点和刻度线未与小节图对齐

来自分类Dev

AChartEngine y轴编号范围

来自分类Dev

AmChart x轴数据日期格式,以小时,分钟和秒为单位显示每天的值

来自分类Dev

nPLot x轴日期变量和rCharts中的默认堆叠条形图

来自分类Dev

带有x轴的年份(无日)和“过滤器”开始日期的ggplot

来自分类Dev

matplotlib显示带有自定义日期格式和间隔的x轴

来自分类Dev

如何将x轴上的日期格式化为R中的月份和年份

来自分类Dev

如何使用Javascript和JSON数据在X轴上绘制日期格式

来自分类Dev

nPLot x轴日期变量和rCharts中的默认堆叠条形图

Related 相关文章

  1. 1

    高图日期/时间和X轴

  2. 2

    X轴上的Gnuplot日期和时间

  3. 3

    高图日期/时间和X轴

  4. 4

    X和Y轴绘图日期

  5. 5

    以辅助 y 轴和 x 轴作为日期的图表

  6. 6

    将线性和日期时间x轴与Highcharts混合

  7. 7

    MPAndroidChart:x 轴上的日期 - 粘性月份和年份

  8. 8

    在 x 轴上带有日期和时间的烛台图

  9. 9

    SSRS - 更改 x 轴以显示当天的日期和小时 - 问题

  10. 10

    Achartengine,使用TimeChartView 2 x轴标签值

  11. 11

    与aChartEngine中的X轴计数无关的绘图点

  12. 12

    achartengine轴标签问题

  13. 13

    AChartEngine-轴的线宽

  14. 14

    如何在JavaFX ScatterChart中从点到X轴绘制一条线?

  15. 15

    Amchart X轴日期格式

  16. 16

    x轴刻度作为日期

  17. 17

    散景:X轴日期格式和烛台图悬停日期问题

  18. 18

    如何在带有日期的x轴上看到月份和年份?和y轴上的值?与Chartkick

  19. 19

    SSRS 根据一个 X 轴 LABEL 上的两个日期组合两个值

  20. 20

    如何使用achartengine使用TimeChartView在轴上显示特定日期周期

  21. 21

    R:组合barplot和lineplot;x轴上的线点和刻度线未与小节图对齐

  22. 22

    AChartEngine y轴编号范围

  23. 23

    AmChart x轴数据日期格式,以小时,分钟和秒为单位显示每天的值

  24. 24

    nPLot x轴日期变量和rCharts中的默认堆叠条形图

  25. 25

    带有x轴的年份(无日)和“过滤器”开始日期的ggplot

  26. 26

    matplotlib显示带有自定义日期格式和间隔的x轴

  27. 27

    如何将x轴上的日期格式化为R中的月份和年份

  28. 28

    如何使用Javascript和JSON数据在X轴上绘制日期格式

  29. 29

    nPLot x轴日期变量和rCharts中的默认堆叠条形图

热门标签

归档