簡體   English   中英

Androidplot圖表正在復制標簽值x軸和y軸

[英]Androidplot chart is duplicating label values x-axis & y-axis

我正在使用androidplot顯示圖形,標簽重復出現。 我搜索了論壇,並且stackoverflow尚未找到任何解決方案。

現在我注意到,如果x軸或y軸的arraysize小於10,則數組值將重復。

在此處輸入圖片說明

屏幕截圖顯示左軸重復值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;
    }

}

您正在通過將6天的范圍轉換為長值,然后將這些值內的總范圍細分為10個不同的細分來創建域值:您無法從6天的范圍中獲得10天的標簽。

要解決此問題,您可以減少要顯示的域段的數量,也可以顯示更多天的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM