PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.64k stars 9.01k forks source link

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

Open marlonlom opened 4 years ago

marlonlom commented 4 years ago

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:

More info: this related question on StackOverflow

vldmarton commented 3 years ago

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;
}
utetrapp commented 2 years ago

@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

netolobo commented 2 years ago

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)