1
votes

How do I apply custom x-axis labels to my AChartEngine BarGraph while keeping the same zoom/pan functionality as when default labels are used? The problem is that I have 100+ data points so I only want to draw 5 or 6 labels on the screen at a time.

The default functionality originally draws 3 labels (0, 50, and 100 in my case) and when zooming in, 4/5 labels are drawn. When panning left or right, the labels move along with the corresponding bars:

enter image description hereenter image description here

I have attempted two approaches however neither of them give me the same zoom and pan functionality as the default labels.

1) Generate 5 evenly spaced labels when the graph is first created.

  • The problem with this is that it only creates 5 labels so when you zoom in too far, no labels are shown.

    //For 100 data points, add five labels every 20 datapoints
    mRenderer.addXTextLabel(0, "label 1");
    mRenderer.addXTextLabel(20, "label 2");
    mRenderer.addXTextLabel(40, "label 3");
    mRenderer.addXTextLabel(60, "label 4");
    mRenderer.addXTextLabel(80, "label 5");
    mRenderer.setXLabels(0);
    

2) Implement the ZoomListener and PanListener interfaces on my GraphicalView.

  • This solution almost works, however the labels are always in fixed locations and are just "updated" with a new value when the graph is panned left or right. I need them to move with their corresponding bars.

    graphicalView.addZoomListener( new ZoomListener() {
        @Override
        public void zoomApplied(ZoomEvent e) {
            double start = mRenderer.getXAxisMin();
            double stop = (double)mRenderer.getXAxisMax();
            double step = (double)(stop - start) / 5;
            mRenderer.clearXTextLabels();
            for (double i = start; i <= stop; i += step) 
                mRenderer.addXTextLabel(i+1, "label"+(int)i);
            mRenderer.setXLabels(0);
          }
    
        @Override
        public void zoomReset() { }
    }, true, true);
    
    graphicalView.addPanListener(new PanListener() {
        @Override
        public void panApplied() {
            double start = mRenderer.getXAxisMin();
            double stop = (double)mRenderer.getXAxisMax();
            double step = (double)(stop - start) / 5;
            mRenderer.clearXTextLabels();
            for (double i = start; i <= stop; i += step) 
                mRenderer.addXTextLabel(i+1, "label"+(int)i);
            mRenderer.setXLabels(0);                
        }
    });
    

The custom labels that I want to implement are time in minutes. Can this be done using the TimeSeries class with a bar chart?

1

1 Answers

1
votes

Your solution is quite good, so I can only suggest an improvement for getting to the labels behavior you need: Use MathHelper.getLabels(start, stop, approxNumLabels) instead of your for loop between start and stop.

So your for loop would become something like this:

List<Double> labels = MathHelper.getLabels(start, stop, 10);
for (Double label : labels) {
  mRenderer.addXTextLabel(label, "label " + label);
}