Androidplot图表复制标签值x轴和& y轴

时间:2018-05-11 06:47:15

标签: java android graph charts androidplot

我使用androidplot来显示图形,标签重复出现。我搜索论坛和stackoverflow没有找到任何解决方案。

现在我注意到,如果x轴或y轴的数组大小小于10,则数组值会重复。

enter image description here

屏幕截图显示左轴重复值0,1,2。 x轴值重复出现。解决这个重复标签问题的最佳建议是什么?任何帮助将不胜感激。如果您需要任何进一步的信息,请告诉我。

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

}

0 个答案:

没有答案