设置BarChart高度为screenHeight后为何从屏幕中部而非顶部起始?
Let's break down what's going on here and fix that annoying misplaced chart issue.
The Problem
You've set your Tabs component's preferred height to match the screen height, but the BarChart inside it starts halfway down the screen instead of aligning to the top. Even your component inspector shows no extra spacing, which makes this tricky to pin down at first glance.
Your Relevant Code
First, here's the code you shared for context:
Tabs Initialization
Tabs chartTabs = new Tabs(); chartTabs.setPreferredH(screenHeight); chartTabs.addTab("", icon1, new BarChartGenerator().getBarChart(dealersNames, dealersValues));
BarChart Generation
public Component getBarChart(ArrayList<ArrayList<String>> nameList, ArrayList<ArrayList<Double>> valueList) { String[] titles = new String[]{"2017"}; XYMultipleSeriesRenderer renderer = buildBarRenderer(colors); renderer.setOrientation(Orientation.VERTICAL); double largeValue = getMax(valueList.get(0)) * 1.1; int nameSize = 0; for (ArrayList<String> nameArray : nameList) { int index = 0; nameSize = nameArray.size(); for (String name : nameArray) { renderer.addXTextLabel(index + 1, name); index++; } } int endXValue = (int) (nameSize + 1); setChartSettings(renderer, "", "", "", 0, endXValue, 0, largeValue, ColorUtil.GREEN, ColorUtil.GREEN); renderer.setXLabels(1); renderer.setYLabels(8); initRendererer(renderer); int length = renderer.getSeriesRendererCount(); for (int i = 0; i < length; i++) { XYSeriesRenderer seriesRenderer = (XYSeriesRenderer) renderer.getSeriesRendererAt(i); seriesRenderer.setDisplayChartValues(true); } renderer.setXLabelsAlign(Component.RIGHT); renderer.setXLabelsAngle(0); renderer.setShowTickMarks(true); BarChart chart = new BarChart(buildBarDataset2(titles, valueList), renderer, Type.DEFAULT) { @Override public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) { super.draw(canvas, width / 4, y, width, height, paint); } }; return newChart(chart); }
Likely Causes & Fixes
Let's go through the most probable issues and how to fix them:
Renderer Margins in
initRendererer
TheinitRendererer(renderer)method might be setting large top margins on your chart renderer. If it calls something likerenderer.setMargins(new int[]{200, 50, 50, 50}), that 200px top margin would push your chart down to the middle of the screen.
Fix: Open theinitRendererermethod and either remove the top margin entirely or set it to a small value (like 0 or 10 for padding).Layout of Tabs or Its Parent Container
If yourchartTabsis added to a parent container using a layout that centers components (likeFlowLayout.CENTERorBorderLayout.CENTER), the entire Tabs component will be centered vertically, making the chart look like it starts in the middle.
Fix: Add the Tabs to the top position of its parent container. For example, if using BorderLayout:parentContainer.add(chartTabs, BorderLayout.NORTH);Also, ensure the Tabs itself uses a layout that forces its content to fill the entire space:
chartTabs.setLayout(new BorderLayout());The
newChartWrapper Method
ThenewChart(chart)method might be wrapping your BarChart in a container that uses centered alignment. If it's using something likeFlowLayout.CENTER, the chart will sit in the middle of that container (which takes up the full screen height).
Fix: ModifynewChartto use a layout that pins the chart to the top, like BorderLayout:private Component newChart(BarChart chart) { Container container = new Container(); container.setLayout(new BorderLayout()); container.add(chart, BorderLayout.NORTH); return container; }Or, if possible, just return the
BarChartdirectly instead of wrapping it.BarChart's Preferred Height
If your BarChart's preferred height is smaller than the screen height, and the Tabs layout centers content vertically, the chart will end up in the middle.
Fix: Explicitly set the BarChart's preferred height to match the screen height:BarChart chart = ...; chart.setPreferredH(screenHeight);
Quick Diagnostic Test
As a quick way to narrow things down, temporarily comment out the overridden draw method of your BarChart. That width / 4 x-offset is causing horizontal shifting, but it shouldn't affect vertical position—removing it will help rule out any unintended side effects from that custom draw logic.
内容的提问来源于stack exchange,提问作者tizbn

