Add value from Array to an XY Series - android

I want to add a value from SQLite to an XY series to create a diagram. Here is what I've done so far:
Double a, b;
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
ArrayList x = new ArrayList();
ArrayList y = new ArrayList();
notesCursor.moveToFirst();
for(int i = 0; i<notesCursor.getCount();i++){
a = notesCursor.getDouble(notesCursor.getColumnIndex(mDbHelper.KEY_ROWID));
b = notesCursor.getDouble(notesCursor.getColumnIndex(mDbHelper.KEY_RESULT));
}
x.add(a);
y.add(b);
notesCursor.moveToNext();
mCurrentSeries.add(x, y);
if (mChartView != null) {
mChartView.repaint();
}
But I've got a problem at the line:
mCurrentSeries.add(x, y);
How I can add an Array to an XY series? Any suggestions?

not sure what library you are using here to create your diagram (chart?). I use aChartEngine which is pretty good. The code I use to get a dataset containing a time series in aChartEngine is:
public static XYMultipleSeriesDataset getDemoDataset(Cursor c,
String title, String... columnName) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
TimeSeries series = new TimeSeries(title);
try {
if (c.moveToFirst()) {
do {
int mins = c.getInt(c.getColumnIndex(columnName[0]));
java.util.Date date =null;
try{
date = DateFactory.stringToDate(c.getString(c.getColumnIndex(columnName[1])));
}catch(Exception e){
}
if(date==null){
continue;
}
series.add(date, mins);
} while (c.moveToNext());
} else {
Log.d(TAG, "There were no values in the cursor.");
}
} finally {
Log.d(TAG, "finally from getDemoDataset being called");
c.close();
}
dataset.addSeries(series);
return dataset;
}

Related

DataPoint [] does not accept float variables

I was trying to make a graph based on the two array values, i.e conc and optical density. I have created two activities for saving those arrays and pass the array list using bundle and intent to the third activity using string.
Whenever I try to run the code, my app crashes.
GraphView gpView;
String co;
String od;
Float x,y;
int l1,l2;
DataPoint [] dp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Bundle bundle1 = getIntent().getExtras();
ArrayList<String> array1 = (ArrayList<String>) bundle1.getStringArrayList("Conc");
Bundle bundle2 = getIntent().getExtras();
ArrayList<String> array2 = (ArrayList<String>) bundle2.getStringArrayList("Od");
gpView= findViewById(R.id.graph);
LineGraphSeries <DataPoint> series = new LineGraphSeries<>();
l1 = array1.size();
l2 = array2.size();
if((array1.contains(true))&&(array2.contains(true)))
{
for (int c1 = 0; c1<l1; c1++)
{
for(int c2=0; c2<l2; c2++ ) {
co = array1.get(c1);
od = array2.get(c2);
x = Float.parseFloat(co);
y = Float.parseFloat(od);
dp[c1] = new DataPoint(x,y);
}
}
gpView.addSeries(series);
}
}
Alternately I tried to run a simple input method and it worked using this
private DataPoint[] getDataPoint ()
{
dp = new DataPoint[]
{
new DataPoint(10, 0.2),
new DataPoint(20, 0.38),
new DataPoint(30, 0.45),
new DataPoint(40, 0.69),
new DataPoint(50, 0.81),
};
return (dp);
}
I think you haven't initialized your dp array. Initialize your array
dp = new DataPoint[l1];
before if statement if((array1.contains(true))&&(array2.contains(true)))
Edit:
May be you are having NullPointerException. Check array1 and array2 is not null
if(array1 == null){
Toast.makeText(this, "array1 is null", Toast.LENGTH_LONG).show();
}
if(array2 == null){
Toast.makeText(this, "array2 is null", Toast.LENGTH_LONG).show();
}
if(array1 != null){
l1 = array1.size();
}
if(array2 != null){
l2 = array2.size();
}
if(l1 > 0){
dp = new DataPoint[l1];
for (int c1 = 0; c1<l1; c1++)
{
for(int c2=0; c2<l2; c2++ ) {
co = array1.get(c1);
od = array2.get(c2);
x = Float.parseFloat(co);
y = Float.parseFloat(od);
dp[c1] = new DataPoint(x,y);
}
}
gpView.addSeries(series);
}
if array1 and array2 is null then make sure you are passing both arraylist via Intent.

how to use graph view to represent sqlite data?

