Mehdi Karimi
Mehdi Karimi

Reputation: 528

adding Icons with String to MPAndroid Chart as XAxisValues

github.PhilJay:MPAndroidChart:v2.0.9`

I was wondering if I can add star icons and numbers as XAxisValues like image below? enter image description here

I've already added the numbers and it's working fine but I don't know how to add star icons! Is there a way to combine text and icons?
Here is my code so far:

private ArrayList<BarDataSet> getDataSet(int stars1, int stars2, int stars3, int stars4, int stars5) {
    ArrayList<BarDataSet> dataSets = null;
    ArrayList<BarEntry> valueSet1 = new ArrayList<>();
    BarEntry v1e1 = new BarEntry(stars5, 4);
    valueSet1.add(v1e1);
    BarEntry v1e2 = new BarEntry(stars4, 3);
    valueSet1.add(v1e2);
    BarEntry v1e3 = new BarEntry(stars3, 2);
    valueSet1.add(v1e3);
    BarEntry v1e4 = new BarEntry(stars2, 1);
    valueSet1.add(v1e4);
    BarEntry v1e5 = new BarEntry(stars1, 0);
    valueSet1.add(v1e5);

    BarDataSet barDataSet1 = new BarDataSet(valueSet1, "");
    barDataSet1.setColors(new int[]{getResources().getColor(R.color.chart5), getResources().getColor(R.color.chart4),
            getResources().getColor(R.color.chart3), getResources().getColor(R.color.chart2), getResources().getColor(R.color.chart1)});
    barDataSet1.setValueTextSize(9);

    dataSets = new ArrayList<>();
    dataSets.add(barDataSet1);

    return dataSets;
}

private ArrayList<String> getXAxisValues() {
    ArrayList<String> xAxis = new ArrayList<>();
    xAxis.add("1");
    xAxis.add("2");
    xAxis.add("3");
    xAxis.add("4");
    xAxis.add("5");
    return xAxis;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
BarData data = new BarData(getXAxisValues(), getDataSet(stars1, stars2, stars3, stars4, stars5));
    barChart.setData(data);
    barChart.setDescription("");
    barChart.invalidate();
    barChart.getXAxis().setDrawGridLines(false);
    barChart.getXAxis().setDrawAxisLine(false);
    barChart.setDrawGridBackground(false);
    barChart.getAxisLeft().setDrawGridLines(false);
    barChart.getAxisRight().setDrawGridLines(false);
    barChart.getAxisLeft().setDrawAxisLine(false);
    barChart.getAxisRight().setDrawAxisLine(false);
    barChart.getLegend().setEnabled(false);
    barChart.getXAxis().setDrawLabels(true);
    barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    barChart.getAxisRight().setDrawLabels(false);
    barChart.getAxisLeft().setDrawLabels(false);
}

Thanks for your time... ♥

Upvotes: 0

Views: 448

Answers (1)

Misha Akopov
Misha Akopov

Reputation: 13027

Just use ★ character and it will be displayed well. You can use x axis value formatter for formatting values:

barChart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
                                              @Override
                                              public String getFormattedValue(float value, AxisBase axis) {
                                                  return "value ★";
                                              }
                                          }

Here is result :

enter image description here

Upvotes: 1

Related Questions