现在我正在使用AChartEngine在Android中创建一个条形图。在运行我的应用程序之后,首先模拟器显示如下图所示。但是我需要像第二张图片一样展示如何做到这一点?
而且我还需要更改背景颜色,我也想改变条形图颜色, 怎么做这些事情?
我已尝试过这些代码,但无法正常使用
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.GRAY);
请查阅我的资料来源。
Chart.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();
} }}
BarGraphActivity.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);
} } }
感谢您宝贵的时间!..
答案 0 :(得分: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);
}
如果要创建多个渲染器,则为双栏使用不同的颜色。如果要创建单条,则仅表示一种颜色。它会起作用。