so am plotting a graph using com.jjoe64.graphview.GraphView. and from my API am passing only the last 6 value. But the problem is that it does not plot all the 6 it only plot 4 points on the graph and i don't know why.
the code below is a chunk of code from my onPostExecute method in my Asynctask class.
if (valuationList.size() >6){
valuationList = valuationList.subList(valuationList.size() -6,valuationList.size());
totalUnitPoint = new ArrayList<>(totalUnitPoint.subList(totalUnitPoint.size() - 6, totalUnitPoint.size()));
currentValuePoint = new ArrayList<>(currentValuePoint.subList(currentValuePoint.size() - 6, currentValuePoint.size()));
}
DataPoint [] cummuDataPointArr = totalUnitPoint.toArray(new DataPoint[totalUnitPoint.size()]);
LineGraphSeries<DataPoint> cummulativeSeries = new LineGraphSeries<>(cummuDataPointArr);
StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(cummulativeChart);
staticLabelsFormatter.setHorizontalLabels(valuationList.toArray(new String[valuationList.size()]));
//staticLabelsFormatter.setVerticalLabels(new String[] {"low", "middle", "high"});
cummulativeChart.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
cummulativeChart.addSeries(cummulativeSeries);
cummulativeSeries.setColor(R.color.royal_blue);
cummulativeChart.setTitle("Units Acquired");
cummulativeChart.getGridLabelRenderer().setVerticalAxisTitle("Cummulative Units");
cummulativeChart.getGridLabelRenderer().setHorizontalAxisTitle("Months");
cummulativeSeries.setDrawDataPoints(true);
cummulativeSeries.setDataPointsRadius(10);
cummulativeSeries.setThickness(2);
DataPoint [] currentDataPointArr = currentValuePoint.toArray(new DataPoint[currentValuePoint.size()]);
LineGraphSeries<DataPoint> currentSeries = new LineGraphSeries<>(currentDataPointArr);
StaticLabelsFormatter currentFormatter = new StaticLabelsFormatter(currentChart);
currentFormatter.setHorizontalLabels(valuationList.toArray(new String[valuationList.size()]));
currentChart.getGridLabelRenderer().setLabelFormatter(currentFormatter);
currentChart.addSeries(currentSeries);
currentSeries.setColor(R.color.green);
currentChart.setTitle("RSA Balance (Naira)");
currentChart.getGridLabelRenderer().setVerticalAxisTitle("Current Value");
currentChart.getGridLabelRenderer().setHorizontalAxisTitle("Months");
currentSeries.setDrawDataPoints(true);
currentSeries.setDataPointsRadius(10);
currentSeries.setThickness(2);
Please i need you help to fix this
Related
I am working on android. By using highchart I have created a barchart.
HIOptions options = new HIOptions();
HIChart chart = new HIChart();
chart.setType("bar");
options.setChart(chart);
HITitle title = new HITitle();
title.setText("GOLM");
options.setTitle(title);
HISubtitle subtitle = new HISubtitle();
subtitle.setText("Growth Over Last Month");
options.setSubtitle(subtitle);
HIXAxis xaxis = new HIXAxis();
String[] categories = new String[] { "Mar-2021", "Apr-2021","May-2021"};
xaxis.setCategories(new ArrayList<>(Arrays.asList(categories)));
options.setXAxis(new ArrayList<HIXAxis>(){{add(xaxis);}});
HIYAxis yaxis = new HIYAxis();
yaxis.setMin(0);
yaxis.setTitle(new HITitle());
yaxis.getTitle().setText("Sales");
yaxis.getTitle().setAlign("high");
yaxis.setLabels(new HILabels());
yaxis.getLabels().setOverflow("justify");
options.setYAxis(new ArrayList<HIYAxis>(){{add(yaxis);}});
HITooltip tooltip = new HITooltip();
options.setTooltip(tooltip);
HILegend legend = new HILegend();
legend.setLayout("vertical");
legend.setAlign("right");
legend.setVerticalAlign("top");
legend.setX(-30);
legend.setY(40);
legend.setFloating(true);
legend.setBorderWidth(1);
legend.setBackgroundColor(HIColor.initWithHexValue("FFFFFF"));
legend.setShadow(true);
options.setLegend(legend);
HICredits credits = new HICredits();
credits.setEnabled(false);
options.setCredits(credits);
HIBar bar1 = new HIBar();
bar1.setName("Sale");
Number[] bar1Data = new Number[]{10,15,20};
bar1.setData(new ArrayList<>(Arrays.asList(bar1Data)));
bar1.setColorByPoint(true);
HILegend hiLegend = new HILegend();
hiLegend.setEnabled(false);
options.setLegend(hiLegend);
options.setSeries(new ArrayList<>(Collections.singletonList(bar1)));
golmChart.setOptions(options);
Output
I want to enable the data labels. The sample code does give a code to enable data labels but it's not working
HIPlotOptions plotOptions = new HIPlotOptions();
plotOptions.setBar(new HIBar());
plotOptions.getBar().setDataLabels(new HIDataLabels());
plotOptions.getBar().getDataLabels().setEnabled(true);
options.setPlotOptions(plotOptions);
It's giving me an error
error: incompatible types: HIDataLabels cannot be converted to ArrayList
plotOptions.getBar().setDataLabels(new HIDataLabels());
Source: Android Basic Bar
Any help would be highly appreciated.
They have changed the datalabel property. Now you have to do the following
HIDataLabels dataLabels = new HIDataLabels();
dataLabels.setEnabled(false);
ArrayList<HIDataLabels> dataLabelsList = new ArrayList<>();
dataLabelsList.add(dataLabels);
bar1.setDataLabels(dataLabelsList);
Try the above code after you set data to bar1
For more information see this comment
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'm trying to build and android application with MP Android chart v 2.0.9 and my problem is i'm affecting 4 limit lines to my chart but it displaying just two.
Any solution?
LimitLine llg1 = new LimitLine(Charts.refrms , "");
llg1.setLineWidth(2f);
llg1.setLineColor(greencolor);
holder.lineChart.getAxisLeft().addLimitLine(llg1);
LimitLine llr1 = new LimitLine(Charts.dgrms , "");
llr1.setLineWidth(2f);
holder.lineChart.getAxisLeft().addLimitLine(llr1);
LimitLine lly1 = new LimitLine(Charts.pralrms , "");
lly1.setLineWidth(2f);
lly1.setLineColor(yellowcolor);
holder.lineChart.getAxisLeft().addLimitLine(lly1);
LimitLine llo1 = new LimitLine(Charts.alrms , "");
llo1.setLineWidth(2f);
llo1.setLineColor(orangecolor);
holder.lineChart.getAxisLeft().addLimitLine(llo1);
holder.lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
holder.lineChart.getAxisRight().setEnabled(false);
LineDataSet dataset = new LineDataSet(Charts.rms, "");
//dataset.setDrawCubic(true);
// dataset.setDrawFilled(true);
// dataset.setColors(ColorTemplate.COLORFUL_COLORS);
dataset.setDrawValues(false);
holder.lineChart.animateY(5000);
LineData data = new LineData(Charts.measdate, dataset);
holder.lineChart.setData(data);
holder.lineChart.setDescription("RMS");
holder.lineChart.setDescriptionTextSize(20);
the solution was to fix the y axis min and max value holder.lineChart.getAxisLeft().setAxisMaxValue(Charts.dgpeak+2); holder.lineChart.getAxisLeft().setAxisMinValue(0f);
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/
How can I make my GraphView always start on zero and not on the lowest number of my data ? For example if my data received is {10,44,1,15}, the bottom one will be the 1 and I wanted to be zero. How can I do that ?
And how can I make the GraphViewSeries dynamically ? For example I receive an array with data and I want depending the size of the array to make the GraphViewSeries, like if I receive an array with 3 elements the GraphViewSeries will be like this:
GraphViewSeries exampleSeries = new GraphViewSeries(
new GraphViewData[] {
new GraphViewData(1, total[0]),
new GraphViewData(2, total[1]),
new GraphViewData(3, total[2])});
If the array has 5 elements with would be like this:
GraphViewSeries exampleSeries = new GraphViewSeries(
new GraphViewData[] {
new GraphViewData(1, total[0]),
new GraphViewData(2, total[1]),
new GraphViewData(3, total[2]),
new GraphViewData(4, total[3]),
new GraphViewData(5, total[4]) });
How can I do this ?
You can set the min and max values like this:
GraphView graphView = new LineGraphView(context, "Graph name")
graphView.setManualYAxisBounds((double) max, (double) min);
For dynamic series you can do this:
GraphViewData[] data = new GraphViewData[total.length];
for (int a = 0; a < total.length; a++) {
data[a] = new GraphView.GraphViewData(a, total[a]);
}
then you add your data to a series like this:
GraphViewSeriesStyle style = new GraphViewSeriesStyle(Color.rgb(150, 150, 150), 2);
GraphViewSeries series = new GraphViewSeries(driverName.substring(0, 3).toUpperCase(), style, data);
for the official documentation refer to here.
In GraphView 4.x, setting the axis min and max values has to be done via the viewport:
graphView.getViewport().setYAxisBoundsManual(true);
graphView.getViewport().setMinY(min);
graphView.getViewport().setMaxY(max);
'graphView.setManualYAxisBounds()' no longer exists.
See also http://www.android-graphview.org/documentation/migration-from-31-to-40