我正在使用AchartEngine绘制折线图。
我做了什么:我想使用Achart Engine创建LineGraph。但是像我们在Barchart中那样启用了不同的颜色渐变。
我制作了无忧无虑的折线图。
我知道Gradient仅受Achart Engine中的范围条形图支持。但是,如果任何人有这样的话,应该有一些解决方法。
我的代码如下:
public GraphicalView graph(Context context) {
String[] titles = new String[] { "Nike Line Graph" };
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 14230, 12300, 14240, 15244, 14900, 12200,
11030, 12000, 12500, 15500, 14600, 15000 });
int length = values.get(0).length;
Log.d("****length*********", "" + length);
int[] colors = new int[] { Color.GREEN };
PointStyle[] styles = new PointStyle[] { PointStyle.POINT };
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
setChartSettings(renderer, "Monthly sales in the last 2 years",
"Month", "Units sold", 0.75, 12.25, 10000, 19000,
Color.argb(0x00, 0x01, 0x01, 0x01),
Color.argb(0x00, 0x01, 0x01, 0x01));
renderer.setChartTitleTextSize(20);
renderer.setTextTypeface("sans_serif", Typeface.BOLD);
renderer.setLabelsTextSize(0f);
renderer.setLabelsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
renderer.setAxisTitleTextSize(15);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.argb(0x00, 0x01, 0x01, 0x01));
renderer.setLegendTextSize(15);
renderer.setScale(10);
renderer.setShowAxes(false);
renderer.setXLabelsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
renderer.setYLabelsColor(0, Color.argb(0x00, 0x01, 0x01, 0x01));
renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
renderer.setPanEnabled(false, false);
renderer.setZoomEnabled(false, false);
length = renderer.getSeriesRendererCount();
SimpleSeriesRenderer r1 = new SimpleSeriesRenderer();
r1.setGradientEnabled(true);
r1.setGradientStart(-50, Color.BLACK);
r1.setGradientStop(50, Color.GREEN);
Log.d("****length*********", "" + length);
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = (XYSeriesRenderer) renderer
.getSeriesRendererAt(i);
r.setLineWidth(5f);
}
GraphicalView v = ChartFactory.getCubeLineChartView(context,
buildBarDataset(titles, values), renderer, 0.5f);
return v;
}
拥有方法:
protected XYMultipleSeriesRenderer buildRenderer(int[] colors,
PointStyle[] styles) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
setRenderer(renderer, colors, styles);
return renderer;
}
protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors,
PointStyle[] styles) {
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[] { 20, 30, 15, 20 });
int length = colors.length;
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors[i]);
r.setPointStyle(styles[i]);
renderer.addSeriesRenderer(r);
}
我的目标是ScreenShot,如下所示:
我想要的:
问题:我想在此折线图中添加渐变颜色。从一种颜色到另一种颜色。
任何帮助都会受到高度赞赏。
先谢谢。
答案 0 :(得分:0)
使用几个系列,每个系列都有自己的颜色。
答案 1 :(得分:0)
如果您仍在寻找答案&amp;任何人都会发现这个问题相同,所以 答案是肯定的,我们可以将梯度设置为折线图。 解决方案是继承LineChart并覆盖与以下相同的drawSeries方法
@Override
public void drawSeries(Canvas canvas, Paint paint, float[] points,
SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {
int length = points.length;
XYSeriesRenderer renderer = (XYSeriesRenderer) seriesRenderer;
float lineWidth = paint.getStrokeWidth();
paint.setStrokeWidth(renderer.getLineWidth());
if (renderer.isFillBelowLine() || renderer.isGradientEnabled()) {
int pLength = points.length;
float[] fillPoints = new float[pLength + 4];
System.arraycopy(points, 0, fillPoints, 0, length);
fillPoints[0] = points[0] + 1;
fillPoints[length] = fillPoints[length - 2];
fillPoints[length + 1] = yAxisValue;
fillPoints[length + 2] = fillPoints[0];
fillPoints[length + 3] = fillPoints[length + 1];
float max = 0;
for (int i = 0; i < length + 4; i += 2) {
if (fillPoints[i + 1] < 0) {
fillPoints[i + 1] = 0;
}
else if (fillPoints[i + 1] > max) {
max = fillPoints[i + 1];
}
}
paint.setStyle(Style.FILL);
if (renderer.isGradientEnabled()) {
Shader s = paint.getShader();
// Create gradient from values set in renderer
paint.setShader(new LinearGradient(0, (float)(renderer.getGradientStartValue() * max), 0, (float)(renderer.getGradientStopValue() * max),
renderer.getGradientStartColor(), renderer.getGradientStopColor(), Shader.TileMode.MIRROR));
drawPath(canvas, fillPoints, paint, true);
// Restore paint's shader to previous value (null)
paint.setShader(s);
}
else {
paint.setColor(renderer.getFillBelowLineColor());
drawPath(canvas, fillPoints, paint, true);
}
}
paint.setColor(seriesRenderer.getColor());
paint.setStyle(Style.STROKE);
drawPath(canvas, points, paint, false);
paint.setStrokeWidth(lineWidth);
}
如果您想使用原始粘贴数据,请使用以下代码:
@Override
public void drawSeries(Canvas canvas, Paint paint, float[] points,
SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {
int length = points.length;
XYSeriesRenderer renderer = (XYSeriesRenderer) seriesRenderer;
float lineWidth = paint.getStrokeWidth();
paint.setStrokeWidth(renderer.getLineWidth());
if (renderer.isFillBelowLine() || renderer.isGradientEnabled()) {
int pLength = points.length;
float[] fillPoints = new float[pLength + 4];
System.arraycopy(points, 0, fillPoints, 0, length);
fillPoints[0] = points[0] + 1;
fillPoints[length] = fillPoints[length - 2];
fillPoints[length + 1] = yAxisValue;
fillPoints[length + 2] = fillPoints[0];
fillPoints[length + 3] = fillPoints[length + 1];
float max = 0;
for (int i = 0; i < length + 4; i += 2) {
if (fillPoints[i + 1] < 0) {
fillPoints[i + 1] = 0;
}
else if (fillPoints[i + 1] > max) {
max = fillPoints[i + 1];
}
}
paint.setStyle(Style.FILL);
if (renderer.isGradientEnabled()) {
Shader s = paint.getShader();
// Create gradient from values set in renderer
paint.setShader(new LinearGradient(0, (float)(renderer.getGradientStartValue() * max), 0, (float)(renderer.getGradientStopValue() * max),
renderer.getGradientStartColor(), renderer.getGradientStopColor(), Shader.TileMode.MIRROR));
drawPath(canvas, fillPoints, paint, true);
// Restore paint's shader to previous value (null)
paint.setShader(s);
}
else {
paint.setColor(renderer.getFillBelowLineColor());
drawPath(canvas, fillPoints, paint, true);
}
}
paint.setColor(seriesRenderer.getColor());
paint.setStyle(Style.STROKE);
drawPath(canvas, points, paint, false);
paint.setStrokeWidth(lineWidth);
}
如果有帮助,请告诉我。或者你已经解决了它,然后让我知道你是怎么做到的。