MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

In Dark Mode, Line chart text color not correct as Material Theming

Open marlonlom opened this issue 5 years ago • 3 comments

Summary When displaying the chart in dark mode/night mode, the text color isnt changing.

linechart-textcolor-not-changing-dark_mode

Expected Behavior Add support for color theme attributes as per Material Design Guidelines for Color.

Possible Solution Add support for theme attributes inside the line chart, as Material Design guidelines for Themes and Color.

Color attributes for use: colorOnSurface

When using theme color attributes, i've using this for getting the color attribute and adding it to the text color attributes for the chart (xAxis) and also in the LineDataSet.

Device:

  • Device: Google Pixel 3
  • Android Version: 10.0
  • Library Version: 3.1.0

More info: this related question on StackOverflow

marlonlom avatar Sep 25 '20 05:09 marlonlom

Just found a solution - you can use this code to adjust txt color:

XAxis xaxis = chart.getXAxis();
YAxis yaxis = chart.getAxisRight();

switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
    case Configuration.UI_MODE_NIGHT_YES:
        xaxis.setTextColor(Color.WHITE);
        yaxis.setTextColor(Color.WHITE);
        break;
    case Configuration.UI_MODE_NIGHT_NO:
        ... code for light mode ...
        break;
}

vldmarton avatar Jun 23 '21 20:06 vldmarton

@ColorInt fun Context.getColorThemeRes(@AttrRes id: Int): Int { val resolvedAttr = TypedValue() this.theme.resolveAttribute(id, resolvedAttr, true) return this.getColor(resolvedAttr.resourceId) }

val textColorPrimary = requireContext().getColorThemeRes(android.R.attr.textColorPrimary) val yAxis = chart.getAxisLeft() yAxis.textColor = textColorPrimary similar all other textcolors like radarData.setValueTextColor(textColorPrimary) lineChart.legend.textColor = textColorPrimary

utetrapp avatar Nov 26 '21 20:11 utetrapp

The solution is simple, configure your desired color in the Light and Night Theme then set it

mPPieChartLegend.textColor = ContextCompat.getColor(requireActivity(), R.color.yourColor)

netolobo avatar Apr 15 '22 15:04 netolobo