我创建一个柱状图,但是遇到了一个问题,X轴标签被切断,但在平板电脑上。附件是在手机和平板电脑的布局不正确正确的布局。
有没有人知道为什么这个问题发生在一个平板电脑,以及如何解决它?
手机布局
https://drive.google.com/file/d/ 0B4CxFPSlLEYpdGZCMm8xdGZHTlk /编辑?USP =共享
平板电脑的布局
https://drive.google.com/file/d/ 0B4CxFPSlLEYpakx1WFo3cGhNSTg /编辑?USP =共享
这是我的code创建条形图。
布局文件
<滚动型的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fillViewport =真
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=com.usage.bigpondpro.MainActivity $ PlaceholderFragment
机器人:ID =@ + ID / svDailyContainter>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
< com.androidplot.xy.XYPlot
机器人:ID =@ + ID / dailyXYPlot
机器人:layout_width =match_parent
机器人:layout_height =match_parent
androidPlot.title =样品棒图
androidPlot.titleWidget.labelPaint.textSize =@扪/ title_font_size
androidPlot.graphWidget.marginTop =20dp
androidPlot.graphWidget.marginLeft =10dp
androidPlot.graphWidget.marginBottom =20dp
androidPlot.graphWidget.marginRight =10dp
androidPlot.rangeLabelWidget.labelPaint.textSize =@扪/ range_label_font_size
androidPlot.domainLabelWidget.labelPaint.textSize =@扪/ range_label_font_size
/>
< / LinearLayout中>
< /滚动型>
活动
私人无效CreateGraph()
{
//初始化我们XYPlot参考:
XYPlot剧情=(XYPlot)this.findViewById(R.id.dailyXYPlot);
// 创建
y值的一对夫妇阵列积:
数[] series1Numbers = GenerateGraphValues();
//打开上面的阵列到XYSeries:
XYSeries系列1 =新SimpleXYSeries(
Arrays.asList(series1Numbers),// SimpleXYSeries需要一个列表,以便把我们的数组列表
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,// Y_VALS_ONLY意味着使用元素的索引为x值
系列1); //设置一系列的显示标题
BarFormatter series1Format =新BarFormatter(Color.rgb(51,181,229),Color.TRANSPARENT);
PointLabelFormatter PLF =新PointLabelFormatter();
plf.getTextPaint()setTextSize(18)。
。plf.getTextPaint()setColor(Color.BLACK);
series1Format.setPointLabelFormatter(PLF);
series1Format.setPointLabeler(新PointLabeler(){
DecimalFormat的DF =新的DecimalFormat(## 0.00);
公共字符串getLabel(XYSeries系列,诠释指数){
//需要检查空
如果(series.getY(指数)== NULL)返回;
返回df.format(series.getY(指数));
}
});
//添加一个新的系列到xyplot:
plot.addSeries(系列1,series1Format);
// y轴的配置
plot.setRangeLabel(值); //标签
plot.setRangeBoundaries(0,110,BoundaryMode.FIXED); //规模
plot.setRangeStep(XYStepMode.SUBDIVIDE,5); //脚步
。plot.getGraphWidget()getRangeLabelPaint()setTextSize(26)。 //字体大小
DecimalFormat的NF =新的DecimalFormat(#0);
plot.setRangeValueFormat(NF);
// X AXS配置
plot.setDomainLabel(指数);
plot.setDomainStep(XYStepMode.SUBDIVIDE,25);
// plot.getGraphWidget()setDomainValueFormat(新GraphXLabelFormat());
。plot.getGraphWidget()getDomainLabelPaint()setTextSize(18)。
plot.getGraphWidget()setDomainLabelVerticalOffset(5)。
//其他配置
。plot.getLegendWidget()的setVisible(假); //隐藏传奇
。plot.getGraphWidget()getDomainGridLinePaint()setColor(Color.TRANSPARENT)。 //隐藏网格线
。plot.getGraphWidget()getRangeGridLinePaint()setColor(Color.TRANSPARENT)。 //隐藏网格线
plot.getGraphWidget()setGridPaddingLeft(40)。 //给一些填充
plot.getGraphWidget()setGridPaddingRight(40)。 //给一些填充
。plot.getGraphWidget()getGridBackgroundPaint()setColor(Color.WHITE)。 //背景颜色
plot.getTitleWidget()setPaddingTop(10)。 //给一些填充
//设置条宽
BarRenderer<>渲染=(BarRenderer<>)plot.getRenderer(BarRenderer.class);
renderer.setBarWidth(25);
// x更改吊牌方向
。plot.getGraphWidget()setDomainLabelOrientation(-90);
//设置图形的高度和宽度
/ **
出于某种原因,当设备处于风景模式的整个图形画布是空白的。
经过反复试验,如果在横向模式下,屏幕宽度由0.96乘以图和所有它的数据再次出现.....为什么???
** /
DisplayMetrics displaymetrics =新DisplayMetrics();
((窗口管理器)this.getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()getMetrics(displaymetrics)。
INT宽度=(int)的(displaymetrics.widthPixels * 0.96);
//默认为纵向
的LayoutParams LP =新的LayoutParams(LayoutParams.MATCH_PARENT,750);
//需要检查orienation
如果(IsLandscapeOrientation())
{
LP =新的LayoutParams(宽,560);
}
plot.setLayoutParams(LP);
}
私人号码[] GenerateGraphValues()
{
INT分钟= 30;
INT最大= 100;
诠释PlotPoints = 25;
号码[] series1Numbers =新号码[PlotPoints]
为(中间体X = 0 X - 其中; PlotPoints; X ++){
随机R =新的随机();
INT randomNumber = r.nextInt(最大 - 最小+ 1)+分钟;
series1Numbers [X] = randomNumber;
}
返回series1Numbers;
}
私人布尔IsLandscapeOrientation()
{
显示显示=((窗口管理器)this.getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()。
INT旋转= display.getRotation();
布尔IsLandscape;
开关(旋转){
案例Surface.ROTATION_0:
案例Surface.ROTATION_180:
IsLandscape = FALSE;
打破;
案例Surface.ROTATION_90:
案例Surface.ROTATION_270:
IsLandscape = TRUE;
打破;
默认:
IsLandscape = TRUE;
打破;
}
返回IsLandscape;
}
解决方案
这个问题似乎是在图中的小部件底部边缘空间不足。如果您添加到您的阴谋的XML:
androidPlot.graphWidget.marginBottom =100dp
那么问题应该消失。 100dp可能是矫枉过正,所以你需要拨打它,但你的想法:)
I'm creating a bar chart but have run into an issue with the x-axis labels being cut off on a tablet. Attached is the correct layout on a phone and the incorrect layout on a tablet.
Does any one know why this issue occurs on a tablet and how to fix it?
Phone layout
https://drive.google.com/file/d/0B4CxFPSlLEYpdGZCMm8xdGZHTlk/edit?usp=sharing
Tablet layout
https://drive.google.com/file/d/0B4CxFPSlLEYpakx1WFo3cGhNSTg/edit?usp=sharing
This is my code to create the bar chart.
Layout file
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.usage.bigpondpro.MainActivity$PlaceholderFragment"
android:id="@+id/svDailyContainter" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.androidplot.xy.XYPlot
android:id="@+id/dailyXYPlot"
android:layout_width="match_parent"
android:layout_height="match_parent"
androidPlot.title="Sample Bar Graph"
androidPlot.titleWidget.labelPaint.textSize="@dimen/title_font_size"
androidPlot.graphWidget.marginTop="20dp"
androidPlot.graphWidget.marginLeft="10dp"
androidPlot.graphWidget.marginBottom="20dp"
androidPlot.graphWidget.marginRight="10dp"
androidPlot.rangeLabelWidget.labelPaint.textSize="@dimen/range_label_font_size"
androidPlot.domainLabelWidget.labelPaint.textSize="@dimen/range_label_font_size"
/>
</LinearLayout>
</ScrollView>
Activity
private void CreateGraph()
{
// initialize our XYPlot reference:
XYPlot plot = (XYPlot)this.findViewById(R.id.dailyXYPlot);
// Create
a couple arrays of y-values to plot:
Number[] series1Numbers = GenerateGraphValues();
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), // SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
"Series1"); // Set the display title of the series
BarFormatter series1Format = new BarFormatter(Color.rgb(51, 181, 229), Color.TRANSPARENT);
PointLabelFormatter plf = new PointLabelFormatter();
plf.getTextPaint().setTextSize(18);
plf.getTextPaint().setColor(Color.BLACK);
series1Format.setPointLabelFormatter(plf);
series1Format.setPointLabeler(new PointLabeler() {
DecimalFormat df = new DecimalFormat("##0.00");
public String getLabel(XYSeries series, int index) {
//need to check for null
if(series.getY(index) == null) return "";
return df.format(series.getY(index));
}
});
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
//Y axis config
plot.setRangeLabel("Values"); //label
plot.setRangeBoundaries(0, 110, BoundaryMode.FIXED); //scale
plot.setRangeStep(XYStepMode.SUBDIVIDE, 5); //steps
plot.getGraphWidget().getRangeLabelPaint().setTextSize(26); //font size
DecimalFormat nf = new DecimalFormat("#0");
plot.setRangeValueFormat(nf);
//X Axs config
plot.setDomainLabel("Indexes");
plot.setDomainStep(XYStepMode.SUBDIVIDE, 25);
// plot.getGraphWidget().setDomainValueFormat(new GraphXLabelFormat());
plot.getGraphWidget().getDomainLabelPaint().setTextSize(18);
plot.getGraphWidget().setDomainLabelVerticalOffset(5);
//other config
plot.getLegendWidget().setVisible(false); //hide legend
plot.getGraphWidget().getDomainGridLinePaint().setColor(Color.TRANSPARENT); //hide grid lines
plot.getGraphWidget().getRangeGridLinePaint().setColor(Color.TRANSPARENT); //hide grid lines
plot.getGraphWidget().setGridPaddingLeft(40); //give some padding
plot.getGraphWidget().setGridPaddingRight(40); //give some padding
plot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE); //background color
plot.getTitleWidget().setPaddingTop(10); //give some padding
//set bar width
BarRenderer<?> renderer = (BarRenderer<?>) plot.getRenderer(BarRenderer.class);
renderer.setBarWidth(25);
//change x lable orientation
plot.getGraphWidget().setDomainLabelOrientation(-90);
//set graph height and width
/**
For some reason when the device is in landscape mode the entire graph canvas is blank.
Through trial and error if in landscape mode and the screen width is multiplied by 0.96 the graph and all it's data appear again.....why???
**/
DisplayMetrics displaymetrics = new DisplayMetrics();
((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displaymetrics);
int Width = (int) (displaymetrics.widthPixels * 0.96);
//default to portrait
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 750);
//need to check the orienation
if(IsLandscapeOrientation())
{
lp = new LayoutParams(Width, 560);
}
plot.setLayoutParams(lp);
}
private Number[] GenerateGraphValues()
{
int min = 30;
int max = 100;
int PlotPoints = 25;
Number[] series1Numbers = new Number[PlotPoints];
for(int x = 0; x < PlotPoints; x++){
Random r = new Random();
int randomNumber = r.nextInt(max - min + 1) + min;
series1Numbers[x] = randomNumber;
}
return series1Numbers;
}
private boolean IsLandscapeOrientation()
{
Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
boolean IsLandscape;
switch (rotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_180:
IsLandscape = false;
break;
case Surface.ROTATION_90:
case Surface.ROTATION_270:
IsLandscape = true;
break;
default:
IsLandscape = true;
break;
}
return IsLandscape;
}
解决方案
The problem appears to be insufficient space in the graph widgets bottom margin. If you add this to your plot's XML:
androidPlot.graphWidget.marginBottom="100dp"
Then the problem should go away. 100dp is probably overkill so you'll need to dial it in but you get the idea :)