Android中的条形图问题 [英] Bar-chart's issues in android

查看:42
本文介绍了Android中的条形图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在您的logcat上遇到以下问题吗?在这里,我试图借助achartengine库在android中创建条形图.但是问题是我正在通过消费从Web服务获取x和y轴值.

Has any one met the below issue on your logcat? Here i am trying to create a barchart in android with the help of achartengine library.But the problem is i am getting the x and y axis values from the web service by consuming it.

请提出建议.

感谢前进!..

10-26 10:37:19.936: W/KeyCharacterMap(715): No keyboard for id 0
10-26 10:37:19.936: W/KeyCharacterMap(715): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
10-26 10:37:30.555: D/AndroidRuntime(715): Shutting down VM
10-26 10:37:30.555: W/dalvikvm(715): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-26 10:37:30.565: E/AndroidRuntime(715): FATAL EXCEPTION: main
10-26 10:37:30.565: E/AndroidRuntime(715): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-26 10:37:30.565: E/AndroidRuntime(715):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
10-26 10:37:30.565: E/AndroidRuntime(715):  at java.util.ArrayList.get(ArrayList.java:311)
10-26 10:37:30.565: E/AndroidRuntime(715):  at com.example.barchart_test.Chart_MainActivity$1.onClick(Chart_MainActivity.java:46)
10-26 10:37:30.565: E/AndroidRuntime(715):  at android.view.View.performClick(View.java:2408)
10-26 10:37:30.565: E/AndroidRuntime(715):  at android.view.View$PerformClick.run(View.java:8816)
10-26 10:37:30.565: E/AndroidRuntime(715):  at android.os.Handler.handleCallback(Handler.java:587)
10-26 10:37:30.565: E/AndroidRuntime(715):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 10:37:30.565: E/AndroidRuntime(715):  at android.os.Looper.loop(Looper.java:123)
10-26 10:37:30.565: E/AndroidRuntime(715):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-26 10:37:30.565: E/AndroidRuntime(715):  at java.lang.reflect.Method.invokeNative(Native Method)
10-26 10:37:30.565: E/AndroidRuntime(715):  at java.lang.reflect.Method.invoke(Method.java:521)
10-26 10:37:30.565: E/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-26 10:37:30.565: E/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-26 10:37:30.565: E/AndroidRuntime(715):  at dalvik.system.NativeStart.main(Native Method)
10-26 10:37:32.885: I/Process(715): Sending signal. PID: 715 SIG: 9

Chart_MainActivity.java

public class Chart_MainActivity extends Activity 
{
EditText edt1, edt2;
// TextView txt_1;
Button btn;
ArrayList<String> result;

@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

edt1 = (EditText) findViewById(R.id.edt1_date1);
edt2 = (EditText) findViewById(R.id.edt_date2);
btn = (Button) findViewById(R.id.button_invoke);

result = new ArrayList<String>();

btn.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v) 
    {

        result.addAll(getTMSChart(edt1.getText().toString(), edt2.getText().toString()));

    //  result.add(x);
    //  result.add(y);
        Intent in = new Intent(getApplicationContext(), NextActivity.class);
        in.putExtra("gotonextpageX", result.get(0));
        in.putExtra("gotonextpageY", result.get(1));

        startActivity(in);
    }
});}

private ArrayList<String> getTMSChart(String FromDate, String ToDate) 
{

// txt_1 = (TextView)findViewById(R.id.textView1);
System.setProperty("http.keepAlive", "false");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet = true;

String NAMESPACE = "http://tempuri.org/";
String URL = "http://54.251.60.177/TMSOrdersService/TMSDetails.asmx";
String METHOD = "GetTMSChart";

SoapObject request = new SoapObject(NAMESPACE, METHOD);
request.addProperty("FromDate", FromDate);
request.addProperty("ToDate", ToDate);

envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

String x = "", y = "";

ArrayList<String> stringResult = new ArrayList<String>();

try 
{
    androidHttpTransport.call(NAMESPACE + METHOD, envelope);
    SoapObject result = (SoapObject) envelope.bodyIn;
    SoapObject root = (SoapObject) ((SoapObject) (result).getProperty(0)).getProperty("NewDataSet");
    int tablesCount = root.getPropertyCount();

    for (int i = 0; i < tablesCount; i++) 
    {
        SoapObject table = (SoapObject) root.getProperty(i);
        int propertyCount = table.getPropertyCount();

        for (int j = 0; j < propertyCount; j++) 
        {

            stringResult.add(table.getPropertyAsString("Order_No").toString());
            stringResult.add(table.getPropertyAsString("Freight_Rate").toString());

        //  stringResult.add(table.getPropertyAsString("Margin_Percent").toString());
        //  freightRate = Integer.parseInt(table.getPropertyAsString("Freight_Rate"));

        }
    }
}
catch (ArrayIndexOutOfBoundsException e) 
{}
catch(Exception e)
{}

return stringResult;
}    }

