i want make PieChart with library MPAndroidChart from github and i follow instruction on google or youtube, but i'm stuck in this code
private void addData(){
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
for (int i = 0; i < yData.length; i++)
yVals1.add(new Entry(yData[i], i));
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < xData.length; i++)
xVals.add(xData[i]);
PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);
}
in
PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
I got warn, PieDataSet Cannot Applied to String. I don't know whats wrong because in tutorial is same with me
for pie chart with MPCharts, I've used this snippet
protected PieData generatePieData() {
int count = 10;
ArrayList<PieEntry> entries1 = new ArrayList<PieEntry>();
for(int i = 0; i < count; i++) {
entries1.add(new PieEntry( (float) salary[i], months[i]));
}
PieDataSet ds1 = new PieDataSet(entries1, "Monthly Salary 2017");
ds1.setColors(ColorTemplate.JOYFUL_COLORS);
ds1.setSliceSpace(2f);
ds1.setValueTextColor(Color.WHITE);
ds1.setValueTextSize(10f);
ds1.setSliceSpace(5f);
return new PieData(ds1);
}
where salary, and months are array
I think you need to specify the ArrayList as PieEntry?
ArrayList<PieEntry> yVals1 = new ArrayList<PieEntry>();
EDIT:
I think this is what you might need?;
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < xData.length; i++){
xVals.add(xData[i]);
}
ArrayList<PieEntry> yVals1 = new ArrayList<PieEntry>();
for (int i = 0; i < yData.length; i++){
yVals1.add(new Entry(yData[i], xData[i]));
}
Related
I am using nested for loop for getting from arraylist and that loop run infinte
private ArrayList prepareData()
{
ArrayList arrayList = new ArrayList<>();
for(int i=0;i<android_image_urls.length;i++){
Androidversionclass androidversion = new Androidversionclass();
androidversion.setAndroid_image_url(android_image_urls[i]);
arrayList.add(androidversion);
for (int k= 0; k < android_image_second.length; k++) {
Androidversionclass androidversionclass1 = new Androidversionclass();
androidversion.setAndroid_image_second(android_image_second[k]);
arrayList.add(androidversion);
}
for (int l = 0; l < android_image_thirds.length; l++) {
Androidversionclass androidversionclass2 = new Androidversionclass();
androidversion.setAndroid_image_thirds(android_image_thirds[l]);
arrayList.add(androidversion);
}
for (int m = 0; m < android_image_fourth.length; m++) {
Androidversionclass androidversionclass3 = new Androidversionclass();
androidversion.setAndroid_image_fourth(android_image_fourth[m]);
arrayList.add(androidversion);
}
}
return arrayList;
}
I am using MPAndroidChart library.
performance= result.getJSONArray("performance");
for(int i = 0; i<performance.length();i++){
JSONObject jsonChildNode = performance.getJSONObject(i);
Iterator iterator = jsonChildNode.keys();
int j=0;
duration= new ArrayList<>();
barEntries= new ArrayList<>();
while(iterator.hasNext()){
String key = (String)iterator.next();
String issue = jsonChildNode.optString(key) ;
Float val=parseFloat(issue);
duration.add(key);
barEntries.add(new BarEntry(val,j));
}
j++;
barDataSet= new BarDataSet(barEntries,"Qty");
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
barData= new BarData(duration,barDataSet);
barChart.setData(barData);
barChart.animateY(3000);
output as like bellow
enter image description here
Move j++ inside while loop like this,
while(iterator.hasNext()){
String key = (String)iterator.next();
String issue = jsonChildNode.optString(key) ;
Float val=parseFloat(issue);
duration.add(key);
barEntries.add(new BarEntry(val,j));
j++;
}
I am using the MPAndroidChart library release v2. I'm trying to show 3 bars, with data taken from the database.
Unfortunately, instead of seeing 3 bars with their data, viewing them with the same result. See the screenshot. Thanks for your help.
int [] x = {1,2,3};
Cursor c = db.rawQuery(sql, null);
int count = c.getCount();
float value1 ;
float value2 ;
float value3 ;
String[] mesi = new String[count];
for(int n=0; n<count; n++) {
c.moveToNext();
mesi[n]= c.getString(0);
value1 = c.getFloat(1);
value2 = c.getFloat(2);
value3 = c.getFloat(3);
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i <x.length; i++) {
xVals.add(x.length + " " + mChart.getUnit());
}
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
for (int i = 0; i < x.length; i++) {
yVals1.add(new BarEntry(value1, i));
}
for (int i = 0; i < x.length; i++) {
yVals2.add(new BarEntry(value2, i));
}
for (int i = 0; i < x.length; i++) {
yVals3.add(new BarEntry(value3, i));
}
// create 3 datasets with different types
BarDataSet set1 = new BarDataSet(yVals1, "Company A");
set1.setColor(Color.rgb(104, 241, 175));
BarDataSet set2 = new BarDataSet(yVals2, "Company B");
set2.setColor(Color.rgb(164, 228, 251));
BarDataSet set3 = new BarDataSet(yVals3, "Company C");
set3.setColor(Color.rgb(242, 247, 158));
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
BarData data = new BarData(xVals, dataSets);
// add space between the dataset groups in percent of bar-width
data.setGroupSpace(0);
mChart.setData(data);
mChart.invalidate();
}
c.close();
db.close();
}
the result
Did you make a logcat output of the values the chart should display?
Maybe they are not stored correctly in the database.
Your code seems correct to me.
You could also test it by simply providing a predefined value like e.g. 50 for all bars and see if it is plotted correctly.
To change the color call:
BarDataSet.setColor(...);
UPDATE: There is now a very detailed tutorial on how to create grouped BarCharts available on the official GitHub page of the library, based on release v3.0.0: Grouped BarChart tutorial
i have problem in data binding in expandable list view. here i use
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ExpandListGroup for data binding. but in sum 2d array there is null value.Data is coming dynamically .
eg:
String [] [] array1 = [[one,two,three,null],[seven,six,null]] ;
I want to remove the null column from this two dimensional array
You need to take a intermediate arraylist and result array and follows the below steps.
First copy the source array(containing null) into arraylist.
Then remove null from arraylist
Then create new array and copy data from arraylist to array.
private void remove_nulls()
{
String [] [] array1 = {{"one","two","three",null},{"seven","six",null}} ;
ArrayList<ArrayList<String>> contact = new ArrayList<ArrayList<String>>();
for(int i=0;i<array1.length;i++)
{
ArrayList<String> con = new ArrayList<String>();
for(int j=0;j<array1[i].length;j++)
{
if(array1[i][j]!=null)
con.add(array1[i][j]);
}
if(con.size()>0)
contact.add(con);
}
String [] [] array2 = new String[array1.length][];
for(int i=0;i<contact.size();i++)
{
array2[i]=new String[contact.get(i).size()];
for(int j=0;j<contact.get(i).size();j++)
{
array2[i][j]=contact.get(i).get(j);
}
}
}
Unless there is some trick to the question...
String[][] array1 = {{"one", "two", "three", null}, {"seven", "six", null}};
List<String[]> newList = new ArrayList<>();
for (int i = 0; i < array1.length; ++i) {
List<String> currentLine = new ArrayList<>();
for (int j = 0; j < array1[i].length; ++j) {
if (array1[i][j] != null) {
currentLine.add(array1[i][j]);
}
}
//create the array in place
newList.add(currentLine.toArray(new String[currentLine.size()]));
}
//no need to use an intermediate array
String[][] array2 = newList.toArray(new String [newList.size()][]);
//And a test for array2
for (int i = 0; i < array2.length; ++i) {
for (int j = 0; j < array2[i].length; ++j) {
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
System.out.println("Compared to...");
//Compared to the original array1
for (int i = 0; i < array1.length; ++i) {
for (int j = 0; j < array1[i].length; ++j) {
System.out.print(array1[i][j] + " ");
}
System.out.println();
}
Can u please help me in creating a simple bar chart (for android) where the values are got from sqlite database. for eg consider the following table
year Units Sold
2001 2000
2002 2100
2003 1900
I need to create a bar graph with x-axis as year and y-axis as units sold . Can u Plz consider this as a request . I dont know how to import data from database .
first you have to fetch the values from the data base and stored into array with corresponding data type . Do like this.....
List<int[]> initial;
int[] my_year;
int[] my_units_sold;
public void onCreate()
{
int Column1 = net_db_cur.getColumnIndex("Year");
int Column2 = net_db_cur.getColumnIndex("Units Sold");
cur = sampleDB.rawQuery("SELECT * FROM " + Table_Name , null);
my_year = new int[columncount];
my_units_sold = new int[columncount];
if (cur.moveToFirst())
{
for (int i = 0; i < columncount; i++)
{
my_year [i] = net_db_cur.getInt(Column1);
my_units_sold[i] = net_db_cur.getInt(Column2);
cur.moveToNext();
}
}
initial = new ArrayList<int[]>();
initial1.add(my_year);
initial1.add(my_units_sold);
ll.addView(createBarChart(initial ,Color.BLUE,10,0,14,2000000,20000000,1,x_axis_labels1,x_axis_labels2,y_axis_labels1, y_axis_net_labels2,Color.parseColor("#ffffff")));
}
public View createBarChart(int[] values,int[] colors,int lable_text_size,int xMin,int xMax,int yMin,int yMax,double bar_space,double[] x_axis_lables1,String[] x_axis_lables2,double[] y_axis_lables1,String[] y_axis_lables2, int lable_color)
{
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
/*for (int i = 0; i <values.length; i++)
{*/
CategorySeries series = new CategorySeries("");
/*double[] v = values.get(i);
int seriesLength = v.length;*/
for (int k = 0; k < values.length; k++)
{
series.add(values[k]);
}
dataset.addSeries(series.toXYSeries());
//}
//mrenderer.setMargins(new int[] { 10, 65, 10, 15 });
int color_length = colors.length;
for (int i = 0; i < color_length; i++)
{
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
r.setChartValuesSpacing(15);
mrenderer.addSeriesRenderer(r);
}
mrenderer.setOrientation(Orientation.HORIZONTAL);
mrenderer.setInScroll(false);
mrenderer.setGridColor(Color.BLACK);
mrenderer.setYLabelsAlign(Align.RIGHT);
mrenderer.setPanEnabled(false, false);
mrenderer.setZoomEnabled(false, false);
mrenderer.setBarSpacing(bar_space);
mrenderer.setLabelsTextSize(lable_text_size);
mrenderer.setAxesColor(Color.BLACK);
mrenderer.setXAxisMin(xMin);
mrenderer.setXAxisMax(xMax);
for(int i=0;i<x_axis_lables2.length;i++)
{
mrenderer.addXTextLabel(x_axis_lables1[i],x_axis_lables2[i]);
}
mrenderer.setYAxisMin(yMin);
mrenderer.setYAxisMax(yMax);
for(int j=0;j<y_axis_lables1.length;j++)
{
mrenderer.addYTextLabel(y_axis_lables1[j],y_axis_lables2[j]);
}
mrenderer.setLabelsColor(lable_color);
return (ChartFactory.getBarChartView(Income.this, dataset, mrenderer, Type.DEFAULT));
}
You will get the bar chart for with data base values. All the best.