我正在开展一个需要绘制数据的项目。我在Windows 7 64位PC上使用Android Studio 5.2.0和AChartEngine 1.2.0。 我对y标签的工程格式感兴趣,例如2.3E6 .. 我没有任何成功就研究过我能做到的事情。 我希望1,3,0,1和1为最小整数位数,最大整数位数,最小分数位数,最大小数位数和否。指数部分中的数字分别为。另外,指数必须是3的多重。
这是我的LineGraph类:
public class LineGraph
{
public Intent getIntent(Context context)
{
int size = ( int ) Math.pow(2, 10);
double start = 0;
double end = 2 * Math.PI;
double inc = (end - start) / (size - 1);
double[] x = new double[size];
double[] y = new double[size];
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
TimeSeries series = new TimeSeries("Sin * Cos");
for ( int i = 0; i < size; i++ )
{
x[i] = start + i * inc;
y[i] = 10000 * Math.sin(x[i]) * Math.cos(x[i]);
series.add(x[i], y[i]);
}
dataset.addSeries(series);
XYSeriesRenderer ren = new XYSeriesRenderer();
// XYSeriesRenderer properties
ren.setColor(Color.RED);
ren.setFillPoints(true);
ren.setDisplayBoundingPoints(false);
ren.setFillPoints(true);
ren.setLineWidth(2f);
// XYMultipleSeriesRenderer properties
XYMultipleSeriesRenderer mRen = new XYMultipleSeriesRenderer();
mRen.setAntialiasing(true);
mRen.setMargins(new int[] { 10, 80, 10, 10 });
mRen.setShowGrid(true);
mRen.setApplyBackgroundColor(true);
mRen.setBackgroundColor(Color.LTGRAY);
mRen.setGridColor(Color.WHITE);
mRen.setLabelsTextSize(20f);
mRen.setYLabelsVerticalPadding(-5f);
mRen.setYLabelsPadding(40);
mRen.setShowLegend(false);
mRen.setXLabels(5);
mRen.setYLabels(10);
// Number format
DecimalFormat noFormat = new DecimalFormat("##0.##E0");
noFormat.setMinimumIntegerDigits(1);
noFormat.setMaximumIntegerDigits(3);
noFormat.setMinimumFractionDigits(0);
noFormat.setMaximumFractionDigits(2);
mRen.setYLabelFormat(noFormat, 0);
mRen.addSeriesRenderer(renderer);
// Construct and return the intent.
Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRen, "Line Graph");
return intent;
}
}
这是我的MainActivity:
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if ( id == R.id.action_settings )
{ return true; }
return super.onOptionsItemSelected(item);
}
public void lineGraphHandler(View view)
{
LineGraph lineGraph = new LineGraph();
Intent lineIntent = lineGraph.getIntent(this);
startActivity(lineIntent);
}
}
这是我的activity_main.xml:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ramin.graphtutorial.graph.MainActivity">
<Button
android:id="@+id/button_linegraph"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="lineGraphHandler"
android:text="@string/button_line_graph_text"/>
</RelativeLayout>
最后,这是我的manifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="ramin.graphtutorial.graph"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:allowClearUserData="true"
android:allowTaskReparenting="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="ramin.graphtutorial.graph.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="org.achartengine.GraphicalActivity"/>
</application>
</manifest>
当我将noFormat的格式方法应用于单个double值时,它可以正常工作,但不能用于映射y标签。对于后者,我会做以下事情;
XYMultipleSeriesRenderer mRen = new XYMultipleSeriesRenderer();
XYSeriesRenderer ren = new XYSeriesRenderer ();
mRen.addSeriesRenderer(ren);
int noOfYLabels = 5;
mRen.setYLabels( noOfYLabels );
mRen.setYLabelFormat( noFormat, 0 );
但是我只得到三个零,即沿着y轴的所有值都是000,大于1000。 我还尝试通过以下代码片段手动设置y值文本标签:
Double[] ylabels = mRen.getYTextLabelLocations(0);
mRen.setYLabels( 0 );
for ( int i = 0; i < noOfYLabels; i++ )
{ mRen.addYTextLabel( ylabels[ i ], noFormat.format( ylabels[ i ] ) ); }
然后我在数组yLabels中得到索引超出范围。 有人可以帮我解决这个问题吗? PS:我之前问了这个问题,但没有得到任何答案。也许问题不够明确。 先感谢您。 拉敏。
答案 0 :(得分:0)
尝试这样的事情
private LinearLayout chartLayout;
private GraphicalView mChart;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showlineGraph( );
}
public void showlineGraph()
{
chartLayout= (LinearLayout) findViewById(R.id.graphHolder);
// LineGraph lineGraph = new LineGraph();
int size = (int) Math.pow(2, 10);
double start = 0;
double end = 2 * Math.PI;
double inc = (end - start) / (size - 1);
double[] x = new double[size];
double[] y = new double[size];
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
TimeSeries series = new TimeSeries("Sin * Cos");
for(int i = 0; i < size; i++)
{
x[i] = start + i * inc;
y[i] = 1000 * Math.sin(x[i]) * Math.cos(x[i]);
series.add(x[i], y[i]);
}
dataset.addSeries(series);
XYSeriesRenderer ren = new XYSeriesRenderer();
// XYSeriesRenderer properties
ren.setColor(Color.RED);
ren.setFillPoints(true);
ren.setDisplayBoundingPoints(false);
ren.setFillPoints(true);
ren.setLineWidth(2f);
// XYMultipleSeriesRenderer properties
XYMultipleSeriesRenderer mRen = new XYMultipleSeriesRenderer();
mRen.setAntialiasing(true);
mRen.setMargins(new int[] { 10, 80, 10, 10 });
mRen.setShowGrid(true);
mRen.setApplyBackgroundColor(true);
mRen.setBackgroundColor(Color.LTGRAY);
mRen.setGridColor(Color.WHITE);
mRen.setLabelsTextSize(20f);
mRen.setYLabelsVerticalPadding(-5f);
mRen.setYLabelsPadding(40);
mRen.setShowLegend(false);
mRen.setXLabels(5);
mRen.setYLabels(10);
// Number format
DecimalFormat noFormat = new DecimalFormat("##0.##E0");
noFormat.setMinimumIntegerDigits(1);
noFormat.setMaximumIntegerDigits(3);
noFormat.setMinimumFractionDigits(0);
noFormat.setMaximumFractionDigits(2);
mRen.setYLabelFormat(noFormat, 0);
mRen.addSeriesRenderer(ren);
// Construct and return the intent.
view = ChartFactory.getLineChartView(this, dataset, mRen);
chartLayout.addView(view);
}