i have a graph view to implement an analysis graph
All the x-axis and y-axis data is get from the sqlite db data to show the output
x-axis is showing date
y-axis is showing weight
but I have no idea to implement them out, I am stuck,
below is my code but its wrong and I haven't completed yet, can someone help me to solve and build
DBHelperNote connect = new DBHelperNote(getActivity());
SQLiteDatabase db = connect.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM weight;",null);
String[] weight = new String[cursor.getCount()];
int i = 0;
while(cursor.moveToNext()){
d = cursor.getString(cursor.getColumnIndex("weightnum"));
weight[i] = d;
i++;
}
String[] date = new String[cursor.getCount()];
int r = 0;
while(cursor.moveToNext()){
e = cursor.getString(cursor.getColumnIndex("date"));
date[r] = e;
r++;
}
GraphView line_graph = (GraphView) contentView.findViewById(R.id.graph);
LineGraphSeries<DataPoint> line_series =
new LineGraphSeries<DataPoint>(new DataPoint[] {
>> here "while" getting error
while ( a!=weight.length) {
new DataPoint(Integer.parseInt(weight[i]), Integer.parseInt(date[i]));
i++;
}
}
);
line_graph.addSeries(line_series);
line_series.setDrawDataPoints(true);
line_series.setDataPointsRadius(10);
line_series.setOnDataPointTapListener(new OnDataPointTapListener() {
#Override
public void onTap(Series series, DataPointInterface dataPoint) {
Toast.makeText(getActivity(), "Series: On Data Point clicked: " + dataPoint, Toast.LENGTH_SHORT).show();
}
});
Hmn, it might be an error with the data type in their example they use a double instead of an int.
http://www.android-graphview.org/documentation/category/live-chart
private DataPoint[] generateData() {
int count = 30;
DataPoint[] values = new DataPoint[count];
for (int i=0; i<count; i++) {
double x = i;
double f = mRand.nextDouble()*0.15+0.3;
double y = Math.sin(i*f+2) + mRand.nextDouble()*0.3;
DataPoint v = new DataPoint(x, y);
values[i] = v;
}
return values;

Calling the values out from CSV file for aGraphEngine (Android)

I am trying to obtain the 4th and 5th elements from the csv data and use it as a dataset for the 2nd (mDatasetNormal) and third graph line. I was able to figure out how to obtain the data from the 3rd element however the rest is an issue.
My questions are:
How to read the Heappy_log.csv and than use the arrayList to obtain data for the graph?
After data is obtained from the csv how to use it for the same graph?
CSV file content:
Aug-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
Whole code:
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());
}
}
}
}
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());
}
}
}
}
}

Dates on X-axis are not plotted achartengine line graph

