2017-12-24 7 views
0

막대 그래프를 사용하여 단일 막대 종교 값을 표시하는 MPAndroid 차트 라이브러리에서 작업 중입니다. 나는 이슬람 "녹색", "기독교"= 파란색 등의 종교 이름마다 각 BarEntry에 색을주고 싶습니다.이 포럼에서 많은 예제를 검색했지만이를 해결할 수는 없습니다.막대 그래프 모음의 사용자 정의 색상 세트 MPAndroid 라이브러리

List<BarEntry> entries = new ArrayList<>(); 
    entries.add(new BarEntry(0f, 20,"Islam")); 
    entries.add(new BarEntry(1f, 20,"Christianity")); 
    entries.add(new BarEntry(2f, 20,"Judaism")); 
    entries.add(new BarEntry(3f,20,"Sikhism")); 
    entries.add(new BarEntry(4f,20,"Hinduism")); 

    BarDataSet bSet = new BarDataSet(entries, "Marks"); 
    bSet.setColors(ColorTemplate.VORDIPLOM_COLORS); 

    ArrayList<String> barFactors = new ArrayList<>(); 
    barFactors.add("Islam"); 
    barFactors.add("Christianity"); 
    barFactors.add("Judaism"); 
    barFactors.add("Sikhism"); 
    barFactors.add("Hinduism"); 


    XAxis xAxis = chart.getXAxis(); 
    xAxis.setGranularity(1f); 
    xAxis.setGranularityEnabled(true); 
    BarData data = new BarData(bSet); 
    data.setBarWidth(1f); // set custom bar width 
    data.setValueTextSize(12); 
    Description description = new Description(); 
    description.setTextColor(R.color.colorPrimary); 
    description.setText("Religion analysis"); 
    chart.setDescription(description); 
    chart.setData(data); 
    chart.setFitBars(true); // make the x-axis fit exactly all bars 
    chart.invalidate(); // refresh 
    chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(barFactors)); 

나는이 라이브러리 링크를 사용하고 있습니다 :

implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' 

enter image description here

나는이 그림의 하단에 같은 색상 막대 색상을합니다. 이 문제를 어떻게 해결할 수 있습니까?

답변

1

당신은 다음과 같이 당신의 colors.xml 파일에 색상을 정의해야합니다

<color name="Islam">#1abc9c</color> 
<color name="Christianity">#2ecc71</color> 
<color name="Judaism">#3498db</color> 
<color name="Sikhism">#9b59b6</color> 
<color name="Hinduism">#16a085</color> 

또한 위의 코드에서 각각의 색상 코드를 제공합니다. 당신이 데이터 세트 모음에 값을 제공하는 순서로 색상의 순서를 줄이는 일을 할 것입니다

barDataSet.setColors(new int[] 
      { 
        R.color.Islam, R.color.Christianity, R.color.Judaism, 
        R.color.Sikhism, R.color.Hinduism 
      }, getContext()); 

:

은 그 후 당신은 다음과 같은 이러한 색상에 대한 bardataset 말할 필요가있다.

관련 문제