Android:Wifi分析仪条形图

时间:2014-04-10 07:47:43

标签: android charts bar-chart achartengine

我试图像图片中那样获得相同的图表:

enter image description here

我有一个负数列表{-50,-70,-40 ...}我希望有相同的图表,但如果我正常工作,我会得到这个上升的图表:

enter image description here

我试着用标签代替y轴数字,但是我还没有给出我想要的图表。 请有人告诉我如何做这个图表,我通过4天试图在第一张图片中得到结果。或者提供一个想法,我已经失去了4天的serching和trynig :(。 这是我使用的代码:

public class TruitonAChartEngineActivity extends ActionBarActivity {

private static int SERIES_NR  ; //the number of columns to display,

@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();
        final int nr = 15;
        int [] classObject  =new int[] {70,-56,-40,-80};
        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[k]);
                if (k==1)
                    series.add(classObject[k]);
                if (k==0)
                    series.add(classObject[k]);
                if (k==3)
                    series.add(classObject[k]);
            }
            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);
        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(-90);
    renderer.setYAxisMax(-30);
    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);

}`enter code here`

谢谢。

PS:我正在使用achartengine。

1 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,我确定它不是最好的,但仍然没有更好:

Y轴将从30 to 90(实数)开始,我将设置标签而不是像这样的实数:

-90 correspond to 30
-80 correspond to 40 ...

PS:我确定我的Y坐标会与-90 to -30

不同

这是代码:

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);
    }
}