gpt4 book ai didi

java - MPChartAndroid 库不显示标记 View

转载 作者:行者123 更新时间:2023-11-29 23:24:52 25 4
gpt4 key购买 nike

我遇到了一个问题,我正在构建一个自定义标记,但该标记没有出现。这是我在 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 实例,在我的 MyMarkerView 中传递 super.contentRefresh() 然后我在 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>

我为我工作,希望对你有帮助。

关于java - MPChartAndroid 库不显示标记 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53752205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com