{"action":"created","issue":{"url":"https://api.github.com/repos/PhilJay/MPAndroidChart/issues/1076","repository_url":"https://api.github.com/repos/PhilJay/MPAndroidChart","labels_url":"https://api.github.com/repos/PhilJay/MPAndroidChart/issues/1076/labels{/name}","comments_url":"https://api.github.com/repos/PhilJay/MPAndroidChart/issues/1076/comments","events_url":"https://api.github.com/repos/PhilJay/MPAndroidChart/issues/1076/events","html_url":"https://github.com/PhilJay/MPAndroidChart/issues/1076","id":109977200,"number":1076,"title":"How to have a gradient background under LineChart","user":{"login":"DarkHelmet67","id":14994517,"avatar_url":"https://avatars.githubusercontent.com/u/14994517?v=3","gravatar_id":"","url":"https://api.github.com/users/DarkHelmet67","html_url":"https://github.com/DarkHelmet67","followers_url":"https://api.github.com/users/DarkHelmet67/followers","following_url":"https://api.github.com/users/DarkHelmet67/following{/other_user}","gists_url":"https://api.github.com/users/DarkHelmet67/gists{/gist_id}","starred_url":"https://api.github.com/users/DarkHelmet67/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DarkHelmet67/subscriptions","organizations_url":"https://api.github.com/users/DarkHelmet67/orgs","repos_url":"https://api.github.com/users/DarkHelmet67/repos","events_url":"https://api.github.com/users/DarkHelmet67/events{/privacy}","received_events_url":"https://api.github.com/users/DarkHelmet67/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2015-10-06T10:13:34Z","updated_at":"2016-12-28T02:19:42Z","closed_at":"2015-10-06T10:16:48Z","body":"Being a \"Frequently Asked Qustion\", I did it subclassing  LineChartRenderer in this way.\n\n```\npublic class LineChartRenderer extends com.github.mikephil.charting.renderer.LineChartRenderer {\n\n    public LineChartRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {\n        super(chart, animator, viewPortHandler);\n    }\n\n    @Override\n    protected void drawLinearFill(Canvas c, LineDataSet dataSet, List<Entry> entries, int minx, int maxx, Transformer trans) {\n\n        mRenderPaint.setStyle(Paint.Style.FILL);\n\n        mRenderPaint.setColor(dataSet.getFillColor());\n        // filled is drawn with less alpha\n        mRenderPaint.setAlpha(dataSet.getFillAlpha());\n\n        Path filled = generateFilledPath(entries, dataSet.getFillFormatter().getFillLinePosition(dataSet, mChart), minx, maxx);\n\n        trans.pathValueToPixel(filled);\n\n        // GRADIENT BG - SET SHADER\n        ALog.d(this, \"drawLinearFill @LineChartRenderer - c.getHeight()=\" + c.getHeight());\n        mRenderPaint.setShader(new LinearGradient(0, 0, 0, c.getHeight(), AConstant.COLOR_CHART_LINE, AConstant.COLOR_CHART_BG,\n                Shader.TileMode.CLAMP));\n\n        c.drawPath(filled, mRenderPaint);\n\n        // restore alpha\n        mRenderPaint.setAlpha(255);\n\n        // GRADIENT BG - REMOVE SHADER\n        mRenderPaint.setShader(null);\n    }\n\n    /**\n     * Generates the path that is used for filled drawing.\n     * \n     * @param entries\n     * @return\n     */\n    private Path generateFilledPath(List<Entry> entries, float fillMin, int from, int to) {\n        ALog.d(this, \"generateFilledPath @LineChartRenderer\");\n\n        float phaseX = mAnimator.getPhaseX();\n        float phaseY = mAnimator.getPhaseY();\n\n        Path filled = new Path();\n        filled.moveTo(entries.get(from).getXIndex(), fillMin);\n        filled.lineTo(entries.get(from).getXIndex(), entries.get(from).getVal() * phaseY);\n\n        // create a new path\n        for (int x = from + 1, count = (int) Math.ceil((to - from) * phaseX + from); x < count; x++) {\n\n            Entry e = entries.get(x);\n            filled.lineTo(e.getXIndex(), e.getVal() * phaseY);\n        }\n\n        // close up\n        filled.lineTo(entries.get(Math.max(Math.min((int) Math.ceil((to - from) * phaseX + from) - 1, entries.size() - 1), 0))\n                .getXIndex(), fillMin);\n\n        filled.close();\n\n        return filled;\n    }\n\n}\n```\n\nBeing AConstant.COLOR_CHART_LINE and AConstant.COLOR_CHART_BG the START and END color of the gradient.\n\nHope can be useful to someone.\n"},"comment":{"url":"https://api.github.com/repos/PhilJay/MPAndroidChart/issues/comments/269414168","html_url":"https://github.com/PhilJay/MPAndroidChart/issues/1076#issuecomment-269414168","issue_url":"https://api.github.com/repos/PhilJay/MPAndroidChart/issues/1076","id":269414168,"user":{"login":"androchentw","id":2317501,"avatar_url":"https://avatars.githubusercontent.com/u/2317501?v=3","gravatar_id":"","url":"https://api.github.com/users/androchentw","html_url":"https://github.com/androchentw","followers_url":"https://api.github.com/users/androchentw/followers","following_url":"https://api.github.com/users/androchentw/following{/other_user}","gists_url":"https://api.github.com/users/androchentw/gists{/gist_id}","starred_url":"https://api.github.com/users/androchentw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/androchentw/subscriptions","organizations_url":"https://api.github.com/users/androchentw/orgs","repos_url":"https://api.github.com/users/androchentw/repos","events_url":"https://api.github.com/users/androchentw/events{/privacy}","received_events_url":"https://api.github.com/users/androchentw/received_events","type":"User","site_admin":false},"created_at":"2016-12-28T02:19:42Z","updated_at":"2016-12-28T02:19:42Z","body":"For reference: There's a new way to set this by\r\n\r\n```\r\nlineDataSet.setDrawFilled(true);\r\nlineDataSet.setFillDrawable(gradientDrawable);\r\n```\r\n\r\nDetails are at #104 and [stackoverflow]\r\n\r\n[stackoverflow]: http://stackoverflow.com/questions/38787872/how-to-solve-draw-gradient-below-line-philjay-mpandroidchart-issues-104"}}
{"actor":{"display_login":"androchentw"}}