I am stuck with this problem where I take date values from the sqlite database and try to plot them on X axis against some other values.But even after trying different solutions on SO I am unable to solve this.
Instead of my values I get random dates (1/30/1970) and the same date everywhere.
Please help me with this.
heres the code for getting the dates:
public Date[] getReportDates(ReportGraph reportGraph)
{
// TODO Auto-generated method stub
String [] columns = new String[]{KEY_ROW_ID, KEY_KM, KEY_FUEL_QTY, KEY_FUEL_PRICE, KEY_TOTAL_COST, KEY_MILEAGE, KEY_DATE,KEY_TANK_FULL};
String selectQuery = "SELECT _date FROM fuel_table";
reportCursor = ourDatabase.rawQuery(selectQuery, null);
int i = 0;
int count = reportCursor.getCount();
Date[] reportList = new Date[count];
if(reportCursor.moveToFirst())
{
do
{
reportList[i] = new Date(reportCursor.getLong(0));
i++;
} while(reportCursor.moveToNext());
}
reportCursor.close();
return reportList;
FOr graph plotting
double[] fPrice;
double[] fMileage;
Date[] fDates;
XYMultipleSeriesRenderer rRenderer;
XYMultipleSeriesDataset dataset;
XYSeriesRenderer priceRenderer, mileageRenderer,dateRenderer;
public Intent getIntent(Context context)
{
FuelStoredInfo reportInfo =new FuelStoredInfo(context);
reportInfo.open();
fPrice=reportInfo.getReportData(this);
fMileage = reportInfo.getReportMileage(this);
fDates = reportInfo.getReportDates(this);
reportInfo.close();
TimeSeries fPriceseries = new TimeSeries("Fuel prices");
for(int i=0;i<fDates.length;i++)
{
fPriceseries.add(fDates[i], fPrice[i]);
}
TimeSeries fMileageSeries = new TimeSeries("Mileage");
for(int i=0;i<fDates.length;i++)
{
fMileageSeries.add(fDates[i],fMileage[i]);
}
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(fPriceseries);
dataset.addSeries(fMileageSeries);
}
Try to add date to the series in this way, may it works for you
long value = Math.abs(date.getTime() - 3* TimeChart.DAY);
series.add(new Date(value+ 3* TimeChart.DAY), x[i]);

Error "Dataset and renderer should be not null and should have the same number of series" in Android

I am trying to display a graph with the data in the sqlitedatabase using AChartEngine library. I am having two records in database. BarChart graph displayed when there is one record. But with two records of data, it says, "Dataset and renderer should be not null and should have the same number of series". I dont understand this.
My code is,
Button ReportGraph = (Button) findViewById(R.id.ReportGraph);
ReportGraph.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GraphicalView GraphChartView = ChartFactory.getBarChartView(
getApplicationContext(), getWaterElectricityReading(),
getDemoRenderer(), Type.DEFAULT);
// Intent intent=ChartFactory.getLineChartIntent(this,
// getDemoDataset(), getDemoRenderer());
// startActivity(intent);
if (GraphChartView != null) {
GraphChartView.repaint();
}
relChartView.addView(GraphChartView);
mainLayout.setVisibility(View.GONE);
relChartView.setVisibility(View.VISIBLE);
}
});
private XYMultipleSeriesDataset getWaterElectricityReading() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd", Locale.US);
SimpleDateFormat df2 = new SimpleDateFormat("MMM dd yyyy");
Date StartDate;
Date EndDate;
String Pastdateformat = "";
String CurrentDateFormat = "";
SQLiteDatabase DB = Context
.openOrCreateDatabase("WaterElectricityReadingDataBase.db",
MODE_WORLD_READABLE, null);
Cursor c = DB
.rawQuery(
"select ReadingValue,strftime('%m/%d/%Y',StartDateTime) StartDateTime,strftime('%m/%d/%Y',EndDateTime) EndDateTime from WaterElectricity order by StartDateTime desc ",
null);
int rowCount = c.getCount();
if (c != null) {
if (rowCount > 0) {
if (c.moveToFirst()) {
do {
String ReadingValue = c.getString(c
.getColumnIndex("ReadingValue"));
String PastDate = c.getString(c
.getColumnIndex("StartDateTime"));
String CurrentDate = c.getString(c
.getColumnIndex("EndDateTime"));
try {
StartDate = df2.parse(PastDate);
Pastdateformat = sdf1.format(StartDate);
EndDate = df2.parse(CurrentDate);
CurrentDateFormat = sdf1.format(EndDate);
} catch (Exception e) {
}
XYSeries series = new XYSeries(
"WaterElectricityReading");
double ReadingVal = Double.parseDouble(ReadingValue);
series.add(rowCount, ReadingVal);
XYMultipleSeriesRenderer render = new XYMultipleSeriesRenderer();
dataset.addSeries(series);
rowCount++;
} while (c.moveToNext());
}
}
}
return dataset;
}
private XYMultipleSeriesRenderer getDemoRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(12);
renderer.setChartTitleTextSize(12);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.GREEN);
r.setPointStyle(PointStyle.DIAMOND);
r.setFillBelowLine(false);
r.setFillPoints(true);
renderer.addSeriesRenderer(r);
setChartSettings(renderer);
return renderer;
}
private void setChartSettings(XYMultipleSeriesRenderer renderer) {
renderer.setChartTitle("Chart demo");
renderer.setXTitle("x values");
renderer.setYTitle("y values");
renderer.setApplyBackgroundColor(false);
renderer.setRange(new double[] { 0, 6, -70, 40 });
renderer.setFitLegend(false);
renderer.setAxesColor(Color.BLACK);
renderer.setShowGrid(true);
renderer.setXAxisMin(0);
renderer.setXAxisMax(10.5);
renderer.setYAxisMin(0);
renderer.setZoomEnabled(false);
renderer.setYAxisMax(1000);
}
For two records, I want two bars to be displayed.
Can anyone please point out the Mistake.
Any Help will be appreciated!!
Thanks.
Your chart should be built using one single XYMultipleSeriesDataset and one single XYMultipleSeriesRenderer.
The XYMultipleSeriesDataset should contain as many XYSeries as there are XYSeriesRenderer in XYMultipleSeriesRenderer.
In your code you are creating an XYSeries for each record, which is probably wrong. The data should be in the same series unless you have a good reason not to do that. However, if you add multiple XYSeries then you will need one XYSeriesRenderer for each XYSeries.

Categories

Resources