我在构建自定义标记时遇到问题,但标记未出现。这是我来自char Activity 的代码。可能是问题出在getEntryForIndex(index).icon
吗?
这是我的主要 Activity
class Main2Activity : AppCompatActivity(), OnChartValueSelectedListener {
lateinit var dataSet : LineDataSet
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/**
* Get the first year value from the API and to the logic here for each here
*/
var fistYearValue = 9
var secondYearValue = 10
var thirdYearValue = 11
var fourthYearValue = 12
var fifthYearValue = 13
var sixthYearValue = 14
var sevenearValue = 15
var eigthYearValue = 16
val numArr = intArrayOf(fistYearValue, secondYearValue, thirdYearValue, fourthYearValue, fifthYearValue,
sixthYearValue,sevenearValue,eigthYearValue)
val numMap = HashMap<Int, Any>()
/**
* TODO get current year and increment by 8
*/
numMap[fistYearValue] = "2018"
numMap[secondYearValue] = "2019"
numMap[thirdYearValue] = "2020"
numMap[fourthYearValue] = "2021"
numMap[fifthYearValue] = "2022"
numMap[sixthYearValue] = "2023"
numMap[sevenearValue] = "2024"
numMap[eigthYearValue] = "2025"
val entries1 = ArrayList<Entry>()
/**
* Define xAxix variable
*/
var xAxis = linechart.xAxis
/**
* Configure xAxix
*/
xAxis.setDrawGridLines(false)
xAxis.axisLineColor = Color.TRANSPARENT
xAxis.position = XAxis.XAxisPosition.BOTTOM
linechart.setOnChartValueSelectedListener(this)
for (num in numArr)
{
entries1.add(Entry(num.toFloat(), num.toFloat()))
}
dataSet = LineDataSet(entries1, "Numbers")
/**
* Configure data set
*/
dataSet.setDrawFilled(true)
val drawable = ContextCompat.getDrawable(this, R.drawable.chart_background)
dataSet.fillDrawable = drawable
/**
* Hide grid layout
*/
linechart.axisLeft.setDrawGridLines(false)
linechart.axisRight.setDrawGridLines(false)
linechart.isHighlightPerTapEnabled = true
linechart.axisRight.setDrawLabels(false)
linechart.axisRight.isEnabled = false
linechart.axisLeft.setDrawLabels(false)
linechart.axisLeft.isEnabled = false
linechart.legend.isEnabled = false
linechart.description.text = ""
//dataSet.getEntryForIndex(0).icon = getDrawable(R.drawable.ic_group_7)
/**
* Zooming and pinching functions
*/
linechart.setScaleEnabled(false)
linechart.setPinchZoom(false)
linechart.setDrawMarkers(true)
dataSet.setDrawHorizontalHighlightIndicator(false)
dataSet.highLightColor = Color.parseColor("#4e4e4e")
dataSet.color = Color.BLACK
dataSet.setDrawCircles(false)
dataSet.setDrawValues(false)
val data = LineData(dataSet)
xAxis = linechart.xAxis
xAxis.valueFormatter = IAxisValueFormatter { value, axis -> numMap.get(value.toInt()).toString() }
linechart.data = data
linechart.invalidate()
}
override fun onNothingSelected() {}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onValueSelected(e: Entry?, h: Highlight?) {
var previousIndex = PreferenceManager.getDefaultSharedPreferences(applicationContext)
.getInt("INDEX", 0)
dataSet.getEntryForIndex(previousIndex).icon = null
//save current position
var index = dataSet.getEntryIndex(e)
PreferenceManager.getDefaultSharedPreferences(applicationContext).edit()
.putInt("INDEX", index).apply()
//set current position
dataSet.getEntryForIndex(index).icon = getDrawable(R.drawable.ic_group_7)
var x = linechart.xAxis.valueFormatter.getFormattedValue(e?.x!!, linechart.xAxis)
var y = linechart.xAxis.valueFormatter.getFormattedValue(e.y, linechart.xAxis)
var mv = MyMarkerView(this, R.layout.marker)
mv.chartView = linechart
}
inner class MyMarkerView(context: Context, layoutResource: Int) : MarkerView(context, layoutResource) {
private val tvContent: TextView = findViewById<TextView>(R.id.tvContent)
override fun refreshContent(e: Entry?, highlight: Highlight?) {
tvContent.text = "3"
}
override fun getX(): Float {
return (-(width / 2)).toFloat()
}
override fun getY(): Float {
return (-height).toFloat()
}
}
}
这是我的marker.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="40dp" >
<TextView
android:id="@+id/tvContent"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:text=""
android:textSize="12dp"
android:textColor="@android:color/black"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
最佳答案
我遇到过同样的问题。我解决了将activity
传递给MyMarkerView
实例,将super.contentRefresh()
传递给MyMarkerView
的问题,然后对xml
进行了一些更改。
所以:pieChart.setMarker(new MyMarkerView(MyActivity.this, R.layout.marker_chart_popup));
MyMarkerView
代码:
公共(public)类MyMarkerView扩展了MarkerView {
private TextView tvContent;
public MyMarkerView (Context context, int layoutResource) {
super(context, layoutResource);
tvContent = (TextView) findViewById(R.id.tvContent);
}
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText("my value " + e.getY()));
super.refreshContent(e, highlight); // <----- IMPORTANT
}
}
和我的自定义布局
marker_chart_popup
:<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="20dp"
android:minWidth="60dp"
android:background="@android:color/black"
>
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="test"
android:textSize="20dp"
android:layout_margin="5dp"
android:textColor="@android:color/white"/>
</RelativeLayout>
我为我工作,希望对您有所帮助。