如何用 LinearGradiant 或颜色数组填充 MPAndroidChart 中的 LineDataSet?
Posted
技术标签:
【中文标题】如何用 LinearGradiant 或颜色数组填充 MPAndroidChart 中的 LineDataSet?【英文标题】:How to fill LineDataSet in MPAndroidChart with LinearGradiant or Array of colors? 【发布时间】:2016-03-11 07:40:47 【问题描述】:我正在制作一个需要显示可见光谱(400nm 到 780nm)的应用程序。我正在使用MPandroidchart
。我将wavelength
转换为color
并渲染了光谱。以下是该应用程序的屏幕截图。我可以在背景网格上显示渲染的光谱,但如何在lineDataset
中显示。 LineDataSet
只有一个功能SetFillColor(int)
。我想用这个Paint
填充lineDataset
。这是我的代码。
Paint paint = new Paint();// = chart.setPaint();
int[] colors =new int[7];
float[] pos = 0.0f, 0.15f, 0.275f, 0.325f, 0.5f,0.6625f,1;
final float[] bands = 380, 440, 490, 510, 580, 645, 780;
for(int i =0;i<bands.length;i++)
colors[i]=Wavelength.wvColor(bands[i], gamma);
//Wavelength.wvColor is the function which returns the `int`.
paint.setShader(new LinearGradient(0, 0, chart.getWidht(), 0, colors, pos, Shader.TileMode.CLAMP));
在chart
,我可以很容易地用下面的代码显示它
chart.setPaint(paint, Chart.PAINT_GRID_BACKGROUND);
问题:如何使用 linearGradient 填充我的LineDataset
或填充颜色数组?
【问题讨论】:
只需将渐变设置为图表的背景! 如何设置?抱歉太天真了。 我不想将其设置为图表背景,我想将其设置为线条集的填充颜色。 【参考方案1】:您可以在这里找到解决方案:https://github.com/PhilJay/MPAndroidChart/issues/1076
public class LineChartRenderer extends com.github.mikephil.charting.renderer.LineChartRenderer
public LineChartRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler)
super(chart, animator, viewPortHandler);
@Override
protected void drawLinearFill(Canvas c, LineDataSet dataSet, List<Entry> entries, int minx, int maxx, Transformer trans)
mRenderPaint.setStyle(Paint.Style.FILL);
mRenderPaint.setColor(dataSet.getFillColor());
// filled is drawn with less alpha
mRenderPaint.setAlpha(dataSet.getFillAlpha());
Path filled = generateFilledPath(entries, dataSet.getFillFormatter().getFillLinePosition(dataSet, mChart), minx, maxx);
trans.pathValueToPixel(filled);
// GRADIENT BG - SET SHADER
ALog.d(this, "drawLinearFill @LineChartRenderer - c.getHeight()=" + c.getHeight());
mRenderPaint.setShader(new LinearGradient(0, 0, 0, c.getHeight(), AConstant.COLOR_CHART_LINE, AConstant.COLOR_CHART_BG,
Shader.TileMode.CLAMP));
c.drawPath(filled, mRenderPaint);
// restore alpha
mRenderPaint.setAlpha(255);
// GRADIENT BG - REMOVE SHADER
mRenderPaint.setShader(null);
/**
* Generates the path that is used for filled drawing.
*
* @param entries
* @return
*/
private Path generateFilledPath(List<Entry> entries, float fillMin, int from, int to)
ALog.d(this, "generateFilledPath @LineChartRenderer");
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
Path filled = new Path();
filled.moveTo(entries.get(from).getXIndex(), fillMin);
filled.lineTo(entries.get(from).getXIndex(), entries.get(from).getVal() * phaseY);
// create a new path
for (int x = from + 1, count = (int) Math.ceil((to - from) * phaseX + from); x < count; x++)
Entry e = entries.get(x);
filled.lineTo(e.getXIndex(), e.getVal() * phaseY);
// close up
filled.lineTo(entries.get(Math.max(Math.min((int) Math.ceil((to - from) * phaseX + from) - 1, entries.size() - 1), 0))
.getXIndex(), fillMin);
filled.close();
return filled;
【讨论】:
也在这里:***.com/questions/38787872/…以上是关于如何用 LinearGradiant 或颜色数组填充 MPAndroidChart 中的 LineDataSet?的主要内容,如果未能解决你的问题,请参考以下文章