1

I am using androidplot to show a graph, the labels get repeated. i searched forums and stackoverflow haven't found any solution.

now i noticed, if arraysize for x-axis or y-axis is less than 10, the array values get duplicated.

enter image description here

The screenshots show the left axis repeating the value 0,1,2. x-axis values are getting repeated. What is your best suggestion on solving this repeating label issue? Any help would be grateful. If you need any further information let me know.

/**
 * A simple XYPlot
 */
public class SimpleXYPlotActivity extends Activity {

    private XYPlot plot;
    String[] domainLabels;
    String oldDateStr, newDateStr;
    Date newDate;
    int totalDays;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graph_xy_plot_example);

        // initialize our XYPlot reference:
        plot = (XYPlot) findViewById(R.id.plot);



        Intent intent = getIntent();
        Bundle bd = intent.getExtras();

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

        if (bd != null) {
            oldDateStr = (String) bd.get("START_DATE");
            newDateStr = (String) bd.get("END_DATE");

            plot.setTitle("From: "+oldDateStr+"     To: "+newDateStr);


            try {
                Date date1 = format.parse(oldDateStr);
                Date date2 = format.parse(newDateStr);
                long diff = date2.getTime() - date1.getTime();
                totalDays = Integer.parseInt(String.valueOf(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)));
                System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

        SimpleDateFormat formatterDateGraph = new SimpleDateFormat("MMM d");
        SimpleDateFormat formatterDate = new SimpleDateFormat("yyyy-MM-dd");


        try {
            newDate = format.parse(newDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        DatabaseHandler db = new DatabaseHandler(this);


        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(newDate);

        domainLabels= new String[totalDays];

        Number[] series1Numbers = new Number[totalDays];

        //domain 0 set outside loop for oldDateStr
        domainLabels[0]=String.valueOf(formatterDateGraph.format(newDate.getTime()));

        List<Orders> reportForDayList = db.getReportByTranType(oldDateStr, oldDateStr,"1");
        series1Numbers[0]=reportForDayList.size();



        for (int i = 1; i <totalDays ; i++) {
            cal.add(Calendar.DAY_OF_YEAR, -1);
            domainLabels[i] = String.valueOf(formatterDateGraph.format(cal.getTime()));

            //fetch ordercount for that day
            reportForDayList = db.getReportByTranType(formatterDate.format(cal.getTime()),formatterDate.format(cal.getTime()),"1");
            series1Numbers[i]=reportForDayList.size();
        }

        series1Numbers = reverseSeries(series1Numbers);
        domainLabels = reverseDomain(domainLabels);

        //reverseNumber array

//        domainLabels = reverseNumber(domainLabels);


        // turn the above arrays into XYSeries':
        // (Y_VALS_ONLY means use the element index as the x value)
        XYSeries series1 = new SimpleXYSeries(
                Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Sales");
/*        XYSeries series2 = new SimpleXYSeries(
                Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");*/

        // create formatters to use for drawing a series using LineAndPointRenderer
        // and configure them from xml:
        LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);

        LineAndPointFormatter series2Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);

        // add an "dash" effect to the series2 line:
       /* series2Format.getLinePaint().setPathEffect(new DashPathEffect(new float[] {

                // always use DP when specifying pixel sizes, to keep things consistent across devices:
                PixelUtils.dpToPix(20),
                PixelUtils.dpToPix(15)}, 0));*/

        // just for fun, add some smoothing to the lines:
        // see: http://androidplot.com/smooth-curves-and-androidplot/
        series1Format.setInterpolationParams(
                new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));

        series2Format.setInterpolationParams(
                new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));

        // add a new series' to the xyplot:
        plot.addSeries(series1, series1Format);
//        plot.addSeries(series2, series2Format);

        plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.LEFT).setFormat(new DecimalFormat("#"));

        plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
                int i = Math.round(((Number) obj).floatValue());
                return toAppendTo.append(domainLabels[i]);
            }
            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return null;
            }
        });
    }

    public static Number[] reverseSeries(Number[] a)
    {
        int l = a.length;
        for (int j = 0; j < l / 2; j++)
        {
            Number temp = a[j];
            a[j] = a[l - j - 1];
            a[l - j - 1] = temp;
        }
        return a;
    }
    public static String[] reverseDomain(String[] a)
    {
        int l = a.length;
        for (int j = 0; j < l / 2; j++)
        {
            String temp = a[j];
            a[j] = a[l - j - 1];
            a[l - j - 1] = temp;
        }
        return a;
    }

}
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61

1 Answers1

0

You are creating the domain values by transforming a 6 day range into long values and then subdividing the total range within those values into 10 distinct segments: You can't get 10 day labels from a range of 6 days.

To solve this problem you can either reduce the number of domain segments you are displaying or show more days worth of data.

Nick
  • 8,181
  • 4
  • 38
  • 63
  • i don't see where distinct segments is 10. Can you guide me how to change it? – Amir Dora. May 11 '18 at 12:21
  • 1
    [Heres a link](https://github.com/halfhp/androidplot/blob/master/docs/xyplot.md#domain--range-lines) to the documentation. Should be as simple as something like `plot.setDomainStep(BoundaryMode.SUBDIVIDE, 5);` – Nick May 11 '18 at 12:32
  • 1
    It seems the parameter for `.setDomainStep()` changed. The following worked for me: `plot.setDomainStep(StepMode.SUBDIVIDE, 3.0)` – Mario Codes Feb 06 '21 at 15:52