How to make space between bars in bargraph using Achartengine if each bar is different color containing different values

廉价感情. 提交于 2019-12-09 02:49:24

问题


I am displaying 4 values in a bargraph where each bar is different color. when I am displaying the graph each bar overlapping each other. I want to make space between the bars I tried with setBarSpacing() by putting different values still its not working. is there any other idea.

package com.example.barchartview;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;


@SuppressLint("ParserError")
public class MainActivity extends Activity {
    private GraphicalView BarChartView;


    @SuppressLint("ParserError")
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (BarChartView==null )
        {

        XYMultipleSeriesRenderer renderer = buildBarRenderer();
        setChartSettings(renderer);
        BarChartView  = ChartFactory.getBarChartView(this, getBarDataset(), renderer, Type.DEFAULT);
        LinearLayout playout = (LinearLayout) findViewById(R.id.graph2);
        playout.addView(BarChartView);
        ;

        }
        else
        {
            BarChartView.repaint();
            BarChartView.setVisibility(View.GONE);
        }
    }

    private void setChartSettings(XYMultipleSeriesRenderer renderer)
    {
        renderer.setYTitle("Hits");
        renderer.setXAxisMin(0);
        renderer.setXAxisMax(7);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(900);
    }
    private XYMultipleSeriesDataset getBarDataset()
    {
        int[] y = {624,430,712,323};
        CategorySeries series1 = new CategorySeries("A");
        CategorySeries series2 = new CategorySeries("B");
        CategorySeries series3 = new CategorySeries("C");
        CategorySeries series4 = new CategorySeries("D");

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        series1.add(y[0]);
        dataset.addSeries(series1.toXYSeries());

        series2.add(y[1]);
        dataset.addSeries(series2.toXYSeries());

        series3.add(y[2]);
        dataset.addSeries(series3.toXYSeries());

        series4.add(y[3]);
        dataset.addSeries(series4.toXYSeries());

        return dataset;
    }
    protected XYMultipleSeriesRenderer buildBarRenderer() {
        XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();

        double[] range = {0,5,0,5};

        mrenderer.setAxisTitleTextSize(16);
        mrenderer.setChartTitleTextSize(20);
        mrenderer.setLabelsTextSize(15);
        mrenderer.setLegendTextSize(15);
        mrenderer.setBarSpacing(0);
        mrenderer.setXLabels(1);
        mrenderer.addXTextLabel(2.5, "");
        mrenderer.setMargins(new int[] {20, 30, 15, 0});
        mrenderer.setAxesColor(Color.WHITE);
        mrenderer.setChartTitle("");
        mrenderer.setXTitle("");
        mrenderer.setInitialRange(range, 1);

        XYSeriesRenderer renderer1 = new XYSeriesRenderer();
        renderer1.setColor(Color.GREEN);
        XYSeriesRenderer renderer2 = new XYSeriesRenderer();
        renderer2.setColor(Color.RED);
        XYSeriesRenderer renderer3 = new XYSeriesRenderer();
        renderer3.setColor(Color.BLUE);
        XYSeriesRenderer renderer4 = new XYSeriesRenderer();
        renderer4.setColor(Color.CYAN);

        mrenderer.addSeriesRenderer(renderer1);
        mrenderer.addSeriesRenderer(renderer2);
        mrenderer.addSeriesRenderer(renderer3);
        mrenderer.addSeriesRenderer(renderer4);

        return mrenderer;
    }
}

here is my code.

here is my activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainActivity"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout android:id="@+id/graph2"
       android:layout_width="160dip"
        android:layout_height="200dip"
        android:scaleType="fitEnd"
        android:layout_marginTop="40dip"
        android:layout_marginLeft="790dip"/>

  </RelativeLayout>

回答1:


I changed all your series to XYSeries and add them simply to the dataset, without calling extra methods.

XYSeries series1 = new XYSeries("A");
dataset.addSeries(series1);

Then, add data like in a regular XYChart like this:

series1.add(1, value);
series2.add(2, value);


来源:https://stackoverflow.com/questions/13757166/how-to-make-space-between-bars-in-bargraph-using-achartengine-if-each-bar-is-dif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!