Вопрос

I posted a question earlier regarding using the CustomFormatLabeler in the GraphView library to display time as the x-labels (https://stackoverflow.com/questions/21567853/using-customlabelformatter-to-display-time-in-x-axis). I still can't find a solution yet, so I tried editing the GraphView library. I tried the solution suggested here: Using dates with the Graphview library

I modified:

public GraphViewData(double valueX, double valueY)

as suggested by adding a third input variable (String valueDate) and a method called getTime() which returns this string value. Then I modified generateHorlabels as shown below:

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;
}

I had to subtract 1 from the variable rounded because I was getting out of bounds error. This works better than the custom format labeller as there is no delay between the horizontal labels and real time. However, after about 600 data points,

rounded 

becomes greater than the length of

values

and I get the out of bounds error. Has anyone tried modifying the GraphView library to display time with success? I'm pretty new to java and android programming, so some advice would be great. Thanks for reading.

Это было полезно?

Решение

I found out that:

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

stops increasing in size when it reaches the maxDataCount set by the appendData function in the GraphViewData class. This is the reason I get the array index out of bounds error. Here's my solution. It's not the nicest looking code, but it seems to work. The original GraphView library declared private final List graphSeries; The .get(0).values is from the List class.

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;
}

If you're trying to do the same thing, give it a try, and let me know if something is wrong. I would love some feedback.

Edit: I should add that you also need to have a statement that catches when the graphSeries.size() is 0.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top