whitespace COMPANY whitespace SERVICES whitespace PRODUCTS whitespace PURCHASE whitespace SUPPORT whitespace CONTACTS whitespace Home whitespace Contact Us whitespace Site Map whitespace
whitespace
SUPPORT
whitespace
FORUM
whitespace
FAQS
whitespace
SUBSCRIPTION
whitespace
REQUEST ON-LINE
whitespace
whitespace

Technical Support Forum

Welcome Guest Search | Active Topics | Log In | Register

Bar chart using database not plotting Options · View
jazzy
#1 Posted : Tuesday, December 18, 2012 7:18:21 AM
Rank: Newbie

Groups: Registered

Joined: 12/18/2012
Posts: 2
Location: bangalore
Hi i am new to android and i have written the following code to draw a bar chart but my problem is that the bar chart is not plotting. I am using database to pass the values to the bar chart.


This is my class to draw the bar chart.


public class ChartActivity extends Activity {
private String productName;
private Context Jayant;
private Integer seriesCount;

public Intent createIntent(Context context) throws IOException {

SharedPreferences myPrefs = context.getSharedPreferences("myPrefs",
MODE_PRIVATE);
productName = myPrefs.getString("DAYS", "TOP10");
Jayant = context;
DatabaseAdapter dbA = new DatabaseAdapter(Jayant);
dbA.opendatabase();
seriesCount = dbA.getValues(productName).getCount();
dbA.close();

XYMultipleSeriesRenderer renderer = buildBarRender();
setChartSettings(renderer);

return ChartFactory.getBarChartIntent(context, getDateDemoDataset(),
renderer, Type.DEFAULT);
}

protected XYMultipleSeriesRenderer buildBarRender() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
int[] colors = new int[] { Color.BLUE, Color.DKGRAY, Color.GREEN,
Color.RED, Color.CYAN, };
SimpleSeriesRenderer ssR = new SimpleSeriesRenderer();
for (int i = 0; i < seriesCount; i++) {
ssR = new SimpleSeriesRenderer();
ssR.setDisplayChartValues(true);
ssR.setChartValuesTextSize(30);
ssR.setColor(colors[i]);
renderer.addSeriesRenderer(ssR);

}
return renderer;
}

private XYMultipleSeriesDataset getDateDemoDataset() throws IOException {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
DatabaseAdapter dbA = new DatabaseAdapter(Jayant);

dbA.opendatabase();
Cursor Data = dbA.getValues(productName);
seriesCount = Data.getCount();

for (Data.move(-1); Data.moveToNext(); Data.isAfterLast()) {
CategorySeries series = new CategorySeries((Data.getString(Data
.getColumnIndex("Primary1"))));
series.add(Data.getInt(Data.getColumnIndex("Primary2")));
dataset.addSeries(series.toXYSeries());
}
Data.close();
dbA.close();
return dataset;
}

private void setChartSettings(XYMultipleSeriesRenderer renderer) throws IOException {
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
renderer.setChartTitle("Top 10 Travel Destinations");
renderer.setXTitle("Destinations");
renderer.setYTitle("Top 10");
renderer.setXAxisMin(0);
renderer.setXAxisMax(10.5);
renderer.setBarSpacing(0.0);
renderer.setYAxisMin(0);
renderer.setYAxisMax(maxValue());

}

private Integer maxValue() throws IOException {
Integer mValue = null;
DatabaseAdapter dbA = new DatabaseAdapter(Jayant);
dbA.opendatabase();
Cursor mV = dbA.getMaxValue(productName);
if (mV.moveToFirst())
mValue = mV.getInt(mV.getColumnIndex("Primary2")) * 1;

mV.close();
dbA.close();
return mValue;

}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
}

}



This is my code to extract the database which i have stored in the assets folder of my project.


public class DatabaseAdapter extends SQLiteOpenHelper{

private Context mycontext;

private String DB_PATH = mycontext.getApplicationContext().getPackageName()+"Jayant";
private static String DB_NAME = "Jayant.sqlite";//the extension may be .sqlite or .db
public SQLiteDatabase myDataBase;

public DatabaseAdapter(Context context) throws IOException {
super(context,DB_NAME,null,1);
this.mycontext=context;
boolean dbexist = checkdatabase();
if (dbexist) {
opendatabase();
} else {
System.out.println("Database doesn't exist");
createdatabase();
}
}

public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if(dbexist) {
//System.out.println(" Database exists.");
} else {
this.getReadableDatabase();
try {
copydatabase();
} catch(IOException e) {
throw new Error("Error copying database");
}
}
}

private boolean checkdatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
checkdb = dbfile.exists();
} catch(SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}

private void copydatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);

// Path to the just created empty db
@SuppressWarnings("unused")
String outfilename = DB_PATH + DB_NAME;

//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream("/data/data/flu.solutions.travelsense/databases /Jayant.sqlite");

// transfer byte to inputfile to outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer))>0) {
myoutput.write(buffer,0,length);
}

//Close the streams
myoutput.flush();
myoutput.close();
myinput.close();
}

public void opendatabase() throws SQLException {
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}

public synchronized void close() {
if(myDataBase != null) {
myDataBase.close();
}
super.close();
}

@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

public Cursor getValues(String productName) {
// TODO Auto-generated method stub
return null;
}

public Cursor getMaxValue(String productName) {
// TODO Auto-generated method stub
return null;
}


}


Please help me to correct the error in the above code.
Baydalka Volodymyr
#2 Posted : Tuesday, December 18, 2012 11:28:46 AM
Rank: Developers Team


Groups: Registered, Administrators

Joined: 7/28/2009
Posts: 447
Hi,

Looks like you use not our product.

Best regards,
Volodymyr Baydalka
Andriod development and support team
jazzy
#3 Posted : Tuesday, December 18, 2012 12:31:12 PM
Rank: Newbie

Groups: Registered

Joined: 12/18/2012
Posts: 2
Location: bangalore
Sorry told you i am new here. Please help me and i'll use your product only. I just need a bar chart which takes values from a database
Baydalka Volodymyr
#4 Posted : Tuesday, December 18, 2012 2:07:28 PM
Rank: Developers Team


Groups: Registered, Administrators

Joined: 7/28/2009
Posts: 447
Hi,

Here is quick sample, how to use database with aiCharts.
You may download trial version of aiCharts - http://www.artfulbits.com/products/android/try/aicharts.aspx to test it.

Best regards,
File Attachment(s):
SQLiteDataSample.zip (408kb) downloaded 3 time(s).
Volodymyr Baydalka
Andriod development and support team
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Company | Services | Practices | Technologies | Career | Contacts | Privacy
© 2005-2016 ArtfulBits. All rights reserved.