6

私はアンドロイドで achartengine を作業しており、すべての作業を完了しました。折れ線グラフで 1 つの問題に直面しています。 xaxis で 2,4,6,8,10,12 個のラベルを非表示にします。わからない場合は、下の図を参照してください。

ここに画像の説明を入力

完全なコードは次のとおりです。

public class ChartExample extends Activity {
    private GraphicalView mChartView;

    XYMultipleSeriesRenderer renderer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_chart);

        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout2);

        String[] titles = new String[] { "spens1","2second" };
        List<double[]> x = new ArrayList<double[]>();
        x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
        x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
        // }
        List<double[]> values = new ArrayList<double[]>();
        values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13,
                        10 });
        values.add(new double[] { 11, 13, 21, 10, 40, 10, 12,15, 24, 20, 15,
                12 });


        int[] colors = new int[] { Color.BLUE,Color.RED};
        PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,PointStyle.CIRCLE};

        renderer = buildRenderer(colors, styles);

        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            ((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
                    .setFillPoints(true);
        }

        setChartSettings(renderer, "SCORE CARD VIEW", "Attetempts",
                "Marks Scored", 0, 12.5, 0, 45, Color.LTGRAY, Color.LTGRAY);
        renderer.setXLabels(12);
        renderer.setYLabels(10);
        renderer.setShowGrid(false);
        renderer.setXLabelsAlign(Align.CENTER);
        renderer.setYLabelsAlign(Align.CENTER);

        //renderer.setZoomButtonsVisible(true);
        //renderer.setPanLimits(new double[] { 0, 20, 0, 40 });
        //renderer.setZoomLimits(new double[] { 0, 20, 0, 40 });
        renderer.setPanEnabled(false, false);
        renderer.setZoomEnabled(false, false);
        renderer.setShowLegend(false);       


        for(int k=0;k<12;k++)
        {
            renderer.addXTextLabel(k+1,"test");
        }

        if (mChartView == null) 
        {
            Log.d("Oncreate ", "if (mChartView == null)");
            mChartView = ChartFactory.getLineChartView(this, mDataset(titles,
                    x, values), renderer);
            layout.addView(mChartView, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));




        } 
        else 
        {
            mChartView.repaint();
            Log.d("Oncreate ", "if (mChartView != null)");
        }
    }

    private void setChartSettings(XYMultipleSeriesRenderer renderer3,
            String title, String xTitle, String yTitle, double xMin,
            double xMax, double yMin, double yMax, int axesColor,
            int labelsColor) {
        // TODO Auto-generated method stub

        renderer3.setChartTitle(title);
        renderer3.setXTitle(xTitle);
        renderer3.setYTitle(yTitle);
        renderer3.setXAxisMin(xMin);
        renderer3.setXAxisMax(xMax);
        renderer3.setYAxisMin(yMin);
        renderer3.setYAxisMax(yMax);
        renderer3.setAxesColor(axesColor);
        renderer3.setLabelsColor(labelsColor);
    }

    private XYMultipleSeriesRenderer buildRenderer(int[] colors,
            PointStyle[] styles) {
        // TODO Auto-generated method stub
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        setRenderer(renderer, colors, styles);
        return renderer;
    }

    private void setRenderer(XYMultipleSeriesRenderer renderer2, int[] colors,
            PointStyle[] styles) {
        // TODO Auto-generated method stub
        renderer2.setAxisTitleTextSize(16);
        renderer2.setChartTitleTextSize(20);
        renderer2.setLabelsTextSize(15);
        renderer2.setLegendTextSize(15);
        renderer2.setPointSize(5f);
        renderer2.setMargins(new int[] { 20, 30, 15, 20 });
        int length = colors.length;
        for (int i = 0; i < length; i++) {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            r.setPointStyle(styles[i]);
            renderer2.addSeriesRenderer(r);
        }
    }

    private XYMultipleSeriesDataset mDataset(String[] titles,
            List<double[]> xValues, List<double[]> yValues) {
        // TODO Auto-generated method stub
        XYMultipleSeriesDataset dataset1 = new XYMultipleSeriesDataset();
        addXYSeries(dataset1, titles, xValues, yValues, 0);
        return dataset1;
    }

    private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
            List<double[]> xValues, List<double[]> yValues, int scale) {
        // TODO Auto-generated method stub

        int length = titles.length;
        for (int i = 0; i < length; i++) {


            double[] xV = xValues.get(i);
            XYSeries series = new XYSeries(titles[i], scale);
            double[] yV = yValues.get(i);
            int seriesLength = xV.length;
            for (int k = 0; k < seriesLength; k++) {
                    series.add(xV[k], yV[k]);
            }
            dataset.addSeries(series);
        }



    }

}

大変感謝しています。

4

2 に答える 2

7

最後に、私は以下のコードを試してみました:

renderer.setXLabels(0);

ここに完全なクラスコードがあります:

