我正在制作一个应用程序,我需要显示可见光谱(400nm到780nm)。我正在使用MPAndroidchart
。我将wavelength
转换为color
并渲染了频谱。以下是该应用的屏幕截图。我可以在背景网格上显示渲染的光谱,但是如何在lineDataset
中显示。 LineDataSet
只有一个函数SetFillColor(int)
。我想用lineDataset
填充Paint
。这是我的代码。
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
或填充颜色数组?
答案 0 :(得分: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;
}
}