我试图从csv数据中获取第4和第5个元素,并将其用作第2个(mDatasetNormal)和第3个图形线的数据集。我能够弄清楚如何从第3个元素获取数据,但其余的是一个问题。
我的问题是:
CSV文件内容:
8月30-2014,08:06 AM,0,0,0
Sep-05-2014,08:09 AM,0,3,2
Sep-05-2014,08:09 AM,0,3,2
整个代码:
public class Chart extends Activity {
ArrayList<Integer> stateList = new ArrayList<Integer>();
ArrayList<Integer> normalList = new ArrayList<Integer>();
//Chart creation
private GraphicalView mChart, mChartNormal;
/**
* Create multiple data sets to be used on graph
*/
//Data sets used for graph manipulation
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
private XYSeries mCurrentSeries;
private XYSeriesRenderer mCurrentRenderer;
private XYMultipleSeriesDataset mDatasetNormal = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer mRendere2 = new XYMultipleSeriesRenderer();
private XYSeries mCurrentSeriesNormal;
private XYSeriesRenderer mCurrentRendererNormal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Code which makes activity full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_chart);
readHappyLog();
}
//Create a method for the aChart engine
private void initChart() {
//Series description for both charts
mCurrentSeries = new XYSeries("Amount of happy kids");
mDataset.addSeries(mCurrentSeries);
mCurrentSeriesNormal = new XYSeries("Amount of normal kids");
mDatasetNormal.addSeries(mCurrentSeriesNormal);
//Renderer for happy kids
mCurrentRenderer = new XYSeriesRenderer();
mRenderer.addSeriesRenderer(mCurrentRenderer);
mRenderer.setAxisTitleTextSize(24);
mRenderer.setYLabelsPadding(20);
mRenderer.setXLabelsPadding(10);
mRenderer.setXTitle(" Date of practice ");
mRenderer.setYTitle(" Number of kids ");
//Renderer for normal kids
mCurrentRendererNormal = new XYSeriesRenderer();
mRendere2.addSeriesRenderer(mCurrentRendererNormal);
mRendere2.setYTitle("Number of normal kids");
//Applying background
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.BLACK);
mRenderer.setMarginsColor(Color.BLACK);
mRenderer.setPointSize(20);
mRenderer.setShowGrid(true);
mRenderer.setAxesColor(Color.MAGENTA);
mRenderer.setGridColor(Color.MAGENTA);
mRenderer.setPanEnabled(true, true);
mRenderer.setXAxisMin(0.0);
mRenderer.setYAxisMin(0.0);
mRenderer.setLabelsTextSize(24);
//Sets the color of the graph line and width
mCurrentRenderer.setColor(Color.GREEN);
mCurrentRenderer.setLineWidth(10f);
mCurrentRendererNormal.setColor(Color.BLUE);
mCurrentRenderer.setLineWidth(10f);
}
//Add x and y data into the graph and mark the x graph
private void addHappyData(){
Integer x = 0;
for (Integer happy : stateList ){
mCurrentSeries.add(x += 10, happy);
}
}
//Add x and y for normal to the graph
private void addNormalData(){
Integer y = 0;
for (Integer happy : normalList ){
mCurrentSeriesNormal.add(y += 10, happy);
}
}
// Draw the graph
@Override
protected void onResume() {
super.onResume();
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
if (mChart == null){
initChart();
addHappyData();
addNormalData();
//Set chart type
mChart = ChartFactory.getLineChartView(this, mDataset , mRenderer);
layout.addView(mChart);
mChartNormal = ChartFactory.getLineChartView(this, mDatasetNormal,mRendere2);
layout.addView(mChartNormal);
}else{
mChart.repaint();
mChartNormal.repaint();
}
}
/**
* create a method to read the happy log
*/
private void readHappyLog(){
String FILENAME = "happy_log.csv"; //Open the file name under this string
FileInputStream inputStream = null; // Import the package
String temp;
String[] a;
try {
inputStream = openFileInput(FILENAME); //Open file desceriptor (Object)
byte[] reader = new byte [inputStream.available() ]; //Everything from disk is byte
while (inputStream.read( reader ) != -1 ){}
//Reader array now holds the entire file
//Needs to create scanner in order to read the file properly
Scanner s = new Scanner (new String(reader));
s.useDelimiter(("\\n"));
while (s.hasNext()){
//Split the string lines if he sees comma value
temp = s.next();
a = temp.split(",");
stateList.add(Integer.parseInt(a[2]));
normalList.add(Integer.parseInt(a[3]));
}
s.close();
}catch (Exception e ){
Log.e("Chart", e.getMessage());
}finally {
if (inputStream != null ){
try{ inputStream.close();
} catch (IOException e){
Log.e( "Chart", e.getMessage());
}
}
}
}
答案 0 :(得分:1)
Problem solved:
//Add x and y data to series
private void addHappyData() {
Integer x = 0;
for (Integer happy : stateList) {
mCurrentSeries.add(x += 10, happy);
}
}
private void addNormalData() {
Integer x = 0;
for (Integer normal : normalList) {
mNormalSeries.add(x += 10, normal);
}
}
// Draw the graph
@Override
protected void onResume() {
super.onResume();
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
if (mChart == null) {
initChart();
addHappyData();
addNormalData();
//Set chart type
mChart = ChartFactory.getLineChartView(this, mDataset, mRenderer);
layout.addView(mChart);
} else {
mChart.repaint();
}
}
/**
* create a method to read the happy log
*/
private void readHappyLog() {
String FILENAME = "happy_log.csv"; //Open the file name under this string
FileInputStream inputStream = null; // Import the package
String temp;
String[] a;
try {
inputStream = openFileInput(FILENAME); //Open file desceriptor (Object)
byte[] reader = new byte[inputStream.available()]; //Everything from disk is byte
while (inputStream.read(reader) != -1) {
}
//Reader array now holds the entire file
//Needs to create scanner in order to read the file properly
Scanner s = new Scanner(new String(reader));
s.useDelimiter(("\\n"));
while (s.hasNext()) {
//Split the string lines if he sees comma value
temp = s.next();
a = temp.split(",");
stateList.add(Integer.parseInt(a[2]));
normalList.add(Integer.parseInt(a[3]));
}
s.close();
} catch (Exception e) {
Log.e("Chart", e.getMessage());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
Log.e("Chart", e.getMessage());
}
}
}
}
}