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
Related
I want to display my CountValue from the database given below in a piechart. I saved the CountValue in an ArrayList finalCount and I dont know why am I getting the last value only of the arrayList in the piechart and not all the value.
Here's my code
for (QueryDocumentSnapshot snapshot : Objects.requireNonNull(task.getResult())) {
String value1 = snapshot.getString("QuestionName");
q_tv1.setText("Poll: " + value1);
ArrayList<String> CountValue = (ArrayList<String>) snapshot.get("CountValue");
Log.d(Tag, "output for des: " + CountValue);
List<String> noOfOptions = (List<String>) snapshot.get("Options");
Log.d(Tag, "output for option: " + noOfOptions);
ArrayList<String> finalCount=(ArrayList<String>) snapshot.get("CountValue");
int size = noOfOptions.size();
Log.d(Tag, "size: " + size);
for (int i = 0; i <= size-1; i++) {
String op = noOfOptions.get(i);
String n = finalCount.get(i);
n1 = Integer.parseInt(String.valueOf(n));
Log.d(Tag, "i->>: " + n1);
ArrayList<PieEntry> visitors = new ArrayList<>();
visitors.add(new PieEntry(n1, op));
PieDataSet pieDataSet = new PieDataSet(visitors, "Answer");
pieDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
pieDataSet.setValueTextColor(Color.BLACK);
pieDataSet.setValueTextSize(16f);
PieData pieData = new PieData(pieDataSet);
pieChart.setData(pieData);
pieChart.getDescription().setEnabled(false);
pieChart.setCenterText("Answer");
pieChart.animate();
}
}
Here's my database
In the code, I converted String n to int n1 which has the finalCount values which is [10,15] but I'm only getting 15 in the piechart and the last option name
ArrayList<PieEntry> visitors = new ArrayList<>();
visitors.add(new PieEntry(n1, op));
My output screenshot looks like this, as you can see only 15 is displayed and not 10 and 15 both
ArrayList<PieEntry> visitors = new ArrayList<>();
visitors.add(new PieEntry(n1, op));
Add ArrayList<PieEntry> visitors = new ArrayList<>(); top of the for loop.
ArrayList<PieEntry> visitors = new ArrayList<>();
for (int i = 0; i <= size-1; i++) {
//code goes here
visitors.add(new PieEntry(n1, op));
//code goes here
}
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]));
}
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 MPAndroidChart2.2.5 to draw charts in my project,which contains 2 group of BarDataand 1 group of LineData.When I use the BarData in Barchart,the bars shows well.But when I use the same BarData in CombinedChart,the position of the bars differs.Here's my activity.
Here's a screenshot:
.
public class CombinedChartActivity extends BaseActivity {
private CombinedChart combinedChart;
private BarChart barChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_combined_chart);
combinedChart = (CombinedChart) findViewById(R.id.chart);
barChart = (BarChart) findViewById(R.id.barChart);
initChart(combinedChart);
initChart(barChart);
barChart.setData(getBarData());
CombinedData combinedData = new CombinedData();
combinedData.setXVals(getXVals());
combinedData.setData(getLineData());
combinedData.setData(getBarData());
combinedChart.setData(combinedData);
}
private void initChart(BarLineChartBase barLineChartBase) {
barLineChartBase.setDescription("");
barLineChartBase.getAxisLeft().setEnabled(false);
barLineChartBase.getAxisRight().setEnabled(false);
barLineChartBase.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
barLineChartBase.getXAxis().setGridColor(R.color.white);
barLineChartBase.getXAxis().setAvoidFirstLastClipping(true);
barLineChartBase.setBackgroundColor(Color.WHITE);
}
private BarData getBarData() {
List<IBarDataSet> iBarDataSets = new ArrayList<>();
ArrayList<BarEntry> barEntries = new ArrayList<>();
for (int i = 0; i < 7; i++) {
BarEntry barEntry = new BarEntry(i + 1, i);
barEntries.add(barEntry);
}
BarDataSet barDataSet = new BarDataSet(barEntries, "Bar1");
barDataSet.setColor(Color.rgb(60, 220, 78));
barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<BarEntry> barEntries2 = new ArrayList<>();
for (int i = 0; i < 7; i++) {
BarEntry barEntry = new BarEntry((float) (i + Math.random() * 10), i);
barEntries2.add(barEntry);
}
BarDataSet barDataSet1 = new BarDataSet(barEntries2, "Bar2");
barDataSet1.setColor(R.color.level_3);
barDataSet1.setAxisDependency(YAxis.AxisDependency.RIGHT);
iBarDataSets.add(barDataSet);
iBarDataSets.add(barDataSet1);
BarData barData = new BarData(getXVals(), iBarDataSets);[![enter image description here][1]][1]
return barData;
}
private LineData getLineData() {
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < 7; i++) {
Entry entry = new Entry((float) (Math.random() * 10), i);
entries.add(entry);
}
List<ILineDataSet> iLineDataSets = new ArrayList<>();
LineDataSet lds1 = new LineDataSet(entries, "Line");
lds1.setLineWidth(0.2f);
lds1.setColor(R.color.level_3);
lds1.setDrawFilled(true);
lds1.setFillAlpha(150);
lds1.setFillColor(R.color.level_2);
lds1.setDrawValues(false);
lds1.setHighlightEnabled(true);
lds1.setDrawHorizontalHighlightIndicator(false);
lds1.setHighLightColor(R.color.white);
lds1.setDrawCubic(true);
lds1.setDrawCircles(true);
lds1.setCircleColor(R.color.white);
lds1.setCircleColorHole(R.color.theme_orange);
lds1.setAxisDependency(YAxis.AxisDependency.LEFT);
iLineDataSets.add(lds1);
LineData lineData = new LineData(getXVals(), iLineDataSets);
return lineData;
}
private List<String> getXVals() {
ArrayList<String> xVals = new ArrayList<>();
xVals.add("Monday");
xVals.add("Tuesday");
xVals.add("Wednesday");
xVals.add("Thursday");
xVals.add("Friday");
xVals.add("Saturday");
xVals.add("Sunday");
return xVals;
}
}
I use same BarData in both charts.What causes the difference.How can I make the bars in Combinedchart show as in the Barchart?
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.