2012-09-21 79 views
0

如何在android中創建條形圖?如何在android中使用web服務創建條形圖?

注: - 但我需要從Web服務使用x軸和y軸值。 這意味着Web服務將顯示x和y軸的值。然後,我需要使用這些值作爲x和y軸值來創建條形圖。

如何做到這一點?建議請。

logcat的

09-21 15:09:53.103: D/AndroidRuntime(271): Shutting down VM 
09-21 15:09:53.103: W/dalvikvm(271): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
09-21 15:09:53.114: E/AndroidRuntime(271): FATAL EXCEPTION: main 
09-21 15:09:53.114: E/AndroidRuntime(271): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xmlparsing_test/com.example.xmlparsing_test.BarGraphActivity}: java.lang.NullPointerException 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.os.Looper.loop(Looper.java:123) 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.app.ActivityThread.main(ActivityThread.java:4627) 
09-21 15:09:53.114: E/AndroidRuntime(271): at java.lang.reflect.Method.invokeNative(Native Method) 
09-21 15:09:53.114: E/AndroidRuntime(271): at java.lang.reflect.Method.invoke(Method.java:521) 
09-21 15:09:53.114: E/AndroidRuntime(271): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
09-21 15:09:53.114: E/AndroidRuntime(271): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
09-21 15:09:53.114: E/AndroidRuntime(271): at dalvik.system.NativeStart.main(Native Method) 
09-21 15:09:53.114: E/AndroidRuntime(271): Caused by: java.lang.NullPointerException 
09-21 15:09:53.114: E/AndroidRuntime(271): at com.example.xmlparsing_test.BarGraphActivity.buildBarDataset(BarGraphActivity.java:115) 
09-21 15:09:53.114: E/AndroidRuntime(271): at com.example.xmlparsing_test.BarGraphActivity.DrawGraph(BarGraphActivity.java:57) 
09-21 15:09:53.114: E/AndroidRuntime(271): at com.example.xmlparsing_test.BarGraphActivity.onCreate(BarGraphActivity.java:22) 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
09-21 15:09:53.114: E/AndroidRuntime(271): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
09-21 15:09:53.114: E/AndroidRuntime(271): ... 11 more 
09-21 15:10:04.033: I/Process(271): Sending signal. PID: 271 SIG: 9 

chart.java

public class Chart extends Activity 
{  
    EditText edt1,edt2; 
TextView txtv1; 
Button btn; 

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

    edt1 = (EditText)findViewById(R.id.editText1); 
    edt2 = (EditText)findViewById(R.id.editText2); 

    btn = (Button)findViewById(R.id.button1); 
    btn.setOnClickListener(new View.OnClickListener() 
    { 

public void onClick(View v) 
{ 
    getTMSChart(edt1.getText().toString(),edt2.getText().toString()); 

    Intent intnt = new Intent(v.getContext(), BarGraphActivity.class); 
      startActivity(intnt); 
     } 
    }); 
    } 
public void getTMSChart(String FromDate, String ToDate) 
    { 
    System.setProperty("http.keepAlive", "false");   
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);   

    envelope.dotNet = true; 

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

    SoapObject request = new SoapObject(NAMESPACE, METHOD); 

    request.addProperty("FromDate","");    
    request.addProperty("ToDate",""); 

    envelope.setOutputSoapObject(request); 


    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    try 
    { 
     androidHttpTransport.call(SOAP_ACTION, 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++) 
    {   
     //String ord = table.getPropertyAsString(""); 

     int orderNo = Integer.parseInt(table.getPropertyAsString("Order_No")); 
     int freightRate = Integer.parseInt(table.getPropertyAsString("Freight_Rate")); 
     int marginPercent =  Integer.parseInt(table.getPropertyAsString("Margin_Percent")); 
     } 
    } 
} 
catch (Exception e) 
{} 
} } 

BarGraphActivity.java

public class BarGraphActivity extends Chart 
{ 
public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main1); 
    DrawGraph(); 
    } 
Chart ch = new Chart(); 


