0

在此处输入图像描述

在此处输入图像描述

现在我正在使用 AChartEngine 在 android 中创建一个条形图。运行我的应用程序后,首先模拟器显示如下图所示。但我需要像下面的第二张图片一样展示如何做到这一点?

而且我还需要改变背景的颜色,我也想改变条形图的颜色,如何做这些事情?

我已经尝试过这些代码,但无法正常工作

renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.GRAY);

请找到我的来源以供参考。

图表.java

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

String[] orderNo = new String[10];
int[] freightRate = new int[10];
int[] marginPercent = new int[10];

@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);
            intnt.putExtras(data);

            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", FromDate.trim());
    request.addProperty("ToDate", ToDate.trim());
    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();

        data = new Bundle();

        for (int i = 0; i < tablesCount; i++) {

            SoapObject table = (SoapObject) root.getProperty(i);
            orderNo[i] = table.getPropertyAsString("Order_No");
            freightRate[i] = Integer.parseInt(table.getPropertyAsString("Freight_Rate"));
            marginPercent[i] = Integer.parseInt(table.getPropertyAsString("Margin_Percent"));               

/*Toast.makeText(getApplicationContext(), "Order No:" + orderNo[i] + "\nFreigtRate:" + freightRate[i] + "\nMarginPc:" + marginPercent[i], Toast.LENGTH_SHORT).show();*/
        }
        data.putStringArray("orderno", orderNo);
        data.putIntArray("freightrate", freightRate);
        data.putIntArray("marginpercent", marginPercent);
        data.putInt("count",tablesCount);
    }
catch (Exception e) 
{
    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    e.printStackTrace();
    }   }}

条形图活动.java

public class BarGraphActivity extends Activity{

String[] orderNo = new String[10];
int[] freightRate = new int[10];
int[] marginPercent = new int[10];
int count;


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

    Bundle idata = getIntent().getExtras();
    if (idata !=null)
    {
        orderNo = idata.getStringArray("orderno");  
        freightRate = idata.getIntArray("freightrate");
        marginPercent = idata.getIntArray("marginpercent");
        count = idata.getInt("count");


        CategorySeries series = new CategorySeries("Order-FreightRate Graph");
        for ( int i = 0; i < count; i++){
            series.add(orderNo[i], freightRate[i]);
        }


        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(series.toXYSeries());

        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setDisplayChartValues(true);
        //renderer.setChartValuesSpacing(1);



        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
        mRenderer.addSeriesRenderer(renderer);

        for ( int i = 0; i < count; i++){
            mRenderer.addXTextLabel(i+1, orderNo[i]);
            }

        mRenderer.setZoomButtonsVisible(true);
        mRenderer.setBarSpacing(0.5);


        Intent intent = ChartFactory.getBarChartIntent(getApplicationContext(), dataset, mRenderer, Type.DEFAULT);
        startActivity(intent);      
        }        }  }

感谢您宝贵的时间!..

4

1 回答 1

1

嗨,如果您想更改图表的背景颜色,只需使用这些方法

   mrenderer.setApplyBackgroundColor(true);
   mrenderer.setBackgroundColor(Color.parseColor("#898989")); // here you have to give hexa decimal code 
   mrenderer.setMarginsColor(Color.parseColor("#898989")); // here you have to give hexa decimal code

如果你想改变你的酒吧颜色意味着这样做

       int[] colors = new int[] { Color.parseColor("#DAE1EB"), Color.parseColor("#406193")};
     int color_length = colors.length;
     for (int i = 0; i < color_length; i++) 
     {
         SimpleSeriesRenderer r = new SimpleSeriesRenderer();
         r.setColor(colors[i]);
         r.setChartValuesSpacing(5);
         mrenderer.addSeriesRenderer(r);
     }   

如果您要创建多个渲染器,则意味着对双条使用不同的颜色。如果您正在创建单条,则意味着只提供一种颜色。它会起作用。

于 2012-11-16T09:33:18.067 回答