所以我希望能夠在條形圖中選擇一個條形,當我選擇一個條形時,它會改變條形的顏色(我知道該怎麼做),而且還要改變相應的x軸的顏色,軸標籤。有沒有辦法做到這一點,如果有的話,有人可以幫我嗎?MPAndroidChart:我可以爲X軸標籤設置不同的顏色嗎?
回答
是的,可以爲xAxis標籤設置不同的顏色。你將不得不使用一個自定義渲染,類似下面:
import android.graphics.Canvas;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.renderer.XAxisRenderer;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.util.Collections;
import java.util.List;
/**
* Created by rawsond on 29/01/17.
*/
public class ColoredLabelXAxisRenderer extends XAxisRenderer {
List<Integer> labelColors;
public ColoredLabelXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) {
super(viewPortHandler, xAxis, trans);
labelColors = Collections.EMPTY_LIST;
}
public ColoredLabelXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans, List<Integer> colors) {
super(viewPortHandler, xAxis, trans);
this.labelColors = colors;
}
@Override
protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled();
float[] positions = new float[mXAxis.mEntryCount * 2];
for (int i = 0; i < positions.length; i += 2) {
// only fill x values
if (centeringEnabled) {
positions[i] = mXAxis.mCenteredEntries[i/2];
} else {
positions[i] = mXAxis.mEntries[i/2];
}
}
mTrans.pointValuesToPixel(positions);
for (int i = 0; i < positions.length; i += 2) {
float x = positions[i];
if (mViewPortHandler.isInBoundsX(x)) {
String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i/2], mXAxis);
int color = getColorForXValue(mXAxis.mEntries[i/2]); //added
mAxisLabelPaint.setColor(color);
if (mXAxis.isAvoidFirstLastClippingEnabled()) {
// avoid clipping of the last
if (i == mXAxis.mEntryCount - 1 && mXAxis.mEntryCount > 1) {
float width = Utils.calcTextWidth(mAxisLabelPaint, label);
if (width > mViewPortHandler.offsetRight() * 2
&& x + width > mViewPortHandler.getChartWidth())
x -= width/2;
// avoid clipping of the first
} else if (i == 0) {
float width = Utils.calcTextWidth(mAxisLabelPaint, label);
x += width/2;
}
}
drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees);
}
}
}
private int getColorForXValue(int index) {
if (index >= labelColors.size()) return mXAxis.getTextColor();
if (index < 0) return mXAxis.getTextColor();
return labelColors.get(index);
}
}
消耗它是這樣的:
mChart.setXAxisRenderer(new ColoredLabelXAxisRenderer(mChart.getViewPortHandler(), mChart.getXAxis(), mChart.getTransformer(AxisDependency.LEFT), colors));
其中colors
是解決顏色的List<Integer>
(不是資源ID)相同大小的IDataSet
中的條目數。既然您已經知道如何更改突出顯示欄上的酒吧的顏色,該部分取決於您。就像你平常一樣操縱colors
。下面是一個示例輸出:
可能相關http://stackoverflow.com/q/43443787 –
非常有幫助!對不起,需要很長時間來驗證它。 – rohan
@rohan謝謝!我很高興這對你很有幫助 –
- 1. BalloonTip可以爲標題設置不同的顏色嗎?
- 2. 在Google條形圖中,我可以設置不同的單軸標籤嗎?
- 3. MATLAB x軸標籤設置爲載體
- 4. 爲同一軸上的抽搐設置不同的顏色
- 5. 圖表js - 爲每個x軸標籤應用不同的顏色
- 6. 如何在同一個標籤中設置不同的顏色?
- 7. 將顏色設置爲標籤
- 8. 使用NSString爲標籤設置顏色
- 9. 爲複選框標籤設置顏色
- 10. 設置joyplot的x軸標籤
- 11. 設置Highcharts x軸標籤的寬度
- 12. 我可以設置不同的背景顏色意見頁dynamicaly
- 13. Matplotlib - 根據它們在x軸上的位置設置不同的顏色
- 14. 標籤顏色不同
- 15. Recharts:設置X軸標籤空白
- 16. 我們可以設置背景圖片爲標籤嗎?
- 17. 人力車:改變X軸標籤的顏色
- 18. MPAndroidChart x軸的日期/時間標籤格式化
- 19. mpandroidchart縮小時的X軸標籤密度
- 20. 我可以爲特殊配置獲取顏色資源的顏色嗎?
- 21. Matplotlib:多種顏色的y軸標籤
- 22. FLEX:我可以設置標籤的可視性嗎?
- 23. 我可以在顏色主題中設置rgba顏色
- 24. 如何爲MPAndroidChart中的值設置顏色?
- 25. 我可以在我的HTML標籤上設置屬性嗎?
- 26. 標籤x軸
- 27. 我可以在UITableView中添加不同的文字顏色嗎?
- 28. Highcharts列類型的可視化:爲同一X軸標籤
- 29. 我不能使用jquery爲標籤標籤設置html嗎?
- 30. 更改標籤顏色Y和X軸chart.js
請參閱這些[#139](https://github.com/PhilJay/MPAndroidChart/issues/319)和[#387](HTTPS:// github上。 com/danielgindi/Charts/issues/387)的問題。另外,看看這些問題在這裏:http://stackoverflow.com/questions/28632489/mpandroidchart-how-to-set-label-color和在這裏:http://stackoverflow.com/questions/29888850/mpandroidchart-set-不同顏色對杆在-A-柱形圖基於-ON-y軸值。我期望他們的組合能幫助你獲得它(我不知道它是否真的可行,但也許你可以用這些數據做一些解決方法)。 –
@ Error404這些鏈接都不能幫助您提出問題 –