Android HighChart : Not loading data first time with 50k Points - android

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

Related

How to call method to plot Graph Android

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.

I can't solve this MPAndroidChart null object reference error

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 am trying to make values of X axis show on the bottom of the graph not the top, using MP android chart

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

MP Piechart data entry counts

I created a Piechart with MPandroidchart for my app. The chart works great. The problem is that the original data contains 4 data entry but the chart only show 3. Can somebody help? What is the limitation for this?
My PieChart
Hi Thank you so much for getting me back. The xVals and yVals are from SQLite database. xVals{30-,30-45,46-60,60-} String and yVals(6,14,13,3) float.
My codes are posted here:
private void AddDataCYNewCustomersAge(){
DataBaseHelper databasehelper = new DataBaseHelper (NewCustomerReportCurrentYear.this);
PieChart ageChart = new PieChart(this);
ageChart = (PieChart)findViewById(R.id.agechart);
//get yVals and datasets of marriage chart
ArrayList<Entry> yValsageCounts = new ArrayList<Entry>();
for(int i = 0;i < databasehelper.yDataIncomeCounts().size(); i++)
yValsageCounts.add(new Entry(databasehelper.yDataAgeCounts().get(i), i));
//get xVals of marriage chart
ArrayList<String> xValsage = new ArrayList<String>();
for(int i = 0;i < databasehelper.xDataAge().size();i++)
xValsage.add(databasehelper.xDataAge().get(i));
PieDataSet ageCounts = new PieDataSet(yValsageCounts, "");
//set yvals attribute
ageCounts.setSliceSpace(3);
ageCounts.setSelectionShift(5);
//add many colors
ageCounts.setColors(new int[]{R.color.Fuchsia, R.color.Aqua,R.color.Yellow,R.color.LightSeaGreen,R.color.Blue}, this);
// configue pie chart
ageChart.setUsePercentValues(true);
ageChart.setRotationEnabled(true);
ageChart.setRotationAngle(0);
ageChart.setDescriptionPosition(350, 550);
ageChart.setDrawHoleEnabled(true);
ageChart.setHoleColor(Color.TRANSPARENT);
ageChart.setHoleRadius(13);
ageChart.setTransparentCircleRadius(18);
ageChart.getLegend().setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
//ageChart.getLegend().setXEntrySpace(3);
// ageChart.getLegend().setYEntrySpace(3);
ageChart.getLegend().setTextColor(Color.BLUE);
ageChart.setDescription("Income");
ageChart.setDescriptionTextSize(13);
ageChart.setDescriptionColor(Color.BLUE);
//get income chart data
PieData ageData = new PieData(xValsage,ageCounts);
ageData.setValueFormatter(new PercentFormatter());
ageData.setValueTextSize(11);
ageData.setValueTextColor(Color.BLUE);
ageChart.setData(ageData);
//refresh data
ageChart.invalidate();
}

Android GraphView Bar chart Multiple series

Hi i have a Bar graph using android GraphView. have 3 series that i need to pass into it but its currently drawing all 3 series onto of each other is there a way to change the width of the bar so that it shows
heres what i have sofar
try{
JSONArray graphArray = returnGraphyArray(statistics);
if(graphArray != null){
int graphLength = graphArray.length();
GraphViewData[] calls = new GraphViewData[graphLength];
GraphViewData[] length = new GraphViewData[graphLength];
GraphViewData[] transfered = new GraphViewData[graphLength];
for(int r = 0; r < graphLength; r++){
JSONObject row = graphArray.getJSONObject(r);
calls[r] = new GraphViewData(r,Integer.parseInt(row.getJSONObject("data").getString("total_calls")));
length[r] = new GraphViewData(r,Integer.parseInt(row.getJSONObject("data").getString("avg_call_length"))/60);
transfered[r] = new GraphViewData(r,Integer.parseInt(row.getJSONObject("data").getString("calls_transferred")));
}
GraphViewSeries totalCallsSeries = new GraphViewSeries( "Total Calls", new GraphViewSeriesStyle(getResources().getColor(R.color.pink), 5),calls);
GraphViewSeries totalCallLengthSeries = new GraphViewSeries( "Total Call Length", new GraphViewSeriesStyle(getResources().getColor(R.color.green), 5),length);
GraphViewSeries totalCallsTransSeries = new GraphViewSeries( "Calls Transfered", new GraphViewSeriesStyle(getResources().getColor(R.color.blue), 5),transfered);
BarGraphView graphView = new BarGraphView(context, "");
graphView.addSeries(totalCallLengthSeries);
graphView.addSeries(totalCallsTransSeries);
graphView.addSeries(totalCallsSeries); // data
graphView.setHorizontalLabels(bottomlabels);
graphView.setShowLegend(false);
graphView.setLegendAlign(LegendAlign.TOP);
graphView.setLegendWidth(SM_Global.pxFromDp(context, 100));
graphView.getGraphViewStyle().setNumHorizontalLabels(6);
layout.addView(graphView);
}else{
layout.setVisibility(View.GONE);
ImageView legend = (ImageView)v.findViewById(R.id.graph_legend);
legend.setVisibility(View.GONE);
}
According to the GraphView website:
"Note: It is currently not possible to plot multiple bar charts. To say it more in detail: It is possible, but the bars will be rendered at the same position and so they will be overlapped."
I would suggest using AChartEngine instead. Here's the link to the jar file
https://code.google.com/p/achartengine/downloads/list
and here's an example
http://wptrafficanalyzer.in/blog/android-drawing-bar-chart-using-achartengine/

Categories

Resources