title: Save a MPAndroidChart chart to an Image without displaying it on an activity
body: <p>I want to generate a PDF, but first I need to create an image from a <strong>LineChart</strong> (MPAndroidChart library) without displaying it in an activity.</p>
  
  <p>I've followed this tutorial <a href="http://android-crap.blogspot.com/2013/02/create-bitmap-from-layoutview.html" rel="nofollow noreferrer">http://android-crap.blogspot.com/2013/02/create-bitmap-from-layoutview.html</a>, and I've modified the code to my own requirement:</p>
  
  <pre><code>private void createLineChart(double frequency) {
  
      LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      RelativeLayout view = new RelativeLayout(activity);
      inflater.inflate(R.layout.line_chart_layout, view, true);
  
      LineChart lc = view.findViewById(R.id.lncMain);
      lc.setData(createLineData(frequency));
      lc.invalidate();
  
      view.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
  
      view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
              View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  
      view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  
      Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
  
      Canvas c = new Canvas(b);
      view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
      view.draw(c);
  
      String filename = "graph.png";
      File sd = Environment.getExternalStorageDirectory();
      File dest = new File(sd, filename);
  
      try {
          FileOutputStream out = new FileOutputStream(dest);
          b.compress(Bitmap.CompressFormat.PNG, 90, out);
          out.flush();
          out.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      Log.i(TAG, filename);
  }
  </code></pre>
  
  <p>The <code>activity</code> variable is an instance of <code>AppCompatActivity</code>. The <code>R.layout.line_chart_layout</code>'s content:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/layParent"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      &gt;
  
      &lt;com.github.mikephil.charting.charts.LineChart
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/lncMain"
          android:orientation="horizontal"
          &gt;
  
      &lt;/com.github.mikephil.charting.charts.LineChart&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Once the app is executed I get an image file called <code>graph.png</code>, but its content does not relfect the desire plot:</p>
  
  <p><a href="https://i.stack.imgur.com/9HQda.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9HQda.png" alt="graph.png"></a></p>
  
  <p>I have tried different values for <code>view.setLayoutParams</code>, <code>view.layout</code>, but it does not work... Some suggestions, please!</p>