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);
Related
I'm using MPandroidCharts in android application to make a bar chart it data from NodeJs server my code works fine but I found that when I change nbs of data (number of articles "on XAxe" < 5) the label nb and size still the some ( max 5 elements) that make my graph Not understood, I need a solution plz
to understand my situation plz look at pictures:
My code:
ArrayList<BarEntry> entries = new ArrayList<>();
final ArrayList<String> months = new ArrayList<>();
Log.d("liste size ",product_List.size()+"");
for(int i=0; i<product_List.size();i++){
Log.d("get List nb ",product_List.get(i).getId());
Log.d("get List produit ",product_List.get(i).getProduit());
months.add(i,product_List.get(i).getProduit());
int nb = Integer.parseInt(product_List.get(i).getId());
entries.add(new BarEntry(i, nb));
}
BarDataSet barDataSet = new BarDataSet(entries, "Produits");
barDataSet.setBarBorderWidth(0.9f);
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData barData = new BarData(barDataSet);
XAxis xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
IndexAxisValueFormatter formatter = new IndexAxisValueFormatter(months);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(formatter);
barChart.setData(barData);
barChart.setFitBars(true);
barChart.animateY(5000);
barChart.invalidate();
One more thing, if I when to clear chart data and make others by a button which code I need to use.
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();
}
I want to Display a Stacked Bar Chart in my Android Application with tooltip functionality. I used the MarkerView to display it but no Success. Here is the sample code.
Also I am facing issues in Legends..!
As text wrapping is not supported by the Library the legends are not showing.
And the values at Y-axis are showing in Integer. which I want to display in Floats. Can I give '%' in Y-axis values? Something like this,
https://drive.google.com/a/findabilitysciences.com/file/d/0B-kRufrwei7wSHFMQjVDMkdTNVk/view?usp=sharing
chart1 = (BarChart) findViewById(R.id.chart1);
chart1.setDrawYValues(true);
//chart1.setDrawValueAboveBar(true);
chart1.setDescription("");
chart1.set3DEnabled(false);
chart1.setUnit("%");
chart1.setDrawGridBackground(false);
chart1.setDrawHorizontalGrid(true);
chart1.setValueTextSize(8f);
chart1.setDrawBorder(false);
chart1.setYRange(0.0f,100.0f,true);
chart1.setDrawLegend(true);
chart1.setPinchZoom(true);
chart1.setTouchEnabled(true);
//Code for Tooltip at barchart.
/*MyMarkerView mv = new MyMarkerView(getApplicationContext(), R.layout.custom_marker_view);
mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight());
chart1.setMarkerView(mv);*/
XLabels xl = chart1.getXLabels();
xl.setPosition(XLabels.XLabelPosition.BOTTOM);
xl.setSpaceBetweenLabels(10);
xl.setCenterXLabelText(true);
YLabels yl = chart1.getYLabels();
//yl.setLabelCount(10);
yl.setPosition(YLabels.YLabelPosition.LEFT);
setData();
Legend l = chart1.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_RIGHT);
l.setFormSize(8f);
l.setYEntrySpace(4f);
chart1.animateY(3000);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error Occurred [Server's JSON response might be invalid]!" + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
private void setData() {
ArrayList<String> xVals = new <String>ArrayList<String>();
xVals.add("Donors");
xVals.add("Amount");
ArrayList<BarEntry> yVals1 = new <BarEntry>ArrayList<BarEntry>();
yVals1.add(new BarEntry(new float[]{donoravg.get(0),donoravg.get(1),donoravg.get(2),donoravg.get(3),donoravg.get(4),donoravg.get(5),donoravg.get(6),donoravg.get(7),donoravg.get(8),donoravg.get(9),donoravg.get(10)}, 0));
yVals1.add(new BarEntry(new float[]{amountavg.get(0),amountavg.get(1),amountavg.get(2),amountavg.get(3),amountavg.get(4),amountavg.get(5),amountavg.get(6),amountavg.get(7),amountavg.get(8),amountavg.get(9)}, 1));
BarDataSet set1 = new BarDataSet(yVals1, "");
set1.setStackLabels(new String[]{"All other defector count", "Last Year Defector Count", "At Risk Donor Count"});
set1.setBarSpacePercent(40f);
set1.setColors(new int[]{R.color.pink_400, R.color.purple_300, R.color.deep_purple_300, R.color.indigo_300, R.color.cyan_300, R.color.teal_300, R.color.amber_300, R.color.deep_orange_300, R.color.brown_300, R.color.blue_grey_300}, getApplicationContext());
ArrayList<BarDataSet> dataSets = new <BarDataSet>ArrayList<BarDataSet>();
dataSets.add(set1);
BarData data = new BarData(xVals, dataSets);
chart1.setData(data);
}
The new library of mpAndroidChart v2.0.7 resolved all the issues.
using .setValueFormatter(); we can format Y axis value as well as chart values.
For Legend problem,
Instead of showing Legends I added Touch Listener which can work on stack values.
The touch listener can show the details of value.
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