【问题标题】:Using GraphView Library for functions to display multiple graphs使用 GraphView 库的函数来显示多个图形
【发布时间】:2014-09-01 18:51:47
【问题描述】:

我目前正在开发一个安卓应用程序,用于通过蓝牙读取多个传感器值并将它们显示在图表中。当我偶然发现 jjoe64 的 GraphViewLibrary 时,我知道这完全符合我的目的。但现在我有点卡住了。基本上,我写了一个小函数,它可以生成并在 3 个不同的图表中显示三个传感器的值。当活动首先启动时,这工作得很好,所有三个图表都很好地呈现和显示。但是,当我想使用 resetData() 方法更新具有不同值的图形以在每个图形中呈现新值时,只有三个图形中的最后一个被更新。显然,因为它是使用这个相当简单的函数生成的最后一个图。我的问题是:有没有其他优雅的方式来使用像我这样的函数来一个接一个地生成和更新所有三个图表?我已经尝试将 GraphView 变量设置回 null 以及删除和添加视图的不同组合。向函数传递单个 GraphView 变量(如 graphView1、graphView2...)也不起作用。

函数如下:

 private GraphView graphView;
 private GraphViewSeries graphViewSerie;
 private Boolean graphExisting = false;

...

public void makeGraphs (float[] valueArray, String heading, int graphId) {     
   String graphNumber = "graph"+graphId;
   int resId = getResources().getIdentifier(graphNumber,"id", getPackageName());
   LinearLayout layout = (LinearLayout) findViewById(resId);  

   int numElements = valueArray.length;
   GraphViewData[] data = new GraphViewData[numElements];       
   for (int c = 0; c<numElements; c++) {
       data[c] = new GraphViewData(c+1, valueArray[c]);
       Log.i(tag, "GraphView Graph"+graphId+": ["+(c+1)+"] ["+valueArray[c]+"].");
   }

   if (!graphExisting) {
       // init temperature series data            
       graphView = new LineGraphView(  
             this // context  
             , heading // heading  
       );    
       graphViewSerie = new GraphViewSeries(data);
       graphView.addSeries(graphViewSerie); 
       ((LineGraphView) graphView).setDrawBackground(true);       
       graphView.getGraphViewStyle().setNumHorizontalLabels(numElements); 
       graphView.getGraphViewStyle().setNumVerticalLabels(5);
       graphView.getGraphViewStyle().setTextSize(10);   
       layout.addView(graphView);
   }
   else {
       //graphViewSerie = new GraphViewSeries(data);
       //graphViewSerie.resetData(data);
        graphViewSerie.resetData(new GraphViewData[] {
                new GraphViewData(1, 1.2f)
                , new GraphViewData(2, 1.4f)
                , new GraphViewData(2.5, 1.5f) // another frequency
                , new GraphViewData(3, 1.7f)
                , new GraphViewData(4, 1.3f)
                , new GraphViewData(5, 1.0f)
                });

  }

这是取决于先前生成的数组的函数调用(正在监视以填充正确的值):

makeGraphs(graphData[0], "TempHistory", 1);
makeGraphs(graphData[1], "AirHistory", 2);
makeGraphs(graphData[2], "SensHistory", 3);
graphExisting = true;

非常感谢任何帮助和/或任何一般反馈!提前非常感谢!


编辑/更新: 感谢 jjoe64 的回答,我能够修改该功能以正常工作。我的想法显然有误,因为我认为我还会更改一个 GraphViewSeries 对象,我会将我的函数作为附加参数处理(我之前尝试过)。当然,这是行不通的。但是,通过这些小的改进,我设法使用 Graphviewseries 数组完成了这项工作。为了让遇到类似问题的人了解我必须改变什么,这里是解决方案的快速粗略的草稿。

我刚刚换了

private GraphViewSeries graphViewSerie;

private GraphViewSeries graphViewSerie[] = new GraphViewSeries[3]; 

并使用函数(if-clause)中已经给定的参数graphId访问正确的系列,如下所示:

int graphIndex = graphId - 1;
graphViewSerie[graphIndex] = new GraphViewSeries(data);

在 else 子句中,我同样通过调用来更新系列

graphViewSerie[graphIndex].resetData(data);

所以,再次感谢您的支持,jjoe64。很抱歉我没能早点更新这个问题,但我没有找到时间。

【问题讨论】:

    标签: graph bluetooth android-graphview


    【解决方案1】:

    当然它不能正常工作,因为您总是将最新的 graphseries-object 保存在成员 graphViewSerie 中。

    首先您必须存储 3 个不同的 graphviewseries(可能通过数组或映射),然后您必须在 else 子句中访问正确的 graphviewseries-object。

    【讨论】:

    • 非常感谢,帮助很大。我显然是想错了……问题已更新。
    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多