【问题标题】:Time Labels in GraphViewGraphView 中的时间标签
【发布时间】:2014-03-01 14:09:14
【问题描述】:

我之前发布了一个关于在 GraphView 库中使用 CustomFormatLabeler 将时间显示为 x-labels (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编程很陌生,所以一些建议会很棒。感谢阅读。

【问题讨论】:

    标签: android android-graphview


    【解决方案1】:

    我发现:

    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 时,您还需要有一个捕获的语句。

    【讨论】:

    • 您的 graphSeries 变量的数据类型是什么?我假设它是 GraphViewSeries,但它没有 get() 方法。此答案不完整。
    • 嗨隐形拉比,原来的graphview库声明了一个私有的最终List graphSeries;在顶部,因此 get(0).values 函数来自 List 类。我已经编辑了上面的内容以澄清这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多