Skip to content

Commit

Permalink
Apply necessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed May 12, 2024
1 parent 041dbe7 commit d57df34
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,68 +155,78 @@ class DataTools {
values.add(Entry(i.toFloat(), value, ContextCompat.getDrawable(context, R.drawable.star)))
}
}
val lineDataSet0: LineDataSet
if (lineChart.data != null && lineChart.data.dataSetCount > 0) {
lineDataSet0 = lineChart.data.getDataSetByIndex(0) as LineDataSet
lineDataSet0.entries = values
lineDataSet0.notifyDataSetChanged()
lineChart.data.notifyDataChanged()
lineChart.notifyDataSetChanged()
} else {
// create a dataset and give it a type
lineDataSet0 = LineDataSet(values, "DataSet 1")
lineDataSet0.setDrawIcons(false)

// draw dashed line
lineDataSet0.enableDashedLine(10f, 5f, 0f)

// black lines and points
lineDataSet0.color = Color.BLACK
lineDataSet0.setCircleColor(Color.BLACK)

// line thickness and point size
lineDataSet0.lineWidth = 1f
lineDataSet0.circleRadius = 3f

// draw points as solid circles
lineDataSet0.setDrawCircleHole(false)

// customize legend entry
lineDataSet0.formLineWidth = 1f
lineDataSet0.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
lineDataSet0.formSize = 15f

// text size of values
lineDataSet0.valueTextSize = 9f

// draw selection line as dashed
lineDataSet0.enableDashedHighlightLine(10f, 5f, 0f)
lineChart.data?.let {
if (it.dataSetCount > 0) {
val lineDataSet0 = it.getDataSetByIndex(0) as LineDataSet
lineDataSet0.entries = values
lineDataSet0.notifyDataSetChanged()
it.notifyDataChanged()
lineChart.notifyDataSetChanged()
} else
createDataset(values, lineChart, context)
} ?: run {
createDataset(values, lineChart, context)
}
}

// set the filled area
lineDataSet0.setDrawFilled(true)
lineDataSet0.fillFormatter = object : IFillFormatter {
override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float {
return lineChart.axisLeft.axisMinimum
}
private fun createDataset(
values: ArrayList<Entry>,
lineChart: LineChart,
context: Context
) {
// create a dataset and give it a type
val lineDataSet01 = LineDataSet(values, "DataSet 1")
lineDataSet01.setDrawIcons(false)

// draw dashed line
lineDataSet01.enableDashedLine(10f, 5f, 0f)

// black lines and points
lineDataSet01.color = Color.BLACK
lineDataSet01.setCircleColor(Color.BLACK)

// line thickness and point size
lineDataSet01.lineWidth = 1f
lineDataSet01.circleRadius = 3f

// draw points as solid circles
lineDataSet01.setDrawCircleHole(false)

// customize legend entry
lineDataSet01.formLineWidth = 1f
lineDataSet01.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
lineDataSet01.formSize = 15f

// text size of values
lineDataSet01.valueTextSize = 9f

// draw selection line as dashed
lineDataSet01.enableDashedHighlightLine(10f, 5f, 0f)

// set the filled area
lineDataSet01.setDrawFilled(true)
lineDataSet01.fillFormatter = object : IFillFormatter {
override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float {
return lineChart.axisLeft.axisMinimum
}
}

// set color of filled area
if (Utils.getSDKInt() >= 18) {
// drawables only supported on api level 18 and above
val drawable = ContextCompat.getDrawable(context, R.drawable.fade_blue)
lineDataSet0.fillDrawable = drawable
} else {
lineDataSet0.fillColor = Color.BLACK
}
val dataSets = ArrayList<ILineDataSet>()
dataSets.add(lineDataSet0) // add the data sets
// set color of filled area
if (Utils.getSDKInt() >= 18) {
// drawables only supported on api level 18 and above
val drawable = ContextCompat.getDrawable(context, R.drawable.fade_blue)
lineDataSet01.fillDrawable = drawable
} else {
lineDataSet01.fillColor = Color.BLACK
}
val dataSets = ArrayList<ILineDataSet>()
dataSets.add(lineDataSet01) // add the data sets

// create a data object with the data sets
val data = LineData(dataSets)
// create a data object with the data sets
val data = LineData(dataSets)

// set data
lineChart.data = data
}
// set data
lineChart.data = data
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,70 +139,63 @@ class LineChartActivity1 : DemoBase(), OnSeekBarChangeListener, OnChartValueSele
}

