如何在条形图中使用Achartengine在不同颜色的条形之间留出空间

时间:2022-06-08 14:56:23

Hii 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.

Hii在一个条形图中显示4个值,每个条都是不同的颜色。当我显示图形时,每条线互相重叠。我想要在setbarspace()中设置空格,方法是将不同的值仍然设置为无效。还有别的办法吗?

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

这是我的activity_main。xml文件

<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 个解决方案

#1


3  

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

我将您的所有系列都更改为XYSeries,并将它们添加到数据集中,而不调用额外的方法。

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

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

然后,像这样在常规的XYChart中添加数据:

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

#1


3  

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

我将您的所有系列都更改为XYSeries,并将它们添加到数据集中,而不调用额外的方法。

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

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

然后,像这样在常规的XYChart中添加数据:

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