1.我的条形图有一个系列。
2.在系列中有10个值。
3.当显示图表时,它有10个条,但所有条形图都是相同的颜色
4.如何画出不同颜色的条纹?
我试着这样做,但它不起作用
class MyBarRenderer extends BarRenderer<MyBarFormatter> {
public MyBarRenderer(XYPlot plot) {
super(plot);
}
protected BarFormatter getFormatter(int index, XYSeries series) {
//return getFormatter(series);
if(index % 2 == 1) {
return new MyBarFormatter(Color.BLUE, Color.TRANSPARENT);
} else {
return new MyBarFormatter(Color.RED, Color.TRANSPARENT);
}
}
}
答案 0 :(得分:3)
我的代码与您的代码几乎完全相同,并且对我有用。这是我的其余代码:
class MyBarFormatter extends BarFormatter
{
public MyBarFormatter(int fillColor, int borderColor)
{
super(fillColor, borderColor);
}
@Override public Class<? extends SeriesRenderer> getRendererClass()
{
return MyBarRenderer.class;
}
@Override public SeriesRenderer getRendererInstance(XYPlot plot)
{
return new MyBarRenderer(plot);
}
}
class MyBarRenderer extends BarRenderer<MyBarFormatter>
{
public MyBarRenderer(XYPlot plot)
{
super(plot);
}
public MyBarFormatter getFormatter(int index, XYSeries series)
{
// return getFormatter(series);
if(index % 2 == 1)
{
return new MyBarFormatter(Color.BLUE, Color.TRANSPARENT);
}
else
{
return new MyBarFormatter(Color.RED, Color.TRANSPARENT);
}
}
}
要使用这两个类,我调用以下代码:
// Create an array of y-values to plot:
Number[] values = { 380, 1433, 1965, 3200, 3651, 3215, 3217, 1000, 500, 4300, 3000, 2100 };
// Turn the above array into XYSeries':
XYSeries series1 = new SimpleXYSeries(Arrays.asList(values), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1");
// Add a new series to the xyplot:
MyBarFormatter formatter1 = new MyBarFormatter(Color.argb(200, 100, 150, 100), Color.LTGRAY);
mySimpleXYPlot.addSeries(series1, formatter1);
// Give each bar a fixed width
MyBarRenderer renderer = ((MyBarRenderer) mySimpleXYPlot.getRenderer(MyBarRenderer.class));
renderer.setBarWidthStyle(BarRenderer.BarWidthStyle.FIXED_WIDTH);
renderer.setBarWidth(50);
这是它的样子,在两种不同的颜色之间交替出现:
另外,要添加虚线水平线:
YValueMarker yValueMarker = new YValueMarker(2300, "", new XPositionMetric(PixelUtils.dpToPix(0), XLayoutStyle.ABSOLUTE_FROM_RIGHT), Color.BLACK, Color.BLACK);
DashPathEffect dashPathEffect = new DashPathEffect(new float[] { PixelUtils.dpToPix(16), PixelUtils.dpToPix(8) }, 0);
yValueMarker.getLinePaint().setPathEffect(dashPathEffect);
yValueMarker.getLinePaint().setStrokeWidth(PixelUtils.dpToPix(1));
yValueMarker.getLinePaint().setColor(Color.BLACK);
addMarker(yValueMarker);