How to create a BarChart with grouped bars with MPAndroidChart?

前端 未结 4 1085
我在风中等你
我在风中等你 2020-12-29 05:38

How can i compare two set of data using BarChart of MPAndroidChart.

It should look like this image below:

I edited a code, I get from a sample

4条回答
  •  甜味超标
    2020-12-29 05:51

    • Step1 Divide first number of groups in bar chart. Like below sample code displaying for 5 groups. Every group have 5 bars in that.

                 xaxis0 = new ArrayList<>();
                 for (int i = 0; i < cData.size(); i++) {
      
                     String str = cData.get(i).get("count");
                     str = str.replaceAll("\\[", "").replaceAll("\\]", "");
                     String[] finalString = str.split(",");
                     if (i == 0) {
                         for (int k = 0; k < finalString.length; k++) {
      
                             int data22 = Integer.parseInt(finalString[k]);
                             BarEntry v1e1 = new BarEntry(data22, position);
                             valueSet1.add(v1e1);
                         }
                     }
                     if (i == 1) {
                         for (int k = 0; k < finalString.length; k++) {
                             int data22 = Integer.parseInt(finalString[k] + "");
                             BarEntry v1e1 = new BarEntry(data22, position);
                             valueSet2.add(v1e1);
                         }
                     }
                     if (i == 2) {
                         for (int k = 0; k < finalString.length; k++) {
                             int data22 = Integer.parseInt(finalString[k] + "");
                             BarEntry v1e1 = new BarEntry(data22, position);
                             valueSet3.add(v1e1);
                         }
                     }
                     if (i == 3) {
                         for (int k = 0; k < finalString.length; k++) {
                             int data22 = Integer.parseInt(finalString[k] + "");
                             BarEntry v1e1 = new BarEntry(data22, position);
                             valueSet4.add(v1e1);
                         }
                     }
                     if (i == 4) {
                         for (int k = 0; k < finalString.length; k++) {
                             int data22 = Integer.parseInt(finalString[k] + "");
                             BarEntry v1e1 = new BarEntry(data22, position);
                             valueSet5.add(v1e1);
                         }
                     }
                     xaxis0.add(i, xdata.get(i).get("date"));
      
    • Step2 In above code you observe that 5 groups of bar entry loading data in loop of every valueset - ArrayList valueSet2 = new ArrayList<>(); Initialize before this valuesets and

    • Step3 And load that 5 sets to Bardataset like below

      ` BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Set1"); barDataSet1.setColors(whitecolors); barDataSet1.setValueTextColor(Color.WHITE);

      BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Set2"); barDataSet2.setColors(whitecolors); barDataSet2.setValueTextColor(Color.WHITE);

      BarDataSet barDataSet3 = new BarDataSet(valueSet3, "Set3"); barDataSet3.setColors(whitecolors);

      barDataSet3.setValueTextColor(Color.WHITE); BarDataSet barDataSet4 = new BarDataSet(valueSet4, "Set4");

      barDataSet4.setColors(whitecolors); barDataSet4.setValueTextColor(Color.WHITE);

      BarDataSet barDataSet5 = new BarDataSet(valueSet5, "Set5"); barDataSet5.setColors(whitecolors);

      barDataSet5.setValueTextColor(Color.WHITE); dataSets = new ArrayList<>(); dataSets.add(barDataSet1); dataSets.add(barDataSet2); dataSets.add(barDataSet3); dataSets.add(barDataSet4); dataSets.add(barDataSet5); `

    • Last step need to attached this data to Bardata like below code

    BarData data11 = new BarData(xaxis0, dataSets); data11.setGroupSpace(100f);

                   holder.chart.setData(data11);
    
                   XAxis xAxis = holder.chart.getXAxis();
    
                   xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    
                   xAxis.setDrawGridLines(true);  
    

    xAxis.setGridColor(context.getResources().getColor(R.color.white));

                   xAxis.isDrawLabelsEnabled();    
    

    xAxis.setAxisLineColor(context.getResources().getColor(R.color.accentColor)); xAxis.setTextColor(context.getResources().getColor(R.color.white));

                   xAxis.isAdjustXLabelsEnabled();
    
                   xAxis.setAdjustXLabels(true);
    
                   holder.chart.setDescription("");
    
                   holder.chart.animateXY(2000, 2000);
     holder.chart.getAxisLeft().setTextColor(context.getResources().getColor(R.color.white));
                   holder.chart.getAxisRight().setTextColor(context.getResources().getColor(R.color.white));
                   holder.chart.setDrawGridBackground(false);
                   holder.chart.getAxisRight().setEnabled(false);
                   holder.chart.setDrawValueAboveBar(true);
                   holder.chart.getAxisLeft().setEnabled(false);
                   holder.chart.setSoundEffectsEnabled(true);
                   holder.chart.getXAxis().setDrawGridLines(false);
                   holder.chart.setTransitionGroup(true);
                   YAxis yAxis = holder.chart.getAxisLeft();
                   yAxis.setDrawGridLines(false);
                   yAxis.setLabelCount(5);
                   yAxis = holder.chart.getAxisRight();
                   yAxis.setDrawGridLines(false);
                   yAxis.setTextColor(context.getResources().getColor(R.color.white));
                   Legend l =  holder.chart.getLegend();
                   l.setEnabled(false);     
                   Paint p = holder.chart.getPaint(Chart.PAINT_INFO);
                   p.setTextSize(10);
                   p.setColor(context.getResources().getColor(R.color.white));
                   p.setTypeface(gotham);
                   holder.chart.invalidate();
                   l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
                   l.setTextSize(200);
                   yAxis.setValueFormatter(new LargeValueFormatter());
    
       # Thats it if you have doubt about this code ask me any time .......
    

提交回复
热议问题