I am working on an application that will let a user make a graph using the number of hours that the user has slept. Currently the attached code will take the input and make a graph expecting an integer values from the user. I want to be able to instead, allow the user to input a date or a string value, and place that into the x-axis instead. Is there a way to do this? Currently the add method for the mCurrentSeries is expecting two double values in its parameters. I have been searching for a solution all night, but I am not even sure if this is possible. Most of the examples that I found, have the dates already hardcoded in. I want everything to come from the editText boxes. Any help will be appreciated. My code below:
.XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/add_values"/>
<RelativeLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/x" android:padding="5dp"
android:id="@+id/dateTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText android:id="@+id/xValue" android:layout_width="wrap_content"
android:inputType="numberDecimal"
android:layout_height="wrap_content" android:layout_weight="1" android:enabled="false"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/dateTextView" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/y" android:padding="5dp"
android:layout_below="@+id/xValue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/sleepHoursTextView" />
<EditText android:id="@+id/yValue" android:layout_width="wrap_content"
android:inputType="number"
android:layout_height="wrap_content" android:layout_weight="1" android:enabled="false"
android:layout_alignBottom="@+id/sleepHoursTextView"
android:layout_toRightOf="@+id/sleepHoursTextView"
android:layout_toEndOf="@+id/sleepHoursTextView" />
<Button android:id="@+id/add" android:text="@string/add"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="false"
android:layout_alignBottom="@+id/yValue"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<LinearLayout android:id="@+id/chart" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/new_series" android:text="@string/new_series"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
.Java file
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class XYChartBuilder extends Activity {
/** The main dataset that includes all the series that go into a chart. */
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
/** The main renderer that includes all the renderers customizing a chart. */
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
/** The most recently added series. */
private XYSeries mCurrentSeries;
/** The most recently created renderer, customizing the current series. */
private XYSeriesRenderer mCurrentRenderer;
/** Button for creating a new series of data. */
private Button mNewSeries;
/** Button for adding entered data to the current series. */
private Button mAdd;
/** Edit text field for entering the X value of the data to be added. */
private EditText mX;
/** Edit text field for entering the Y value of the data to be added. */
private EditText mY;
/** The chart view that displays the data. */
private GraphicalView mChartView;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save the current data, for instance when changing screen orientation
outState.putSerializable("dataset", mDataset);
outState.putSerializable("renderer", mRenderer);
outState.putSerializable("current_series", mCurrentSeries);
outState.putSerializable("current_renderer", mCurrentRenderer);
}
@Override
protected void onRestoreInstanceState(Bundle savedState) {
super.onRestoreInstanceState(savedState);
// restore the current data, for instance when changing the screen
// orientation
mDataset = (XYMultipleSeriesDataset) savedState.getSerializable("dataset");
mRenderer = (XYMultipleSeriesRenderer) savedState.getSerializable("renderer");
mCurrentSeries = (XYSeries) savedState.getSerializable("current_series");
mCurrentRenderer = (XYSeriesRenderer) savedState.getSerializable("current_renderer");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xy_chart);
// the top part of the UI components for adding new data points
mX = (EditText) findViewById(R.id.xValue);
mY = (EditText) findViewById(R.id.yValue);
mAdd = (Button) findViewById(R.id.add);
// set some properties on the main renderer
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
mRenderer.setAxisTitleTextSize(50);
mRenderer.setChartTitleTextSize(50);
mRenderer.setLabelsTextSize(50);
mRenderer.setLegendTextSize(50);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setPointSize(5);
// the button that handles the new series of data creation
mNewSeries = (Button) findViewById(R.id.new_series);
mNewSeries.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String seriesTitle = "Sleep Data " + (mDataset.getSeriesCount() + 1);
// create a new series of data
XYSeries series = new XYSeries(seriesTitle);
mDataset.addSeries(series);
mCurrentSeries = series;
// create a new renderer for the new series
XYSeriesRenderer renderer = new XYSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
// set some renderer properties
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(false);
renderer.setDisplayChartValues(true);
renderer.setDisplayChartValuesDistance(50);
renderer.setLineWidth(10);
renderer.setPointStrokeWidth(20);
mCurrentRenderer = renderer;
setSeriesWidgetsEnabled(true);
mChartView.repaint();
}
});
mAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
double x = 0;
double y = 0;
try {
x = Double.parseDouble(mX.getText().toString());
} catch (NumberFormatException e) {
mX.requestFocus();
return;
}
try {
y = Double.parseDouble(mY.getText().toString());
} catch (NumberFormatException e) {
mY.requestFocus();
return;
}
// add a new data point to the current series
mCurrentSeries.add(x, y);
mX.setText("");
mY.setText("");
mX.requestFocus();
// repaint the chart such as the newly added point to be visible
mChartView.repaint();
}
});
}
@Override
protected void onResume() {
super.onResume();
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
// enable the chart click events
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
mChartView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// handle the click event on the chart
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(XYChartBuilder.this, "No chart element", Toast.LENGTH_SHORT).show();
} else {
// display information of the clicked point
Toast.makeText(
XYChartBuilder.this,
"Chart element in series index " + seriesSelection.getSeriesIndex()
+ " data point index " + seriesSelection.getPointIndex() + " was clicked"
+ " closest point value X=" + seriesSelection.getXValue() + ", Y="
+ seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});
layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
boolean enabled = mDataset.getSeriesCount() > 0;
setSeriesWidgetsEnabled(enabled);
} else {
mChartView.repaint();
}
}
/**
* Enable or disable the add data to series widgets
*
* @param enabled the enabled state
*/
private void setSeriesWidgetsEnabled(boolean enabled) {
mX.setEnabled(enabled);
mY.setEnabled(enabled);
mAdd.setEnabled(enabled);
}
}