我想用我的代码做几件事
- 我想查看所有 Y 轴标签(在我的代码中,最后一个标签未显示)。
- 我想设置我需要的图表高度,使它看起来像风景(在我的代码中,图表是如此渴望)。
由于我的声誉低,我无法发布图片
请帮助我,对不起英语不好。
提前致谢。这是我的代码
public class MainActivity extends Activity
{私人查看mChart;
private String[] mMonth = new String[] {
"Jan", "Feb" , "Mar", "Apr", "May", "Jun",
"Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openChart();
}
private void openChart()
{
int[] x ={1, 2, 3, 4, 5, 6, 7, 8};
int[] income ={20, 25, 27, 30, 28, 35, 37, 38};
int[] expense ={22, 27, 29, 28, 26, 30, 33, 34};
//Creating an XYSeries for Income
XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Expense
XYSeries expenseSeries = new XYSeries("Expense");
// Adding data to Income and Expense Series
for(int i=0 ; i<x.length ; i++)
{
incomeSeries.add(x[i], income[i]);
expenseSeries.add(x[i],expense[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
dataset.addSeries(expenseSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.WHITE);
incomeRenderer.setPointStyle(PointStyle.SQUARE);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setChartValuesSpacing(10);
incomeRenderer.setChartValuesTextAlign(Align.CENTER);
incomeRenderer.setChartValuesTextSize(15);
incomeRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
expenseRenderer.setColor(Color.YELLOW);
expenseRenderer.setPointStyle(PointStyle.SQUARE);
expenseRenderer.setFillPoints(true);
expenseRenderer.setLineWidth(2);
expenseRenderer.setChartValuesSpacing(10);
expenseRenderer.setChartValuesTextAlign(Align.CENTER);
expenseRenderer.setChartValuesTextSize(15);
expenseRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
// Use To Display The Line In The Middle Of Chart
multiRenderer.setShowGridX(true);
multiRenderer.setShowGridY(true);
// (new int[] {top, left, bottom, right})
multiRenderer.setMargins(new int[] {0,60,1,20});
multiRenderer.setAxisTitleTextSize(15);
multiRenderer.setChartTitle("\n Income vs Expense Chart");
multiRenderer.setLabelsTextSize(15);
multiRenderer.setYLabels(2);
multiRenderer.setYLabelsAlign(Align.RIGHT);
multiRenderer.setYTitle("\nAmount in Dollars");
multiRenderer.setXTitle("\n\n\n\n Year 2013");
multiRenderer.setXLabels(0);
// Visible-InVisible Zoom Button
multiRenderer.setZoomButtonsVisible(false);
// Stop Moving Chart
multiRenderer.setPanEnabled(false);
for(int i=0;i<x.length;i++)
{
multiRenderer.addXTextLabel(i+1, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
multiRenderer.addSeriesRenderer(expenseRenderer);
// Getting a reference to LinearLayout of the MainActivity Layout
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
// Creating a Line Chart
mChart = ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);
// Adding the Line Chart to the LinearLayout
chartContainer.addView(mChart);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}