AchartEngine XYchart背景颜色问题

时间:2013-01-22 13:42:57

标签: android achartengine

我在使用achart引擎的xychart中遇到了问题。实际上,背景颜色仅在轴区域内设置。 我的代码:

private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer mRendererXY = new   XYMultipleSeriesRenderer();

private XYSeries mSeriesXY;
private GraphicalView mXYView;

private ViewFlinger mFlingy;
private int index = 0;

public StatChallengeTabManager(Context ctx, ViewFlinger flingy) {
    mCtx = ctx;
    mFlingy = flingy;
    mStatDBManagerCh = new StatDBManagerChallenge(ctx);
}

public void drawStats() {
    LayoutInflater inflate = (LayoutInflater) mCtx
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    v1 = inflate.inflate(R.layout.v1, null);
    v2 = inflate.inflate(R.layout.v2, null);
    mFlingy.addView(v1);
    mFlingy.addView(v2);

    drawFirstChart();
    drawSecondChart();

}

private void drawFirstChart() {

    if (mXYView == null) {

        mRendererXY.setAxisTitleTextSize(30);
        mRendererXY.setChartTitleTextSize(40);
        mRendererXY.setChartTitle(mCtx.getString(R.string.score_trend));
        mRendererXY.setLabelsColor(Color.BLACK);
        mRendererXY.setXLabelsColor(Color.BLACK);
        mRendererXY.setYLabelsColor(0, Color.BLACK);
        mRendererXY.setMargins(new int[] { 50, 60, 0, 50 });
        mRendererXY.setLabelsTextSize(30);
        mRendererXY.setZoomButtonsVisible(false);
        mRendererXY.setPointSize(8);
        mRendererXY.setDisplayValues(true);
        mRendererXY.setShowLegend(false);
        mRendererXY.setScale(.8f);

        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(COLORS[0]);
        r.setPointStyle(PointStyle.CIRCLE);
        r.setFillBelowLine(false);
        r.setFillPoints(true);
        mRendererXY.addSeriesRenderer(r);

        mRendererXY.setXTitle(mCtx.getString(R.string.game_id));
        mRendererXY.setYTitle(mCtx.getString(R.string.game_score));
        mRendererXY.setFitLegend(true);
        mRendererXY.setAxesColor(Color.BLACK);
        mRendererXY.setShowGrid(true);
        mRendererXY.setXAxisMin(-0.5);
        mRendererXY.setYAxisMin(0);
        mRendererXY.setXAxisMax(10.5);
        mRendererXY.setZoomEnabled(false);
        mRendererXY.setYAxisMax(100000);
        mRendererXY.setInScroll(true);
        mRendererXY.setClickEnabled(true);

        mRendererXY.setBackgroundColor(Color.argb(255, 255, 248, 220));
        mRendererXY.setApplyBackgroundColor(true);

        int[] res = mStatDBManagerCh.getGameScores();
        mSeriesXY = new XYSeries(mCtx.getString(R.string.score_trend));
        for (int i = 0; i < res.length; i++) {
            if (res[i] == 0)
                continue;
            mSeriesXY.add(i, res[i]);

        }
        mDataset.addSeries(mSeriesXY);
        mXYView = ChartFactory.getLineChartView(mCtx, mDataset, mRendererXY);

        LinearLayout layout = (LinearLayout) v1.findViewById(R.id.chart1);
        layout.addView(mXYView, new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    } else {
        LinearLayout layout = (LinearLayout) v1.findViewById(R.id.chart1);
        layout.addView(mXYView, new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mXYView.repaint();
    }

}

v1.xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/chart1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" />
</LinearLayout>

结果是https://www.dropbox.com/s/z2wuq1j0bjwz1wj/problem.png

1 个答案:

答案 0 :(得分:5)

只需致电mRendererXY.setMarginsColor(color);,您也可以设置边距背景。