以使光柱条之间的

以使光柱条之间的

本文介绍了如何使用Achartengine如果每个酒吧不同的颜色contaning不同的值,以使光柱条之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许志国我在一个柱形图,其中每个酒吧是不同的color.when我显示每个栏重叠other.i想使酒吧我试着用 setBarSpacing之间的空间(图中显示4个值)通过将不同的价值仍然是其不working.is有任何其他的想法。

 包com.example.barchartview;

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


@燮pressLint(ParserError)
公共类MainActivity延伸活动{
    私人GraphicalView BarChartView;




    @燮pressLint(ParserError)
    @覆盖
        公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        如果(BarChartView == NULL)
        {

        XYMultipleSeriesRenderer渲染= buildBarRenderer();
        setChartSettings(渲染);
        BarChartView = ChartFactory.getBarChartView(这一点,getBarDataset(),渲染器,Type.DEFAULT);
        LinearLayout中播出=(的LinearLayout)findViewById(R.id.graph2);
        playout.addView(BarChartView);
        ;

        }
        其他
        {

            BarChartView.repaint();
            BarChartView.setVisibility(View.GONE);


        }
    }

    私人无效setChartSettings(XYMultipleSeriesRenderer渲染器)
    {
      renderer.setYTitle(点击);
      renderer.setXAxisMin(0);
      renderer.setXAxisMax(7);
      renderer.setYAxisMin(0);
      renderer.setYAxisMax(900);

      }
    私人XYMultipleSeriesDataset getBarDataset()
    {
            INT [] Y = {624430712323};
            CategorySeries系列1 =新CategorySeries(A);
            CategorySeries系列2 =新CategorySeries(B);
            CategorySeries系列3 =新CategorySeries(C);
            CategorySeries系列4 =新CategorySeries(D);


            XYMultipleSeriesDataset数据集=新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());

              返回的数据集;

    }
    保护XYMultipleSeriesRenderer buildBarRenderer(){
        XYMultipleSeriesRenderer mrenderer =新XYMultipleSeriesRenderer();

        双[]范围= {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(新INT [] {20,30,15,0});
         mrenderer.setAxesColor(Color.WHITE);
         mrenderer.setChartTitle();
         mrenderer.setXTitle();
         mrenderer.setInitialRange(范围,1);


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


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


          返回mrenderer;

    }




}
 

这是我的code。

这是我的activity_main.xml文件

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / mainActivity
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <的LinearLayout机器人:ID =@ + ID / graph2
       机器人:layout_width =160dip
        机器人:layout_height =200dip
        机器人:scaleType =fitEnd
        机器人:layout_marginTop =40dip
        机器人:layout_marginLeft =790dip/>

  < / RelativeLayout的>
 

解决方案

我改变了所有的系列 XYSeries 并简单地将它们添加到数据集,而无需调用额外的方法。

  XYSeries系列1 =新XYSeries(A);
dataset.addSeries(系列1);
 

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

  series1.add(1值);
series2.add(2值);
 

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.

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

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

这篇关于如何使用Achartengine如果每个酒吧不同的颜色contaning不同的值,以使光柱条之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:09