Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

android - How do I remove all space around a chart in AndroidPlot?

I've got a minimalist chart using androidPlot in an application, and have been trying to remove extraneous visual elements. I've got everything done, except that I cannot get rid of some empty spaces around the chart itself within the view. Here's what it looks like with markup turned on, just sitting in the root view of an Activity:

What my chart looks like

How can I eliminate the black space between the inner chart boundaries and the outer view boundary? Here's my code:

    mDynamicPlot = (XYPlot) findViewById(R.id.dynamicPlot);
    mDynamicPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("0"));
    mDynamicPlot.addSeries(mCpuData, new LineAndPointFormatter(Color.rgb(51, 204, 255), null, Color.rgb(8, 153, 194)));
    mDynamicPlot.setDomainStepMode(XYStepMode.INCREMENT_BY_VAL);
    mDynamicPlot.setRangeBoundaries(0, 100, BoundaryMode.FIXED);
    mDynamicPlot.setDomainStepValue(10);
    mDynamicPlot.setRangeStepValue(5.0d);
    mDynamicPlot.setRangeValueFormat(new DecimalFormat("#"));

    mDynamicPlot.setTicksPerDomainLabel(Integer.MAX_VALUE);
    mDynamicPlot.getLayoutManager().remove(mDynamicPlot.getLegendWidget());
    mDynamicPlot.getLayoutManager().remove(mDynamicPlot.getRangeLabelWidget());
    mDynamicPlot.getLayoutManager().remove(mDynamicPlot.getDomainLabelWidget());
    mDynamicPlot.getLayoutManager().remove(mDynamicPlot.getTitleWidget());

    mDynamicPlot.getGraphWidget().setMarginTop(10);        

    mDynamicPlot.setBackgroundPaint(null);
    mDynamicPlot.getGraphWidget().setBackgroundPaint(null);
    mDynamicPlot.getGraphWidget().setGridBackgroundPaint(null);

    mDynamicPlot.getGraphWidget().setDomainLabelPaint(null);
    mDynamicPlot.getGraphWidget().setDomainOriginLabelPaint(null);

    mDynamicPlot.getGraphWidget().setGridLinePaint(null);
    mDynamicPlot.getGraphWidget().setDomainOriginLinePaint(null);
    mDynamicPlot.getGraphWidget().setRangeOriginLinePaint(null);

    mDynamicPlot.setDrawBorderEnabled(false);

And my XML. The right margin is to offset the left space which I want to get rid of:

<com.androidplot.xy.XYPlot
    android:id="@+id/dynamicPlot"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_marginTop="16dp"
    android:layout_marginRight="10dp"
    title="Dynamic Plot" />

Nothing I do in XML seems to affect the inner bounds, including changing the enclosing layout gravity. I have also tried these, to no effect:

    mDynamicPlot.getRangeLabelWidget().setHeight(0);
    mDynamicPlot.getRangeLabelWidget().setPadding(0, 0, 0, 0);
    mDynamicPlot.getRangeLabelWidget().setMargins(0, 0, 0, 0);

    mDynamicPlot.getLegendWidget().setSize(new SizeMetrics(0, SizeLayoutType.ABSOLUTE, 0, SizeLayoutType.ABSOLUTE));
    mDynamicPlot.getRangeLabelWidget().setSize(new SizeMetrics(0, SizeLayoutType.ABSOLUTE, 0, SizeLayoutType.ABSOLUTE));
    mDynamicPlot.getDomainLabelWidget().setSize(new SizeMetrics(0, SizeLayoutType.ABSOLUTE, 0, SizeLayoutType.ABSOLUTE));

What do I need to do to get rid of all that black space?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Plot itself (mDynamicPlot in your example) is essentially a collection of widgets positioned in a shared space. The actual class that handles the positioning is LayoutManager.

In addition to positioning, the size of the Widget will also need to be adjusted to fill the entire Plot. Each Widget keeps track of its own size using an instance of SizeMetrics. You can modify the size of any Widget by invoking setSize(SizeMetrics newInstance); The "chart" area you are interested in corresponds to the XYGraphWidget. 

I have not personally tested this but I believe the code below should do what you want:

// get a handle to the XYGraphWidget:

Widget gw = mDynamicPlot.getGraphWidget();

// FILL mode with values of 0 means fill 100% of container:
SizeMetrics sm = new SizeMetrics(0,SizeLayoutType.FILL,
                                 0,SizeLayoutType.FILL));
gw.setSize(sm);

// get a handle to the layout manager:
LayoutManager lm = mDynamicPlot.getLayoutManager();

// position the upper left corner of gw in the upper left corner of the space 
// controlled by lm.
lm.position(gw, 0, XLayoutStyle.ABSOLUTE_FROM_LEFT,
            0, YLayoutStyle.ABSOLUTE_FROM_TOP);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...