Tôi đang sử dụng MPAndroidChart library. Có ai có vấn đề này không? Khi tôi đặt các nhãn ở vị trí BOTTOM, chúng được cắt.MPAndroidChart - Nhãn chú giải đang bị cắt
Cảm ơn bạn
Tôi đang sử dụng MPAndroidChart library. Có ai có vấn đề này không? Khi tôi đặt các nhãn ở vị trí BOTTOM, chúng được cắt.MPAndroidChart - Nhãn chú giải đang bị cắt
Cảm ơn bạn
Chúng được cắt bởi vì văn bản của bạn quá dài và thư viện không hỗ trợ "gói" của nhãn cho một dòng mới.
Bạn sẽ phải rút ngắn nhãn chú thích hoặc tự mình thực hiện chức năng mong muốn.
Bạn phải thực hiện huyền thoại tùy chỉnh với truyền thuyết màu sắc của họ và lables bởi bước sau Bước 1
Truyền thuyết huyền thoại = mChart.getLegend();
Bước 2
int colorcodes [] = legend.Colors();
bước 3
for (int i = 0;. I < legend.Colors() chiều dài-1; i ++) { ..... ..... }
Các bước 4
Sau đó, bạn phải thực hiện một bố cục ngang hoặc dọc. Bạn phải có được mã màu huyền thoại và truyền thuyết lable và theo truyền thuyết chiều dài tạo bố trí và nhãn. mẫu mã được đưa ra dưới đây
LinearLayout.LayoutParams parms_left_layout = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
parms_left_layout.weight = 1F;
LinearLayout left_layout = new LinearLayout(context);
left_layout.setOrientation(LinearLayout.HORIZONTAL);
left_layout.setGravity(Gravity.CENTER);
left_layout.setLayoutParams(parms_left_layout);
LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
20, 20);
parms_legen_layout.setMargins(0, 0, 20, 0);
LinearLayout legend_layout = new LinearLayout(context);
legend_layout.setLayoutParams(parms_legen_layout);
legend_layout.setOrientation(LinearLayout.HORIZONTAL);
legend_layout.setBackgroundColor(colorcodes[i]);
left_layout.addView(legend_layout);
TextView txt_unit = new TextView(context);
txt_unit.setText(legend.getLabel(i));
Hy vọng điều này sẽ giúp bạn
Ở đây tôi sẽ chỉ cho bạn một cách dễ dàng bằng cách "truyền thống Android Way", nó khá đơn giản, mã của tôi là dưới đây:
<LinearLayout
android:id="@+id/i_am_chart_view_container"
...
android:paddingRight="20dp"
android:clipChildren="false"
android:clipToPadding="false"
.../>
Chỉ cần thêm một số padding
vào bố cục vùng chứa hoặc một số margin
vào chế độ xem biểu đồ và cuối cùng đặt clipChildren
& clipToPadding
thành sai.
Kết quả dưới đây:
khu vực màu xanh là đệm hoặc lề khu vực.
này có vẻ là một mới feature kể từ tháng Sáu (2015):
chart.getLegend().setWordWrapEnabled(true);
Javadoc:
/**
* Should the legend word wrap?/this is currently supported only for:
* BelowChartLeft, BelowChartRight, BelowChartCenter./note that word
* wrapping a legend takes a toll on performance./you may want to set
* maxSizePercent when word wrapping, to set the point where the text wraps.
*/default: false
*
* @param enabled
*/
public void setWordWrapEnabled(boolean enabled) {
mWordWrapEnabled = enabled;
}
Legend l = pieChart.getLegend();
l.setPosition(LegendPosition.BELOW_CHART_LEFT);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);
l.setDirection(LegendDirection.LEFT_TO_RIGHT);
l.setWordWrapEnabled(true);
Để tránh clipping của huyền thoại đánh giá cao sử dụng khối mã sau đây
Legend legend=lineChart.getLegend();
legend.setWordWrapEnabled(true);
Doc (đối với huyền thoại):
/**
* Should the legend word wrap?/this is currently supported only for:
* BelowChartLeft, BelowChartRight, BelowChartCenter./note that word
* wrapping a legend takes a toll on performance./you may want to set
* maxSizePercent when word wrapping, to set the point where the text wraps.
*/default: false
*
* @param enabled
*/
public void setWordWrapEnabled(boolean enabled) {
mWordWrapEnabled = enabled;
}
Để tránh cắt nhãn trục x sử dụng
XAxis xAxis = lineChart.getXAxis();
xAxis.setAvoidFirstLastClipping(true);
Doc (đối với nhãn trục x):
/**
* if set to true, the chart will avoid that the first and last label entry
* in the chart "clip" off the edge of the chart or the screen
*
* @param enabled
*/
public void setAvoidFirstLastClipping(boolean enabled) {
mAvoidFirstLastClipping = enabled;
}
Cảm ơn, công trình này, nhưng giá trị điểm dữ liệu đầu tiên và cuối cùng cũng bị cắt, bất kỳ giải pháp nào? – erical
@erical Bạn có thể vui lòng cung cấp ảnh chụp màn hình cho sự cố của mình không? –
Set hỗ trợ nội dung quấn chỉ Legends postion khi vị trí huyền thoại được BelowChartLeft, BelowChartRight, BelowChartCenter
Legend legend = pieChart.getLegend();
legend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
legend.setWordWrapEnabled(true);
Sau bộ tập dữ liệu này
pieChart.setData(pieData)
nó sẽ làm việc tốt
Sau một chặng đường dài nghiên cứu, tôi đã tìm ra giải pháp. Đoạn mã dưới giải quyết nó.
chart.getLegend(). SetWordWrapEnabled (true);
Cảm ơn bạn đã trả lời. – rguerra