public void DrawGraph() 
{ 

String[] titles = new String[] { " Barcharts",""}; 

int[] x_axis =null; 
int[] y_axis =null; 

List<int[]> values = new ArrayList<int[]>(); 
values.add(x_axis); 
values.add(y_axis); 

int[] colors = new int[] { Color.WHITE, Color.BLACK}; 

XYMultipleSeriesRenderer renderer = buildBarRenderer(colors); 
renderer.setOrientation(Orientation.HORIZONTAL); 
setChartSettings(renderer, "some texts", " ", " ",0,x_axis, 0,y_axis, Color.GRAY, Color.LTGRAY); 

renderer.setXLabels(1); 
renderer.addXTextLabel(2, "orderNo"); 
renderer.addYTextLabel(2, "freight"); 
renderer.setPanEnabled(false, false); 

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

    ChartFactory.getBarChartIntent(this, buildBarDataset(titles, values), renderer,Type.DEFAULT); 
    } 

public XYMultipleSeriesRenderer buildBarRenderer(int[] colors) 
{ 
    // creates a SeriesRenderer and initializes it with useful default values as well as colors 

XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); 

renderer.setAxisTitleTextSize(15); 
renderer.setChartTitleTextSize(20); 
renderer.setLabelsTextSize(15); 
renderer.setLegendTextSize(15); 

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

public void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,String yTitle, int xMin, int[] x_axis, int yMin, int[] y_axis, int axesColor,int labelsColor) 
{ 
    // sets lots of default values for this renderer 

renderer.setChartTitle(title); 

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

renderer.setXAxisMin(xMin); 

renderer.setYAxisMin(yMin); 
renderer.setAxesColor(axesColor); 
renderer.setLabelsColor(labelsColor); 

renderer.setApplyBackgroundColor(true); 
renderer.setBackgroundColor(Color.BLACK); 
} 

public XYMultipleSeriesDataset buildBarDataset(String[] titles, List<int[]> values) 
{ 
    // adds the axis titles and values into the dataset 

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 

int length = titles.length; 

for (int i = 0; i < length; i++) 
{ 
    CategorySeries series = new CategorySeries(titles[i]); 

    int[] 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; 
    } } 

謝謝s爲您的寶貴時間!

+0

問題是什麼? - 你可以馬上寫一個的AsyncTask在doInBackground調用WS和分析數據分成兩個ArrayList中,然後傳入的AsyncTask的onPostExecute數組列表,並有使用這兩個數組列表創建圖表。 – MKJParekh

+0

@MKJParekh其實我試過了,就像你所建議的那樣。但是我在logcat上得到了一些空指針異常。 – Nakshatran

+0

然後發佈您的日誌。 –

回答

2

1-從Web服務獲取值。

這與條形圖無關。只需調用Web服務並獲取所需的值即可。

2-一旦你有了值,你可以使用achartengine來創建一個條形圖。

編輯:

在這裏讀您的更新是我的總體思路。

chart.java中,您正在調用Web服務並將值讀入局部變量,因此這些值不會存儲在任何位置。

BarGraphActivity.DrawGraph()x_axisy_axis總是null,因此NullPointerException

解決辦法:

在您的第一項活動收集自己的價值觀int陣列和在當你開始你的第二個活動的意圖給他們。

在後者中,檢索數組並將它們傳遞給DrawGraph()

還有一個注意事項:您正在主線程上調用Web服務,這不可取。您應該將其轉換爲AsyncTaskdoInBackground()方法。

+0

請訪問我的問題,我再次給我logcat的參考 – Nakshatran

+0

其實我從完美網絡服務獲取價值,但我解析到條形圖的方式是錯誤的,我認爲? – Nakshatran

+0

請發佈你的'onCreate()'和'DrawGraph()'方法。 –

0

看起來像一些不好的XML給我。你的解析器給了你一個NullPointerException

我不知道你的Android代碼正在使用哪種解析器,SAX或DOM。我不知道您調用的服務是否發回XML raw或包裝在SOAP信封中。我不知道您是否使用了知道如何從SOAP信封中提取有效內容的SOAP客戶端。一旦擁有了數據,我不知道您的數據正在做什麼。

但你應該。

+0

的值時,得到了一些錯誤,但我在將這些值解析到條形圖時出現一些錯誤。 – Nakshatran