Android Achartengine:Barchart处理大量系列

时间:2014-04-08 16:52:12

标签: android wifi bar-chart achartengine

我试图获得一个条形图,wifi通道和接收信号强度。 好吧,我可以创建mannualy 2,3..series蝙蝠在现实中我有25条显示。所以他们有不同的颜色,以及我如何设置随机颜色到那些酒吧。我知道如何做到manualy,但我可以&# 39;用25个条形图来做,并且特别是25个数字正在改变(25代表检测到的接入点) 这是我的2系列代码:

public class TruitonAChartEngineActivity extends ActionBarActivity {

private static int SERIES_NR  ; //the number of columns to display,
ArrayList<ScanResult> classObject = new ArrayList<ScanResult>();
int [ ] [ ] Center_Frequency_2 = {   { 1,2,3,4,5,6,7,8,9,10,11,12,13,14 },
        { 2412, 2417, 2422, 2427, 2432,2437,2442,2447,2452 ,2457,2462,2467,2472,2484},
    };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_truiton_achart_engine);
    XYMultipleSeriesRenderer renderer = getTruitonBarRenderer();
    myChartSettings(renderer);
    Bundle v = getIntent().getExtras();
    //ArrayList<ScanResult> classObject = (ArrayList<ScanResult>) v.getSerializable("key");
     classObject = (ArrayList<ScanResult>) v.getSerializable("key");    
     Intent intent = ChartFactory.getBarChartIntent(this, getTruitonBarDataset(), renderer, Type.DEFAULT);
        startActivity(intent);

}
 private XYMultipleSeriesDataset getTruitonBarDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        final int nr = 4;
        Random r = new Random();
        ArrayList<String> legendTitles = new ArrayList<String>();
        legendTitles.add("Sales");
        legendTitles.add("Expenses");
        SERIES_NR = 2 ;
        for (int i = 0; i < SERIES_NR; i++) {
            CategorySeries series = new CategorySeries(legendTitles.get(i));
            for (int k = 0; k < nr; k++) {
                if (k==2)
                series.add(classObject.get(2).level);
                if (k==1)
                    series.add(classObject.get(1).level);
                if (k==3)
                    series.add(classObject.get(3).level);
            }
            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 });
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.BLUE);
        renderer.addSeriesRenderer(r);
    r = new SimpleSeriesRenderer();
    r.setColor(Color.RED);
    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(10, "-90");
    renderer.addYTextLabel(20, "-80");
    renderer.addYTextLabel(30, "-70");
    renderer.addYTextLabel(40, "-60");
    renderer.addYTextLabel(50, "-50");
    renderer.addYTextLabel(60, "-40");
    renderer.addYTextLabel(70, "-30");
    renderer.addYTextLabel(80, "");*/
    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);
}

}

1 个答案:

答案 0 :(得分:2)

每次想要制作新的随机颜色时都使用此功能。

int randomColor = Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)Math.random() * 255));