/** * Saves the current state of the chart to the gallery as a PNG image. * NOTE: Needs permission WRITE_EXTERNAL_STORAGE * * @param fileName e.g. "my_image" * @return returns true if saving was successful, false if not */ public boolean saveToGallery(String fileName) { return saveToGallery(fileName, "", "MPAndroidChart-Library Save", Bitmap.CompressFormat.PNG, 40); }
@Override protected void onDraw(Canvas canvas) { // super.onDraw(canvas); if (mData == null) { boolean hasText = !TextUtils.isEmpty(mNoDataText); if (hasText) { MPPointF c = getCenter(); canvas.drawText(mNoDataText, c.x, c.y, mInfoPaint); } return; } if (!mOffsetsCalculated) { calculateOffsets(); mOffsetsCalculated = true; } }
/** * Returns the bitmap that represents the chart. * * @return */ public Bitmap getChartBitmap() { // Define a bitmap with the same size as the view Bitmap returnedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565); // Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); // Get the view's background Drawable bgDrawable = getBackground(); if (bgDrawable != null) // has background drawable, then draw it on the canvas bgDrawable.draw(canvas); else // does not have background drawable, then draw white background on // the canvas canvas.drawColor(Color.WHITE); // draw the view on the canvas draw(canvas); // return the bitmap return returnedBitmap; }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { for (int i = 0; i < getChildCount(); i++) { getChildAt(i).layout(left, top, right, bottom); } }
out = new FileOutputStream(filePath); Bitmap b = getChartBitmap(); b.compress(format, quality, out); values.put(Images.Media.SIZE, size); return getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values) != null;
/** * initialize all paints and stuff */ protected void init() { setWillNotDraw(false); // setLayerType(View.LAYER_TYPE_HARDWARE, null); mAnimator = new ChartAnimator(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // ViewCompat.postInvalidateOnAnimation(Chart.this); postInvalidate(); } }); // initialize the utils Utils.init(getContext()); mMaxHighlightDistance = Utils.convertDpToPixel(500f); mDescription = new Description(); mLegend = new Legend(); mLegendRenderer = new LegendRenderer(mViewPortHandler, mLegend); mXAxis = new XAxis(); mDescPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mInfoPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mInfoPaint.setColor(Color.rgb(247, 189, 51)); // orange mInfoPaint.setTextAlign(Align.CENTER); mInfoPaint.setTextSize(Utils.convertDpToPixel(12f)); if (mLogEnabled) Log.i("", "Chart.init()"); }
setWillNotDraw(false); Utils.init(getContext()); mMaxHighlightDistance = Utils.convertDpToPixel(500f); initXAxis();
/** * Returns a recyclable MPPointF instance. * * @return */ @Override public MPPointF getCenterOfView() { return getCenter(); }
public ChartTouchListener(T chart) { this.mChart = chart; mGestureDetector = new GestureDetector(chart.getContext(), this); }
/** * Saves the current chart state with the given name to the given path on * the sdcard leaving the path empty "" will put the saved file directly on * the SD card chart is saved as a PNG image, example: * saveToPath("myfilename", "foldername1/foldername2"); * * @param title * @param pathOnSD e.g. "folder1/folder2/folder3" * @return returns true on success, false on error */ public boolean saveToPath(String title, String pathOnSD) { Bitmap b = getChartBitmap(); OutputStream stream = null; try { stream = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + pathOnSD + "/" + title + ".png"); /* * Write bitmap to file using JPEG or PNG and 40% quality hint for * JPEG. */ b.compress(CompressFormat.PNG, 40, stream); stream.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
@Override public void drawHighlighted(Canvas c, Highlight[] indices) { Chart chart = mChart.get(); if (chart == null) return; for (DataRenderer renderer : mRenderers) { ChartData data = null; if (renderer instanceof BarChartRenderer) data = ((BarChartRenderer)renderer).mChart.getBarData(); else if (renderer instanceof LineChartRenderer) data = ((LineChartRenderer)renderer).mChart.getLineData(); else if (renderer instanceof CandleStickChartRenderer) data = ((CandleStickChartRenderer)renderer).mChart.getCandleData(); else if (renderer instanceof ScatterChartRenderer) data = ((ScatterChartRenderer)renderer).mChart.getScatterData(); else if (renderer instanceof BubbleChartRenderer) data = ((BubbleChartRenderer)renderer).mChart.getBubbleData(); int dataIndex = data == null ? -1 : ((CombinedData)chart.getData()).getAllData().indexOf(data); mHighlightBuffer.clear(); for (Highlight h : indices) { if (h.getDataIndex() == dataIndex || h.getDataIndex() == -1) mHighlightBuffer.add(h); } renderer.drawHighlighted(c, mHighlightBuffer.toArray(new Highlight[mHighlightBuffer.size()])); } }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { for (int i = 0; i < getChildCount(); i++) { getChildAt(i).layout(left, top, right, bottom); } }
out = new FileOutputStream(filePath); Bitmap b = getChartBitmap(); b.compress(format, quality, out); values.put(Images.Media.SIZE, size); return getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values) != null;
setWillNotDraw(false); Utils.init(getContext()); mMaxHighlightDistance = Utils.convertDpToPixel(500f);
/** * Returns a recyclable MPPointF instance. * * @return */ @Override public MPPointF getCenterOfView() { return getCenter(); }
public ChartTouchListener(T chart) { this.mChart = chart; mGestureDetector = new GestureDetector(chart.getContext(), this); }
/** * Saves the current chart state with the given name to the given path on * the sdcard leaving the path empty "" will put the saved file directly on * the SD card chart is saved as a PNG image, example: * saveToPath("myfilename", "foldername1/foldername2"); * * @param title * @param pathOnSD e.g. "folder1/folder2/folder3" * @return returns true on success, false on error */ public boolean saveToPath(String title, String pathOnSD) { Bitmap b = getChartBitmap(); OutputStream stream = null; try { stream = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + pathOnSD + "/" + title + ".png"); /* * Write bitmap to file using JPEG or PNG and 40% quality hint for * JPEG. */ b.compress(CompressFormat.PNG, 40, stream); stream.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
@Override public void drawHighlighted(Canvas c, Highlight[] indices) { Chart chart = mChart.get(); if (chart == null) return; for (DataRenderer renderer : mRenderers) { ChartData data = null; if (renderer instanceof BarChartRenderer) data = ((BarChartRenderer)renderer).mChart.getBarData(); else if (renderer instanceof LineChartRenderer) data = ((LineChartRenderer)renderer).mChart.getLineData(); else if (renderer instanceof CandleStickChartRenderer) data = ((CandleStickChartRenderer)renderer).mChart.getCandleData(); else if (renderer instanceof ScatterChartRenderer) data = ((ScatterChartRenderer)renderer).mChart.getScatterData(); else if (renderer instanceof BubbleChartRenderer) data = ((BubbleChartRenderer)renderer).mChart.getBubbleData(); int dataIndex = data == null ? -1 : ((CombinedData)chart.getData()).getAllData().indexOf(data); mHighlightBuffer.clear(); for (Highlight h : indices) { if (h.getDataIndex() == dataIndex || h.getDataIndex() == -1) mHighlightBuffer.add(h); } renderer.drawHighlighted(c, mHighlightBuffer.toArray(new Highlight[mHighlightBuffer.size()])); } }
/** * Returns the bitmap that represents the chart. * * @return */ public Bitmap getChartBitmap() { // Define a bitmap with the same size as the view Bitmap returnedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565); // Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); // Get the view's background Drawable bgDrawable = getBackground(); if (bgDrawable != null) // has background drawable, then draw it on the canvas bgDrawable.draw(canvas); else // does not have background drawable, then draw white background on // the canvas canvas.drawColor(Color.WHITE); // draw the view on the canvas draw(canvas); // return the bitmap return returnedBitmap; }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { for (int i = 0; i < getChildCount(); i++) { getChildAt(i).layout(left, top, right, bottom); } }