动态添加多个系列

时间:2014-04-10 09:48:58

标签: android bar-chart achartengine

我需要添加显示动态数量的条形图(条形图),每个条形图都有不同的颜色。为此,我需要动态添加系列。

修改 现在我没有例外,但所有系列都显示在Y = 1,我不知道为什么。我想要的是将系列1放在X = 4中,系列2放在X = 6中...我该如何控制X轴?我需要得到像第二张照片的结果。

已编辑代码

public class TruitonAChartEngineActivity extends ActionBarActivity {

private static int SERIES_NR  ; //the number of columns to display,
List<int[]> values = new ArrayList<int[]>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_truiton_achart_engine);
    XYMultipleSeriesRenderer renderer = getTruitonBarRenderer();
    myChartSettings(renderer);



     Intent intent = ChartFactory.getBarChartIntent(this, getTruitonBarDataset(), renderer, Type.DEFAULT);
        startActivity(intent);

}

 private XYMultipleSeriesDataset getTruitonBarDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        int [] classObject  =new int[] {-70,-56,-45,-80,-47,-55,-80 , 70, 20, 75,
                95, 35,45,10};//Signal strength receiver from each access point
        for (int j=0;j<classObject.length;j++){
        values.add(new int[] { classObject[j]}); //every serie will concern an access point and contain one RSSI
        }

        final int nr = classObject.length;
        ArrayList<String> legendTitles = new ArrayList<String>();
        for (int k = 0; k < classObject.length; k++) {
             legendTitles.add("Sales"+k);//every serie will have a legend
        }
        SERIES_NR = classObject.length ;//number of access point
        for (int i = 0; i < SERIES_NR; i++) {
            CategorySeries series = new CategorySeries(legendTitles.get(i));
            int[] v = values.get(i);
            int seriesLength = v.length;
            for (int k = 0; k < seriesLength; k++) {

                switch (v[k] / 10){
                case -8 :
                    //series.add(classObject[k]);
                    if (v[k] %10 == 0)
                        series.add(40);
                    else 
                    series.add(40 - v[k] %10);

                    break;
                case -7 :
                    //series.add(classObject[k]);
                    if (v[k] %10 == 0)
                        series.add(50);
                    else
                    series.add(50 - v[k] %10);

                    break;
                case -6 :
                    //series.add(classObject[k]);
                    if (v[k] %10 == 0)
                        series.add(60);
                    else
                    series.add(60 - v[k] %10);

                    break;

                case -5 :
                    //series.add(classObject[k]);
                    if (v[k] %10 == 0)
                        series.add(70);
                    else
                    series.add(70 - v[k] %10);

                    break;
                case -4 :
                    //series.add(classObject[k]);
                    if (v[k] %10 == 0)
                        series.add(80);
                    else
                    series.add(80 - v[k] %10);

                    break;
                case -3 :
                    //series.add(classObject[k]);
                    if (v[k] %10 == 0)
                        series.add(90);
                    else
                    series.add(90 - v[k] %10);

                    break;


                }


           }
            dataset.addSeries(series.toXYSeries());
        }
        return dataset;
    }


public XYMultipleSeriesRenderer getTruitonBarRenderer() {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    renderer.setAxisTitleTextSize(10);
    renderer.setChartTitleTextSize(20);
    renderer.setLabelsTextSize(10);
    renderer.setLegendTextSize(10);
    renderer.setMargins(new int[] { 30, 40, 15, 0 });
    int randomColor = Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)Math.random() * 255);
    for (int k = 0; k < 14; k++)
    {
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)Math.random() * 255));
        renderer.addSeriesRenderer(r);
    }
    /*  SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)Math.random() * 255));
        renderer.addSeriesRenderer(r);
    r = new SimpleSeriesRenderer();
    r.setColor(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)Math.random() * 255));
    renderer.addSeriesRenderer(r);*/

    return renderer;
}

@SuppressWarnings("deprecation")
private void myChartSettings(XYMultipleSeriesRenderer renderer) {
    renderer.setChartTitle("Wifi Channels");
    renderer.setXAxisMin(0.5);
    renderer.setXAxisMax(14.5);
    renderer.setYAxisMin(30);
    renderer.setYAxisMax(90);
    renderer.addXTextLabel(1, "1");
    renderer.addXTextLabel(2, "2");
    renderer.addXTextLabel(3, "3");
    renderer.addXTextLabel(4, "4");
    renderer.addXTextLabel(5, "5");
    renderer.addXTextLabel(6, "6");
    renderer.addXTextLabel(7, "7");
    renderer.addXTextLabel(8, "8");
    renderer.addXTextLabel(9, "9");
    renderer.addXTextLabel(10, "10");
    renderer.addXTextLabel(11, "11");
    renderer.addXTextLabel(12, "12");
    renderer.addXTextLabel(13, "13");
    renderer.addXTextLabel(14, "14");

    renderer.addYTextLabel(0, "");
    renderer.addYTextLabel(30, "-90");
    renderer.addYTextLabel(40, "-80");
    renderer.addYTextLabel(50, "-70");
    renderer.addYTextLabel(60, "-60");
    renderer.addYTextLabel(70, "-50");
    renderer.addYTextLabel(80, "-40");
    renderer.addYTextLabel(90, "-30");
    renderer.addYTextLabel(100, "");
    renderer.setPanEnabled(true, true);
    renderer.setZoomEnabled(false, false);


    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.BLACK);
    renderer.setYLabelsAlign(Align.RIGHT);
    renderer.setBarSpacing(0.5);
    renderer.setXTitle("Canal du WiFi");
    renderer.setYTitle("Puissance du signal[dbm]");
    renderer.setShowGrid(true);
    renderer.setGridColor(Color.GRAY);
    renderer.setXLabels(0); // sets the number of integer labels to appear
  renderer.setXLabels((int) 0.5);//25
    renderer.setYLabels(10);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.truiton_achart_engine, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

我有这张图表:

enter image description here

第二张图片

enter image description here

请帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

在您的情况下,您应该构建CategorySeries并添加指定X和Y值的值,而不是使用XYSeries