开发者

Code for bar graph in android

开发者 https://www.devze.com 2023-03-15 11:27 出处:网络
Can anyone help me coding a bar graph in android? I tried running the sample code of http://code.google.com/p/achartengine/

Can anyone help me coding a bar graph in android?

I tried running the sample code of http://code.google.com/p/achartengine/

But that sample is too big to just retrieve a bar code example from it. Can any one suggest me any good website or documentation with detailed information for coding a graph in android? I need to show changes in the graph after frequent intervals.

Can anyone 开发者_运维百科help me who has tried this before?


There have been a lot of graph library threads on SO, you should have a look at them and try the different ones out.

I listed a few here: https://stackoverflow.com/questions/5156740/im-looking-for-fancy-charts-library-for-android/5157576#5157576

Personally I would recommend using Steemas library - they have a lot of experience creating graphing libraries which is definitely shown in the API. The evaluation version can be found here:

http://www.steema.com/download/mobile

If you want to create a Bar graph with Steemas library here's a very simple example of doing so:

import android.app.Activity;
import android.os.Bundle;

import com.steema.teechart.TChart;
import com.steema.teechart.styles.Bar;

public class ExampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TChart chart = new TChart(this);
        Bar exampleBar = new Bar();

        double[] exampleValues = {1,5,8,2.5,-3.3,10.75};

        exampleBar.add(exampleValues);

        chart.addSeries(exampleBar);

        setContentView(chart);
    }
}

Just some of the observations I got from using the library:

TeeChart info:
    Disable 3D:         chart.getAspect().setView3D(false);
    Disable Legend:     chart.getLegend().setVisible(false);
    Disable footer:     chart.getFooter().setVisible(false);
    Disable header:     chart.getHeader().setVisible(false);

How to set marks to data value instead of cylinder number?
    For a Bar data-set: bar.getMarks().setStyle(MarksStyle.VALUE);

How to move the marks closer to the chart rectangle?
    bar.getMarks().setArrowLength(5); - or negative values to put them on top of the bar

Different styles:
    bar.setBarStyle(BarStyle.RECTANGLE);

How do I make the lines thicker in a graph?
    line.getLinePen().setWidth(5); works, but it will change the width of the Legend as well.

How do I change the color of labels on axes?
    chart.getAxes().getLeft().getLabels().getFont().setColor(Color.WHITE);
    chart.getAxes().getBottom().getLabels().getFont().setColor(Color.WHITE);


SOLVED WORKAROUND - How do I set the background color of the chart itself? Tried so far (the TChart methods that take a Color from the TChart Color class, not the View methods) - only managed to make the 'surrounding' area black.
        chart.setBackground(Color.BLACK);
        chart.getCanvas().setBackColor(Color.BLACK);
        chart.getGraphics3D().setBackColor(Color.BLACK);
            ---> WORKAROUND: Use the setBackground above, then use: chart.getWalls().getBack().setVisible(false); --- setColor() on walls is bugged?


How to choose the bounds of a chart manually?
    chart.getAxes().getLeft().setAutomatic(false);
    chart.getAxes().getLeft().setMinMax(-2, 2);


here Source Code For any Chart in android and
link 2

AChartEngine is a charting library for Android applications. It currently supports the following chart types:

  • line chart
  • area chart
  • scatter chart
  • time chart
  • bar chart
  • pie chart
  • bubble chart
  • doughnut chart
  • range (high-low) bar chart
  • dial chart / gauge

combined (any combination of line, cubic line, scatter, bar, range bar, bubble) chart cubic line chart


Go through This Click Here and enjoy the code, Hope it will help you.


If you want a minimal chart library i suggest this one (this version it's a fork of Daniel Nadeau's library): https://github.com/Androguide/HoloGraphLibrary. It's very simple to use.

Here the original library: https://bitbucket.org/danielnadeau/holographlibrary


For Simple Bar chart You can use this code may be this solve your problem

  1. Firstly you add achartengine-1.1.0.jar in your lib folder

  2. then you declare in manifest

    <activity android:name="org.achartengine.GraphicalActivity"/>
    

create class:

    package com.example.barchartexample;    
    import java.net.Proxy.Type;
    import org.achartengine.ChartFactory;
    import org.achartengine.model.CategorySeries;
    import org.achartengine.model.XYMultipleSeriesDataset;
    import org.achartengine.renderer.XYMultipleSeriesRenderer;
    import org.achartengine.renderer.XYSeriesRenderer;

    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;

    public class BarGraph {
        public Intent getIntent(Context context){
        int y[]={25,10,15,20};
        CategorySeries series= new CategorySeries("BAR 1");
        for (int i = 0; i < y.length; i++) {
            series.add("Bar"+(i+1),y[i]);

        }
        XYMultipleSeriesDataset dataset= new XYMultipleSeriesDataset();
        dataset.addSeries(series.toXYSeries());
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setColor(Color.RED);
        renderer.setDisplayChartValues(true);
        renderer.setChartValuesSpacing((float) 5.5d);
        renderer.setLineWidth((float) 10.5d);


        XYMultipleSeriesRenderer mRenderer= new XYMultipleSeriesRenderer();
        mRenderer.addSeriesRenderer(renderer);
        mRenderer.setChartTitle("Simple Chart");
        mRenderer.setYTitle("Rupee");
        mRenderer.setZoomButtonsVisible(true);
        mRenderer.setShowGrid(true);
        mRenderer.setBarSpacing(.5);
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.BLACK);
        mRenderer.setXAxisMin(0);
        mRenderer.setXAxisMax(5);
        mRenderer.setYAxisMax(50);
        mRenderer.setXLabels(0);
        mRenderer.addXTextLabel(1,"Income");
        mRenderer.addXTextLabel(2,"Saving");
        mRenderer.addXTextLabel(3,"Expenditure");
        mRenderer.addXTextLabel(4,"NetIncome");
        mRenderer.setPanEnabled(true,true);
        Intent intent = ChartFactory.getBarChartIntent(context, dataset, mRenderer,org.achartengine.chart.BarChart.Type.DEFAULT);

        return intent;

       }    
    }


    package com.example.barchartexample;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MainActivity extends Activity {

       Button but;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        but=(Button)findViewById(R.id.BarGraph);
        but.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                BarGraph bar = new BarGraph();
                Intent barIntent= bar.getIntent(MainActivity.this);
                startActivity(barIntent);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消