AChartEngine条形图未正确显示

时间:2016-02-20 04:00:07

标签: android achartengine

我是android的新手。我创建一个应用程序绘制图表。我的图表对于轴X上的每个标签具有12对值。每个条形聊天具有与其值相对应的颜色(值<50,值> 50&amp;&amp;值&lt; 100,值&gt; 100&amp;&amp; value&lt; 200,value&gt; 200)。

所以,我有这样的问题

enter image description here

我想要我的酒吧聊天,如this

enter image description here

你可以给我一些解决方案。这是我的MainActivity:

private View mChart;
private String[] mMonth = new String[]{
        "Tì", "Can", "Vị", "Đởm", "Thận", "BQ",
        "Phế", "Đt", "Tb", "Ttiêu", "Tâm", "Ttr"
};

private float tiTrai = 160;
private float tiPhai = 80;

private float canTrai = 90;
private float canPhai = 26;

private float viTrai = 120;
private float viPhai = 85;

private float domTrai = 165;
private float domPhai = 190;

private float thanTrai = 120;
private float thanPhai = 190;

private float bangQuangTrai = 165;
private float bangQuangPhai = 40;

private float pheTrai = 160;
private float phePhai = 80;

private float daiTruongTrai = 190;
private float daiTruongPhai = 246;

private float tamBaoTrai = 120;
private float tamBaoPhai = 85;

private float tamTieuTrai = 145;
private float tamTieuPhai = 120;

private float tamTrai = 130;
private float tamPhai = 70;

private float tieuTruongTrai = 125;
private float tieuTruongPhai = 180;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    openChart();
}

