0

我之前发布了一个关于在 GraphView 库中使用 CustomFormatLabeler 将时间显示为 x 标签的问题(https://stackoverflow.com/questions/21567853/using-customlabelformatter-to-display-time-in-x-axis) . 我仍然找不到解决方案,所以我尝试编辑 GraphView 库。我尝试了这里建议的解决方案:Using dates with the Graphview library

我修改了:

public GraphViewData(double valueX, double valueY)

正如通过添加第三个输入变量 (String valueDate) 和一个名为 getTime() 的方法所建议的那样,该方法返回此字符串值。然后我修改了generateHorlabels,如下图:

private String[] generateHorlabels(float graphwidth) {
    int numLabels = getGraphViewStyle().getNumHorizontalLabels()-1;
    if (numLabels < 0) {
        numLabels = (int) (graphwidth/(horLabelTextWidth*2));
    }

    String[] labels = new String[numLabels+1];
    double min = getMinX(false);
    double max = getMaxX(false);

    for (int i=0; i<=numLabels; i++) {
        Double temp =  min + ((max-min)*i/numLabels);
        int rounded =(int)Math.round(temp)-1;
        if(rounded < 0){
            labels[i] = " ";
        }else{
            if(graphSeries.size() > 0){
                GraphViewDataInterface[] values = graphSeries.get(0).values;
                if(values.length > rounded){
                    labels[i] = values[rounded].getTime();
                }else{
                    labels[i] = " ";
                }
            }
        }
    }
    return labels;
}

我不得不从四舍五入的变量中减去 1,因为我超出了边界错误。这比自定义格式标签器效果更好,因为水平标签和实时之间没有延迟。然而,在大约 600 个数据点之后,

rounded 

变得大于长度

values

我得到了越界错误。有没有人尝试修改 GraphView 库以成功显示时间?我对java和android编程很陌生,所以一些建议会很棒。谢谢阅读。

4

1 回答 1

0

我发现:

GraphViewDataInterface[] values = graphSeries.get(0).values;

当达到 GraphViewData 类中 appendData 函数设置的 maxDataCount 时,大小停止增加。这就是我得到数组索引越界错误的原因。这是我的解决方案。这不是最好看的代码,但它似乎工作。原始的 GraphView 库声明私有 final List graphSeries;.get(0).values 来自 List 类。

private String[] generateHorlabels(float graphwidth) {
    int numLabels = getGraphViewStyle().getNumHorizontalLabels()-1;
    if (numLabels < 0) {
        numLabels = (int) (graphwidth/(horLabelTextWidth*2));
    }

    String[] labels = new String[numLabels+1];
    double min = getMinX(false);
    double max = getMaxX(false);
    double temp = 0;

    GraphViewDataInterface[] values = graphSeries.get(0).values;

    for (int i=0; i<=numLabels; i++) {

        if( max < values.length){
            temp =  min + ((max-min)*i/numLabels);
        }else{
            temp = (values.length - (max-min)) + ((max-min)*i/numLabels);
        }
        int rounded =(int)Math.round(temp)-1;

        if(rounded < 0){
            labels[i] = " ";
        }else{
            if(values.length > rounded){
                labels[i] = values[rounded].getTime();
            }else{
                labels[i] = " ";
            }
        }
    }
    return labels;
}

如果您尝试做同样的事情,请尝试一下,如果有问题,请告诉我。我希望得到一些反馈。

编辑:我应该补充一点,当 graphSeries.size() 为 0 时,您还需要有一个捕获的语句。

于 2014-02-05T21:06:24.640 回答