Android SDK下ColumnChart使用PaletteProviderBase设置strokeColor异常
Hey there! Let’s break down the stroke color problems you’re seeing with your ColumnChart when using a transparent RenderSurface—this looks like a common pitfall when combining custom palette providers and transparent backgrounds in charting SDKs (I’m assuming this is SciChart given the classes you’re using).
What’s Likely Causing the Problem?
Looking at your code, I spot two key issues driving the abnormal behavior:
- Shared Color Array for Fill and Stroke: Your
ColumnsPaletteProvideruses the sameIntegerValuesinstance for both fill and stroke colors. When you set a transparent color for fill, the stroke also becomes transparent—explaining why some bars lose their stroke entirely. - Incomplete Transparent RenderSurface Setup: Just setting
setBackgroundColor(Color.TRANSPARENT)isn’t enough for proper transparent rendering in SurfaceView-based components. Missing pixel format and Z-order configurations can mess with how strokes are composited onto the background.
Step-by-Step Fixes
1. Separate Fill and Stroke Color Arrays
First, split the color storage so fill and stroke colors are independent. This way, you can control each one separately without unwanted side effects.
Modified ColumnsPaletteProvider code:
private class ColumnsPaletteProvider extends PaletteProviderBase<FastColumnRenderableSeries> implements IFillPaletteProvider, IStrokePaletteProvider { private final IntegerValues fillColors = new IntegerValues(); private final IntegerValues strokeColors = new IntegerValues(); private final int[] desiredFillColors; // Add dedicated stroke colors (adjust these to your needs) private final int[] desiredStrokeColors; protected ColumnsPaletteProvider() { super(FastColumnRenderableSeries.class); desiredFillColors = new int[]{Color.RED, Color.GREEN, Color.TRANSPARENT}; // Set stroke colors—here we use solid versions of fill colors, no transparent desiredStrokeColors = new int[]{Color.RED, Color.GREEN, Color.GRAY}; } @Override public void update() { ColumnRenderPassData passData = (ColumnRenderPassData) this.renderableSeries.getCurrentRenderPassData(); final int size = passData.pointsCount(); fillColors.setSize(size); strokeColors.setSize(size); // Initialize stroke color array size XyzDataSeries<Date, Double, Double> series = (XyzDataSeries<Date, Double, Double>) renderableSeries.getDataSeries(); int startIndex = passData.xPointRange.getMin(); int dataCount = series.getCount(); final int[] fillColorsArray = fillColors.getItemsArray(); final int[] strokeColorsArray = strokeColors.getItemsArray(); for (int i = 0; i < size; i++) { int index = startIndex + i; if (index >= dataCount) { fillColorsArray[i] = desiredFillColors[2]; strokeColorsArray[i] = desiredStrokeColors[2]; // Use a visible stroke for out-of-range if needed } else { double direction = series.getZValues().get(index); if (direction == 1) { fillColorsArray[i] = desiredFillColors[0]; strokeColorsArray[i] = desiredStrokeColors[0]; } else { fillColorsArray[i] = desiredFillColors[1]; strokeColorsArray[i] = desiredStrokeColors[1]; } } } } @Override public IntegerValues getFillColors() { return fillColors; } @Override public IntegerValues getStrokeColors() { return strokeColors; } }
2. Properly Configure Transparent RenderSurface
Adjust your RenderSurface setup to include the correct pixel format and Z-order, which ensures transparent rendering works as expected without breaking stroke compositing:
RenderSurface renderSurface = new RenderSurface(surface.getContext()); renderSurface.setBackgroundColor(Color.TRANSPARENT); // Enable proper transparent rendering for SurfaceView SurfaceView surfaceView = renderSurface.getSurfaceView(); surfaceView.setZOrderOnTop(true); surfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT); mSurface.setRenderSurface(renderSurface);
3. Verify Stroke Width is Set
Double-check that your FastColumnRenderableSeries has a non-zero stroke width set, otherwise even correct colors won’t be visible:
FastColumnRenderableSeries columnSeries = new FastColumnRenderableSeries(); columnSeries.setStrokeWidth(2f); // Adjust to your desired thickness columnSeries.setPaletteProvider(new ColumnsPaletteProvider());
Why This Works
- Independent Color Arrays: Separating fill and stroke colors lets you control transparency for each independently—no more accidental transparent strokes when you want transparent fills.
- Correct Surface Configuration: Setting
PixelFormat.TRANSLUCENTandZOrderOnTop(true)ensures the SurfaceView properly composites the chart onto your transparent background without rendering artifacts.
内容的提问来源于stack exchange,提问作者Rey