package com.example.lockexample;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class ChartExample extends Activity {
    private GraphicalView mChartView;

    XYMultipleSeriesRenderer renderer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_chart);

        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout2);

        String[] titles = new String[] { "spens1","2second" };
        List<double[]> x = new ArrayList<double[]>();
        x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
        x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
        // }
        List<double[]> values = new ArrayList<double[]>();
        values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13,
                        10 });
        values.add(new double[] { 11, 13, 21, 10, 40, 10, 12,15, 24, 20, 15,
                12 });


        int[] colors = new int[] { Color.BLUE,Color.RED};
        PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,PointStyle.CIRCLE};

        renderer = buildRenderer(colors, styles);

        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            ((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
                    .setFillPoints(true);
        }

        setChartSettings(renderer, "SCORE CARD VIEW", "Attetempts",
                "Marks Scored", 0, 12.5, 0, 45, Color.LTGRAY, Color.LTGRAY);
        //renderer.setXLabels(12);
        renderer.setYLabels(10);
        renderer.setXLabels(0);
        renderer.setShowGrid(true);
        renderer.setXLabelsAlign(Align.CENTER);
        renderer.setYLabelsAlign(Align.RIGHT);
        renderer.setMargins(new int[] { 25,35, 10, 15 });
        //renderer.setZoomButtonsVisible(true);
        //renderer.setPanLimits(new double[] { 0, 20, 0, 40 });
        //renderer.setZoomLimits(new double[] { 0, 20, 0, 40 });
        renderer.setPanEnabled(false, false);
        renderer.setZoomEnabled(false, false);

        renderer.setMarginsColor(Color.parseColor("#EEEDED"));
        renderer.setXLabelsColor(Color.BLACK);
        renderer.setYLabelsColor(0,Color.BLACK);

        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
        //renderer.setShowLegend(false);       


        for(int k=0;k<12;k++)
        {
            renderer.addXTextLabel(k+1,"test");
        }

        if (mChartView == null) 
        {
            Log.d("Oncreate ", "if (mChartView == null)");
            mChartView = ChartFactory.getLineChartView(this, mDataset(titles,
                    x, values), renderer);
            layout.addView(mChartView, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));




        } 
        else 
        {
            mChartView.repaint();
            Log.d("Oncreate ", "if (mChartView != null)");
        }
    }

    private void setChartSettings(XYMultipleSeriesRenderer renderer3,
            String title, String xTitle, String yTitle, double xMin,
            double xMax, double yMin, double yMax, int axesColor,
            int labelsColor) {
        // TODO Auto-generated method stub

        renderer3.setChartTitle(title);
        renderer3.setXTitle(xTitle);
        renderer3.setYTitle(yTitle);
        renderer3.setXAxisMin(xMin);
        renderer3.setXAxisMax(xMax);
        renderer3.setYAxisMin(yMin);
        renderer3.setYAxisMax(yMax);
        renderer3.setAxesColor(axesColor);
        renderer3.setLabelsColor(labelsColor);

    }

    private XYMultipleSeriesRenderer buildRenderer(int[] colors,
            PointStyle[] styles) {
        // TODO Auto-generated method stub
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        setRenderer(renderer, colors, styles);
        return renderer;
    }

    private void setRenderer(XYMultipleSeriesRenderer renderer2, int[] colors,
            PointStyle[] styles) {
        // TODO Auto-generated method stub
        renderer2.setAxisTitleTextSize(16);
        renderer2.setChartTitleTextSize(20);
        renderer2.setLabelsTextSize(15);
        renderer2.setLegendTextSize(15);
        renderer2.setPointSize(5f);
        //renderer2.setMargins(new int[] { 20, 30, 15, 20 });
        renderer2.setMargins(new int[] { 25,35, 10, 15 });

        int length = colors.length;
        for (int i = 0; i < length; i++) {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            r.setPointStyle(styles[i]);
            renderer2.addSeriesRenderer(r);
        }
    }

    private XYMultipleSeriesDataset mDataset(String[] titles,
            List<double[]> xValues, List<double[]> yValues) {
        // TODO Auto-generated method stub
        XYMultipleSeriesDataset dataset1 = new XYMultipleSeriesDataset();
        addXYSeries(dataset1, titles, xValues, yValues, 0);
        return dataset1;
    }

    private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
            List<double[]> xValues, List<double[]> yValues, int scale) {
        // TODO Auto-generated method stub

        int length = titles.length;
        for (int i = 0; i < length; i++) {


            double[] xV = xValues.get(i);
            XYSeries series = new XYSeries(titles[i], scale);
            double[] yV = yValues.get(i);
            int seriesLength = xV.length;
            for (int k = 0; k < seriesLength; k++) {
                    series.add(xV[k], yV[k]);
            }
            dataset.addSeries(series);
        }



    }

}
于 2013-03-06T04:52:15.900 に答える
-1

現在のコードに追加する必要があるコードは、次の 2 行だけです。

renderer.setXLabels(RESULT_OK);
renderer.clearXTextLabels();

これらのステートメントは、レンダラー設定を宣言した後、カスタム X 軸のテキスト ラベルを設定するコードの前に必ず追加してください。

プログラムで複数のレンダラー メソッドが実行されているため、XYMultipleSeriesRenderer を実装するメソッドの最後にこれら 2 行のコードを含める必要があります。繰り返しますが、カスタム テキスト ラベルを実装する前に、必ずコードにこれを追加してください。:)

于 2013-04-04T19:43:51.477 に答える