如何将第一个域名标签从源

时间:2016-11-27 16:12:49

标签: java androidplot

我在我的应用程序中实现了AndroidPlot并且它工作正常但是我需要在原点之后的第一个值,因为日期不明确。

我在问题here中尝试了建议的解决方案,其中添加了setDomainValueFormat方法但是会显示错误消息:

  

“方法无法解决”

andriodplot

有任何建议如何在原点后一步启动x轴域?

plot = (XYPlot) findViewById(R.id.plot);

        XYSeries series = new SimpleXYSeries(Arrays.asList(dates_in_m_seconds), Arrays.asList(values_as_numbers), "BP Status");

        LineAndPointRenderer and configure them
        LineAndPointFormatter seriesFormat = new LineAndPointFormatter(Color.RED, Color.GREEN,null, null);


               plot.addSeries(series, seriesFormat);

        // Specify x and y axes labels amount
        plot.setRangeStep(StepMode.SUBDIVIDE,3);
        plot.setDomainStep(StepMode.SUBDIVIDE,dates.size());

               plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
            @Override

            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {


                Date date_Label = new Date(Math.round(((Number) obj).doubleValue()));

                return format.format(date_Label, toAppendTo, pos);
            }

            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return null;
            }
        });



        plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.LEFT).setFormat(new Format() {


            @Override

            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {

                Number num = (Number) obj;
                switch (num.intValue()) {
                    case 0:
                        toAppendTo.append("Low");
                        break;
                    case 1:
                        toAppendTo.append("Normal");
                        break;
                    case 2:
                        toAppendTo.append("High");
                        break;

                    default:
                        toAppendTo.append("Unknown");
                        break;
                }
                return toAppendTo;

            }

            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return null;
            }
        });


            }

1 个答案:

答案 0 :(得分:0)

有几种方法可以解决这个问题。从表面上看,似乎需要在xVals之间保持严格的均匀间距,但是因为你的xVals不仅是时间戳而是原始输入的圆形版本,你只能获得大致均匀的间距。如果要完全解决该问题,可以使用Y_VALS_ONLY实例化XYSeries,然后使用dates_as_m_seconds作为查找表来呈现域格式实例中的标签。

话虽如此,我已经提供了一个适用于您现有代码的解决方案。我没有尝试编译或运行它所以可能存在拼写错误,但总体思路将起作用:

  1. 手动设置绘图的域边界,以便在时间戳数据中的第一个元素之前包含一些额外的空格。
  2. 当您细分域区域时,添加一个额外的元素以为新的不可见步骤腾出空间
  3. 修改您的域名标签格式实例以跳过值<数据中的第一个时间戳,有效地使新的第一个元素不可见。
  4. 代码:

                // this value must be final in order to be visible to the anonymous inner class instance
                // of Format below.  Note that this will fail miserably if you try to plot a series of
                // size < 2.
                final double firstX = Math.round(((Number) series.getX(0)).doubleValue());
                double secondX = Math.round(((Number) series.getX(1)).doubleValue());
                double lastX = Math.round(((Number) series.getX(series.size() - 1)).doubleValue());
    
                // assuming the step interval is uniform, this is the distance between each xVal:
                double stepInterval = secondX - firstX;
    
                // add in extra space for the first "invisible index" by setting the starting
                // domain boundary exactly one interval before the first actual element:
                plot.setDomainBoundaries(firstX - stepInterval, lastX, BoundaryMode.FIXED);
    
                // add an extra "invisible index":
                plot.setDomainStep(StepMode.SUBDIVIDE,dates.size() + 1);
    
                ...
    
                plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
    
                    @Override
                    public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
                        double timestampMs = Math.round(((Number) obj).doubleValue());
    
                        // only render indices corresponding to values in the series:
                        if(timestampMs >= firstX) {
                            Date date_Label = new Date();
                            return format.format(date_Label, toAppendTo, pos);
                        }
                    }
    
                    @Override
                    public Object parseObject(String source, ParsePosition pos) {
                        return null;
                    }
                });