R.id.actionToggleValues -> {
val sets = binding.chart1.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
binding.chart1.data?.dataSets?.forEach {
val set = it as LineDataSet
set.setDrawValues(!set.isDrawValuesEnabled)
}
binding.chart1.invalidate()
}

R.id.actionToggleIcons -> {
val sets = binding.chart1.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
binding.chart1.data?.dataSets?.forEach {
val set = it as LineDataSet
set.setDrawIcons(!set.isDrawIconsEnabled)
binding.chart1.invalidate()
}
binding.chart1.invalidate()
}

R.id.actionToggleHighlight -> {
if (binding.chart1.data != null) {
binding.chart1.data.isHighlightEnabled = !binding.chart1.data.isHighlightEnabled
binding.chart1.data?.let {
it.isHighlightEnabled = !it.isHighlightEnabled
binding.chart1.invalidate()
}
}

R.id.actionToggleFilled -> {
val sets = binding.chart1.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
binding.chart1.data?.dataSets?.let {
val set = it as LineDataSet
set.setDrawFilled(!set.isDrawFilledEnabled)
binding.chart1.invalidate()
}
binding.chart1.invalidate()
}

R.id.actionToggleCircles -> {
val sets = binding.chart1.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
binding.chart1.data?.dataSets?.forEach {
val set = it as LineDataSet
set.setDrawCircles(!set.isDrawCirclesEnabled)
}
binding.chart1.invalidate()
}

R.id.actionToggleCubic -> {
val sets = binding.chart1.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
binding.chart1.data?.dataSets?.forEach {
val set = it as LineDataSet
set.mode = if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER
}
binding.chart1.invalidate()
}

R.id.actionToggleStepped -> {
val sets = binding.chart1.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
binding.chart1.data?.dataSets?.forEach {
val set = it as LineDataSet
set.mode = if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED
}
binding.chart1.invalidate()
}

R.id.actionToggleHorizontalCubic -> {
val sets = binding.chart1.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
binding.chart1.data?.dataSets?.forEach {
val set = it as LineDataSet
set.mode = if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER
}
binding.chart1.invalidate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,61 +140,55 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener,
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.actionToggleValues -> {
val sets = mChart!!.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
mChart!!.data?.dataSets?.forEach {
val set = it as LineDataSet
set.setDrawValues(!set.isDrawValuesEnabled)
}
mChart!!.invalidate()
}

R.id.actionToggleHighlight -> {
if (mChart!!.data != null) {
mChart!!.data.isHighlightEnabled = !mChart!!.data.isHighlightEnabled
mChart!!.data?.let {
it.isHighlightEnabled = !it.isHighlightEnabled
mChart!!.invalidate()
}
}

R.id.actionToggleFilled -> {
val sets = mChart!!.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
mChart!!.data?.dataSets?.forEach {
val set = it as LineDataSet
set.setDrawFilled(!set.isDrawFilledEnabled)
}
mChart!!.invalidate()
}

R.id.actionToggleCircles -> {
val sets = mChart!!.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
mChart!!.data?.dataSets?.forEach {
val set = it as LineDataSet
set.setDrawCircles(!set.isDrawCirclesEnabled)
}
mChart!!.invalidate()
}

R.id.actionToggleCubic -> {
val sets = mChart!!.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
mChart!!.data?.dataSets?.forEach {
val set = it as LineDataSet
set.mode = if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER
}
mChart!!.invalidate()
}

R.id.actionToggleStepped -> {
val sets = mChart!!.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
mChart!!.data?.dataSets?.forEach {
val set = it as LineDataSet
set.mode = if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED
}
mChart!!.invalidate()
}

R.id.actionToggleHorizontalCubic -> {
val sets = mChart!!.data.dataSets
for (iSet in sets) {
val set = iSet as LineDataSet
mChart!!.data?.dataSets?.forEach {
val set = it as LineDataSet
set.mode = if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER
}
mChart!!.invalidate()
Expand Down Expand Up @@ -251,45 +245,51 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener,
val `val` = (sampleValues[i]!!.toFloat() * range) + 3
values.add(Entry(i.toFloat(), `val`))
}
val set1: LineDataSet
if (mChart!!.data != null && mChart!!.data.dataSetCount > 0) {
set1 = mChart!!.data.getDataSetByIndex(0) as LineDataSet
set1.entries = values
mChart!!.data.notifyDataChanged()
mChart!!.notifyDataSetChanged()
mChart!!.data?.let {
if (it.dataSetCount > 0) {
val set1 = it.getDataSetByIndex(0) as LineDataSet
set1.entries = values
it.notifyDataChanged()
mChart!!.notifyDataSetChanged()
} else
createDataset(values)
} ?: run {
createDataset(values)
}
}

private fun createDataset(values: ArrayList<Entry>) {
// create a dataset and give it a type
val set11 = LineDataSet(values, "DataSet 1")

// set the line to be drawn like this "- - - - - -"
set11.enableDashedLine(10f, 5f, 0f)
set11.enableDashedHighlightLine(10f, 5f, 0f)
set11.color = Color.BLACK
set11.setCircleColor(Color.BLACK)
set11.lineWidth = 1f
set11.circleRadius = 3f
set11.setDrawCircleHole(false)
set11.valueTextSize = 9f
set11.setDrawFilled(true)
set11.formLineWidth = 1f
set11.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
set11.formSize = 15f
if (Utils.getSDKInt() >= 18) {
// fill drawable only supported on api level 18 and above
val drawable = ContextCompat.getDrawable(this, R.drawable.fade_blue)
set11.fillDrawable = drawable
} else {
// create a dataset and give it a type
set1 = LineDataSet(values, "DataSet 1")

// set the line to be drawn like this "- - - - - -"
set1.enableDashedLine(10f, 5f, 0f)
set1.enableDashedHighlightLine(10f, 5f, 0f)
set1.color = Color.BLACK
set1.setCircleColor(Color.BLACK)
set1.lineWidth = 1f
set1.circleRadius = 3f
set1.setDrawCircleHole(false)
set1.valueTextSize = 9f
set1.setDrawFilled(true)
set1.formLineWidth = 1f
set1.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
set1.formSize = 15f
if (Utils.getSDKInt() >= 18) {
// fill drawable only supported on api level 18 and above
val drawable = ContextCompat.getDrawable(this, R.drawable.fade_blue)
set1.fillDrawable = drawable
} else {
set1.fillColor = Color.BLACK
}
val dataSets = ArrayList<ILineDataSet>()
dataSets.add(set1) // add the datasets
set11.fillColor = Color.BLACK
}
val dataSets = ArrayList<ILineDataSet>()
dataSets.add(set11) // add the datasets

// create a data object with the datasets
val data = LineData(dataSets)
// create a data object with the datasets
val data = LineData(dataSets)

// set data
mChart!!.data = data
}
// set data
mChart!!.data = data
}

override fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,6 @@ public void setDrawMarkers(boolean enabled) {
/**
* Returns the ChartData object that has been set for the chart.
*/
@NonNull
public T getData() {
return mData;
}
Expand Down

0 comments on commit d57df34

Please sign in to comment.