【问题标题】:How to move the first domain label one step from origin如何将第一个域标签从原点移动一步
【发布时间】:2016-11-27 16:12:48
【问题描述】:

我已经在我的应用程序中实现了 AndroidPlot,它工作正常,但是由于日期不明确,我需要在 origin 之后一步的第一个值。

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

"方法无法解析"

任何建议如何在原点后一步启动 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;
            }
        });


            }

【问题讨论】:

  • 所以你想将你的域值向右移动一步,这样元素 0,0 的域和范围标签就不会重叠,对吗?您还可以发布您的实例化 XYSeries 的代码吗?
  • 是的,这就是我的意思,请检查代码,我用 xy 系列@Nick 更新帖子

标签: java androidplot


【解决方案1】:

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

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

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

代码:

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

【讨论】:

  • 您好,感谢您的帮助,但是当我尝试您的建议时,静态类型的错误显示它显示错误,此处不允许使用修饰符静态 @Nick
  • 我的错误 - 应该是最终的,而不是静态的。固定。
猜你喜欢
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多