栏位置安装Androidplot

时间:2013-12-08 11:00:03

标签: android androidplot

我正在尝试在androidplot中制作条形码。在我的域轴中,我放置了时间戳值。当我放置几天时间戳不同的图表值时,我之间的差距很大。你可以在下面的图片上看到它。

enter image description here

我希望我的酒吧在下面的屏幕上彼此相邻。

enter image description here

这可能在androidplot中吗?这是我的代码:

public class ChartActivity extends Activity
{
    private ArrayList<Measure> measures;
    private XYPlot plot;

    private MyBarFormatter formatter1;

    private Number minXSeriesValue;
    private Number maxXSeriesValue;
    private Number minYSeriesValue;
    private Number maxYSeriesValue;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart);

        measures = getIntent().getParcelableArrayListExtra(getString(R.string.constant_measures_intent_tag));

        plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
        plot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1);

        plot.setRangeLowerBoundary(0, BoundaryMode.FIXED);
        plot.getGraphWidget().setRangeLabelOrientation(-45);
        plot.getGraphWidget().setGridPadding(30, 10, 30, 0);


        plot.setTicksPerDomainLabel(2);

        plot.setDomainValueFormat(new MyDateFormat());
        plot.getGraphWidget().setDomainLabelOrientation(-45);
        plot.getGraphWidget().setDomainLabelVerticalOffset(5);



        plot.setRangeValueFormat(new Format() {
            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
                Number num = (Number) obj;
                switch(num.intValue()) {
                    case 1:
                        toAppendTo.append("1");
                        break;
                    case 2:
                        toAppendTo.append("2");
                        break;
                    case 3:
                        toAppendTo.append("3");
                        break;
                    case 4:
                        toAppendTo.append("4");
                        break;
                    case 5:
                        toAppendTo.append("5");
                        break;
                    case 6:
                        toAppendTo.append("6");
                        break;
                    case 7:
                        toAppendTo.append("7");
                        break;
                    case 8:
                        toAppendTo.append("8");
                        break;
                    case 9:
                        toAppendTo.append("9");
                        break;
                    case 10:
                        toAppendTo.append("10");
                        break;
                    default:
                        toAppendTo.append("");
                        break;
                }
                return toAppendTo;
            }

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

        List<Number> values = new ArrayList<Number>();
        List<Number> domain = new ArrayList<Number>();



        for(Measure measure : measures) {
            values.add(measure.getThreshold().getValue());
            domain.add(measure.getDate().getTime());


        }

        minXSeriesValue = domain.get(0);
        maxXSeriesValue = domain.get(0);

        for(int i=0 ;i<domain.size(); i++) {
            if(minXSeriesValue == null ||  minXSeriesValue.doubleValue() > domain.get(i).doubleValue())
                minXSeriesValue = domain.get(i);
            if(maxXSeriesValue == null || maxXSeriesValue.doubleValue() < domain.get(i).doubleValue())
                maxXSeriesValue = domain.get(i);
        }





        XYSeries series = new SimpleXYSeries(domain,values,"");

        plot.setRangeTopMin(11);
        plot.getLegendWidget().setVisible(false);


        formatter1 = new MyBarFormatter(Color.argb(200, 100, 150, 100), Color.LTGRAY);

        plot.addSeries(series, formatter1);

        MyBarRenderer renderer = ((MyBarRenderer)plot.getRenderer(MyBarRenderer.class));
        renderer.setBarRenderStyle(BarRenderer.BarRenderStyle.SIDE_BY_SIDE);
        renderer.setBarWidthStyle(BarRenderer.BarWidthStyle.FIXED_WIDTH);
        renderer.setBarWidth(10);
        renderer.setBarGap(5);

        plot.setDomainBoundaries(minXSeriesValue.longValue()-1,maxXSeriesValue.longValue()+1, BoundaryMode.FIXED);


        plot.getGraphWidget().getDomainSubGridLinePaint().setColor(Color.TRANSPARENT);

        plot.redraw();
    }

    class MyBarFormatter extends BarFormatter {
        public MyBarFormatter(int fillColor, int borderColor) {
            super(fillColor, borderColor);
        }

        @Override
        public Class<? extends SeriesRenderer> getRendererClass() {
            return MyBarRenderer.class;
        }

        @Override
        public SeriesRenderer getRendererInstance(XYPlot plot) {
            return new MyBarRenderer(plot);
        }
    }

    class MyBarRenderer extends BarRenderer<MyBarFormatter> {

        public MyBarRenderer(XYPlot plot) {
            super(plot);
        }


        //@Override
        // TODO: figure out why using @Override screws up the Maven builds
        protected MyBarFormatter getFormatter(int index, XYSeries series) { 
            return getFormatter(series);
        }
    }

    private class MyDateFormat extends Format { 

        private static final long serialVersionUID = 1L;    
        private SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd");// HH.mm");

        @Override
        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
            long timestamp = ((Number) obj).longValue();
            Date date = new Date(timestamp);
            return dateFormat.format(date, toAppendTo, pos);
        }

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

2 个答案:

答案 0 :(得分:1)

Androidplot可以做很多事情,虽然需要一些时间来适应。

在androidplot中,当您想要制作条形图时,您只能选择设置条形宽度的间隙宽度,但不能同时设置两者。能够编辑间隙宽度和条宽度可以将图形中条形的实际宽度设置为两个不同的值。

根据Androidplot网站上的文档http://androidplot.com/javadoc/0.6.0/com/androidplot/xy/BarRenderer.html,如果BarWidthStyle设置为setBarWidth,则只能使用FIXED_WIDTH方法,

    MyBarRenderer renderer = ((MyBarRenderer)plot.getRenderer(MyBarRenderer.class));
    renderer.setBarWidthStyle(BarRenderer.BarWidthStyle.FIXED_WIDTH);
    renderer.setBarWidth(10); // set the width of each bar; overlapping could occur though for large values

如果BarWidthStyle设置为setBarGap,则只能使用VARIABLE_WIDTH

    MyBarRenderer renderer = ((MyBarRenderer)plot.getRenderer(MyBarRenderer.class));
    renderer.setBarWidthStyle(BarRenderer.BarWidthStyle.VARIABLE_WIDTH);
    renderer.setBarGap(0f); // close the gap between bars

希望这有帮助!

答案 1 :(得分:0)

在代码中你应该增加最大的baundary plot.setDomainBoundaries(minXSeriesValue.longValue()-1,maxXSeriesValue.longValue()+1, BoundaryMode.FIXED);

将其设为

plot.setDomainBoundaries(minXSeriesValue.longValue()-1,maxXSeriesValue.longValue()+5, BoundaryMode.FIXED);

你会看到差距缩小