gpt4 book ai didi

android - 使用 achartengine 的条形图

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:10 25 4
gpt4 key购买 nike

我想绘制一个条形图,其中包含五个单独的条形图 - 我使用了 Achartengine。我能够以相同的颜色显示所有五个条,但我想用不同的颜色来区分一个条,但我不能显示超过一种颜色。请告诉我如何显示不同的颜色。我的代码...

         values.add(new double[] {21,56,33,10,20});         
int[] colors = new int[] { Color.rgb(227, 121, 15) };
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
setChartSettings(renderer, "", "", "", 0,5, 0,100, Color.WHITE, Color.WHITE);
renderer.setXLabels(8);
renderer.setYLabels(10);
renderer.setDisplayChartValues(true);
mChartView= ChartFactory.getBarChartView(context, buildBarDataset(titles, values), renderer,
Type.DEFAULT);
layout.addView(mChartView, 350, 500);

最佳答案

可以通过扩展 SimpleSeriesRenderer 和 BarChart 类来实现。这是我对 RangeBarChart 的解决方案(感谢 gilenodm,希望我有一些声誉来支持你的回答):

import org.achartengine.renderer.SimpleSeriesRenderer;
public class AdvancedSeriesRenderer extends SimpleSeriesRenderer
{
private int [] colors;

public void AdvancedSeriesRenderer ()
{
}

public int getColor ( int position )
{
return colors[position];
}
}

import org.achartengine.chart.RangeBarChart;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;

public class AdvancedRangeBarChart extends RangeBarChart
{
private int [] barChartColors;

public AdvancedRangeBarChart ( XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, Type type )
{
super ( dataset, renderer, type );
}

public void setColors ( int [] colorsIn )
{
barChartColors = colorsIn;
}

@Override
public void drawSeries ( Canvas canvas, Paint paint, float [] points, SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex )
{
int seriesNr = mDataset.getSeriesCount ();
int length = points.length;
paint.setStyle ( Style.FILL );
float halfDiffX = getHalfDiffX ( points, length, seriesNr );
int start = 0;
if ( startIndex > 0 )
{
start = 2;
}
for ( int i = start; i < length; i += 4 )
{
int colorIndex = (int) ( i / 4 ) % barChartColors.length;
paint.setColor ( barChartColors[colorIndex] );
if ( points.length > i + 3 )
{
float xMin = points[i];
float yMin = points[i + 1];
// xMin = xMax
float xMax = points[i + 2];
float yMax = points[i + 3];
drawBar ( canvas, xMin, yMin, xMax, yMax, halfDiffX, seriesNr, seriesIndex, paint );
}
}
}
}

关于android - 使用 achartengine 的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5405858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com