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.63k stars 9.02k forks source link

Dark theme is not supported #5057

Open jarinrocks opened 3 years ago

jarinrocks commented 3 years ago

When I changed theme to dark mode the text color of the the texts in the Pie Chart is not getting changed in accordance with the dark mode.

Device : Moto G4 Android : Nougat(7.0) MPAndroidChart Version : 2.2.4

Kenny50 commented 3 years ago
    private fun getThemeTextColor(): Int {
        val typedValue = TypedValue()
        val theme = this.requireContext().theme
        theme.resolveAttribute(android.R.attr.textColor, typedValue, true)
        return typedValue.data
    }
    private fun initUI() {
        val color = getThemeTextColor()
        binding.lineChart.data = viewModel.getLineData()
        binding.lineChart.data.setValueTextColor(color)
        binding.lineChart.xAxis.textColor= color
        binding.lineChart.axisLeft.textColor = color
        binding.lineChart.axisRight.textColor = color
        binding.lineChart.legend.textColor = color

        //pie chart
        binding.pie.data.setValueTextColor(color)
        binding.pie.legend.textColor = color
    }

this work fine to me, i assume color should be same as textcolor, because it should be clear from background, however you can custom in theme if you prefer

jarinrocks commented 3 years ago

@Kenny50 Thank you. I will try this.

NomadicDeveloper22 commented 3 years ago

Here's a java version that worked better for me pulling from theme

public static int getThemeTextColor(Context context) {
        TypedValue typedValue = new TypedValue();
        Resources.Theme theme = context.getTheme();
        theme.resolveAttribute(R.attr.colorOnSecondary, typedValue, true);
        @ColorInt int color = typedValue.data;
        return color;
    }

and

int color = App.getThemeTextColor(requireContext());
        chart.getData().setValueTextColor(color);
        chart.getData().setValueTextColor(color);
        chart.getXAxis().setTextColor(color);
        chart.getAxisLeft().setTextColor(color);
        chart.getAxisRight().setTextColor(color);
        chart.getLegend().setTextColor(color);