现在我正在使用 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);
} } }
感谢您宝贵的时间!..