private void openChart() {

    int[] x = {0, 1, 2, 3, 4, 5, 6, 7, 8,  9, 10, 11};
    float ti = tiTrai;
    float ti_ = tiPhai;
    float can = canTrai;
    float can_ = canPhai;
    float vi = viTrai;
    float vi_ = viPhai;
    float dom = domTrai;
    float dom_ = domPhai;
    float than = thanTrai;
    float than_ = thanPhai;
    float bangQuang = bangQuangTrai;
    float bangQuang_ = bangQuangPhai;

    float phe = pheTrai;
    float phe_ = phePhai;
    float daiTruong = daiTruongTrai;
    float daiTruong_ = daiTruongPhai;
    float tamBao = tamBaoTrai;
    float tamBao_ = tamBaoPhai;
    float tamTieu = tamTieuTrai;
    float tamTieu_ = tamTieuPhai;
    float tam = tamTrai;
    float tam_ = tamPhai;
    float tieuTruong = tieuTruongTrai;
    float tieuTruong_ = tieuTruongPhai;

    XYSeries tiTraiSeries = new XYSeries("Ti");
    XYSeries tiPhaiSeries = new XYSeries("Ti_");

    XYSeries canTraiSeries = new XYSeries("Can");
    XYSeries canPhaiSeries = new XYSeries("Can_");

    XYSeries viTraiSeries = new XYSeries("Vi");
    XYSeries viPhaiSeries = new XYSeries("Vi_");

    XYSeries domTraiSeries = new XYSeries("Dom");
    XYSeries domPhaiSeries = new XYSeries("Dom_");

    XYSeries thanTraiSeries = new XYSeries("Than");
    XYSeries thanPhaiSeries = new XYSeries("Than_");

    XYSeries bangQuangTraiSeries = new XYSeries("BQ");
    XYSeries bangQuangPhaiSeries = new XYSeries("BQ_");

    XYSeries pheTraiSeries = new XYSeries("Phe");
    XYSeries phePhaiSeries = new XYSeries("Phe_");

    XYSeries daiTruongTraiSeries = new XYSeries("Dt");
    XYSeries daiTruongPhaiSeries = new XYSeries("Dt_");

    XYSeries tamBaoTraiSeries = new XYSeries("Tb");
    XYSeries tamBaoPhaiSeries = new XYSeries("Tb_");

    XYSeries tamTieuTraiSeries = new XYSeries("Tt");
    XYSeries tamTieuPhaiSeries = new XYSeries("Tt_");

    XYSeries tamTraiSeries = new XYSeries("Tam");
    XYSeries tamPhaiSeries = new XYSeries("Tam_");

    XYSeries tieuTruongTraiSeries = new XYSeries("TT");
    XYSeries tieuTruongPhaiSeries = new XYSeries("TT_");


    tiTraiSeries.add(0, ti);
    tiPhaiSeries.add(0, ti_);

    canTraiSeries.add(1, can);
    canPhaiSeries.add(1, can_);

    viTraiSeries.add(2, vi);
    viPhaiSeries.add(2, vi_);

    domTraiSeries.add(3, dom);
    domPhaiSeries.add(3, dom_);

    thanTraiSeries.add(4, than);
    thanPhaiSeries.add(4, than_);

    bangQuangTraiSeries.add(5, bangQuang);
    bangQuangPhaiSeries.add(5, bangQuang_);

    pheTraiSeries.add(6, phe);
    phePhaiSeries.add(6, phe_);

    daiTruongTraiSeries.add(7, daiTruong);
    daiTruongPhaiSeries.add(7, daiTruong_);

    tamBaoTraiSeries.add(8, tamBao);
    tamBaoPhaiSeries.add(8, tamBao_);

    tamTieuTraiSeries.add(9, tamTieu);
    tamTieuPhaiSeries.add(9, tamTieu_);

    tamTraiSeries.add(10, tam);
    tamPhaiSeries.add(10, tam_);

    tieuTruongTraiSeries.add(11, tieuTruong);
    tieuTruongPhaiSeries.add(11, tieuTruong_);

    // Creating a dataset to hold each series
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

    dataset.addSeries(tiTraiSeries);
    dataset.addSeries(tiPhaiSeries);

    dataset.addSeries(canTraiSeries);
    dataset.addSeries(canPhaiSeries);

    dataset.addSeries(viTraiSeries);
    dataset.addSeries(viPhaiSeries);

    dataset.addSeries(viTraiSeries);
    dataset.addSeries(viPhaiSeries);

    dataset.addSeries(domTraiSeries);
    dataset.addSeries(domPhaiSeries);

    dataset.addSeries(bangQuangTraiSeries);
    dataset.addSeries(bangQuangPhaiSeries);

    dataset.addSeries(pheTraiSeries);
    dataset.addSeries(phePhaiSeries);

    dataset.addSeries(daiTruongTraiSeries);
    dataset.addSeries(daiTruongPhaiSeries);

    dataset.addSeries(tamBaoTraiSeries);
    dataset.addSeries(tamBaoPhaiSeries);

    dataset.addSeries(tamTieuTraiSeries);
    dataset.addSeries(tamTieuPhaiSeries);

    dataset.addSeries(tamTraiSeries);
    dataset.addSeries(tamPhaiSeries);

    dataset.addSeries(tieuTruongTraiSeries);
    dataset.addSeries(tieuTruongPhaiSeries);


    XYSeriesRenderer tiTraiRenderer = new XYSeriesRenderer();
    tiTraiRenderer.setColor(getMyColor(ti));

    XYSeriesRenderer tiPhaiRenderer = new XYSeriesRenderer();
    tiPhaiRenderer.setColor(getMyColor(ti_));

    XYSeriesRenderer canTraiRenderer = new XYSeriesRenderer();
    canTraiRenderer.setColor(getMyColor(can));

    XYSeriesRenderer canPhaiRenderer = new XYSeriesRenderer();
    canPhaiRenderer.setColor(getMyColor(can_));

    XYSeriesRenderer viTraiRenderer = new XYSeriesRenderer();
    viTraiRenderer.setColor(getMyColor(vi));

    XYSeriesRenderer viPhaiRenderer = new XYSeriesRenderer();
    viPhaiRenderer.setColor(getMyColor(vi_));

    XYSeriesRenderer domTraiRenderer = new XYSeriesRenderer();
    domTraiRenderer.setColor(getMyColor(dom));

    XYSeriesRenderer domPhaiRenderer = new XYSeriesRenderer();
    domPhaiRenderer.setColor(getMyColor(dom_));

    XYSeriesRenderer thanTraiRenderer = new XYSeriesRenderer();
    thanTraiRenderer.setColor(getMyColor(than));

    XYSeriesRenderer thanPhaiRenderer = new XYSeriesRenderer();
    thanPhaiRenderer.setColor(getMyColor(than_));

    XYSeriesRenderer bangQuangTraiRenderer = new XYSeriesRenderer();
    bangQuangTraiRenderer.setColor(getMyColor(bangQuang));

    XYSeriesRenderer bangQuangPhaiRenderer = new XYSeriesRenderer();
    bangQuangPhaiRenderer.setColor(getMyColor(bangQuang_));

    XYSeriesRenderer pheTraiRenderer = new XYSeriesRenderer();
    pheTraiRenderer.setColor(getMyColor(phe));

    XYSeriesRenderer phePhaiRenderer = new XYSeriesRenderer();
    phePhaiRenderer.setColor(getMyColor(phe_));

    XYSeriesRenderer daiTruongTraiRenderer = new XYSeriesRenderer();
    daiTruongTraiRenderer.setColor(getMyColor(daiTruong));

    XYSeriesRenderer daiTruongPhaiRenderer = new XYSeriesRenderer();
    daiTruongPhaiRenderer.setColor(getMyColor(daiTruong_));

    XYSeriesRenderer tamBaoTraiRenderer = new XYSeriesRenderer();
    tamBaoTraiRenderer.setColor(getMyColor(tamBao));

    XYSeriesRenderer tamBaoPhaiRenderer = new XYSeriesRenderer();
    tamBaoPhaiRenderer.setColor(getMyColor(tamBao_));

    XYSeriesRenderer tamTieuTraiRenderer = new XYSeriesRenderer();
    tamTieuTraiRenderer.setColor(getMyColor(tamTieu));

    XYSeriesRenderer tamTieuPhaiRenderer = new XYSeriesRenderer();
    tamTieuPhaiRenderer.setColor(getMyColor(tamTieu_));

    XYSeriesRenderer tamTraiRenderer = new XYSeriesRenderer();
    tamTraiRenderer.setColor(getMyColor(tam));

    XYSeriesRenderer tamPhaiRenderer = new XYSeriesRenderer();
    tamPhaiRenderer.setColor(getMyColor(tam_));

    XYSeriesRenderer tieuTruongTraiRenderer = new XYSeriesRenderer();
    tieuTruongTraiRenderer.setColor(getMyColor(tieuTruong));

    XYSeriesRenderer tieuTruongPhaiRenderer = new XYSeriesRenderer();
    tieuTruongPhaiRenderer.setColor(getMyColor(tieuTruong_));

    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setOrientation(XYMultipleSeriesRenderer.Orientation.HORIZONTAL);
    multiRenderer.setXLabels(0);
    multiRenderer.setChartTitle("Biểu đồ kết quả đo");
    multiRenderer.setXTitle("Các bộ phận");
    multiRenderer.setYTitle("Phần trăm");

    multiRenderer.setChartTitleTextSize(28);
    multiRenderer.setAxisTitleTextSize(24);
    //setting text size of the graph lable
    multiRenderer.setLabelsTextSize(24);
    //setting zoom buttons visiblity
    multiRenderer.setZoomButtonsVisible(false);
    //setting pan enablity which uses graph to move on both axis
    multiRenderer.setPanEnabled(false, false);
    //setting click false on graph
    multiRenderer.setClickEnabled(false);
    //setting zoom to false on both axis
    multiRenderer.setZoomEnabled(false, false);
    //setting lines to display on y axis
    multiRenderer.setShowGridY(true);
    //setting lines to display on x axis
    multiRenderer.setShowGridX(false);
    //setting legend to fit the screen size
    multiRenderer.setFitLegend(true);
    //setting displaying line on grid
    multiRenderer.setShowGrid(true);
    //setting zoom to false
    multiRenderer.setZoomEnabled(false);
    //setting external zoom functions to false
    multiRenderer.setExternalZoomEnabled(false);
    //setting displaying lines on graph to be formatted(like using graphics)
    multiRenderer.setAntialiasing(true);
    //setting to in scroll to false
    multiRenderer.setInScroll(false);
    //setting to set legend height of the graph
    multiRenderer.setLegendHeight(50);

    multiRenderer.setShowLegend(true);
    //setting x axis label align
    multiRenderer.setXLabelsAlign(Paint.Align.CENTER);
    multiRenderer.setYLabelsAlign(Paint.Align.LEFT);

    multiRenderer.setTextTypeface("sans_serif", Typeface.NORMAL);
    //setting no of values to display in y axis
    multiRenderer.setYLabels(10);
    multiRenderer.setYAxisMax(300);
    multiRenderer.setXAxisMin(-2);
    multiRenderer.setXAxisMax(13);
    multiRenderer.setBarSpacing(0.5);
    multiRenderer.setBarWidth(10);
    multiRenderer.setBackgroundColor(Color.TRANSPARENT);
    //Setting margin color of the graph to transparent
    multiRenderer.setMarginsColor(getResources().getColor(R.color.colorPrimary));
    multiRenderer.setApplyBackgroundColor(true);

    //setting the margin size for the graph in the order top, left, bottom, right
    multiRenderer.setMargins(new int[]{30, 30, 30, 30});

    for (int i = 0; i < x.length; i++) {
        multiRenderer.addXTextLabel(i, mMonth[i]);
    }

    multiRenderer.addSeriesRenderer(tiTraiRenderer);
    multiRenderer.addSeriesRenderer(tiPhaiRenderer);
    multiRenderer.addSeriesRenderer(canTraiRenderer);
    multiRenderer.addSeriesRenderer(canPhaiRenderer);
    multiRenderer.addSeriesRenderer(viTraiRenderer);
    multiRenderer.addSeriesRenderer(viPhaiRenderer);
    multiRenderer.addSeriesRenderer(domTraiRenderer);
    multiRenderer.addSeriesRenderer(domPhaiRenderer);
    multiRenderer.addSeriesRenderer(thanTraiRenderer);
    multiRenderer.addSeriesRenderer(thanPhaiRenderer);
    multiRenderer.addSeriesRenderer(bangQuangTraiRenderer);
    multiRenderer.addSeriesRenderer(bangQuangPhaiRenderer);

    multiRenderer.addSeriesRenderer(pheTraiRenderer);
    multiRenderer.addSeriesRenderer(phePhaiRenderer);
    multiRenderer.addSeriesRenderer(daiTruongTraiRenderer);
    multiRenderer.addSeriesRenderer(daiTruongPhaiRenderer);
    multiRenderer.addSeriesRenderer(tamBaoTraiRenderer);
    multiRenderer.addSeriesRenderer(tamBaoPhaiRenderer);
    multiRenderer.addSeriesRenderer(tamTieuTraiRenderer);
    multiRenderer.addSeriesRenderer(tamTieuPhaiRenderer);
    multiRenderer.addSeriesRenderer(tamTraiRenderer);
    multiRenderer.addSeriesRenderer(tamPhaiRenderer);
    multiRenderer.addSeriesRenderer(tieuTruongTraiRenderer);
    multiRenderer.addSeriesRenderer(tieuTruongPhaiRenderer);

    LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart);
    chartContainer.removeAllViews();
    mChart = ChartFactory.getBarChartView(MainActivity.this, dataset, multiRenderer, BarChart.Type.DEFAULT);
    chartContainer.addView(mChart);

方法public int getMyColor(float barValue)

if(barValue <50){
        return Color.GREEN;
    } else if(barValue >=50 && barValue<=100){
        return Color.BLUE;
    } else if(barValue >100 &&barValue<=200){
        return Color.YELLOW;
    }
    return Color.RED;

0 个答案:

没有答案