4

I tried to make graph, where x-axis contains dates as a string.So I use customLabelFormatter it gives me all dates of one year around 365 dates.

Now,the problem is x-axis show all dates but in very conjugated space so it looks like a straight line.

Here is my code :

public class TestActivity extends Activity {

private RelativeLayout grapahcontainer;

private String[] dates;
private Random rand;
private SimpleDateFormat dateFormat = new SimpleDateFormat("d/M");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    grapahcontainer = (RelativeLayout) findViewById(R.id.graph);
    rand = new Random();
    dates = new String[366];
    getDates();

    int size = dates.length;

    GraphViewData[] data = new GraphViewData[size];
    for (int i = 0; i < size; i++) {
        data[i] = new GraphViewData(i, rand.nextInt(800)); 
    }


    LineGraphView graphView = new LineGraphView(this, "GraphViewDemo");
    graphView.setCustomLabelFormatter(new CustomLabelFormatter() {

        @Override
        public String formatLabel(double value, boolean isValueX) {
            // TODO Auto-generated method stub
            if (isValueX) {
                return dateFormat.format(new Date());
            }
            return null;
        }
    });

    graphView.setDrawDataPoints(true);
    graphView.setDataPointsRadius(5f);

    // add data
    graphView.addSeries(new GraphViewSeries(data));


    graphView.getGraphViewStyle().setGridColor(Color.TRANSPARENT);
    graphView.getGraphViewStyle().setHorizontalLabelsColor(Color.GRAY);
    graphView.getGraphViewStyle().setVerticalLabelsColor(Color.GRAY);
    graphView.getGraphViewStyle().setTextSize(10);

    graphView.getGraphViewStyle().setVerticalLabelsWidth(30);

    graphView.setHorizontalLabels(dates);
    graphView.setVerticalLabels(getResources().getStringArray(
            R.array.verticalLbls));


    // set view port, start=0, size=8
    graphView.setViewPort(0, 8);
    graphView.setScrollable(true);

    RelativeLayout.LayoutParams graphViewpParams = new RelativeLayout.LayoutParams(
            300, 500);
    graphView.setLayoutParams(graphViewpParams);
    grapahcontainer.addView(graphView);
}

private void getDates() {

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    calendar.add(Calendar.DATE, -365);
    dates[0] = (dateFormat.format(calendar.getTime()));
    for (int i = 0; i < 365; i++) {
        calendar.add(Calendar.DATE, 1);
        String dateStr = dateFormat.format(calendar.getTime());
        // System.out.println("Date : " + dateStr);
        dates[i + 1] = dateStr;
    }
}

}

User_1191
  • 981
  • 2
  • 8
  • 24
Nikhil Sathawara
  • 161
  • 1
  • 2
  • 8

1 Answers1

1

you can set the count of labels with this two methods of GraphViewStyle:

> setNumHorizontalLabels(int numHorizontalLabels) 
> setNumVerticalLabels(int numVerticalLabels)
appsthatmatter
  • 6,347
  • 3
  • 36
  • 40
  • I'm having the same problem. I've tried to use the "setNumHorizontalLabels" but this does not work if you set your labels manually (using setHorizontalLabels()). Any idea of a workaround for this? – Oliver Drummond May 18 '14 at 22:37
  • 1
    if you use static labels (setHorizontalLabels()) it's completely up to you how many labels you use and thus setNumHorizontalLabels makes no sense. – appsthatmatter May 20 '14 at 10:38
  • Sure, but is it possible to use setHorizontalLabels() and set the maximum number of labels that appear at the screen? For example: I've used setHorizontalLabels() passing a list with 10 values, but I set the setNumHorizontalLabels to be only 5 values. With that, it would be great to show 5 values at the page, while being able to scroll for the rest of the values. – Oliver Drummond May 20 '14 at 16:24
  • static labels are not meant to scroll – appsthatmatter May 27 '14 at 13:45