Hot questions for Text and Font in MPAndroidChart

MPAndroidChart change font (Typeface)

Question: I was trying to change the font using Typeface but it doesn't work. Here is my code below

val tf = Typeface.createFromFile("app/src/main/res/font/montserrat_regular.ttf")
data.setValueTypeface(tf)

Answer: Try this

val tf = Typeface.createFromAsset(assetManager, "font/montserrat_regular.ttf")
data.setValueTypeface(tf)

How to increase font size of bottom bar MPAndroidChart

Question: I have a MPAndroidChart BarChart and I have looked through the javadoc and online documentation but am unable to find a way to increase the size of the text at the bottom of the chart.

Here is an image which shows the chart and the font at the bottom left which is too small to view properly on this higher resolution tablet. Is there a way to increase this font size?

Note: I recognize that increasing the axis label size is done with:

chart.getXAxis().setTextSize(20);

I have searched online at a variety of topics and looked through the MPAndroidChart github project but have been unable to find code to make this change. I have been able to change all the other font sizes so can only assume this must also be possible.

Answer: First load the legend:

Legend l = pieChart.getLegend();

Then you can modify the legends properties such as the text size,color, or the shape of the objects in your legend:

    l.setTextSize(20f);
    l.setTextColor(Color.BLACK);
    l.setForm(Legend.LegendForm.CIRCLE);

MPAndroidChart Marker's TextView appearance

Question: I'm trying to add a marker when I touch a point on a LineChart. I tried different ways for it to work, but I can't get it to present the text in the TextView.

private IMarker markerView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    mChart = findViewById(R.id.peaks_chart);
    mChart.setTouchEnabled(true);
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setPinchZoom(true);
    mChart.setBackgroundColor(Color.LTGRAY);

    markerView = new CustomMarkerView(getApplicationContext(),R.layout.tv_content);
    mChart.setMarker(markerView);
    mChart.setDrawMarkers(true);

    mChart.getLegend().setEnabled(true);

    XAxis xl = mChart.getXAxis();
    xl.setTypeface(mTfLight);
    xl.setTextColor(Color.WHITE);
    xl.setDrawGridLines(false);
    xl.setDrawLabels(true);
    xl.setEnabled(true);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTypeface(mTfLight);
    leftAxis.setTextColor(Color.WHITE);
    leftAxis.setDrawGridLines(false);
    leftAxis.setDrawLabels(true);
    leftAxis.setEnabled(true);

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setEnabled(false);

    dataSet_r = new LineDataSet(null,"right nost");
    dataSet_r.setDrawValues(true);
    rChartLineData = new LineData();
}

After I enter entries to the DataSet I call notifyDataSetChanged() and invalidate().

Custom marker view class:

public class CustomMarkerView extends MarkerView implements IMarker
{
    private TextView textView;

    public CustomMarkerView(Context context, int layoutResource)
    {
        super(context, layoutResource);
        textView = findViewById(R.id.tvContent);
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight)
    {
        textView.setText(String.valueOf(e.getX())+"777");
        super.refreshContent(e, highlight);
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/battery_full"
android:gravity="center"
 >

<TextView
    android:id="@+id/tvContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="1234"
    android:textAlignment="center"
    android:textColor="#000000"
    android:textSize="14sp" />

</RelativeLayout>

The chart is drawn as expected, but when I press on a point on the chart is shows only the background image and no Text (even if I don't try to change the text there's no text in the TextView).

What am I doing wrong?

Answer: Tested your case, the problem is

android:textAlignment="center"

of your marker, remove it and the text will be displayed.

Marker has some conflicts with textAlignment and gravity attributes. Just make your TextView width wrap_content and it will be centered.


How to add highlight text on my candlechart by MPandroidChart?

Question: I just wanted to add "To show a small square around the highlight crosshair, which shows the data from the Y-axis when I swipe on the chart" in my chart

For example, If I use a linechart Entry data is constructed by the following for loop

for (int i = 0; i <= barcount; i ++) {
LinValues.add (new Entry (i, 10000 + 2 * i));
}
///Set LineDataSet
LineDataSet setline = new LineDataSet (LinValues, "AVER");
setline.setAxisDependency (YAxis.AxisDependency.LEFT);
setline.setColor (Color.GREEN);
setline.setLineWidth (1f);
setline.setDrawCircles (false);
setline.setDrawValues ​​(false);
setline.setHighLightColor (Color.WHITE);
LineData line = new LineData (setline);
Legend l =combchart.getLegend();
l.setTextColor(Color.WHITE);
YAxis yLAxis =combchart.getAxisLeft();
yLAxis.setTextSize(22f);
yLAxis.setLabelCount(5,true);
yLAxis.setTextColor(Color.WHITE);
YAxis yRAxis = combchart.getAxisRight();
yRAxis.setEnabled(false);
Linechat.setData (line);

ow can I to add the "ith value" from "LinValues" ​​next to the ith crosshair and when I slide to the "nth data point" , the value near the crosshair will change value to"nth value"from"LinValues?

Answer: this can be done by using marker view for that follow instructions below:

First of all add java class CustomMarkerView in your project and write below code in it.

public class CustomMarkerView extends MarkerView
{
private TextView tvContent;

public CustomMarkerView(Context context, int layoutResource, int valueSize, int valueColor)
{
    super(context, layoutResource);
    // this markerview only displays a textview
    tvContent = findViewById(R.id.tvContent);
    tvContent.setTextSize(valueSize);
    tvContent.setTextColor(valueColor);
}

// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)

@Override
public void refreshContent(Entry e, Highlight highlight)
{
    tvContent.setText("x: " + e.getX() + "\n" +"y: " + e.getY()); // set the entry-value as the display text
}

}

After that create a custom_marker.xml file in your layout folder and write following code in it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="@drawable/markers_bg"
>

<TextView
    android:id="@+id/tvContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:paddingBottom="15dp"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

After that add method in your activity where your are creating chart:

public CustomMarkerView markerView(Context context)
{
    CustomMarkerView mv = new CustomMarkerView(context, R.layout.custom_marker, 16, Color.WHITE);
    mv.setOffset(- mv.getWidth() / 2, -mv.getHeight()-25);
    return mv;
}

After that add following lines:

    lineChart.setDrawMarkers(true);
    lineChart.setMarker(markerView(context));

Now run your project and when you click point of your line chart you will get what you want.