NextActivity.java

 public class NextActivity extends Activity 
 {
double x,y;

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

    Bundle extras = getIntent().getExtras();
    if(extras != null)
    {   
        x = extras.getDouble("gotonextpageX");
        y = extras.getDouble("gotonextpageY");
    }
    else
    {}

    final GraphicalView gv =createIntent();
    RelativeLayout rl=(RelativeLayout)findViewById(R.id.rlt);
    rl.addView(gv);
}   

 public GraphicalView createIntent() 
 {

    String[] titles = new String[] { "Orders profit"};

    List<double[]> values = new ArrayList<double[]>();
    values.add(new double[] {x,x,x});
    values.add(new double[] {y,y,y});


    int[] colors = new int[] { Color.parseColor("#77c4d3")};

    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
    renderer.setOrientation(Orientation.HORIZONTAL);

    setChartSettings(renderer,"","Order number","Freight Rate",0.5,12.5, 0, 1000, Color.GREEN, Color.GREEN);

    renderer.setXLabels(1);
    renderer.setYLabels(5);

    renderer.setPanEnabled(true, false);

    /*renderer.addXTextLabel(1, "Jan");
    renderer.addXTextLabel(2, "Feb");
    renderer.addXTextLabel(3, "Mar");
    renderer.addXTextLabel(4, "Apr");
    renderer.addXTextLabel(5, "May");
    renderer.addXTextLabel(6, "Jun");
    */

    int length = renderer.getSeriesRendererCount();

    for (int i = 0; i < length; i++) 
    {
        SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
        seriesRenderer.setDisplayChartValues(true);
      }

   final GraphicalView grfv = ChartFactory.getBarChartView(NextActivity.this, buildBarDataset(titles, values), renderer,Type.DEFAULT);
   return grfv;
   }

   protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) 
   {

       XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

       renderer.setAxisTitleTextSize(16);
       renderer.setChartTitleTextSize(20);

       renderer.setLabelsTextSize(15);
       renderer.setLegendTextSize(15);

       renderer.setBarSpacing(1);

       renderer.setMarginsColor(Color.parseColor("#0E120E"));
       renderer.setXLabelsColor(Color.WHITE);
       renderer.setYLabelsColor(0,Color.WHITE);

       renderer.setApplyBackgroundColor(true);
       renderer.setBackgroundColor(Color.parseColor("#000000"));

       int length = colors.length;

       for (int i = 0; i < length; i++) 
       {
           SimpleSeriesRenderer r = new SimpleSeriesRenderer();
           r.setColor(colors[i]);
           r.setChartValuesSpacing(-90);
           r.setChartValuesSpacing(15);
           renderer.addSeriesRenderer(r);
           }
          return renderer;
         }

   protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) 
   {
      XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
      int length = titles.length;

   for (int i = 0; i < length; i++) 
   {
       CategorySeries series = new CategorySeries(titles[i]);
       double[] v = values.get(i);
       int seriesLength = v.length;

       for (int k = 0; k < seriesLength; k++) 
       {
           series.add(v[k]);
           }
        dataset.addSeries(series.toXYSeries());
        }

   return dataset;
  }

 protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
 String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,int labelsColor) 
 {

     renderer.setChartTitle(title);
     renderer.setYLabelsAlign(Align.RIGHT);

     renderer.setXTitle(xTitle);
     renderer.setYTitle(yTitle);

     renderer.setXAxisMin(xMin);
     renderer.setXAxisMax(xMax);
     renderer.setYAxisMin(yMin);
     renderer.setYAxisMax(yMax);

     renderer.setMargins(new int[] { 10, 65, 10, 15 });

     renderer.setAxesColor(axesColor);
     renderer.setLabelsColor(labelsColor);
  } }

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<AbsoluteLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/edt1_date1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="46dp"
        android:layout_y="116dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/edt_date2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="46dp"
        android:layout_y="180dp"
        android:ems="10" />

    <Button
        android:id="@+id/button_invoke"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="204dp"
        android:layout_y="246dp"
        android:text="Invoke" />
</AbsoluteLayout>
</LinearLayout>

main1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

</RelativeLayout>

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.barchart_test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" android:maxSdkVersion="15"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity   android:name=".Chart_MainActivity"
                android:label="@string/title_activity_chart__main" >
    <intent-filter>
       <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

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

</application>
</manifest>

推荐答案

即使问题并未描述所有方面,这里看起来也很简单.

It looks like a simple issue here even though the question is not describing all the aspects.

我们可以很容易地猜测到主活动类中的第46行是以下内容:

We can easily guess that line 46 in the main activity class is the one below:

in.putExtra("gotonextpageX", result.get(0));

结果 result 列表中没有元素,并且您尝试获取第一个元素,因此抛出了 IndexOutOfBounds 异常.

The result list has no elements in it and you are trying to get the first one, so the IndexOutOfBounds exception is thrown.

这篇关于Android中的条形图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