8.Setting Colors(MPAndroidChart中文翻譯)

目錄

第8節.Setting Colors(MPAndroidChart中文翻譯)
第9節.Formatting Data Values (ValueFormatter)(MPAndroidChart中文翻譯)
第10節-Formatting Axis Values (AxisValueFormatter)(MPAndroidChart中文翻譯)
第11節.General Settings & Styling(MPAndroidChart中文翻譯)
第12節.Specific Settings & Styling(MPAndroidChart中文翻譯)
第13節.Legend(MPAndroidChart中文翻譯)
第14節.Dynamic & Realtime Data(MPAndroidChart中文翻譯)
第15節. Modifying the Viewport(MPAndroidChart中文翻譯)
第16節.Animations(MPAndroidChart中文翻譯)
第17節. MarkerView (Popup View)(MPAndroidChart中文翻譯)
第18節. The ChartData class(MPAndroidChart中文翻譯)
第19節. ChartData subclasses(MPAndroidChart中文翻譯)
第20節. The DataSet class (general DataSet styling)(MPAndroidChart中文翻譯)
第21節. DataSet subclasses (specific DataSet styling)(MPAndroidChart中文翻譯)
第22節. The ViewPortHandler(MPAndroidChart中文翻譯)
第23節. Customizing the Fill-Line-Position (FillFormatter)(MPAndroidChart中文翻譯)
第24節. Proguard(MPAndroidChart中文翻譯)
第25節. Realm.io mobile database(MPAndroidChart中文翻譯)
第26節. Creating your own (custom) DataSets(MPAndroidChart中文翻譯)
第27節. Miscellaneous (more useful stuff)(MPAndroidChart中文翻譯)

設置顏色

v1.4.0正式版之后,在以前版本中負責設置顏色的ColorTemplate 對象將不再需要.盡管如此,它仍然擁有所有的預定顏色數組(舉個栗子:ColorTemplate.VORDIPLOM_COLORS )并且提供將顏色從資源(資源數值)轉換為真實顏色的便利方法.

取代ColorTemplate對象的功能,顏色可以直接通過DataSet對象直接設置,這允許為每個DataSet設置單獨的樣式.

舉個簡單的例子,我們有兩個不同的LineDataSet對象,代表兩個公司的季度收入(以前在設置數據的教程中提到過),現在我們要給他們設置不同的顏色.

任務:

  • Company1的數據需要被設置四種不同顏色的紅色
  • Company2的數據需要被設置四宗不同顏色的綠色
    代碼示例:
LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1");
  // sets colors for the dataset, resolution of the resource name to a "real" color is done internally
  setComp1.setColors(new int[] { R.color.red1, R.color.red2, R.color.red3, R.color.red4 }, Context);
  
  LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2");
  setComp2.setColors(new int[] { R.color.green1, R.color.green2, R.color.green3, R.color.green4 }, Context);

除此之外,還有許多方法來設置DataSet的顏色值,以下是完整文檔:

  • setColors(int [] colors, Context c):設置DataSet顏色,當DataSet的集合長度比顏色集合長度達時,顏色值將被復用.你可以使用"new int[]{R.color,R.color.green,...}" 這個方法提供顏色值,程序內部,顏色是通過geResources().getColor(...)方法來獲取的.
  • setColors(int [] colors): 設置DataSet顏色,當DataSet的集合長度比顏色集合長度達時,顏色值將被復用.確定顏色在添加到DataSet之前已經被準備好(通過getResources().getColor(...)).
  • setColors(ArrayList<Integer> colors): 設置DataSet顏色,當DataSet的集合長度比顏色集合長度達時,顏色值將被復用.確定顏色在添加到DataSet之前已經被準備好(通過getResources().getColor(...)).
  • setColor(int color):給DataSet對象設置唯一的一種顏色.程序內部,將重新創建colors數組并添加指定的顏色;

ColorTempate example:

LineDataSet set = new LineDataSet(...);
set.setColors(ColorTemplate.VORDIPLOM_COLORS);

如果DataSet沒有設置顏色,將會使用默認顏色.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容