I've been trying to plot a graph from two ArrayLists on my Android Application. I got the values from ArrayList succeed, but I can't call method to get values.
I create this method which gets values from my database:
public DataPoint[] setData(String data)
{
TextView editText4;
editText4 = (TextView) findViewById(R.id.editText4);
String[] lucas = data.split(",");
List<String> ok = new ArrayList<String>(Arrays.asList(lucas));
List<String> yes = new ArrayList<String>(Arrays.asList(lucas));
int n = ok.size(); //to find out the no. of data-points
editText4.setText(String.valueOf(n));
DataPoint[] values = new DataPoint[n]; //creating an object of type DataPoint[] of size 'n'
for (int i = 0; i < n; i++)
{
DataPoint v = new DataPoint(Double.parseDouble(ok.get(i)), Double.parseDouble(yes.get(i)));
values[i] = v;
}
return values;
}
My problem is here in setData (Cannot Resolve symbol 'setData') :
GraphView graph;
PointsGraphSeries<DataPoint> series; //an Object of the PointsGraphSeries for plotting scatter graphs
graph = (GraphView) findViewById(R.id.graph);
series = new PointsGraphSeries<>(setData); //initializing/defining series to get the data from the method 'data()'
graph.addSeries(series); //adding the series to the GraphView
series.setShape(PointsGraphSeries.Shape.POINT);
series.setSize(6);
As looking for your code looks that you have method setData(String data) but in series = new PointsGraphSeries<>(setData); you are calling variable. Please assing result from setData(String data) method to local variable and then use it in mentioned line of code.
Related
I am using HighChart for rendering graphs on android devices. But on the first time, it is not showing all data points when data points are more than 10k. When I tried to zoom in or zoom out several times, all data points became visible. I need a graph with 50k points at least. I am testing with sample code provided by HighChartjs.
public class MainActivity extends AppCompatActivity {
private ArrayList dataList;
int noOfData = 50000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataList = data();
HIChartView chartView = findViewById(R.id.hc);
chartView.plugins = new ArrayList<>(Arrays.asList("boost"));
HIOptions options = new HIOptions();
HIChart chart = new HIChart();
chart.setType("line");
chart.setZoomType("x");
options.setChart(chart);
HIBoost boost = new HIBoost();
boost.setUseGPUTranslations(true);
options.setBoost(boost);
HITitle title = new HITitle();
options.setTitle(title);
HISubtitle subtitle = new HISubtitle();
subtitle.setText("Using the Boost module");
options.setSubtitle(subtitle);
HITooltip tooltip = new HITooltip();
tooltip.setValueDecimals(2);
options.setTooltip(tooltip);
HILine line = new HILine();
line.setData(dataList);
int noOfData = this.dataList.size();
title.setText("Highcharts drawing " +noOfData +" points");
line.setLineWidth(1);
options.setSeries(new ArrayList<HISeries>(Collections.singletonList(line)));
chartView.setOptions(options);
}
private ArrayList data(){
ArrayList dataList = new ArrayList();
for (int i = 0; i < noOfData; i++) {
dataList.add(i, (Math.random() * 100));
}
return dataList;
}
}
Getting error logs: Highcharts error #26: www.highcharts.com/errors/26
E/Highcharts: Uncaught TypeError: Cannot read property 'forEach' of undefined
I have 3 string list and I want to add all values to menus but it gives error which is "Invalid index 0, size is 0". Briefly, menus is null and how can I add them?
private List<List<Restaurant.Menu>> menus = new ArrayList<>();
ArrayList<String> MenuName = new ArrayList<>();
ArrayList<String> FoodName = new ArrayList<>();
ArrayList<String> FoodPrice = new ArrayList<>();
//I get values in DB. DB is full.
MenusName = tinydb.getListString("MenuName");
FoodName = tinydb.getListString("FoodName");
FoodPrice = tinydb.getListString("FoodPrice");
int restaurantCounter = 0;
int menuCounter = 0;
for (int j = 0; j < MenusName.size(); j++)
{
menus.get(restaurantCounter).get(j).name = MenusName.get(j))
}
Example, I created them for each value, it works but if string is long, it enforces app and I wait 10 sec. for this process. I need efficient way. Thanks in advance.
menus.get(resCounter).add(new Restaurant.Menu());
menus.get(resCounter).get(menuCounter).foods.add(new Restaurant.Food());
.
.
menus.get(resCounter).get(menuCounter).name = MenuName.get(i));
menus.get(resCounter).get(menuCounter).foods.get(foodCounter).name = FoodName.get(i);
menus.get(resCounter).get(menuCounter).foods.get(foodCounter).price = FoodPrice.get(i);
You seem to be confusing arrays with lists here. Arrays have a fixed size once initialised, Lists don't. You need to add something to your menus list using menus.add(/*Add Menu Object here*/);
You haven't add any values to menus.so the object doesn't contain value at the index 0. This might be a reason.
I couldn't find a solution so I changed structure. I get all strings in restaurants and I used gson to convert them before store them. Also you can use gson for each string. It it same logic.
Gson gson = new Gson();
ArrayList<String> gsonString = new ArrayList<>();
for(int i=0; i<restaurants.size(); i++)
gsonString.add(gson.toJson(restaurants.get(i)));
tinydb.putListString("tinyRestaurant",gsonString);
Convert again...
Gson gson = new Gson();
for(int i=0; i<tinydb.getListString("tinyRestaurant").size(); i++)
restaurants.add(gson.fromJson(tinydb.getListString("tinyRestaurant").get(i), Restaurant.class));
I am trying to create a pie chart from the data stored in the database using MPAndroidChart.
The size of the database is not fixed so it might have 2 to 7 data.
This is how I'm taking the data from the database.
myDb = new DatabaseHelper(this);
Cursor pie = myDb.showKharcha();
String[] xData = new String[pie.getCount()];
float[] yData = new float[pie.getCount()];
int i = 0;
float total = 0;
while (pie.moveToNext()){
String kname = pie.getString(1);
Float kh= pie.getFloat(2);
float kharcha = kh.floatValue();
total = total + kharcha ;
xData[i]=kname;
yData[i]=kharcha;
i++;
}
And this is the part of using the data for pie chart.
ArrayList<PieEntry> yEntry= new ArrayList<>();
ArrayList<String> xEntry = new ArrayList<>();
for( i =0;i < yData.length;i++){
yData[i] = yData[i]*100/total;
yEntry.add(new PieEntry(yData[i],i));
}
for(i =0;i < xData.length;i++){
xEntry.add(xData[i]);
}
The error I get is null pointer exception error and this to be exact
Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.PieChart.setRotationEnabled(boolean)' on a null object reference
The part containing setRotationEnabled(boolean) is as follows which is before calling the addDataSet method where everything I've provided takes place.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.piechart);
Log.d(TAG, "onCreate: starting to create chart");
pieChart.setRotationEnabled(true);
pieChart.setHoleRadius(25f);
pieChart.setTransparentCircleAlpha(0);
pieChart.setCenterText("Kharcha");
pieChart.setCenterTextSize(10);
addDataSet();}
I think I'm not being able to pass the float data to be used in a piechart.
Any help would be appreciated.
you forgot to get the PieChart from the layout
pieChart = (PieChart) findViewById(R.id.chart1);
I created a graph app using MP android chart, i used it to plot two different line data sets . But my problem is that the value labels for the xaxis appear on the top of the graph . I need a way to make it to the bottom . below is my code
final LineChart linechart = (LineChart)findViewById(R.id.idlinechart);
//created list enties to hold the values
List<Entry> valuesCompany1 = new ArrayList<>(); List<Entry> valuesCompany2 = new ArrayList<>();
//created entry values to be entered into the lsit entry b
Entry c1e1 = new Entry(0f, 100000f); valuesCompany1.add(c1e1);
Entry c1e2 = new Entry (1f, 140000f);valuesCompany1.add(c1e2);
Entry c1e3 = new Entry(2f,90000f);valuesCompany1.add(c1e3);
Entry c1e4 = new Entry (3f, 150000f); valuesCompany1.add(c1e4);
Entry c2e1 = new Entry(0f, 50000f); valuesCompany2.add(c2e1);
Entry c2e2 = new Entry (1f, 50000f);valuesCompany2.add(c2e2);
Entry c2e3 = new Entry(2f,150000f);valuesCompany2.add(c2e3);
Entry c2e4 = new Entry (3f, 110000f); valuesCompany2.add(c2e4);
//so now we create line data to hold the list
LineDataSet linedatasetComp1 = new LineDataSet(valuesCompany1, "company 1");
linedatasetComp1.setAxisDependency(YAxis.AxisDependency.LEFT);
linedatasetComp1.setColor(Color.BLUE);
LineDataSet linedatasetComp2 = new LineDataSet(valuesCompany2, "company 2");
linedatasetComp2.setAxisDependency(YAxis.AxisDependency.LEFT);
linedatasetComp2.setColor(Color.RED);
List<ILineDataSet> iLinedata = new ArrayList<ILineDataSet>();
iLinedata.add(linedatasetComp1); //adding the line data sets into the line data list
iLinedata.add(linedatasetComp2);
//setting the Ilinedata list into line data
LineData linedata = new LineData(iLinedata);
//setting the line data into line chart
linechart.setData(linedata);
linechart.invalidate(); //refreshing the line chart
//Saving the picture to galery
Button savebutton = (Button)findViewById(R.id.button);
savebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linechart.saveToGallery("new Line chart", 150);
Toast.makeText(getApplicationContext(), "graph saved ", Toast.LENGTH_LONG).show();
}
});
final String[] Quaters = {"Q1", "Q2", "Q3", "Q4"};
IAxisValueFormatter valueFormater = new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return Quaters[(int) value];
}
#Override
public int getDecimalDigits() {
return 0;
}
};
XAxis xAxis = linechart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setValueFormatter(valueFormater);
Description desc = new Description();
desc.setText("A graph comparing revenuews of two companies ");
desc.setEnabled(true);
desc.setPosition(0,0);
desc.setTextColor(Color.BLACK);
linechart.setDescription(desc);
I don't know if LineChart has that option, but in case it does you just have to get the XAxis adjust the Position like this:
linechart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
Hope it helps!
Use MPAndroid chart version 2.2.4 or above and use the code
mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
as said by Ponchotg
I want to plot a XY line graph with some double values on Y axis and some String values on X axis. But the XYSeries takes only double/long values. So,is there any way I can use a string array on the X axis?
Thanks for any suggestions.
code:
//fDates are strings
XYSeries fPriceseries = new XYSeries("Fuel prices");
for(int i=0;i<fDates.length;i++)
{
long fDate = Long.parseLong(fDates[i]);
fPriceseries.add(fDate, fPrice[i]);
}
XYSeries fMileageSeries = new XYSeries("Mileage");
for(int i=0;i<fDates.length;i++)
{
long fDate = Long.parseLong(fDates[i]);
fMileageSeries.add(fDate, fMileage[i]);
}
Use TimeSeries:
fDates as Date array
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]);
}
fDates as String array - Using SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
TimeSeries fPriceseries = new TimeSeries("Fuel prices");
for(int i=0;i<fDates.length;i++)
{
fPriceseries.add(sdf.parse(fDates[i]), fPrice[i]);
}
TimeSeries fMileageSeries = new TimeSeries("Mileage");
for(int i=0;i<fDates.length;i++)
{
fMileageSeries.add(sdf.parse(fDates[i]), fMileage[i]);
}
Referencies:
http://www.achartengine.org/content/javadoc/org/achartengine/model/TimeSeries.html
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You cannot plot a graph using string on any axis.
Explanation:
When we plot a graph we compare two values... as in 3,5 or 7,22 or 12.3,10000..
Plotting string with respect to an integer or double,is like comparing apples and oranges.
So m sorry friend but its not possible.. unless you convert the string or create a reference for its contents..