From a4596eb65b1e6151d78251b55206e7dad5d3e5ff Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sat, 13 Sep 2025 15:36:07 -0400 Subject: [PATCH 1/9] Rename .java to .kt --- .../charting/animation/{ChartAnimator.java => ChartAnimator.kt} | 0 .../github/mikephil/charting/animation/{Easing.java => Easing.kt} | 0 .../mikephil/charting/charts/{BarChart.java => BarChart.kt} | 0 .../charts/{BarLineChartBase.java => BarLineChartBase.kt} | 0 .../mikephil/charting/charts/{BubbleChart.java => BubbleChart.kt} | 0 .../charts/{CandleStickChart.java => CandleStickChart.kt} | 0 .../com/github/mikephil/charting/charts/{Chart.java => Chart.kt} | 0 .../charting/charts/{CombinedChart.java => CombinedChart.kt} | 0 .../charts/{HorizontalBarChart.java => HorizontalBarChart.kt} | 0 .../mikephil/charting/charts/{PieChart.java => PieChart.kt} | 0 .../charts/{PieRadarChartBase.java => PieRadarChartBase.kt} | 0 .../mikephil/charting/charts/{RadarChart.java => RadarChart.kt} | 0 .../charting/charts/{ScatterChart.java => ScatterChart.kt} | 0 .../mikephil/charting/components/{AxisBase.java => AxisBase.kt} | 0 .../charting/components/{Description.java => Description.kt} | 0 .../mikephil/charting/components/{IMarker.java => IMarker.kt} | 0 .../mikephil/charting/components/{Legend.java => Legend.kt} | 0 .../charting/components/{LegendEntry.java => LegendEntry.kt} | 0 .../mikephil/charting/components/{LimitLine.java => LimitLine.kt} | 0 .../charting/components/{LimitRange.java => LimitRange.kt} | 0 .../charting/components/{MarkerImage.java => MarkerImage.kt} | 0 .../charting/components/{MarkerView.java => MarkerView.kt} | 0 .../github/mikephil/charting/components/{XAxis.java => XAxis.kt} | 0 .../github/mikephil/charting/components/{YAxis.java => YAxis.kt} | 0 .../github/mikephil/charting/data/{BarData.java => BarData.kt} | 0 .../mikephil/charting/data/{BarDataSet.java => BarDataSet.kt} | 0 .../github/mikephil/charting/data/{BarEntry.java => BarEntry.kt} | 0 ...terCandleBubbleData.java => BarLineScatterCandleBubbleData.kt} | 0 ...dleBubbleDataSet.java => BarLineScatterCandleBubbleDataSet.kt} | 0 .../mikephil/charting/data/{BubbleData.java => BubbleData.kt} | 0 .../charting/data/{BubbleDataSet.java => BubbleDataSet.kt} | 0 .../mikephil/charting/data/{BubbleEntry.java => BubbleEntry.kt} | 0 .../mikephil/charting/data/{CandleData.java => CandleData.kt} | 0 .../charting/data/{CandleDataSet.java => CandleDataSet.kt} | 0 .../mikephil/charting/data/{CandleEntry.java => CandleEntry.kt} | 0 .../mikephil/charting/data/{ChartData.java => ChartData.kt} | 0 .../mikephil/charting/data/{CombinedData.java => CombinedData.kt} | 0 .../github/mikephil/charting/data/{DataSet.java => DataSet.kt} | 0 .../github/mikephil/charting/data/{LineData.java => LineData.kt} | 0 .../mikephil/charting/data/{LineDataSet.java => LineDataSet.kt} | 0 .../charting/data/{LineRadarDataSet.java => LineRadarDataSet.kt} | 0 ...erCandleRadarDataSet.java => LineScatterCandleRadarDataSet.kt} | 0 .../github/mikephil/charting/data/{PieData.java => PieData.kt} | 0 .../mikephil/charting/data/{PieDataSet.java => PieDataSet.kt} | 0 .../github/mikephil/charting/data/{PieEntry.java => PieEntry.kt} | 0 .../mikephil/charting/data/{RadarData.java => RadarData.kt} | 0 .../mikephil/charting/data/{RadarDataSet.java => RadarDataSet.kt} | 0 .../mikephil/charting/data/{RadarEntry.java => RadarEntry.kt} | 0 .../mikephil/charting/data/{ScatterData.java => ScatterData.kt} | 0 .../charting/data/{ScatterDataSet.java => ScatterDataSet.kt} | 0 .../charting/data/filter/{Approximator.java => Approximator.kt} | 0 .../charting/data/filter/{ApproximatorN.java => ApproximatorN.kt} | 0 ...CreatedException.java => DrawingDataSetNotCreatedException.kt} | 0 .../charting/highlight/{BarHighlighter.java => BarHighlighter.kt} | 0 .../highlight/{ChartHighlighter.java => ChartHighlighter.kt} | 0 .../{CombinedHighlighter.java => CombinedHighlighter.kt} | 0 ...{HorizontalBarHighlighter.java => HorizontalBarHighlighter.kt} | 0 .../charting/highlight/{IHighlighter.java => IHighlighter.kt} | 0 .../{PieRadarHighlighter.java => PieRadarHighlighter.kt} | 0 .../highlight/{RadarHighlighter.java => RadarHighlighter.kt} | 0 .../github/mikephil/charting/highlight/{Range.java => Range.kt} | 0 .../dataprovider/{BarDataProvider.java => BarDataProvider.kt} | 0 .../{BubbleDataProvider.java => BubbleDataProvider.kt} | 0 .../{CandleDataProvider.java => CandleDataProvider.kt} | 0 .../dataprovider/{ChartInterface.java => ChartInterface.kt} | 0 .../{CombinedDataProvider.java => CombinedDataProvider.kt} | 0 .../{ScatterDataProvider.java => ScatterDataProvider.kt} | 0 .../interfaces/datasets/{IBarDataSet.java => IBarDataSet.kt} | 0 .../datasets/{IBubbleDataSet.java => IBubbleDataSet.kt} | 0 .../datasets/{ICandleDataSet.java => ICandleDataSet.kt} | 0 .../charting/interfaces/datasets/{IDataSet.java => IDataSet.kt} | 0 .../interfaces/datasets/{ILineDataSet.java => ILineDataSet.kt} | 0 .../datasets/{ILineRadarDataSet.java => ILineRadarDataSet.kt} | 0 .../interfaces/datasets/{IPieDataSet.java => IPieDataSet.kt} | 0 .../interfaces/datasets/{IRadarDataSet.java => IRadarDataSet.kt} | 0 .../datasets/{IScatterDataSet.java => IScatterDataSet.kt} | 0 .../jobs/{AnimatedMoveViewJob.java => AnimatedMoveViewJob.kt} | 0 .../jobs/{AnimatedViewPortJob.java => AnimatedViewPortJob.kt} | 0 .../charting/jobs/{AnimatedZoomJob.java => AnimatedZoomJob.kt} | 0 .../mikephil/charting/jobs/{MoveViewJob.java => MoveViewJob.kt} | 0 .../mikephil/charting/jobs/{ViewPortJob.java => ViewPortJob.kt} | 0 .../github/mikephil/charting/jobs/{ZoomJob.java => ZoomJob.kt} | 0 .../listener/{ChartTouchListener.java => ChartTouchListener.kt} | 0 .../{OnChartGestureListener.java => OnChartGestureListener.kt} | 0 ...ValueSelectedListener.java => OnChartValueSelectedListener.kt} | 0 ...ineChartTouchListener.java => OnDrawLineChartTouchListener.kt} | 0 .../charting/listener/{OnDrawListener.java => OnDrawListener.kt} | 0 ...RadarChartTouchListener.java => PieRadarChartTouchListener.kt} | 0 .../github/mikephil/charting/matrix/{Vector3.java => Vector3.kt} | 0 .../charting/model/{GradientColor.java => GradientColor.kt} | 0 ...eBubbleRenderer.java => BarLineScatterCandleBubbleRenderer.kt} | 0 ...{ChevronDownShapeRenderer.java => ChevronDownShapeRenderer.kt} | 0 .../{ChevronUpShapeRenderer.java => ChevronUpShapeRenderer.kt} | 0 .../scatter/{CircleShapeRenderer.java => CircleShapeRenderer.kt} | 0 .../scatter/{CrossShapeRenderer.java => CrossShapeRenderer.kt} | 0 .../renderer/scatter/{IShapeRenderer.java => IShapeRenderer.kt} | 0 .../scatter/{SquareShapeRenderer.java => SquareShapeRenderer.kt} | 0 .../{TriangleShapeRenderer.java => TriangleShapeRenderer.kt} | 0 .../renderer/scatter/{XShapeRenderer.java => XShapeRenderer.kt} | 0 .../charting/utils/{ColorTemplate.java => ColorTemplate.kt} | 0 .../charting/utils/{EntryXComparator.java => EntryXComparator.kt} | 0 .../com/github/mikephil/charting/utils/{FSize.java => FSize.kt} | 0 .../mikephil/charting/utils/{FileUtils.java => FileUtils.kt} | 0 .../com/github/mikephil/charting/utils/{Fill.java => Fill.kt} | 0 ...orizontalViewPortHandler.java => HorizontalViewPortHandler.kt} | 0 .../github/mikephil/charting/utils/{MPPointD.java => MPPointD.kt} | 0 .../github/mikephil/charting/utils/{MPPointF.java => MPPointF.kt} | 0 .../mikephil/charting/utils/{ObjectPool.java => ObjectPool.kt} | 0 .../mikephil/charting/utils/{Transformer.java => Transformer.kt} | 0 ...erHorizontalBarChart.java => TransformerHorizontalBarChart.kt} | 0 .../com/github/mikephil/charting/utils/{Utils.java => Utils.kt} | 0 111 files changed, 0 insertions(+), 0 deletions(-) rename MPChartLib/src/main/java/com/github/mikephil/charting/animation/{ChartAnimator.java => ChartAnimator.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/animation/{Easing.java => Easing.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{BarChart.java => BarChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{BarLineChartBase.java => BarLineChartBase.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{BubbleChart.java => BubbleChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{CandleStickChart.java => CandleStickChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{Chart.java => Chart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{CombinedChart.java => CombinedChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{HorizontalBarChart.java => HorizontalBarChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{PieChart.java => PieChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{PieRadarChartBase.java => PieRadarChartBase.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{RadarChart.java => RadarChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/charts/{ScatterChart.java => ScatterChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{AxisBase.java => AxisBase.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{Description.java => Description.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{IMarker.java => IMarker.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{Legend.java => Legend.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{LegendEntry.java => LegendEntry.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{LimitLine.java => LimitLine.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{LimitRange.java => LimitRange.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{MarkerImage.java => MarkerImage.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{MarkerView.java => MarkerView.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{XAxis.java => XAxis.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/components/{YAxis.java => YAxis.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{BarData.java => BarData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{BarDataSet.java => BarDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{BarEntry.java => BarEntry.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{BarLineScatterCandleBubbleData.java => BarLineScatterCandleBubbleData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{BarLineScatterCandleBubbleDataSet.java => BarLineScatterCandleBubbleDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{BubbleData.java => BubbleData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{BubbleDataSet.java => BubbleDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{BubbleEntry.java => BubbleEntry.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{CandleData.java => CandleData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{CandleDataSet.java => CandleDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{CandleEntry.java => CandleEntry.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{ChartData.java => ChartData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{CombinedData.java => CombinedData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{DataSet.java => DataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{LineData.java => LineData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{LineDataSet.java => LineDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{LineRadarDataSet.java => LineRadarDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{LineScatterCandleRadarDataSet.java => LineScatterCandleRadarDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{PieData.java => PieData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{PieDataSet.java => PieDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{PieEntry.java => PieEntry.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{RadarData.java => RadarData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{RadarDataSet.java => RadarDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{RadarEntry.java => RadarEntry.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{ScatterData.java => ScatterData.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/{ScatterDataSet.java => ScatterDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/{Approximator.java => Approximator.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/{ApproximatorN.java => ApproximatorN.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/exception/{DrawingDataSetNotCreatedException.java => DrawingDataSetNotCreatedException.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/highlight/{BarHighlighter.java => BarHighlighter.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/highlight/{ChartHighlighter.java => ChartHighlighter.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/highlight/{CombinedHighlighter.java => CombinedHighlighter.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/highlight/{HorizontalBarHighlighter.java => HorizontalBarHighlighter.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/highlight/{IHighlighter.java => IHighlighter.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/highlight/{PieRadarHighlighter.java => PieRadarHighlighter.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/highlight/{RadarHighlighter.java => RadarHighlighter.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/highlight/{Range.java => Range.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/{BarDataProvider.java => BarDataProvider.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/{BubbleDataProvider.java => BubbleDataProvider.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/{CandleDataProvider.java => CandleDataProvider.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/{ChartInterface.java => ChartInterface.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/{CombinedDataProvider.java => CombinedDataProvider.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/{ScatterDataProvider.java => ScatterDataProvider.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{IBarDataSet.java => IBarDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{IBubbleDataSet.java => IBubbleDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{ICandleDataSet.java => ICandleDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{IDataSet.java => IDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{ILineDataSet.java => ILineDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{ILineRadarDataSet.java => ILineRadarDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{IPieDataSet.java => IPieDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{IRadarDataSet.java => IRadarDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/{IScatterDataSet.java => IScatterDataSet.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/jobs/{AnimatedMoveViewJob.java => AnimatedMoveViewJob.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/jobs/{AnimatedViewPortJob.java => AnimatedViewPortJob.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/jobs/{AnimatedZoomJob.java => AnimatedZoomJob.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/jobs/{MoveViewJob.java => MoveViewJob.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/jobs/{ViewPortJob.java => ViewPortJob.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/jobs/{ZoomJob.java => ZoomJob.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/listener/{ChartTouchListener.java => ChartTouchListener.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/listener/{OnChartGestureListener.java => OnChartGestureListener.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/listener/{OnChartValueSelectedListener.java => OnChartValueSelectedListener.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/listener/{OnDrawLineChartTouchListener.java => OnDrawLineChartTouchListener.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/listener/{OnDrawListener.java => OnDrawListener.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/listener/{PieRadarChartTouchListener.java => PieRadarChartTouchListener.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/matrix/{Vector3.java => Vector3.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/model/{GradientColor.java => GradientColor.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/{BarLineScatterCandleBubbleRenderer.java => BarLineScatterCandleBubbleRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/{ChevronDownShapeRenderer.java => ChevronDownShapeRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/{ChevronUpShapeRenderer.java => ChevronUpShapeRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/{CircleShapeRenderer.java => CircleShapeRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/{CrossShapeRenderer.java => CrossShapeRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/{IShapeRenderer.java => IShapeRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/{SquareShapeRenderer.java => SquareShapeRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/{TriangleShapeRenderer.java => TriangleShapeRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/{XShapeRenderer.java => XShapeRenderer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{ColorTemplate.java => ColorTemplate.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{EntryXComparator.java => EntryXComparator.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{FSize.java => FSize.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{FileUtils.java => FileUtils.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{Fill.java => Fill.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{HorizontalViewPortHandler.java => HorizontalViewPortHandler.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{MPPointD.java => MPPointD.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{MPPointF.java => MPPointF.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{ObjectPool.java => ObjectPool.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{Transformer.java => Transformer.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{TransformerHorizontalBarChart.java => TransformerHorizontalBarChart.kt} (100%) rename MPChartLib/src/main/java/com/github/mikephil/charting/utils/{Utils.java => Utils.kt} (100%) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/animation/ChartAnimator.java b/MPChartLib/src/main/java/com/github/mikephil/charting/animation/ChartAnimator.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/animation/ChartAnimator.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/animation/ChartAnimator.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/animation/Easing.java b/MPChartLib/src/main/java/com/github/mikephil/charting/animation/Easing.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/animation/Easing.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/animation/Easing.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BubbleChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BubbleChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/BubbleChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/BubbleChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/XAxis.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/XAxis.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/XAxis.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/XAxis.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/LineData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/LineData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/PieDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/PieDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterData.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterData.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterData.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterData.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.java b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.java b/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/ChartHighlighter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/ChartHighlighter.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/highlight/ChartHighlighter.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/highlight/ChartHighlighter.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/IHighlighter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/IHighlighter.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/highlight/IHighlighter.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/highlight/IHighlighter.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/Range.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/Range.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/highlight/Range.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/highlight/Range.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CombinedDataProvider.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CombinedDataProvider.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CombinedDataProvider.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CombinedDataProvider.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBubbleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBubbleDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBubbleDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBubbleDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IPieDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IPieDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IPieDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IPieDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IRadarDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IRadarDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IRadarDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IRadarDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IScatterDataSet.java b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IScatterDataSet.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IScatterDataSet.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IScatterDataSet.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.java b/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/model/GradientColor.java b/MPChartLib/src/main/java/com/github/mikephil/charting/model/GradientColor.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/model/GradientColor.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/model/GradientColor.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ChevronDownShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ChevronDownShapeRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ChevronDownShapeRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ChevronDownShapeRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ChevronUpShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ChevronUpShapeRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ChevronUpShapeRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ChevronUpShapeRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CrossShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CrossShapeRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CrossShapeRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CrossShapeRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/IShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/IShapeRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/IShapeRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/IShapeRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/XShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/XShapeRenderer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/XShapeRenderer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/XShapeRenderer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt similarity index 100% rename from MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt From 0591f90eb8d26cd55277861e2a024457669d6bc1 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sat, 13 Sep 2025 15:36:08 -0400 Subject: [PATCH 2/9] First pass at Kotlin conversion # Conflicts: # MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt --- .../charting/animation/ChartAnimator.kt | 211 +- .../mikephil/charting/animation/Easing.kt | 436 +-- .../mikephil/charting/charts/BarChart.kt | 268 +- .../charting/charts/BarLineChartBase.kt | 1736 ++++----- .../mikephil/charting/charts/BubbleChart.kt | 49 +- .../charting/charts/CandleStickChart.kt | 54 +- .../github/mikephil/charting/charts/Chart.kt | 3200 ++++++++--------- .../mikephil/charting/charts/CombinedChart.kt | 485 ++- .../charting/charts/HorizontalBarChart.kt | 449 ++- .../mikephil/charting/charts/LineChart.kt | 64 +- .../mikephil/charting/charts/PieChart.kt | 879 ++--- .../charting/charts/PieRadarChartBase.kt | 588 ++- .../mikephil/charting/charts/RadarChart.kt | 487 ++- .../mikephil/charting/charts/ScatterChart.kt | 82 +- .../mikephil/charting/components/AxisBase.kt | 1006 +++--- .../charting/components/Description.kt | 93 +- .../mikephil/charting/components/IMarker.kt | 36 +- .../mikephil/charting/components/Legend.kt | 877 ++--- .../charting/components/LegendEntry.kt | 54 +- .../mikephil/charting/components/LimitLine.kt | 261 +- .../charting/components/LimitRange.kt | 463 ++- .../charting/components/MarkerImage.kt | 196 +- .../charting/components/MarkerView.kt | 140 +- .../mikephil/charting/components/XAxis.kt | 85 +- .../mikephil/charting/components/YAxis.kt | 461 +-- .../github/mikephil/charting/data/BarData.kt | 107 +- .../mikephil/charting/data/BarDataSet.kt | 260 +- .../github/mikephil/charting/data/BarEntry.kt | 294 +- .../data/BarLineScatterCandleBubbleData.kt | 26 +- .../data/BarLineScatterCandleBubbleDataSet.kt | 43 +- .../mikephil/charting/data/BaseDataSet.kt | 229 +- .../mikephil/charting/data/BubbleData.kt | 30 +- .../mikephil/charting/data/BubbleDataSet.kt | 79 +- .../mikephil/charting/data/BubbleEntry.kt | 60 +- .../mikephil/charting/data/CandleData.kt | 22 +- .../mikephil/charting/data/CandleDataSet.kt | 226 +- .../mikephil/charting/data/CandleEntry.kt | 225 +- .../mikephil/charting/data/ChartData.kt | 739 ++-- .../mikephil/charting/data/CombinedData.kt | 309 +- .../github/mikephil/charting/data/DataSet.kt | 456 +-- .../github/mikephil/charting/data/Entry.kt | 2 +- .../github/mikephil/charting/data/LineData.kt | 25 +- .../mikephil/charting/data/LineDataSet.kt | 369 +- .../charting/data/LineRadarDataSet.kt | 109 +- .../data/LineScatterCandleRadarDataSet.kt | 128 +- .../github/mikephil/charting/data/PieData.kt | 126 +- .../mikephil/charting/data/PieDataSet.kt | 263 +- .../github/mikephil/charting/data/PieEntry.kt | 102 +- .../mikephil/charting/data/RadarData.kt | 51 +- .../mikephil/charting/data/RadarDataSet.kt | 144 +- .../mikephil/charting/data/RadarEntry.kt | 59 +- .../mikephil/charting/data/ScatterData.kt | 49 +- .../mikephil/charting/data/ScatterDataSet.kt | 161 +- .../charting/data/filter/Approximator.kt | 108 +- .../charting/data/filter/ApproximatorN.kt | 187 +- .../DrawingDataSetNotCreatedException.kt | 16 +- .../formatter/DefaultFillFormatter.kt | 2 +- .../charting/formatter/IAxisValueFormatter.kt | 2 +- .../charting/highlight/BarHighlighter.kt | 196 +- .../charting/highlight/ChartHighlighter.kt | 208 +- .../charting/highlight/CombinedHighlighter.kt | 120 +- .../highlight/HorizontalBarHighlighter.kt | 144 +- .../charting/highlight/IHighlighter.kt | 8 +- .../charting/highlight/PieRadarHighlighter.kt | 103 +- .../charting/highlight/RadarHighlighter.kt | 79 +- .../mikephil/charting/highlight/Range.kt | 48 +- .../dataprovider/BarDataProvider.kt | 15 +- .../BarLineScatterCandleBubbleDataProvider.kt | 4 +- .../dataprovider/BubbleDataProvider.kt | 9 +- .../dataprovider/CandleDataProvider.kt | 9 +- .../interfaces/dataprovider/ChartInterface.kt | 45 +- .../dataprovider/CombinedDataProvider.kt | 9 +- .../dataprovider/LineDataProvider.kt | 4 +- .../dataprovider/ScatterDataProvider.kt | 9 +- .../interfaces/datasets/IBarDataSet.kt | 31 +- .../interfaces/datasets/IBubbleDataSet.kt | 25 +- .../interfaces/datasets/ICandleDataSet.kt | 30 +- .../charting/interfaces/datasets/IDataSet.kt | 197 +- .../interfaces/datasets/ILineDataSet.kt | 46 +- .../interfaces/datasets/ILineRadarDataSet.kt | 22 +- .../interfaces/datasets/IPieDataSet.kt | 39 +- .../interfaces/datasets/IRadarDataSet.kt | 32 +- .../interfaces/datasets/IScatterDataSet.kt | 17 +- .../charting/jobs/AnimatedMoveViewJob.kt | 117 +- .../charting/jobs/AnimatedViewPortJob.kt | 118 +- .../mikephil/charting/jobs/AnimatedZoomJob.kt | 192 +- .../mikephil/charting/jobs/MoveViewJob.kt | 86 +- .../mikephil/charting/jobs/ViewPortJob.kt | 65 +- .../github/mikephil/charting/jobs/ZoomJob.kt | 141 +- .../listener/BarLineChartTouchListener.kt | 167 +- .../charting/listener/ChartTouchListener.kt | 153 +- .../listener/OnChartGestureListener.kt | 24 +- .../listener/OnChartValueSelectedListener.kt | 15 +- .../listener/OnDrawLineChartTouchListener.kt | 21 +- .../charting/listener/OnDrawListener.kt | 55 +- .../listener/PieRadarChartTouchListener.kt | 296 +- .../mikephil/charting/matrix/Vector3.kt | 259 +- .../mikephil/charting/model/GradientColor.kt | 103 +- .../charting/renderer/BarChartRenderer.kt | 45 +- .../BarLineScatterCandleBubbleRenderer.kt | 76 +- .../charting/renderer/BubbleChartRenderer.kt | 14 +- .../renderer/CandleStickChartRenderer.kt | 14 +- .../renderer/CombinedChartRenderer.kt | 4 +- .../renderer/HorizontalBarChartRenderer.kt | 33 +- .../charting/renderer/LegendRenderer.kt | 136 +- .../charting/renderer/LineChartRenderer.kt | 14 +- .../charting/renderer/LineRadarRenderer.kt | 6 +- .../LineScatterCandleRadarRenderer.kt | 2 +- .../charting/renderer/PieChartRenderer.kt | 7 +- .../charting/renderer/RadarChartRenderer.kt | 12 +- .../renderer/RoundedBarChartRenderer.kt | 14 +- .../RoundedHorizontalBarChartRenderer.kt | 6 +- .../charting/renderer/ScatterChartRenderer.kt | 14 +- .../charting/renderer/XAxisRenderer.kt | 4 +- .../XAxisRendererHorizontalBarChart.kt | 40 +- .../renderer/XAxisRendererRadarChart.kt | 2 +- .../charting/renderer/YAxisRenderer.kt | 186 +- .../renderer/YAxisRendererRadarChart.kt | 4 +- .../scatter/ChevronDownShapeRenderer.kt | 64 +- .../scatter/ChevronUpShapeRenderer.kt | 65 +- .../renderer/scatter/CircleShapeRenderer.kt | 90 +- .../renderer/scatter/CrossShapeRenderer.kt | 63 +- .../renderer/scatter/IShapeRenderer.kt | 21 +- .../renderer/scatter/SquareShapeRenderer.kt | 91 +- .../renderer/scatter/TriangleShapeRenderer.kt | 125 +- .../renderer/scatter/XShapeRenderer.kt | 64 +- .../mikephil/charting/utils/ColorTemplate.kt | 124 +- .../charting/utils/EntryXComparator.kt | 19 +- .../github/mikephil/charting/utils/FSize.kt | 104 +- .../mikephil/charting/utils/FileUtils.kt | 205 +- .../github/mikephil/charting/utils/Fill.kt | 538 ++- .../utils/HorizontalViewPortHandler.kt | 27 +- .../mikephil/charting/utils/MPPointD.kt | 63 +- .../mikephil/charting/utils/MPPointF.kt | 139 +- .../mikephil/charting/utils/ObjectPool.kt | 260 +- .../mikephil/charting/utils/Transformer.kt | 422 +-- .../utils/TransformerHorizontalBarChart.kt | 38 +- .../github/mikephil/charting/utils/Utils.kt | 797 ++-- .../charting/utils/ViewPortHandler.kt | 6 +- .../chartexample/AnotherBarActivity.java | 4 +- .../appdev/chartexample/BarChartActivity.java | 6 +- .../BarChartActivityMultiDataset.java | 7 +- .../chartexample/BarChartActivitySinus.java | 4 +- .../chartexample/BubbleChartActivity.java | 4 +- .../CandleStickChartActivity.java | 6 +- .../chartexample/CombinedChartActivity.java | 4 +- .../chartexample/DynamicalAddingActivity.java | 8 +- .../HorizontalBarChartActivity.java | 9 +- .../HorizontalBarNegativeChartActivity.java | 9 +- .../LineChartDualAxisActivity.java | 3 +- .../appdev/chartexample/PieChartActivity.java | 4 +- .../chartexample/PieChartRoundedActivity.java | 4 +- .../PiePolylineChartActivity.java | 2 +- .../chartexample/RadarChartActivity.java | 6 +- .../RealtimeLineChartActivity.java | 4 +- .../chartexample/StackedBarActivity.java | 2 +- .../StackedBarActivityNegative.java | 2 +- .../custom/CustomScatterShapeRenderer.java | 2 +- 158 files changed, 11067 insertions(+), 13910 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/animation/ChartAnimator.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/animation/ChartAnimator.kt index e5b82db0b6..c4967b74b4 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/animation/ChartAnimator.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/animation/ChartAnimator.kt @@ -1,10 +1,8 @@ -package com.github.mikephil.charting.animation; +package com.github.mikephil.charting.animation -import android.animation.ObjectAnimator; -import android.animation.ValueAnimator.AnimatorUpdateListener; -import androidx.annotation.RequiresApi; - -import com.github.mikephil.charting.animation.Easing.EasingFunction; +import android.animation.ObjectAnimator +import android.animation.ValueAnimator.AnimatorUpdateListener +import com.github.mikephil.charting.animation.Easing.EasingFunction /** * Object responsible for all animations in the Chart. Animations require API level 11. @@ -12,44 +10,36 @@ import com.github.mikephil.charting.animation.Easing.EasingFunction; * @author Philipp Jahoda * @author Mick Ashton */ -public class ChartAnimator { - - /** object that is updated upon animation update */ - private AnimatorUpdateListener mListener; +open class ChartAnimator { + /** object that is updated upon animation update */ + private var mListener: AnimatorUpdateListener? = null - /** The phase of drawn values on the y-axis. 0 - 1 */ - @SuppressWarnings("WeakerAccess") - protected float mPhaseY = 1f; + /** The phase of drawn values on the y-axis. 0 - 1 */ + protected var mPhaseY: Float = 1f - /** The phase of drawn values on the x-axis. 0 - 1 */ - @SuppressWarnings("WeakerAccess") - protected float mPhaseX = 1f; + /** The phase of drawn values on the x-axis. 0 - 1 */ + protected var mPhaseX: Float = 1f - public ChartAnimator() { } + constructor() - @RequiresApi(11) - public ChartAnimator(AnimatorUpdateListener listener) { - mListener = listener; + constructor(listener: AnimatorUpdateListener?) { + mListener = listener } - @RequiresApi(11) - private ObjectAnimator xAnimator(int duration, EasingFunction easing) { + private fun xAnimator(duration: Int, easing: EasingFunction?): ObjectAnimator { + val animatorX = ObjectAnimator.ofFloat(this, "phaseX", 0f, 1f) + animatorX.interpolator = easing + animatorX.setDuration(duration.toLong()) - ObjectAnimator animatorX = ObjectAnimator.ofFloat(this, "phaseX", 0f, 1f); - animatorX.setInterpolator(easing); - animatorX.setDuration(duration); - - return animatorX; + return animatorX } - @RequiresApi(11) - private ObjectAnimator yAnimator(int duration, EasingFunction easing) { - - ObjectAnimator animatorY = ObjectAnimator.ofFloat(this, "phaseY", 0f, 1f); - animatorY.setInterpolator(easing); - animatorY.setDuration(duration); + private fun yAnimator(duration: Int, easing: EasingFunction?): ObjectAnimator { + val animatorY = ObjectAnimator.ofFloat(this, "phaseY", 0f, 1f) + animatorY.interpolator = easing + animatorY.setDuration(duration.toLong()) - return animatorY; + return animatorY } /** @@ -57,9 +47,8 @@ public class ChartAnimator { * * @param durationMillis animation duration */ - @RequiresApi(11) - public void animateX(int durationMillis) { - animateX(durationMillis, Easing.Linear); + fun animateX(durationMillis: Int) { + animateX(durationMillis, Easing.Linear) } /** @@ -68,12 +57,10 @@ public class ChartAnimator { * @param durationMillis animation duration * @param easing EasingFunction */ - @RequiresApi(11) - public void animateX(int durationMillis, EasingFunction easing) { - - ObjectAnimator animatorX = xAnimator(durationMillis, easing); - animatorX.addUpdateListener(mListener); - animatorX.start(); + fun animateX(durationMillis: Int, easing: EasingFunction?) { + val animatorX = xAnimator(durationMillis, easing) + animatorX.addUpdateListener(mListener) + animatorX.start() } /** @@ -82,9 +69,8 @@ public class ChartAnimator { * @param durationMillisX animation duration along the X axis * @param durationMillisY animation duration along the Y axis */ - @RequiresApi(11) - public void animateXY(int durationMillisX, int durationMillisY) { - animateXY(durationMillisX, durationMillisY, Easing.Linear, Easing.Linear); + fun animateXY(durationMillisX: Int, durationMillisY: Int) { + animateXY(durationMillisX, durationMillisY, Easing.Linear, Easing.Linear) } /** @@ -94,20 +80,18 @@ public class ChartAnimator { * @param durationMillisY animation duration along the Y axis * @param easing EasingFunction for both axes */ - @RequiresApi(11) - public void animateXY(int durationMillisX, int durationMillisY, EasingFunction easing) { - - ObjectAnimator xAnimator = xAnimator(durationMillisX, easing); - ObjectAnimator yAnimator = yAnimator(durationMillisY, easing); + fun animateXY(durationMillisX: Int, durationMillisY: Int, easing: EasingFunction?) { + val xAnimator = xAnimator(durationMillisX, easing) + val yAnimator = yAnimator(durationMillisY, easing) if (durationMillisX > durationMillisY) { - xAnimator.addUpdateListener(mListener); + xAnimator.addUpdateListener(mListener) } else { - yAnimator.addUpdateListener(mListener); + yAnimator.addUpdateListener(mListener) } - xAnimator.start(); - yAnimator.start(); + xAnimator.start() + yAnimator.start() } /** @@ -118,21 +102,21 @@ public class ChartAnimator { * @param easingX EasingFunction for the X axis * @param easingY EasingFunction for the Y axis */ - @RequiresApi(11) - public void animateXY(int durationMillisX, int durationMillisY, EasingFunction easingX, - EasingFunction easingY) { - - ObjectAnimator xAnimator = xAnimator(durationMillisX, easingX); - ObjectAnimator yAnimator = yAnimator(durationMillisY, easingY); + fun animateXY( + durationMillisX: Int, durationMillisY: Int, easingX: EasingFunction?, + easingY: EasingFunction? + ) { + val xAnimator = xAnimator(durationMillisX, easingX) + val yAnimator = yAnimator(durationMillisY, easingY) if (durationMillisX > durationMillisY) { - xAnimator.addUpdateListener(mListener); + xAnimator.addUpdateListener(mListener) } else { - yAnimator.addUpdateListener(mListener); + yAnimator.addUpdateListener(mListener) } - xAnimator.start(); - yAnimator.start(); + xAnimator.start() + yAnimator.start() } /** @@ -140,9 +124,8 @@ public class ChartAnimator { * * @param durationMillis animation duration */ - @RequiresApi(11) - public void animateY(int durationMillis) { - animateY(durationMillis, Easing.Linear); + fun animateY(durationMillis: Int) { + animateY(durationMillis, Easing.Linear) } /** @@ -151,57 +134,53 @@ public class ChartAnimator { * @param durationMillis animation duration * @param easing EasingFunction */ - @RequiresApi(11) - public void animateY(int durationMillis, EasingFunction easing) { - - ObjectAnimator animatorY = yAnimator(durationMillis, easing); - animatorY.addUpdateListener(mListener); - animatorY.start(); - } - - /** - * Gets the Y axis phase of the animation. - * - * @return float value of {@link #mPhaseY} - */ - public float getPhaseY() { - return mPhaseY; + fun animateY(durationMillis: Int, easing: EasingFunction?) { + val animatorY = yAnimator(durationMillis, easing) + animatorY.addUpdateListener(mListener) + animatorY.start() } - /** - * Sets the Y axis phase of the animation. - * - * @param phase float value between 0 - 1 - */ - public void setPhaseY(float phase) { - if (phase > 1f) { - phase = 1f; - } else if (phase < 0f) { - phase = 0f; + var phaseY: Float + /** + * Gets the Y axis phase of the animation. + * + * @return float value of [.mPhaseY] + */ + get() = mPhaseY + /** + * Sets the Y axis phase of the animation. + * + * @param phase float value between 0 - 1 + */ + set(phase) { + var phase = phase + if (phase > 1f) { + phase = 1f + } else if (phase < 0f) { + phase = 0f + } + mPhaseY = phase } - mPhaseY = phase; - } - /** - * Gets the X axis phase of the animation. - * - * @return float value of {@link #mPhaseX} - */ - public float getPhaseX() { - return mPhaseX; - } - - /** - * Sets the X axis phase of the animation. - * - * @param phase float value between 0 - 1 - */ - public void setPhaseX(float phase) { - if (phase > 1f) { - phase = 1f; - } else if (phase < 0f) { - phase = 0f; + var phaseX: Float + /** + * Gets the X axis phase of the animation. + * + * @return float value of [.mPhaseX] + */ + get() = mPhaseX + /** + * Sets the X axis phase of the animation. + * + * @param phase float value between 0 - 1 + */ + set(phase) { + var phase = phase + if (phase > 1f) { + phase = 1f + } else if (phase < 0f) { + phase = 0f + } + mPhaseX = phase } - mPhaseX = phase; - } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/animation/Easing.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/animation/Easing.kt index acb7dcc965..d3a6192236 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/animation/Easing.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/animation/Easing.kt @@ -1,7 +1,11 @@ -package com.github.mikephil.charting.animation; +package com.github.mikephil.charting.animation -import android.animation.TimeInterpolator; -import androidx.annotation.RequiresApi; +import android.animation.TimeInterpolator +import kotlin.math.asin +import kotlin.math.cos +import kotlin.math.pow +import kotlin.math.sin +import kotlin.math.sqrt /** * Easing options. @@ -9,301 +13,307 @@ import androidx.annotation.RequiresApi; * @author Daniel Cohen Gindi * @author Mick Ashton */ -@SuppressWarnings("WeakerAccess") -@RequiresApi(11) -public class Easing { +object Easing { + private const val DOUBLE_PI = 2f * Math.PI.toFloat() - public interface EasingFunction extends TimeInterpolator { - @Override - float getInterpolation(float input); - } - - private static final float DOUBLE_PI = 2f * (float) Math.PI; - - @SuppressWarnings("unused") - public static final EasingFunction Linear = new EasingFunction() { - public float getInterpolation(float input) { - return input; + @Suppress("unused") + val Linear: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return input } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInQuad = new EasingFunction() { - public float getInterpolation(float input) { - return input * input; + @Suppress("unused") + val EaseInQuad: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return input * input } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseOutQuad = new EasingFunction() { - public float getInterpolation(float input) { - return -input * (input - 2f); + @Suppress("unused") + val EaseOutQuad: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return -input * (input - 2f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutQuad = new EasingFunction() { - public float getInterpolation(float input) { - input *= 2f; + @JvmField + @Suppress("unused") + val EaseInOutQuad: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + input *= 2f if (input < 1f) { - return 0.5f * input * input; + return 0.5f * input * input } - return -0.5f * ((--input) * (input - 2f) - 1f); + return -0.5f * ((--input) * (input - 2f) - 1f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInCubic = new EasingFunction() { - public float getInterpolation(float input) { - return (float) Math.pow(input, 3); + @Suppress("unused") + val EaseInCubic: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return input.toDouble().pow(3.0).toFloat() } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseOutCubic = new EasingFunction() { - public float getInterpolation(float input) { - input--; - return (float) Math.pow(input, 3) + 1f; + @Suppress("unused") + val EaseOutCubic: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + input-- + return input.toDouble().pow(3.0).toFloat() + 1f } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutCubic = new EasingFunction() { - public float getInterpolation(float input) { - input *= 2f; + @JvmField + @Suppress("unused") + val EaseInOutCubic: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + input *= 2f if (input < 1f) { - return 0.5f * (float) Math.pow(input, 3); + return 0.5f * input.toDouble().pow(3.0).toFloat() } - input -= 2f; - return 0.5f * ((float) Math.pow(input, 3) + 2f); + input -= 2f + return 0.5f * (input.toDouble().pow(3.0).toFloat() + 2f) } - }; - - @SuppressWarnings("unused") - public static final EasingFunction EaseInQuart = new EasingFunction() { + } - public float getInterpolation(float input) { - return (float) Math.pow(input, 4); + @Suppress("unused") + val EaseInQuart: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return input.toDouble().pow(4.0).toFloat() } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseOutQuart = new EasingFunction() { - public float getInterpolation(float input) { - input--; - return -((float) Math.pow(input, 4) - 1f); + @Suppress("unused") + val EaseOutQuart: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + input-- + return -(input.toDouble().pow(4.0).toFloat() - 1f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutQuart = new EasingFunction() { - public float getInterpolation(float input) { - input *= 2f; + @Suppress("unused") + val EaseInOutQuart: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + input *= 2f if (input < 1f) { - return 0.5f * (float) Math.pow(input, 4); + return 0.5f * input.toDouble().pow(4.0).toFloat() } - input -= 2f; - return -0.5f * ((float) Math.pow(input, 4) - 2f); + input -= 2f + return -0.5f * (input.toDouble().pow(4.0).toFloat() - 2f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInSine = new EasingFunction() { - public float getInterpolation(float input) { - return -(float) Math.cos(input * (Math.PI / 2f)) + 1f; + @Suppress("unused") + val EaseInSine: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return -cos(input * (Math.PI / 2f)).toFloat() + 1f } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseOutSine = new EasingFunction() { - public float getInterpolation(float input) { - return (float) Math.sin(input * (Math.PI / 2f)); + @Suppress("unused") + val EaseOutSine: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return sin(input * (Math.PI / 2f)).toFloat() } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutSine = new EasingFunction() { - public float getInterpolation(float input) { - return -0.5f * ((float) Math.cos(Math.PI * input) - 1f); + @Suppress("unused") + val EaseInOutSine: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return -0.5f * (cos(Math.PI * input).toFloat() - 1f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInExpo = new EasingFunction() { - public float getInterpolation(float input) { - return (input == 0) ? 0f : (float) Math.pow(2f, 10f * (input - 1f)); + @Suppress("unused") + val EaseInExpo: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return if (input == 0f) 0f else 2.0.pow((10f * (input - 1f)).toDouble()).toFloat() } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseOutExpo = new EasingFunction() { - public float getInterpolation(float input) { - return (input == 1f) ? 1f : (-(float) Math.pow(2f, -10f * (input + 1f))); + @Suppress("unused") + val EaseOutExpo: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return if (input == 1f) 1f else (-2.0.pow((-10f * (input + 1f)).toDouble()).toFloat()) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutExpo = new EasingFunction() { - public float getInterpolation(float input) { - if (input == 0) { - return 0f; + @Suppress("unused") + val EaseInOutExpo: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + if (input == 0f) { + return 0f } else if (input == 1f) { - return 1f; + return 1f } - input *= 2f; + input *= 2f if (input < 1f) { - return 0.5f * (float) Math.pow(2f, 10f * (input - 1f)); + return 0.5f * 2.0.pow((10f * (input - 1f)).toDouble()).toFloat() } - return 0.5f * (-(float) Math.pow(2f, -10f * --input) + 2f); + return 0.5f * (-2.0.pow((-10f * --input).toDouble()).toFloat() + 2f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInCirc = new EasingFunction() { - public float getInterpolation(float input) { - return -((float) Math.sqrt(1f - input * input) - 1f); + @Suppress("unused") + val EaseInCirc: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return -(sqrt((1f - input * input).toDouble()).toFloat() - 1f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseOutCirc = new EasingFunction() { - public float getInterpolation(float input) { - input--; - return (float) Math.sqrt(1f - input * input); + @Suppress("unused") + val EaseOutCirc: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + input-- + return sqrt((1f - input * input).toDouble()).toFloat() } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutCirc = new EasingFunction() { - public float getInterpolation(float input) { - input *= 2f; + @Suppress("unused") + val EaseInOutCirc: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + input *= 2f if (input < 1f) { - return -0.5f * ((float) Math.sqrt(1f - input * input) - 1f); + return -0.5f * (sqrt((1f - input * input).toDouble()).toFloat() - 1f) } - return 0.5f * ((float) Math.sqrt(1f - (input -= 2f) * input) + 1f); + return 0.5f * (sqrt((1f - (2f.let { input -= it; input }) * input).toDouble()).toFloat() + 1f) } - }; - - @SuppressWarnings("unused") - public static final EasingFunction EaseInElastic = new EasingFunction() { - public float getInterpolation(float input) { - if (input == 0) { - return 0f; - } else if (input == 1) { - return 1f; + } + + @Suppress("unused") + val EaseInElastic: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + if (input == 0f) { + return 0f + } else if (input == 1f) { + return 1f } - float p = 0.3f; - float s = p / DOUBLE_PI * (float) Math.asin(1f); - return -((float) Math.pow(2f, 10f * (input -= 1f)) - *(float) Math.sin((input - s) * DOUBLE_PI / p)); + val p = 0.3f + val s = p / DOUBLE_PI * asin(1.0).toFloat() + return -(2.0.pow((10f * (1f.let { input -= it; input })).toDouble()).toFloat() * sin(((input - s) * DOUBLE_PI / p).toDouble()).toFloat()) } - }; - - @SuppressWarnings("unused") - public static final EasingFunction EaseOutElastic = new EasingFunction() { - public float getInterpolation(float input) { - if (input == 0) { - return 0f; - } else if (input == 1) { - return 1f; + } + + @Suppress("unused") + val EaseOutElastic: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + if (input == 0f) { + return 0f + } else if (input == 1f) { + return 1f } - float p = 0.3f; - float s = p / DOUBLE_PI * (float) Math.asin(1f); - return 1f - + (float) Math.pow(2f, -10f * input) - * (float) Math.sin((input - s) * DOUBLE_PI / p); + val p = 0.3f + val s = p / DOUBLE_PI * asin(1.0).toFloat() + return (1f + + 2.0.pow((-10f * input).toDouble()).toFloat() * sin(((input - s) * DOUBLE_PI / p).toDouble()).toFloat()) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutElastic = new EasingFunction() { - public float getInterpolation(float input) { - if (input == 0) { - return 0f; + @Suppress("unused") + val EaseInOutElastic: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + if (input == 0f) { + return 0f } - input *= 2f; - if (input == 2) { - return 1f; + input *= 2f + if (input == 2f) { + return 1f } - float p = 1f / 0.45f; - float s = 0.45f / DOUBLE_PI * (float) Math.asin(1f); + val p = 1f / 0.45f + val s = 0.45f / DOUBLE_PI * asin(1.0).toFloat() if (input < 1f) { - return -0.5f - * ((float) Math.pow(2f, 10f * (input -= 1f)) - * (float) Math.sin((input * 1f - s) * DOUBLE_PI * p)); + return (-0.5f + * (2.0.pow((10f * (1f.let { input -= it; input })).toDouble()) + .toFloat() * sin(((input * 1f - s) * DOUBLE_PI * p).toDouble()).toFloat())) } - return 1f + 0.5f - * (float) Math.pow(2f, -10f * (input -= 1f)) - * (float) Math.sin((input * 1f - s) * DOUBLE_PI * p); + return 1f + (0.5f + * 2.0.pow((-10f * (1f.let { input -= it; input })).toDouble()).toFloat() * sin(((input * 1f - s) * DOUBLE_PI * p).toDouble()).toFloat()) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInBack = new EasingFunction() { - public float getInterpolation(float input) { - final float s = 1.70158f; - return input * input * ((s + 1f) * input - s); + @Suppress("unused") + val EaseInBack: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + val s = 1.70158f + return input * input * ((s + 1f) * input - s) } - }; - - @SuppressWarnings("unused") - public static final EasingFunction EaseOutBack = new EasingFunction() { - public float getInterpolation(float input) { - final float s = 1.70158f; - input--; - return (input * input * ((s + 1f) * input + s) + 1f); + } + + @Suppress("unused") + val EaseOutBack: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + val s = 1.70158f + input-- + return (input * input * ((s + 1f) * input + s) + 1f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutBack = new EasingFunction() { - public float getInterpolation(float input) { - float s = 1.70158f; - input *= 2f; + @Suppress("unused") + val EaseInOutBack: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + var s = 1.70158f + input *= 2f if (input < 1f) { - return 0.5f * (input * input * (((s *= (1.525f)) + 1f) * input - s)); + return 0.5f * (input * input * (((1.525f.let { s *= it; s }) + 1f) * input - s)) } - return 0.5f * ((input -= 2f) * input * (((s *= (1.525f)) + 1f) * input + s) + 2f); + return 0.5f * ((2f.let { input -= it; input }) * input * (((1.525f.let { s *= it; s }) + 1f) * input + s) + 2f) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInBounce = new EasingFunction() { - public float getInterpolation(float input) { - return 1f - EaseOutBounce.getInterpolation(1f - input); + @Suppress("unused") + val EaseInBounce: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + return 1f - EaseOutBounce.getInterpolation(1f - input) } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseOutBounce = new EasingFunction() { - public float getInterpolation(float input) { - float s = 7.5625f; + @Suppress("unused") + val EaseOutBounce: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { + var input = input + val s = 7.5625f if (input < (1f / 2.75f)) { - return s * input * input; + return s * input * input } else if (input < (2f / 2.75f)) { - return s * (input -= (1.5f / 2.75f)) * input + 0.75f; + return s * (1.5f / 2.75f.let { input -= it; input }) * input + 0.75f } else if (input < (2.5f / 2.75f)) { - return s * (input -= (2.25f / 2.75f)) * input + 0.9375f; + return s * (2.25f / 2.75f.let { input -= it; input }) * input + 0.9375f } - return s * (input -= (2.625f / 2.75f)) * input + 0.984375f; + return s * (2.625f / 2.75f.let { input -= it; input }) * input + 0.984375f } - }; + } - @SuppressWarnings("unused") - public static final EasingFunction EaseInOutBounce = new EasingFunction() { - public float getInterpolation(float input) { + @Suppress("unused") + val EaseInOutBounce: EasingFunction = object : EasingFunction { + override fun getInterpolation(input: Float): Float { if (input < 0.5f) { - return EaseInBounce.getInterpolation(input * 2f) * 0.5f; + return EaseInBounce.getInterpolation(input * 2f) * 0.5f } - return EaseOutBounce.getInterpolation(input * 2f - 1f) * 0.5f + 0.5f; + return EaseOutBounce.getInterpolation(input * 2f - 1f) * 0.5f + 0.5f } - }; + } + interface EasingFunction : TimeInterpolator { + override fun getInterpolation(input: Float): Float + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt index 3dd6eac65f..e9f2a316c6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt @@ -1,93 +1,79 @@ -package com.github.mikephil.charting.charts; - -import android.content.Context; -import android.graphics.RectF; -import android.util.AttributeSet; -import android.util.Log; - -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.highlight.BarHighlighter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.renderer.BarChartRenderer; - -import java.util.Locale; +package com.github.mikephil.charting.charts + +import android.content.Context +import android.graphics.RectF +import android.util.AttributeSet +import android.util.Log +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.highlight.BarHighlighter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.renderer.BarChartRenderer +import java.util.Locale /** * Chart that draws bars. * * @author Philipp Jahoda */ -public class BarChart extends BarLineChartBase implements BarDataProvider { - +open class BarChart : BarLineChartBase, BarDataProvider { /** * flag that indicates whether the highlight should be full-bar oriented, or single-value? */ - protected boolean mHighlightFullBarEnabled = false; + protected var mHighlightFullBarEnabled: Boolean = false /** * if set to true, all values are drawn above their bars, instead of below their top */ - private boolean mDrawValueAboveBar = true; + private var mDrawValueAboveBar = true /** * if set to true, a grey area is drawn behind each bar that indicates the maximum value */ - private boolean mDrawBarShadow = false; + private var mDrawBarShadow = false /** * if set to true, the bar chart's bars would be round on all corners instead of rectangular */ - private boolean mDrawRoundedBars; + private var mDrawRoundedBars = false /** * the radius of the rounded bar chart bars */ - private float mRoundedBarRadius = 0f; + private var mRoundedBarRadius = 0f - private boolean mFitBars = false; + private var mFitBars = false - public BarChart(Context context) { - super(context); - } + constructor(context: Context) : super(context) - public BarChart(Context context, AttributeSet attrs) { - super(context, attrs); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - public BarChart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - @Override - protected void init() { - super.init(); + override fun init() { + super.init() - mRenderer = new BarChartRenderer(this, mAnimator, mViewPortHandler, mDrawRoundedBars, mRoundedBarRadius); + mRenderer = BarChartRenderer(this, mAnimator, viewPortHandler, mDrawRoundedBars, mRoundedBarRadius) - setHighlighter(new BarHighlighter(this)); + setHighlighter(BarHighlighter(this)) - getXAxis().setSpaceMin(0.5f); - getXAxis().setSpaceMax(0.5f); + xAxis.spaceMin = 0.5f + xAxis.spaceMax = 0.5f } - @Override - protected void calcMinMax() { - + override fun calcMinMax() { if (mFitBars) { - mXAxis.calculate(mData.getXMin() - mData.getBarWidth() / 2f, mData.getXMax() + mData.getBarWidth() / 2f); + mXAxis?.calculate(mData!!.xMin - mData!!.barWidth / 2f, mData!!.xMax + mData!!.barWidth / 2f) } else { - mXAxis.calculate(mData.getXMin(), mData.getXMax()); + mXAxis?.calculate(mData!!.xMin, mData!!.xMax) } // calculate axis range (min / max) according to provided data - mAxisLeft.calculate(mData.getYMin(YAxis.AxisDependency.LEFT), mData.getYMax(YAxis.AxisDependency.LEFT)); - mAxisRight.calculate(mData.getYMin(YAxis.AxisDependency.RIGHT), mData.getYMax(YAxis.AxisDependency - .RIGHT)); + mAxisLeft?.calculate(mData!!.getYMin(AxisDependency.LEFT), mData!!.getYMax(AxisDependency.LEFT)) + mAxisRight?.calculate(mData!!.getYMin(AxisDependency.RIGHT), mData!!.getYMax(AxisDependency.RIGHT)) } /** @@ -97,21 +83,21 @@ public class BarChart extends BarLineChartBase implements BarDataProvid * * @param x * @param y - */ - @Override - public Highlight getHighlightByTouchPoint(float x, float y) { - + */ + override fun getHighlightByTouchPoint(x: Float, y: Float): Highlight? { if (mData == null) { - Log.e(LOG_TAG, "Can't select by touch. No data set."); - return null; + Log.e(Chart.Companion.LOG_TAG, "Can't select by touch. No data set.") + return null } else { - Highlight h = getHighlighter().getHighlight(x, y); - if (h == null || !isHighlightFullBarEnabled()) return h; + val h = highlighter?.getHighlight(x, y) + if (h == null || !isHighlightFullBarEnabled) return h // For isHighlightFullBarEnabled, remove stackIndex - return new Highlight(h.getX(), h.getY(), - h.getXPx(), h.getYPx(), - h.getDataSetIndex(), -1, h.getAxis()); + return Highlight( + h.x, h.y, + h.xPx, h.yPx, + h.dataSetIndex, -1, h.axis + ) } } @@ -120,13 +106,12 @@ public class BarChart extends BarLineChartBase implements BarDataProvid * found in the charts data. Performance-intensive code should use void getBarBounds(BarEntry, RectF) instead. * * @param barEntry - */ - public RectF getBarBounds(BarEntry barEntry) { - - RectF bounds = new RectF(); - getBarBounds(barEntry, bounds); + */ + fun getBarBounds(barEntry: BarEntry): RectF { + val bounds = RectF() + getBarBounds(barEntry, bounds) - return bounds; + return bounds } /** @@ -134,63 +119,60 @@ public class BarChart extends BarLineChartBase implements BarDataProvid * The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data. * * @param barEntry - */ - public void getBarBounds(BarEntry barEntry, RectF outputRect) { - - IBarDataSet set = mData.getDataSetForEntry(barEntry); + */ + open fun getBarBounds(barEntry: BarEntry, outputRect: RectF) { + val set = mData!!.getDataSetForEntry(barEntry) if (set == null) { - outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE); - return; + outputRect.set(Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE) + return } - float y = barEntry.getY(); - float x = barEntry.getX(); + val y = barEntry.y + val x = barEntry.x - float barWidth = mData.getBarWidth(); + val barWidth = mData!!.barWidth - float left = x - barWidth / 2f; - float right = x + barWidth / 2f; - float top = y >= 0 ? y : 0; - float bottom = y <= 0 ? y : 0; + val left = x - barWidth / 2f + val right = x + barWidth / 2f + val top = if (y >= 0) y else 0f + val bottom = if (y <= 0) y else 0f - outputRect.set(left, top, right, bottom); + outputRect.set(left, top, right, bottom) - getTransformer(set.getAxisDependency()).rectValueToPixel(outputRect); + getTransformer(set.axisDependency).rectValueToPixel(outputRect) } /** * If set to true, all values are drawn above their bars, instead of below their top. * - */ - public void setDrawValueAboveBar(boolean enabled) { - mDrawValueAboveBar = enabled; + */ + fun setDrawValueAboveBar(enabled: Boolean) { + mDrawValueAboveBar = enabled } /** * returns true if drawing values above bars is enabled, false if not * - */ - public boolean isDrawValueAboveBarEnabled() { - return mDrawValueAboveBar; - } + */ + override val isDrawValueAboveBarEnabled: Boolean + get() = mDrawValueAboveBar /** * If set to true, a grey area is drawn behind each bar that indicates the maximum value. Enabling his will reduce * performance by about 50%. * - */ - public void setDrawBarShadow(boolean enabled) { - mDrawBarShadow = enabled; + */ + fun setDrawBarShadow(enabled: Boolean) { + mDrawBarShadow = enabled } /** * returns true if drawing shadows (maxvalue) for each bar is enabled, false if not * - */ - public boolean isDrawBarShadowEnabled() { - return mDrawBarShadow; - } + */ + override val isDrawBarShadowEnabled: Boolean + get() = mDrawBarShadow /** * Set this to true to make the highlight operation full-bar oriented, false to make it highlight single values (relevant @@ -198,18 +180,16 @@ public class BarChart extends BarLineChartBase implements BarDataProvid * was tapped. * Default: false * - */ - public void setHighlightFullBarEnabled(boolean enabled) { - mHighlightFullBarEnabled = enabled; + */ + fun setHighlightFullBarEnabled(enabled: Boolean) { + mHighlightFullBarEnabled = enabled } /** * @return true the highlight operation is be full-bar oriented, false if single-value */ - @Override - public boolean isHighlightFullBarEnabled() { - return mHighlightFullBarEnabled; - } + override val isHighlightFullBarEnabled: Boolean + get() = mHighlightFullBarEnabled /** * Highlights the value at the given x-value in the given DataSet. Provide @@ -217,25 +197,23 @@ public class BarChart extends BarLineChartBase implements BarDataProvid * * @param x * @param dataSetIndex - * @param stackIndex the index inside the stack - only relevant for stacked entries + * @param dataIndex the index inside the stack - only relevant for stacked entries */ - public void highlightValue(float x, int dataSetIndex, int stackIndex) { - highlightValue(new Highlight(x, dataSetIndex, stackIndex), false); + override fun highlightValue(x: Float, dataSetIndex: Int, dataIndex: Int) { + highlightValue(Highlight(x, dataSetIndex, dataIndex), false) } - @Override - public BarData getBarData() { - return mData; - } + override val barData: BarData? + get() = mData /** * Adds half of the bar width to each side of the x-axis range in order to allow the bars of the barchart to be * fully displayed. * Default: false * - */ - public void setFitBars(boolean enabled) { - mFitBars = enabled; + */ + fun setFitBars(enabled: Boolean) { + mFitBars = enabled } /** @@ -248,53 +226,53 @@ public class BarChart extends BarLineChartBase implements BarDataProvid * @param groupSpace the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f * @param barSpace the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f */ - public void groupBars(float fromX, float groupSpace, float barSpace) { - - if (getBarData() == null) { - throw new RuntimeException("You need to set data for the chart before grouping bars."); + fun groupBars(fromX: Float, groupSpace: Float, barSpace: Float) { + if (barData == null) { + throw RuntimeException("You need to set data for the chart before grouping bars.") } else { - getBarData().groupBars(fromX, groupSpace, barSpace); - notifyDataSetChanged(); + barData!!.groupBars(fromX, groupSpace, barSpace) + notifyDataSetChanged() } } + /** * Used to enable rounded bar chart bars and set the radius of the rounded bars * * @param mRoundedBarRadius - the radius of the rounded bars */ - public void setRoundedBarRadius(float mRoundedBarRadius) { - this.mRoundedBarRadius = mRoundedBarRadius; - this.mDrawRoundedBars = true; - init(); + fun setRoundedBarRadius(mRoundedBarRadius: Float) { + this.mRoundedBarRadius = mRoundedBarRadius + this.mDrawRoundedBars = true + init() } - @Override - public String getAccessibilityDescription() { - - BarData barData = getBarData(); - if (barData == null) { - return ""; - } - - int entryCount = barData.getEntryCount(); + override val accessibilityDescription: String? + get() { + val barData = barData + if (barData == null) { + return "" + } - // Find the min and max index - IAxisValueFormatter yAxisValueFormatter = getAxisLeft().getValueFormatter(); - String minVal = yAxisValueFormatter.getFormattedValue(barData.getYMin(), null); - String maxVal = yAxisValueFormatter.getFormattedValue(barData.getYMax(), null); + val entryCount = barData.entryCount - // Data range... - IAxisValueFormatter xAxisValueFormatter = getXAxis().getValueFormatter(); - String minRange = xAxisValueFormatter.getFormattedValue(barData.getXMin(), null); - String maxRange = xAxisValueFormatter.getFormattedValue(barData.getXMax(), null); + // Find the min and max index + val yAxisValueFormatter = axisLeft.valueFormatter + val minVal = yAxisValueFormatter!!.getFormattedValue(barData.yMin, null) + val maxVal = yAxisValueFormatter.getFormattedValue(barData.yMax, null) - String entries = entryCount == 1 ? "entry" : "entries"; + // Data range... + val xAxisValueFormatter = xAxis.valueFormatter + val minRange = xAxisValueFormatter!!.getFormattedValue(barData.xMin, null) + val maxRange = xAxisValueFormatter.getFormattedValue(barData.xMax, null) - // Format the values of min and max; to recite them back + val entries = if (entryCount == 1) "entry" else "entries" - return String.format(Locale.getDefault(), "The bar chart has %d %s. " + + // Format the values of min and max; to recite them back + return String.format( + Locale.getDefault(), "The bar chart has %d %s. " + "The minimum value is %s and maximum value is %s." + "Data ranges from %s to %s.", - entryCount, entries, minVal, maxVal, minRange, maxRange); - } + entryCount, entries, minVal, maxVal, minRange, maxRange + ) + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt index 8ef4c5e2b0..0c941663cf 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt @@ -1,40 +1,42 @@ - -package com.github.mikephil.charting.charts; - -import android.annotation.SuppressLint; -import android.annotation.TargetApi; -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Matrix; -import android.graphics.Paint; -import android.graphics.Paint.Style; -import android.graphics.RectF; -import android.util.AttributeSet; -import android.util.Log; -import android.view.MotionEvent; - -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.ChartHighlighter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider; -import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet; -import com.github.mikephil.charting.jobs.AnimatedMoveViewJob; -import com.github.mikephil.charting.jobs.AnimatedZoomJob; -import com.github.mikephil.charting.jobs.MoveViewJob; -import com.github.mikephil.charting.jobs.ZoomJob; -import com.github.mikephil.charting.listener.BarLineChartTouchListener; -import com.github.mikephil.charting.listener.OnDrawListener; -import com.github.mikephil.charting.renderer.XAxisRenderer; -import com.github.mikephil.charting.renderer.YAxisRenderer; -import com.github.mikephil.charting.utils.MPPointD; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.Utils; +package com.github.mikephil.charting.charts + +import android.annotation.SuppressLint +import android.annotation.TargetApi +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Matrix +import android.graphics.Paint +import android.graphics.RectF +import android.util.AttributeSet +import android.util.Log +import android.view.MotionEvent +import com.github.mikephil.charting.components.Legend.LegendHorizontalAlignment +import com.github.mikephil.charting.components.Legend.LegendOrientation +import com.github.mikephil.charting.components.Legend.LegendVerticalAlignment +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.components.YAxis +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.ChartHighlighter +import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider +import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet +import com.github.mikephil.charting.jobs.AnimatedMoveViewJob +import com.github.mikephil.charting.jobs.AnimatedZoomJob +import com.github.mikephil.charting.jobs.MoveViewJob +import com.github.mikephil.charting.jobs.ZoomJob +import com.github.mikephil.charting.listener.BarLineChartTouchListener +import com.github.mikephil.charting.listener.OnDrawListener +import com.github.mikephil.charting.renderer.XAxisRenderer +import com.github.mikephil.charting.renderer.YAxisRenderer +import com.github.mikephil.charting.utils.MPPointD +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.Utils +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min /** * Base-class of LineChart, BarChart, ScatterChart and CandleStickChart. @@ -42,513 +44,565 @@ import com.github.mikephil.charting.utils.Utils; * @author Philipp Jahoda */ @SuppressLint("RtlHardcoded") -public abstract class BarLineChartBase>> - extends Chart implements BarLineScatterCandleBubbleDataProvider { - +abstract class BarLineChartBase, T : BarLineScatterCandleBubbleData> + : Chart, BarLineScatterCandleBubbleDataProvider { /** * the maximum number of entries to which values will be drawn * (entry numbers greater than this value will cause value-labels to disappear) */ - protected int mMaxVisibleCount = 100; + protected var mMaxVisibleCount: Int = 100 + /** + * @return true if auto scaling on the y axis is enabled. + * @default false + */ + /** + * Flag that indicates if auto scaling on the y axis is enabled. This is + * especially interesting for charts displaying financial data. + * + * @param enabled the y axis automatically adjusts to the min and max y + * values of the current x axis range whenever the viewport + * changes + */ /** * flag that indicates if auto scaling on the y axis is enabled */ - protected boolean mAutoScaleMinMaxEnabled = false; + var isAutoScaleMinMaxEnabled: Boolean = false + /** + * returns true if pinch-zoom is enabled, false if not + * + * @return + */ /** * flag that indicates if pinch-zoom is enabled. if true, both x and y axis * can be scaled with 2 fingers, if false, x and y axis can be scaled * separately */ - protected boolean mPinchZoomEnabled = false; + var isPinchZoomEnabled: Boolean = false + protected set + /** + * Returns true if zooming via double-tap is enabled false if not. + * + * @return + */ + /** + * Set this to true to enable zooming in by double-tap on the chart. + * Default: enabled + * + * @param enabled + */ /** * flag that indicates if double tap zoom is enabled or not */ - protected boolean mDoubleTapToZoomEnabled = true; + var isDoubleTapToZoomEnabled: Boolean = true + /** + * Set this to true to allow highlighting per dragging over the chart + * surface when it is fully zoomed out. Default: true + * + * @param enabled + */ /** * flag that indicates if highlighting per dragging over a fully zoomed out * chart is enabled */ - protected boolean mHighlightPerDragEnabled = true; + var isHighlightPerDragEnabled: Boolean = true + /** + * Returns true if dragging on the X axis is enabled for the chart, false if not. + * + * @return + */ + /** + * Set this to true to enable dragging on the X axis + * + * @param enabled + */ /** * if true, dragging is enabled for the chart */ - private boolean mDragXEnabled = true; - private boolean mDragYEnabled = true; + var isDragXEnabled: Boolean = true + /** + * Returns true if dragging on the Y axis is enabled for the chart, false if not. + * + * @return + */ + /** + * Set this to true to enable dragging on the Y axis + * + * @param enabled + */ + var isDragYEnabled: Boolean = true - private boolean mScaleXEnabled = true; - private boolean mScaleYEnabled = true; + var isScaleXEnabled: Boolean = true + var isScaleYEnabled: Boolean = true + /** + * Returns true if fling gesture is enabled for the chart, false if not. + * + * @return + */ + /** + * Set this to true to enable fling gesture for the chart + * + * @param enabled + */ /** * if true, fling gesture is enabled for the chart */ - private boolean mFlingEnabled = false; + var isFlingEnabled: Boolean = false /** * paint object for the (by default) lightgrey background of the grid */ - protected Paint mGridBackgroundPaint; + protected var mGridBackgroundPaint: Paint? = null - protected Paint mBorderPaint; + protected var mBorderPaint: Paint? = null /** * flag indicating if the grid background should be drawn or not */ - protected boolean mDrawGridBackground = false; + protected var mDrawGridBackground: Boolean = false - protected boolean mDrawBorders = false; + /** + * When enabled, the borders rectangle will be rendered. + * If this is enabled, there is no point drawing the axis-lines of x- and y-axis. + * + * @return + */ + var isDrawBordersEnabled: Boolean = false + protected set - protected boolean mClipValuesToContent = false; + /** + * When enabled, the values will be clipped to contentRect, + * otherwise they can bleed outside the content rect. + * + * @return + */ + var isClipValuesToContentEnabled: Boolean = false + protected set - protected boolean mClipDataToContent = true; + /** + * When disabled, the data and/or highlights will not be clipped to contentRect. Disabling this option can + * be useful, when the data lies fully within the content rect, but is drawn in such a way (such as thick lines) + * that there is unwanted clipping. + * + * @return + */ + var isClipDataToContentEnabled: Boolean = true + protected set + /** + * Gets the minimum offset (padding) around the chart, defaults to 15.f + */ + /** + * Sets the minimum offset (padding) around the chart, defaults to 15.f + */ /** * Sets the minimum offset (padding) around the chart, defaults to 15 */ - protected float mMinOffset = 15.f; + var minOffset: Float = 15f + /** + * Returns true if keeping the position on rotation is enabled and false if not. + */ + /** + * Sets whether the chart should keep its position (zoom / scroll) after a rotation (orientation change) + */ /** * flag indicating if the chart should stay at the same position after a rotation. Default is false. */ - protected boolean mKeepPositionOnRotation = false; + var isKeepPositionOnRotation: Boolean = false + /** + * Gets the OnDrawListener. May be null. + * + * @return + */ /** * the listener for user drawing on the chart */ - protected OnDrawListener mDrawListener; + var drawListener: OnDrawListener? = null + protected set /** * the object representing the labels on the left y-axis */ - protected YAxis mAxisLeft; + protected var mAxisLeft: YAxis? = null /** * the object representing the labels on the right y-axis */ - protected YAxis mAxisRight; + protected var mAxisRight: YAxis? = null - protected YAxisRenderer mAxisRendererLeft; - protected YAxisRenderer mAxisRendererRight; + protected var mAxisRendererLeft: YAxisRenderer? = null + protected var mAxisRendererRight: YAxisRenderer? = null - protected Transformer mLeftAxisTransformer; - protected Transformer mRightAxisTransformer; + protected var mLeftAxisTransformer: Transformer? = null + protected var mRightAxisTransformer: Transformer? = null - protected XAxisRenderer mXAxisRenderer; + protected var mXAxisRenderer: XAxisRenderer? = null // /** the approximator object used for data filtering */ // private Approximator mApproximator; + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - public BarLineChartBase(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - public BarLineChartBase(Context context, AttributeSet attrs) { - super(context, attrs); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - public BarLineChartBase(Context context) { - super(context); - } + constructor(context: Context) : super(context) - @Override - protected void init() { - super.init(); + override fun init() { + super.init() - mAxisLeft = new YAxis(AxisDependency.LEFT); - mAxisRight = new YAxis(AxisDependency.RIGHT); + mAxisLeft = YAxis(AxisDependency.LEFT) + mAxisRight = YAxis(AxisDependency.RIGHT) - mLeftAxisTransformer = new Transformer(mViewPortHandler); - mRightAxisTransformer = new Transformer(mViewPortHandler); + mLeftAxisTransformer = Transformer(viewPortHandler) + mRightAxisTransformer = Transformer(viewPortHandler) - mAxisRendererLeft = new YAxisRenderer(mViewPortHandler, mAxisLeft, mLeftAxisTransformer); - mAxisRendererRight = new YAxisRenderer(mViewPortHandler, mAxisRight, mRightAxisTransformer); + mAxisRendererLeft = YAxisRenderer(viewPortHandler, mAxisLeft!!, mLeftAxisTransformer) + mAxisRendererRight = YAxisRenderer(viewPortHandler, mAxisRight!!, mRightAxisTransformer) - mXAxisRenderer = new XAxisRenderer(mViewPortHandler, mXAxis, mLeftAxisTransformer); + mXAxisRenderer = XAxisRenderer(viewPortHandler, mXAxis!!, mLeftAxisTransformer) - setHighlighter(new ChartHighlighter(this)); + setHighlighter(ChartHighlighter(this)) - mChartTouchListener = new BarLineChartTouchListener(this, mViewPortHandler.getMatrixTouch(), 3f); + mChartTouchListener = BarLineChartTouchListener(this, viewPortHandler.matrixTouch, 3f) - mGridBackgroundPaint = new Paint(); - mGridBackgroundPaint.setStyle(Style.FILL); + mGridBackgroundPaint = Paint() + mGridBackgroundPaint!!.style = Paint.Style.FILL // mGridBackgroundPaint.setColor(Color.WHITE); - mGridBackgroundPaint.setColor(Color.rgb(240, 240, 240)); // light - // grey + mGridBackgroundPaint!!.setColor(Color.rgb(240, 240, 240)) // light - mBorderPaint = new Paint(); - mBorderPaint.setStyle(Style.STROKE); - mBorderPaint.setColor(Color.BLACK); - mBorderPaint.setStrokeWidth(Utils.convertDpToPixel(1f)); + // grey + mBorderPaint = Paint() + mBorderPaint!!.style = Paint.Style.STROKE + mBorderPaint!!.setColor(Color.BLACK) + mBorderPaint!!.strokeWidth = Utils.convertDpToPixel(1f) } // for performance tracking - private long totalTime = 0; - private long drawCycles = 0; + private var totalTime: Long = 0 + private var drawCycles: Long = 0 - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); + protected override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) - if (mData == null) - return; + if (mData == null) return - long starttime = System.currentTimeMillis(); + val starttime = System.currentTimeMillis() // execute all drawing commands - drawGridBackground(canvas); + drawGridBackground(canvas) - if (mAutoScaleMinMaxEnabled) { - autoScale(); + if (this.isAutoScaleMinMaxEnabled) { + autoScale() } - if (mAxisLeft.isEnabled()) - mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum, mAxisLeft.isInverted()); + if (mAxisLeft!!.isEnabled) mAxisRendererLeft!!.computeAxis(mAxisLeft!!.mAxisMinimum, mAxisLeft!!.mAxisMaximum, mAxisLeft!!.isInverted) - if (mAxisRight.isEnabled()) - mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum, mAxisRight.isInverted()); + if (mAxisRight!!.isEnabled) mAxisRendererRight!!.computeAxis(mAxisRight!!.mAxisMinimum, mAxisRight!!.mAxisMaximum, mAxisRight!!.isInverted) - if (mXAxis.isEnabled()) - mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false); + if (mXAxis!!.isEnabled) mXAxisRenderer!!.computeAxis(mXAxis!!.mAxisMinimum, mXAxis!!.mAxisMaximum, false) // Y-axis labels could have changed in size affecting the offsets - if (mAutoScaleMinMaxEnabled) { - calculateOffsets(); - mViewPortHandler.refresh(mViewPortHandler.getMatrixTouch(), this, false); + if (this.isAutoScaleMinMaxEnabled) { + calculateOffsets() + viewPortHandler.refresh(viewPortHandler.matrixTouch, this, false) } - mXAxisRenderer.renderAxisLine(canvas); - mAxisRendererLeft.renderAxisLine(canvas); - mAxisRendererRight.renderAxisLine(canvas); + mXAxisRenderer!!.renderAxisLine(canvas) + mAxisRendererLeft!!.renderAxisLine(canvas) + mAxisRendererRight!!.renderAxisLine(canvas) - if (mXAxis.isDrawGridLinesBehindDataEnabled()) - mXAxisRenderer.renderGridLines(canvas); + if (mXAxis!!.isDrawGridLinesBehindDataEnabled) mXAxisRenderer!!.renderGridLines(canvas) - if (mAxisLeft.isDrawGridLinesBehindDataEnabled()) - mAxisRendererLeft.renderGridLines(canvas); + if (mAxisLeft!!.isDrawGridLinesBehindDataEnabled) mAxisRendererLeft!!.renderGridLines(canvas) - if (mAxisRight.isDrawGridLinesBehindDataEnabled()) - mAxisRendererRight.renderGridLines(canvas); + if (mAxisRight!!.isDrawGridLinesBehindDataEnabled) mAxisRendererRight!!.renderGridLines(canvas) - if (mXAxis.isEnabled() && mXAxis.isDrawLimitLinesBehindDataEnabled()) - mXAxisRenderer.renderLimitLines(canvas); + if (mXAxis!!.isEnabled && mXAxis!!.isDrawLimitLinesBehindDataEnabled) mXAxisRenderer!!.renderLimitLines(canvas) - if (mAxisLeft.isEnabled() && mAxisLeft.isDrawLimitLinesBehindDataEnabled()) - mAxisRendererLeft.renderLimitLines(canvas); + if (mAxisLeft!!.isEnabled && mAxisLeft!!.isDrawLimitLinesBehindDataEnabled) mAxisRendererLeft!!.renderLimitLines(canvas) - if (mAxisRight.isEnabled() && mAxisRight.isDrawLimitLinesBehindDataEnabled()) - mAxisRendererRight.renderLimitLines(canvas); + if (mAxisRight!!.isEnabled && mAxisRight!!.isDrawLimitLinesBehindDataEnabled) mAxisRendererRight!!.renderLimitLines(canvas) - int clipRestoreCount = canvas.save(); + var clipRestoreCount = canvas.save() - if (isClipDataToContentEnabled()) { + if (this.isClipDataToContentEnabled) { // make sure the data cannot be drawn outside the content-rect - canvas.clipRect(mViewPortHandler.getContentRect()); + canvas.clipRect(viewPortHandler.contentRect) } - mRenderer.drawData(canvas); + mRenderer?.drawData(canvas) - if (!mXAxis.isDrawGridLinesBehindDataEnabled()) - mXAxisRenderer.renderGridLines(canvas); + if (!mXAxis!!.isDrawGridLinesBehindDataEnabled) mXAxisRenderer!!.renderGridLines(canvas) - if (!mAxisLeft.isDrawGridLinesBehindDataEnabled()) - mAxisRendererLeft.renderGridLines(canvas); + if (!mAxisLeft!!.isDrawGridLinesBehindDataEnabled) mAxisRendererLeft!!.renderGridLines(canvas) - if (!mAxisRight.isDrawGridLinesBehindDataEnabled()) - mAxisRendererRight.renderGridLines(canvas); + if (!mAxisRight!!.isDrawGridLinesBehindDataEnabled) mAxisRendererRight!!.renderGridLines(canvas) // if highlighting is enabled - if (valuesToHighlight()) - mRenderer.drawHighlighted(canvas, mIndicesToHighlight); + if (valuesToHighlight()) mRenderer?.drawHighlighted(canvas, highlighted!!) // Removes clipping rectangle - canvas.restoreToCount(clipRestoreCount); + canvas.restoreToCount(clipRestoreCount) - mRenderer.drawExtras(canvas); + mRenderer?.drawExtras(canvas) - if (mXAxis.isEnabled() && !mXAxis.isDrawLimitLinesBehindDataEnabled()) - mXAxisRenderer.renderLimitLines(canvas); + if (mXAxis!!.isEnabled && !mXAxis!!.isDrawLimitLinesBehindDataEnabled) mXAxisRenderer!!.renderLimitLines(canvas) - if (mAxisLeft.isEnabled() && !mAxisLeft.isDrawLimitLinesBehindDataEnabled()) - mAxisRendererLeft.renderLimitLines(canvas); + if (mAxisLeft!!.isEnabled && !mAxisLeft!!.isDrawLimitLinesBehindDataEnabled) mAxisRendererLeft!!.renderLimitLines(canvas) - if (mAxisRight.isEnabled() && !mAxisRight.isDrawLimitLinesBehindDataEnabled()) - mAxisRendererRight.renderLimitLines(canvas); + if (mAxisRight!!.isEnabled && !mAxisRight!!.isDrawLimitLinesBehindDataEnabled) mAxisRendererRight!!.renderLimitLines(canvas) - mXAxisRenderer.renderAxisLabels(canvas); - mAxisRendererLeft.renderAxisLabels(canvas); - mAxisRendererRight.renderAxisLabels(canvas); + mXAxisRenderer!!.renderAxisLabels(canvas) + mAxisRendererLeft!!.renderAxisLabels(canvas) + mAxisRendererRight!!.renderAxisLabels(canvas) - if (isClipValuesToContentEnabled()) { - clipRestoreCount = canvas.save(); - canvas.clipRect(mViewPortHandler.getContentRect()); + if (this.isClipValuesToContentEnabled) { + clipRestoreCount = canvas.save() + canvas.clipRect(viewPortHandler.contentRect) - mRenderer.drawValues(canvas); + mRenderer?.drawValues(canvas) - canvas.restoreToCount(clipRestoreCount); + canvas.restoreToCount(clipRestoreCount) } else { - mRenderer.drawValues(canvas); + mRenderer?.drawValues(canvas) } - mLegendRenderer.renderLegend(canvas); + legendRenderer?.renderLegend(canvas) - drawDescription(canvas); + drawDescription(canvas) - drawMarkers(canvas); + drawMarkers(canvas) - if (mLogEnabled) { - long drawtime = (System.currentTimeMillis() - starttime); - totalTime += drawtime; - drawCycles += 1; - long average = totalTime / drawCycles; - Log.i(LOG_TAG, "Drawtime: " + drawtime + " ms, average: " + average + " ms, cycles: " - + drawCycles); + if (isLogEnabled) { + val drawtime = (System.currentTimeMillis() - starttime) + totalTime += drawtime + drawCycles += 1 + val average = totalTime / drawCycles + Log.i( + LOG_TAG, ("Drawtime: " + drawtime + " ms, average: " + average + " ms, cycles: " + + drawCycles) + ) } } /** * RESET PERFORMANCE TRACKING FIELDS */ - public void resetTracking() { - totalTime = 0; - drawCycles = 0; + fun resetTracking() { + totalTime = 0 + drawCycles = 0 } - protected void prepareValuePxMatrix() { - - if (mLogEnabled) - Log.i(LOG_TAG, "Preparing Value-Px Matrix, xmin: " + mXAxis.mAxisMinimum + ", xmax: " - + mXAxis.mAxisMaximum + ", xdelta: " + mXAxis.mAxisRange); + protected open fun prepareValuePxMatrix() { + if (isLogEnabled) Log.i( + LOG_TAG, ("Preparing Value-Px Matrix, xmin: " + mXAxis!!.mAxisMinimum + ", xmax: " + + mXAxis!!.mAxisMaximum + ", xdelta: " + mXAxis!!.mAxisRange) + ) - mRightAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, - mXAxis.mAxisRange, - mAxisRight.mAxisRange, - mAxisRight.mAxisMinimum); - mLeftAxisTransformer.prepareMatrixValuePx(mXAxis.mAxisMinimum, - mXAxis.mAxisRange, - mAxisLeft.mAxisRange, - mAxisLeft.mAxisMinimum); + mRightAxisTransformer!!.prepareMatrixValuePx( + mXAxis!!.mAxisMinimum, + mXAxis!!.mAxisRange, + mAxisRight!!.mAxisRange, + mAxisRight!!.mAxisMinimum + ) + mLeftAxisTransformer!!.prepareMatrixValuePx( + mXAxis!!.mAxisMinimum, + mXAxis!!.mAxisRange, + mAxisLeft!!.mAxisRange, + mAxisLeft!!.mAxisMinimum + ) } - protected void prepareOffsetMatrix() { - - mRightAxisTransformer.prepareMatrixOffset(mAxisRight.isInverted()); - mLeftAxisTransformer.prepareMatrixOffset(mAxisLeft.isInverted()); + protected fun prepareOffsetMatrix() { + mRightAxisTransformer!!.prepareMatrixOffset(mAxisRight!!.isInverted) + mLeftAxisTransformer!!.prepareMatrixOffset(mAxisLeft!!.isInverted) } - @Override - public void notifyDataSetChanged() { - + override fun notifyDataSetChanged() { if (mData == null) { - if (mLogEnabled) - Log.i(LOG_TAG, "Preparing... DATA NOT SET."); - return; + if (isLogEnabled) Log.i(LOG_TAG, "Preparing... DATA NOT SET.") + return } else { - if (mLogEnabled) - Log.i(LOG_TAG, "Preparing..."); + if (isLogEnabled) Log.i(LOG_TAG, "Preparing...") } - if (mRenderer != null) - mRenderer.initBuffers(); + if (mRenderer != null) mRenderer!!.initBuffers() - calcMinMax(); + calcMinMax() - mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum, mAxisLeft.isInverted()); - mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum, mAxisRight.isInverted()); - mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false); + mAxisRendererLeft!!.computeAxis(mAxisLeft!!.mAxisMinimum, mAxisLeft!!.mAxisMaximum, mAxisLeft!!.isInverted) + mAxisRendererRight!!.computeAxis(mAxisRight!!.mAxisMinimum, mAxisRight!!.mAxisMaximum, mAxisRight!!.isInverted) + mXAxisRenderer!!.computeAxis(mXAxis!!.mAxisMinimum, mXAxis!!.mAxisMaximum, false) - if (mLegend != null) - mLegendRenderer.computeLegend(mData); + if (legend != null) legendRenderer?.computeLegend(mData!!) - calculateOffsets(); + calculateOffsets() } /** * Performs auto scaling of the axis by recalculating the minimum and maximum y-values based on the entries currently in view. */ - protected void autoScale() { - - final float fromX = getLowestVisibleX(); - final float toX = getHighestVisibleX(); + protected fun autoScale() { + val fromX = lowestVisibleX + val toX = highestVisibleX - mData.calcMinMaxY(fromX, toX); + mData!!.calcMinMaxY(fromX, toX) - calcMinMax(); + calcMinMax() } - @Override - protected void calcMinMax() { - - mXAxis.calculate(mData.getXMin(), mData.getXMax()); + override fun calcMinMax() { + mXAxis?.calculate(mData!!.xMin, mData!!.xMax) // calculate axis range (min / max) according to provided data - mAxisLeft.calculate(mData.getYMin(AxisDependency.LEFT), mData.getYMax(AxisDependency.LEFT)); - mAxisRight.calculate(mData.getYMin(AxisDependency.RIGHT), mData.getYMax(AxisDependency - .RIGHT)); - } - - protected void calculateLegendOffsets(RectF offsets) { - - offsets.left = 0.f; - offsets.right = 0.f; - offsets.top = 0.f; - offsets.bottom = 0.f; - - if (mLegend == null || !mLegend.isEnabled() || mLegend.isDrawInsideEnabled()) - return; - - switch (mLegend.getOrientation()) { - case VERTICAL: - - switch (mLegend.getHorizontalAlignment()) { - case LEFT: - offsets.left += Math.min(mLegend.mNeededWidth, - mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent()) - + mLegend.getXOffset(); - break; - - case RIGHT: - offsets.right += Math.min(mLegend.mNeededWidth, - mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent()) - + mLegend.getXOffset(); - break; - - case CENTER: - - switch (mLegend.getVerticalAlignment()) { - case TOP: - offsets.top += Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) - + mLegend.getYOffset(); - break; - - case BOTTOM: - offsets.bottom += Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) - + mLegend.getYOffset(); - break; - - default: - break; - } + mAxisLeft!!.calculate(mData!!.getYMin(AxisDependency.LEFT), mData!!.getYMax(AxisDependency.LEFT)) + mAxisRight!!.calculate(mData!!.getYMin(AxisDependency.RIGHT), mData!!.getYMax(AxisDependency.RIGHT)) + } + + protected open fun calculateLegendOffsets(offsets: RectF) { + offsets.left = 0f + offsets.right = 0f + offsets.top = 0f + offsets.bottom = 0f + + if (legend == null || !legend!!.isEnabled || legend!!.isDrawInsideEnabled) return + + when (legend!!.orientation) { + LegendOrientation.VERTICAL -> when (legend!!.horizontalAlignment) { + LegendHorizontalAlignment.LEFT -> offsets.left += min( + legend!!.mNeededWidth, + viewPortHandler.chartWidth * legend!!.maxSizePercent + ) + legend!!.xOffset + + LegendHorizontalAlignment.RIGHT -> offsets.right += min( + legend!!.mNeededWidth, + viewPortHandler.chartWidth * legend!!.maxSizePercent + ) + legend!!.xOffset + + LegendHorizontalAlignment.CENTER -> when (legend!!.verticalAlignment) { + LegendVerticalAlignment.TOP -> offsets.top += min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + legend!!.yOffset + + LegendVerticalAlignment.BOTTOM -> offsets.bottom += min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + legend!!.yOffset + + else -> {} } + else -> {} + } - break; - - case HORIZONTAL: - - switch (mLegend.getVerticalAlignment()) { - case TOP: - offsets.top += Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) - + mLegend.getYOffset(); - - - break; + LegendOrientation.HORIZONTAL -> when (legend!!.verticalAlignment) { + LegendVerticalAlignment.TOP -> offsets.top += min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + legend!!.yOffset - case BOTTOM: - offsets.bottom += Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) - + mLegend.getYOffset(); + LegendVerticalAlignment.BOTTOM -> offsets.bottom += min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + legend!!.yOffset - break; - default: - break; - } - break; + else -> {} + } } } - private RectF mOffsetsBuffer = new RectF(); - - @Override - public void calculateOffsets() { + private val mOffsetsBuffer = RectF() + public override fun calculateOffsets() { if (!mCustomViewPortEnabled) { + var offsetLeft = 0f + var offsetRight = 0f + var offsetTop = 0f + var offsetBottom = 0f - float offsetLeft = 0f, offsetRight = 0f, offsetTop = 0f, offsetBottom = 0f; + calculateLegendOffsets(mOffsetsBuffer) - calculateLegendOffsets(mOffsetsBuffer); - - offsetLeft += mOffsetsBuffer.left; - offsetTop += mOffsetsBuffer.top; - offsetRight += mOffsetsBuffer.right; - offsetBottom += mOffsetsBuffer.bottom; + offsetLeft += mOffsetsBuffer.left + offsetTop += mOffsetsBuffer.top + offsetRight += mOffsetsBuffer.right + offsetBottom += mOffsetsBuffer.bottom // offsets for y-labels - if (mAxisLeft.needsOffset()) { - offsetLeft += mAxisLeft.getRequiredWidthSpace(mAxisRendererLeft - .getPaintAxisLabels()); + if (mAxisLeft!!.needsOffset()) { + offsetLeft += mAxisLeft!!.getRequiredWidthSpace( + mAxisRendererLeft!! + .paintAxisLabels + ) } - if (mAxisRight.needsOffset()) { - offsetRight += mAxisRight.getRequiredWidthSpace(mAxisRendererRight - .getPaintAxisLabels()); + if (mAxisRight!!.needsOffset()) { + offsetRight += mAxisRight!!.getRequiredWidthSpace( + mAxisRendererRight!! + .paintAxisLabels + ) } - if (mXAxis.isEnabled() && mXAxis.isDrawLabelsEnabled()) { - - float xLabelHeight = mXAxis.mLabelHeight + mXAxis.getYOffset(); + if (mXAxis!!.isEnabled && mXAxis!!.isDrawLabelsEnabled) { + val xLabelHeight = mXAxis!!.mLabelHeight + mXAxis!!.yOffset // offsets for x-labels - if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { - - offsetBottom += xLabelHeight; - - } else if (mXAxis.getPosition() == XAxisPosition.TOP) { - - offsetTop += xLabelHeight; - - } else if (mXAxis.getPosition() == XAxisPosition.BOTH_SIDED) { - - offsetBottom += xLabelHeight; - offsetTop += xLabelHeight; + if (mXAxis!!.position == XAxisPosition.BOTTOM) { + offsetBottom += xLabelHeight + } else if (mXAxis!!.position == XAxisPosition.TOP) { + offsetTop += xLabelHeight + } else if (mXAxis!!.position == XAxisPosition.BOTH_SIDED) { + offsetBottom += xLabelHeight + offsetTop += xLabelHeight } } - offsetTop += getExtraTopOffset(); - offsetRight += getExtraRightOffset(); - offsetBottom += getExtraBottomOffset(); - offsetLeft += getExtraLeftOffset(); - - float minOffset = Utils.convertDpToPixel(mMinOffset); - - mViewPortHandler.restrainViewPort( - Math.max(minOffset, offsetLeft), - Math.max(minOffset, offsetTop), - Math.max(minOffset, offsetRight), - Math.max(minOffset, offsetBottom)); - - if (mLogEnabled) { - Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop - + ", offsetRight: " + offsetRight + ", offsetBottom: " + offsetBottom); - Log.i(LOG_TAG, "Content: " + mViewPortHandler.getContentRect().toString()); + offsetTop += extraTopOffset + offsetRight += extraRightOffset + offsetBottom += extraBottomOffset + offsetLeft += extraLeftOffset + + val minOffset = Utils.convertDpToPixel(this.minOffset) + + viewPortHandler.restrainViewPort( + max(minOffset, offsetLeft), + max(minOffset, offsetTop), + max(minOffset, offsetRight), + max(minOffset, offsetBottom) + ) + + if (isLogEnabled) { + Log.i( + LOG_TAG, ("offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop + + ", offsetRight: " + offsetRight + ", offsetBottom: " + offsetBottom) + ) + Log.i(LOG_TAG, "Content: " + viewPortHandler.contentRect.toString()) } } - prepareOffsetMatrix(); - prepareValuePxMatrix(); + prepareOffsetMatrix() + prepareValuePxMatrix() } /** * draws the grid background */ - protected void drawGridBackground(Canvas c) { - + protected fun drawGridBackground(c: Canvas) { if (mDrawGridBackground) { - // draw the grid background - c.drawRect(mViewPortHandler.getContentRect(), mGridBackgroundPaint); + + c.drawRect(viewPortHandler.contentRect, mGridBackgroundPaint!!) } - if (mDrawBorders) { - c.drawRect(mViewPortHandler.getContentRect(), mBorderPaint); + if (this.isDrawBordersEnabled) { + c.drawRect(viewPortHandler.contentRect, mBorderPaint!!) } } @@ -559,32 +613,23 @@ public abstract class BarLineChartBase? { + val h = getHighlightByTouchPoint(x, y) if (h != null) { - return mData.getDataSetByIndex(h.getDataSetIndex()); + return mData!!.getDataSetByIndex(h.dataSetIndex) } - return null; + return null } /** * buffer for storing lowest visible x point */ - protected MPPointD posForGetLowestVisibleX = MPPointD.getInstance(0, 0); - - /** - * Returns the lowest x-index (value on the x-axis) that is still visible on - * the chart. - * - * @return - */ - @Override - public float getLowestVisibleX() { - getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentLeft(), - mViewPortHandler.contentBottom(), posForGetLowestVisibleX); - float result = (float) Math.max(mXAxis.mAxisMinimum, posForGetLowestVisibleX.x); - return result; - } + protected var posForGetLowestVisibleX: MPPointD = MPPointD.Companion.getInstance(0.0, 0.0) + + override val lowestVisibleX: Float + /** + * Returns the lowest x-index (value on the x-axis) that is still visible on + * the chart. + * + * @return + */ + get() { + getTransformer(AxisDependency.LEFT).getValuesByTouchPoint( + viewPortHandler.contentLeft(), + viewPortHandler.contentBottom(), posForGetLowestVisibleX + ) + val result = max(mXAxis!!.mAxisMinimum.toDouble(), posForGetLowestVisibleX.x).toFloat() + return result + } /** * buffer for storing highest visible x point */ - protected MPPointD posForGetHighestVisibleX = MPPointD.getInstance(0, 0); - - /** - * Returns the highest x-index (value on the x-axis) that is still visible - * on the chart. - * - * @return - */ - @Override - public float getHighestVisibleX() { - getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentRight(), - mViewPortHandler.contentBottom(), posForGetHighestVisibleX); - float result = (float) Math.min(mXAxis.mAxisMaximum, posForGetHighestVisibleX.x); - return result; - } + protected var posForGetHighestVisibleX: MPPointD = MPPointD.Companion.getInstance(0.0, 0.0) + + override val highestVisibleX: Float + /** + * Returns the highest x-index (value on the x-axis) that is still visible + * on the chart. + * + * @return + */ + get() { + getTransformer(AxisDependency.LEFT).getValuesByTouchPoint( + viewPortHandler.contentRight(), + viewPortHandler.contentBottom(), posForGetHighestVisibleX + ) + val result = min(mXAxis!!.mAxisMaximum.toDouble(), posForGetHighestVisibleX.x).toFloat() + return result + } - /** - * Returns the range visible on the x-axis. - * - * @return - */ - public float getVisibleXRange() { - return Math.abs(getHighestVisibleX() - getLowestVisibleX()); - } + val visibleXRange: Float + /** + * Returns the range visible on the x-axis. + * + * @return + */ + get() = abs(highestVisibleX - lowestVisibleX) /** * returns the current x-scale factor */ - public float getScaleX() { - if (mViewPortHandler == null) - return 1f; - else - return mViewPortHandler.getScaleX(); + override fun getScaleX(): Float { + return viewPortHandler.scaleX } /** * returns the current y-scale factor */ - public float getScaleY() { - if (mViewPortHandler == null) - return 1f; - else - return mViewPortHandler.getScaleY(); - } - - /** - * if the chart is fully zoomed out, return true - * - * @return - */ - public boolean isFullyZoomedOut() { - return mViewPortHandler.isFullyZoomedOut(); - } - - /** - * Returns the left y-axis object. In the horizontal bar-chart, this is the - * top axis. - * - * @return - */ - public YAxis getAxisLeft() { - return mAxisLeft; - } - - /** - * Returns the right y-axis object. In the horizontal bar-chart, this is the - * bottom axis. - * - * @return - */ - public YAxis getAxisRight() { - return mAxisRight; - } + override fun getScaleY(): Float { + return viewPortHandler.scaleY + } + + val isFullyZoomedOut: Boolean + /** + * if the chart is fully zoomed out, return true + * + * @return + */ + get() = viewPortHandler.isFullyZoomedOut + + val axisLeft: YAxis + /** + * Returns the left y-axis object. In the horizontal bar-chart, this is the + * top axis. + * + * @return + */ + get() = mAxisLeft!! + + val axisRight: YAxis + /** + * Returns the right y-axis object. In the horizontal bar-chart, this is the + * bottom axis. + * + * @return + */ + get() = mAxisRight!! /** * Returns the y-axis object to the corresponding AxisDependency. In the @@ -1493,16 +1351,13 @@ public abstract class BarLineChartBase mGridBackgroundPaint = p } } - @Override - public Paint getPaint(int which) { - Paint p = super.getPaint(which); - if (p != null) - return p; + override fun getPaint(which: Int): Paint? { + val p = super.getPaint(which) + if (p != null) return p - switch (which) { - case PAINT_GRID_BACKGROUND: - return mGridBackgroundPaint; + when (which) { + PAINT_GRID_BACKGROUND -> return mGridBackgroundPaint } - return null; + return null } - protected float[] mOnSizeChangedBuffer = new float[2]; - - @Override - protected void onSizeChanged(int w, int h, int oldw, int oldh) { + protected var mOnSizeChangedBuffer: FloatArray = FloatArray(2) + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { // Saving current position of chart. - mOnSizeChangedBuffer[0] = mOnSizeChangedBuffer[1] = 0; - if (mKeepPositionOnRotation) { - mOnSizeChangedBuffer[0] = mViewPortHandler.contentLeft(); - mOnSizeChangedBuffer[1] = mViewPortHandler.contentTop(); - getTransformer(AxisDependency.LEFT).pixelsToValue(mOnSizeChangedBuffer); + mOnSizeChangedBuffer[1] = 0f + mOnSizeChangedBuffer[0] = mOnSizeChangedBuffer[1] + + if (this.isKeepPositionOnRotation) { + mOnSizeChangedBuffer[0] = viewPortHandler.contentLeft() + mOnSizeChangedBuffer[1] = viewPortHandler.contentTop() + getTransformer(AxisDependency.LEFT).pixelsToValue(mOnSizeChangedBuffer) } //Superclass transforms chart. - super.onSizeChanged(w, h, oldw, oldh); - - if (mKeepPositionOnRotation) { + super.onSizeChanged(w, h, oldw, oldh) + if (this.isKeepPositionOnRotation) { //Restoring old position of chart. - getTransformer(AxisDependency.LEFT).pointValuesToPixel(mOnSizeChangedBuffer); - mViewPortHandler.centerViewPort(mOnSizeChangedBuffer, this); + + getTransformer(AxisDependency.LEFT).pointValuesToPixel(mOnSizeChangedBuffer) + viewPortHandler.centerViewPort(mOnSizeChangedBuffer, this) } else { - mViewPortHandler.refresh(mViewPortHandler.getMatrixTouch(), this, true); + viewPortHandler.refresh(viewPortHandler.matrixTouch, this, true) } } @@ -1693,9 +1503,9 @@ public abstract class BarLineChartBase implements BubbleDataProvider { - - public BubbleChart(Context context) { - super(context); - } +class BubbleChart : BarLineChartBase, BubbleDataProvider { + constructor(context: Context) : super(context) - public BubbleChart(Context context, AttributeSet attrs) { - super(context, attrs); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - public BubbleChart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - @Override - protected void init() { - super.init(); + override fun init() { + super.init() - mRenderer = new BubbleChartRenderer(this, mAnimator, mViewPortHandler); + mRenderer = BubbleChartRenderer(this, mAnimator, viewPortHandler) } - public BubbleData getBubbleData() { - return mData; - } + override val bubbleData: BubbleData? + get() = mData - @Override - public String getAccessibilityDescription() { - return "This is bubble chart"; - } + override val accessibilityDescription: String? + get() = "This is bubble chart" } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt index 29ba92d56b..74d46e7f32 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt @@ -1,49 +1,37 @@ +package com.github.mikephil.charting.charts -package com.github.mikephil.charting.charts; - -import android.content.Context; -import android.util.AttributeSet; - -import com.github.mikephil.charting.data.CandleData; -import com.github.mikephil.charting.interfaces.dataprovider.CandleDataProvider; -import com.github.mikephil.charting.renderer.CandleStickChartRenderer; +import android.content.Context +import android.util.AttributeSet +import com.github.mikephil.charting.data.CandleData +import com.github.mikephil.charting.data.CandleEntry +import com.github.mikephil.charting.interfaces.dataprovider.CandleDataProvider +import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet +import com.github.mikephil.charting.renderer.CandleStickChartRenderer /** * Financial chart type that draws candle-sticks (OHCL chart). * * @author Philipp Jahoda */ -public class CandleStickChart extends BarLineChartBase implements CandleDataProvider { - - public CandleStickChart(Context context) { - super(context); - } +class CandleStickChart : BarLineChartBase, CandleDataProvider { + constructor(context: Context) : super(context) - public CandleStickChart(Context context, AttributeSet attrs) { - super(context, attrs); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - public CandleStickChart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - @Override - protected void init() { - super.init(); + override fun init() { + super.init() - mRenderer = new CandleStickChartRenderer(this, mAnimator, mViewPortHandler); + mRenderer = CandleStickChartRenderer(this, mAnimator, viewPortHandler) - getXAxis().setSpaceMin(0.5f); - getXAxis().setSpaceMax(0.5f); + xAxis.spaceMin = 0.5f + xAxis.spaceMax = 0.5f } - @Override - public CandleData getCandleData() { - return mData; - } + override val candleData: CandleData? + get() = mData - @Override - public String getAccessibilityDescription() { - return "This is a candlestick chart"; - } + override val accessibilityDescription: String? + get() = "This is a candlestick chart" } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.kt index 5ca21a9885..fbb81c2bb3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.kt @@ -1,1716 +1,1560 @@ -package com.github.mikephil.charting.charts; - -import android.animation.ValueAnimator; -import android.animation.ValueAnimator.AnimatorUpdateListener; -import android.content.ContentValues; -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.Bitmap.CompressFormat; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Paint.Align; -import android.graphics.RectF; -import android.graphics.Typeface; -import android.graphics.drawable.Drawable; -import android.os.Environment; -import android.provider.MediaStore.Images; -import android.text.TextUtils; -import android.util.AttributeSet; -import android.util.Log; -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewParent; -import android.view.accessibility.AccessibilityEvent; - -import com.github.mikephil.charting.animation.ChartAnimator; -import com.github.mikephil.charting.animation.Easing.EasingFunction; -import com.github.mikephil.charting.components.Description; -import com.github.mikephil.charting.components.IMarker; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.formatter.DefaultValueFormatter; -import com.github.mikephil.charting.formatter.IValueFormatter; -import com.github.mikephil.charting.highlight.ChartHighlighter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.highlight.IHighlighter; -import com.github.mikephil.charting.interfaces.dataprovider.ChartInterface; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.listener.ChartTouchListener; -import com.github.mikephil.charting.listener.OnChartGestureListener; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.renderer.DataRenderer; -import com.github.mikephil.charting.renderer.LegendRenderer; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +package com.github.mikephil.charting.charts + +import android.animation.ValueAnimator +import android.animation.ValueAnimator.AnimatorUpdateListener +import android.content.ContentValues +import android.content.Context +import android.graphics.Bitmap +import android.graphics.Bitmap.CompressFormat +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.Paint.Align +import android.graphics.RectF +import android.graphics.Typeface +import android.os.Environment +import android.provider.MediaStore +import android.text.TextUtils +import android.util.AttributeSet +import android.util.Log +import android.view.View +import android.view.ViewGroup +import android.view.accessibility.AccessibilityEvent +import com.github.mikephil.charting.animation.ChartAnimator +import com.github.mikephil.charting.animation.Easing.EasingFunction +import com.github.mikephil.charting.components.Description +import com.github.mikephil.charting.components.IMarker +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.XAxis +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.formatter.DefaultValueFormatter +import com.github.mikephil.charting.formatter.IValueFormatter +import com.github.mikephil.charting.highlight.ChartHighlighter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.highlight.IHighlighter +import com.github.mikephil.charting.interfaces.dataprovider.ChartInterface +import com.github.mikephil.charting.interfaces.datasets.IDataSet +import com.github.mikephil.charting.listener.ChartTouchListener +import com.github.mikephil.charting.listener.OnChartGestureListener +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.renderer.DataRenderer +import com.github.mikephil.charting.renderer.LegendRenderer +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.ViewPortHandler +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.io.OutputStream +import kotlin.math.abs +import kotlin.math.max +import androidx.core.graphics.createBitmap +import androidx.core.view.size /** * Baseclass of all Chart-Views. * * @author Philipp Jahoda */ -@SuppressWarnings("unused") -public abstract class Chart>> extends ViewGroup implements ChartInterface { - - public static final String LOG_TAG = "MPAndroidChart"; - - /** - * flag that indicates if logging is enabled or not - */ - protected boolean mLogEnabled = false; - - /** - * object that holds all data that was originally set for the chart, before - * it was modified or any filtering algorithms had been applied - */ - protected T mData = null; - - /** - * Flag that indicates if highlighting per tap (touch) is enabled - */ - protected boolean mHighLightPerTapEnabled = true; - - /** - * If set to true, chart continues to scroll after touch up - */ - private boolean mDragDecelerationEnabled = true; - - /** - * Deceleration friction coefficient in [0 ; 1] interval, higher values - * indicate that speed will decrease slowly, for example if it set to 0, it - * will stop immediately. 1 is an invalid value, and will be converted to - * 0.999f automatically. - */ - private float mDragDecelerationFrictionCoef = 0.9f; - - /** - * default value-formatter, number of digits depends on provided chart-data - */ - protected DefaultValueFormatter mDefaultValueFormatter = new DefaultValueFormatter(0); - - /** - * paint object used for drawing the description text in the bottom right - * corner of the chart - */ - protected Paint mDescPaint; - - /** - * paint object for drawing the information text when there are no values in - * the chart - */ - protected Paint mInfoPaint; - - /** - * the object representing the labels on the x-axis - */ - protected XAxis mXAxis; - - /** - * if true, touch gestures are enabled on the chart - */ - protected boolean mTouchEnabled = true; - - /** - * the object responsible for representing the description text - */ - protected Description mDescription; - - /** - * the legend object containing all data associated with the legend - */ - protected Legend mLegend; - - /** - * listener that is called when a value on the chart is selected - */ - protected OnChartValueSelectedListener mSelectionListener; - - protected ChartTouchListener mChartTouchListener; - - /** - * text that is displayed when the chart is empty - */ - private String mNoDataText = "No chart data available."; - - /** - * Gesture listener for custom callbacks when making gestures on the chart. - */ - private OnChartGestureListener mGestureListener; - - protected LegendRenderer mLegendRenderer; - - /** - * object responsible for rendering the data - */ - protected DataRenderer mRenderer; - - protected IHighlighter mHighlighter; - - /** - * object that manages the bounds and drawing constraints of the chart - */ - protected ViewPortHandler mViewPortHandler = new ViewPortHandler(); - - /** - * object responsible for animations - */ - protected ChartAnimator mAnimator; - - /** - * Extra offsets to be appended to the viewport - */ - private float mExtraTopOffset = 0.f, mExtraRightOffset = 0.f, mExtraBottomOffset = 0.f, mExtraLeftOffset = 0.f; +@Suppress("unused") +abstract class Chart, T : ChartData> : ViewGroup, ChartInterface { + /** + * Returns true if log-output is enabled for the chart, fals if not. + */ + /** + * Set this to true to enable logcat outputs for the chart. Beware that + * logcat output decreases rendering performance. Default: disabled. + */ + /** + * flag that indicates if logging is enabled or not + */ + var isLogEnabled: Boolean = false + + override val width: Int + get() = getWidth() + + override val height: Int + get() = getHeight() + + /** + * object that holds all data that was originally set for the chart, before + * it was modified or any filtering algorithms had been applied + */ + protected var mData: T? = null + + /** + * Returns true if values can be highlighted via tap gesture, false if not. + */ + /** + * Set this to false to prevent values from being highlighted by tap gesture. + * Values can still be highlighted via drag or programmatically. Default: true + */ + /** + * Flag that indicates if highlighting per tap (touch) is enabled + */ + var isHighlightPerTapEnabled: Boolean = true + + /** + * If set to true, chart continues to scroll after touch up default: true + */ + /** + * If set to true, chart continues to scroll after touch up. Default: true. + */ + /** + * If set to true, chart continues to scroll after touch up + */ + var isDragDecelerationEnabled: Boolean = true + + /** + * Deceleration friction coefficient in [0 ; 1] interval, higher values + * indicate that speed will decrease slowly, for example if it set to 0, it + * will stop immediately. 1 is an invalid value, and will be converted to + * 0.999f automatically. + */ + private var mDragDecelerationFrictionCoef = 0.9f + + /** + * default value-formatter, number of digits depends on provided chart-data + */ + protected var mDefaultValueFormatter: DefaultValueFormatter = DefaultValueFormatter(0) + + /** + * paint object used for drawing the description text in the bottom right + * corner of the chart + */ + protected var mDescPaint: Paint? = null + + /** + * paint object for drawing the information text when there are no values in + * the chart + */ + protected var mInfoPaint: Paint? = null + + /** + * the object representing the labels on the x-axis + */ + protected var mXAxis: XAxis? = null + + /** + * if true, touch gestures are enabled on the chart + */ + protected var mTouchEnabled: Boolean = true + + /** + * Returns the Description object of the chart that is responsible for holding all information related + * to the description text that is displayed in the bottom right corner of the chart (by default). + */ + /** + * Sets a new Description object for the chart. + */ + /** + * the object responsible for representing the description text + */ + var description: Description? = null + + /** + * Returns the Legend object of the chart. This method can be used to get an + * instance of the legend in order to customize the automatically generated + * Legend. + */ + /** + * the legend object containing all data associated with the legend + */ + var legend: Legend? = null + protected set + + /** + * listener that is called when a value on the chart is selected + */ + protected var mSelectionListener: OnChartValueSelectedListener? = null + + protected var mChartTouchListener: ChartTouchListener<*>? = null + + /** + * text that is displayed when the chart is empty + */ + private var mNoDataText = "No chart data available." + + /** + * Returns the custom gesture listener. + */ + /** + * Sets a gesture-listener for the chart for custom callbacks when executing + * gestures on the chart surface. + */ + /** + * Gesture listener for custom callbacks when making gestures on the chart. + */ + var onChartGestureListener: OnChartGestureListener? = null + + /** + * Returns the renderer object responsible for rendering / drawing the + * Legend. + */ + var legendRenderer: LegendRenderer? = null + protected set + + /** + * object responsible for rendering the data + */ + protected var mRenderer: DataRenderer? = null + + var highlighter: IHighlighter? = null + protected set + + /** + * Returns the ViewPortHandler of the chart that is responsible for the + * content area of the chart and its offsets and dimensions. + */ + /** + * object that manages the bounds and drawing constraints of the chart + */ + var viewPortHandler: ViewPortHandler = ViewPortHandler() + protected set + + /** + * object responsible for animations + */ + protected val mAnimator = ChartAnimator { // ViewCompat.postInvalidateOnAnimation(Chart.this); + postInvalidate() + } + + /** + * Extra offsets to be appended to the viewport + */ + private var mExtraTopOffset = 0f + private var mExtraRightOffset = 0f + private var mExtraBottomOffset = 0f + private var mExtraLeftOffset = 0f + + /** + * Additional data on top of dynamically generated description. This can be set by the user. + */ + var accessibilitySummaryDescription: String? = "" + + /** + * default constructor for initialization in code + */ + constructor(context: Context) : super(context) { + init() + } + + /** + * constructor for initialization in xml + */ + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { + init() + } + + /** + * even more awesome constructor + */ + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) { + init() + } + + /** + * initialize all paints and stuff + */ + protected open fun init() { + setWillNotDraw(false) + + // initialize the utils + Utils.init(context) + mMaxHighlightDistance = Utils.convertDpToPixel(500f) + + this.description = Description() + this.legend = Legend() + + this.legendRenderer = LegendRenderer(this.viewPortHandler, this.legend!!) + + mXAxis = XAxis() + + mDescPaint = Paint(Paint.ANTI_ALIAS_FLAG) + + mInfoPaint = Paint(Paint.ANTI_ALIAS_FLAG) + mInfoPaint!!.setColor(Color.rgb(247, 189, 51)) // orange + mInfoPaint!!.setTextAlign(Align.CENTER) + mInfoPaint!!.setTextSize(Utils.convertDpToPixel(12f)) + + if (this.isLogEnabled) { + Log.i("", "Chart.init()") + + // enable being detected by ScreenReader + setFocusable(true) + } + } + + // public void initWithDummyData() { + // ColorTemplate template = new ColorTemplate(); + // template.addColorsForDataSets(ColorTemplate.COLORFUL_COLORS, + // getContext()); + // + // setColorTemplate(template); + // setDrawYValues(false); + // + // ArrayList xVals = new ArrayList(); + // Calendar calendar = Calendar.getInstance(); + // for (int i = 0; i < 12; i++) { + // xVals.add(calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, + // Locale.getDefault())); + // } + // + // ArrayList dataSets = new ArrayList(); + // for (int i = 0; i < 3; i++) { + // + // ArrayList yVals = new ArrayList(); + // + // for (int j = 0; j < 12; j++) { + // float val = (float) (Math.random() * 100); + // yVals.add(new Entry(val, j)); + // } + // + // DataSet set = new DataSet(yVals, "DataSet " + i); + // dataSets.add(set); // add the datasets + // } + // // create a data object with the datasets + // ChartData data = new ChartData(xVals, dataSets); + // setData(data); + // invalidate(); + // } + /** + * Sets a new data object for the chart. The data object contains all values + * and information needed for displaying. + */ + open fun setData(data: T?) { + mData = data + mOffsetsCalculated = false + + if (data == null) { + return + } + + // calculate how many digits are needed + setupDefaultFormatter(data.yMin, data.yMax) + + // let the chart know there is new data + notifyDataSetChanged() + + if (this.isLogEnabled) { + Log.i(LOG_TAG, "Data is set.") + } + } + + /** + * Clears the chart from all data (sets it to null) and refreshes it (by + * calling invalidate()). + */ + fun clear() { + mData = null + mOffsetsCalculated = false + this.highlighted = null + mChartTouchListener!!.setLastHighlighted(null) + invalidate() + } + + /** + * Removes all DataSets (and thereby Entries) from the chart. Does not set the data object to null. Also refreshes the + * chart by calling invalidate(). + */ + fun clearValues() { + mData!!.clearValues() + invalidate() + } + + val isEmpty: Boolean + /** + * Returns true if the chart is empty (meaning it's data object is either + * null or contains no entries). + */ + get() { + if (mData == null) { + return true + } else { + return mData!!.entryCount <= 0 + } + } + + /** + * Lets the chart know its underlying data has changed and performs all + * necessary recalculations. It is crucial that this method is called + * everytime data is changed dynamically. Not calling this method can lead + * to crashes or unexpected behaviour. + */ + abstract fun notifyDataSetChanged() + + /** + * Calculates the offsets of the chart to the border depending on the + * position of an eventual legend or depending on the length of the y-axis + * and x-axis labels and their position + */ + protected abstract fun calculateOffsets() + + /** + * Calculates the y-min and y-max value and the y-delta and x-delta value + */ + protected abstract fun calcMinMax() + + /** + * Calculates the required number of digits for the values that might be + * drawn in the chart (if enabled), and creates the default-value-formatter + */ + protected fun setupDefaultFormatter(min: Float, max: Float) { + val reference: Float + + if (mData == null || mData!!.entryCount < 2) { + reference = max(abs(min), abs(max)) + } else { + reference = abs(max - min) + } + + val digits = Utils.getDecimals(reference) + + // setup the formatter with a new number of digits + mDefaultValueFormatter.setup(digits) + } + + /** + * flag that indicates if offsets calculation has already been done or not + */ + private var mOffsetsCalculated = false + + override fun onDraw(canvas: Canvas) { + // super.onDraw(canvas); + + if (mData == null) { + val hasText = !TextUtils.isEmpty(mNoDataText) + + if (hasText) { + val pt = this.center + + when (mInfoPaint!!.textAlign) { + Align.LEFT -> { + pt.x = 0f + canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint!!) + } + + Align.RIGHT -> { + pt.x *= 2.0.toFloat() + canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint!!) + } + + else -> canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint!!) + } + } + + return + } + + if (!mOffsetsCalculated) { + calculateOffsets() + mOffsetsCalculated = true + } + } + + /** + * Draws the description text in the bottom right corner of the chart (per default) + */ + protected fun drawDescription(c: Canvas) { + // check if description should be drawn + + if (this.description != null && description!!.isEnabled) { + val position = description!!.position + + mDescPaint!!.setTypeface(description!!.typeface) + mDescPaint!!.textSize = description!!.textSize + mDescPaint!!.setColor(description!!.textColor) + mDescPaint!!.textAlign = description!!.textAlign + + val x: Float + val y: Float + + // if no position specified, draw on default position + if (position == null) { + x = width - viewPortHandler.offsetRight() - description!!.xOffset + y = height - viewPortHandler.offsetBottom() - description!!.yOffset + } else { + x = position.x + y = position.y + } + + c.drawText(description!!.text!!, x, y, mDescPaint!!) + } + } + + /** + * Returns the array of currently highlighted values. This might a null or + * empty array if nothing is highlighted. + */ + /** + * array of Highlight objects that reference the highlighted slices in the + * chart + */ + var highlighted: Array? = null + protected set + + /** + * The maximum distance in dp away from an entry causing it to highlight. + */ + protected var mMaxHighlightDistance: Float = 0f + + override val maxHighlightDistance: Float + get() = mMaxHighlightDistance + + /** + * Sets the maximum distance in screen dp a touch can be away from an entry to cause it to get highlighted. + * Default: 500dp + */ + fun setMaxHighlightDistance(distDp: Float) { + mMaxHighlightDistance = Utils.convertDpToPixel(distDp) + } + + /** + * Returns true if there are values to highlight, false if there are no + * values to highlight. Checks if the highlight array is null, has a length + * of zero or if the first object is null. + */ + fun valuesToHighlight(): Boolean { + return this.highlighted != null && highlighted!!.isNotEmpty() + } + + /** + * Sets the last highlighted value for the touchlistener. + */ + protected fun setLastHighlighted(highs: Array?) { + if (highs == null || highs.isEmpty()) { + mChartTouchListener!!.setLastHighlighted(null) + } else { + mChartTouchListener!!.setLastHighlighted(highs[0]) + } + } + + /** + * Highlights the values at the given indices in the given DataSets. Provide + * null or an empty array to undo all highlighting. This should be used to + * programmatically highlight values. + * This method *will not* call the listener. + */ + fun highlightValues(highs: Array?) { + // set the indices to highlight + + this.highlighted = highs + + setLastHighlighted(highs) + + // redraw the chart + invalidate() + } + + fun highlightValues(highs: MutableList, markers: MutableList) { + require(highs.size == markers.size) { "Markers and highs must be mutually corresponding. High size = " + highs.size + " Markers size = " + markers.size } + setMarkers(markers) + highlightValues(highs.toTypedArray()) + } + + /** + * Highlights any y-value at the given x-value in the given DataSet. + * Provide -1 as the dataSetIndex to undo all highlighting. + * This method will call the listener. + * @param x The x-value to highlight + * @param dataSetIndex The dataset index to search in + * @param dataIndex The data index to search in (only used in CombinedChartView currently) + */ + open fun highlightValue(x: Float, dataSetIndex: Int, dataIndex: Int) { + highlightValue(x, dataSetIndex, dataIndex, true) + } + + /** + * Highlights any y-value at the given x-value in the given DataSet. + * Provide -1 as the dataSetIndex to undo all highlighting. + * + * @param x The x-value to highlight + * @param dataSetIndex The dataset index to search in + * @param dataIndex The data index to search in (only used in CombinedChartView currently) + * @param callListener Should the listener be called for this change + */ + /** + * Highlights any y-value at the given x-value in the given DataSet. + * Provide -1 as the dataSetIndex to undo all highlighting. + * This method will call the listener. + * + * @param x The x-value to highlight + * @param dataSetIndex The dataset index to search in + */ + @JvmOverloads + fun highlightValue(x: Float, dataSetIndex: Int, dataIndex: Int = -1, callListener: Boolean = true) { + highlightValue(x, Float.Companion.NaN, dataSetIndex, dataIndex, callListener) + } + + /** + * Highlights any y-value at the given x-value in the given DataSet. + * Provide -1 as the dataSetIndex to undo all highlighting. + * + * @param x The x-value to highlight + * @param dataSetIndex The dataset index to search in + * @param callListener Should the listener be called for this change + */ + fun highlightValue(x: Float, dataSetIndex: Int, callListener: Boolean) { + highlightValue(x, Float.Companion.NaN, dataSetIndex, -1, callListener) + } + + /** + * Highlights any y-value at the given x-value in the given DataSet. + * Provide -1 as the dataSetIndex to undo all highlighting. + * + * @param x The x-value to highlight + * @param y The y-value to highlight. Supply `NaN` for "any" + * @param dataSetIndex The dataset index to search in + * @param dataIndex The data index to search in (only used in CombinedChartView currently) + * @param callListener Should the listener be called for this change + */ + /** + * Highlights the value at the given x-value and y-value in the given DataSet. + * Provide -1 as the dataSetIndex to undo all highlighting. + * This method will call the listener. + * + * @param x The x-value to highlight + * @param y The y-value to highlight. Supply `NaN` for "any" + * @param dataSetIndex The dataset index to search in + */ + /** + * Highlights the value at the given x-value and y-value in the given DataSet. + * Provide -1 as the dataSetIndex to undo all highlighting. + * This method will call the listener. + * + * @param x The x-value to highlight + * @param y The y-value to highlight. Supply `NaN` for "any" + * @param dataSetIndex The dataset index to search in + * @param dataIndex The data index to search in (only used in CombinedChartView currently) + */ + @JvmOverloads + fun highlightValue(x: Float, y: Float, dataSetIndex: Int, dataIndex: Int = -1, callListener: Boolean = true) { + if (dataSetIndex < 0 || dataSetIndex >= mData!!.dataSetCount) { + highlightValue(null, callListener) + } else { + highlightValue(Highlight(x, y, dataSetIndex, dataIndex), callListener) + } + } + + /** + * Highlights any y-value at the given x-value in the given DataSet. + * Provide -1 as the dataSetIndex to undo all highlighting. + * + * @param x The x-value to highlight + * @param y The y-value to highlight. Supply `NaN` for "any" + * @param dataSetIndex The dataset index to search in + * @param callListener Should the listener be called for this change + */ + fun highlightValue(x: Float, y: Float, dataSetIndex: Int, callListener: Boolean) { + highlightValue(x, y, dataSetIndex, -1, callListener) + } + + /** + * Highlights the values represented by the provided Highlight object + * This method *will not* call the listener. + * + * @param highlight contains information about which entry should be highlighted + */ + fun highlightValue(highlight: Highlight?) { + highlightValue(highlight, false) + } + + /** + * Highlights the value selected by touch gesture. Unlike + * highlightValues(...), this generates a callback to the + * OnChartValueSelectedListener. + * + * @param high - the highlight object + * @param callListener - call the listener + */ + fun highlightValue(high: Highlight?, callListener: Boolean) { + var high = high + var e: E? = null + + if (high == null) { + this.highlighted = null + } else { + if (this.isLogEnabled) { + Log.i(LOG_TAG, "Highlighted: " + high) + } + + e = mData!!.getEntryForHighlight(high) + if (e == null) { + this.highlighted = null + high = null + } else { + // set the indices to highlight + + this.highlighted = arrayOf(high) + } + } + + setLastHighlighted(this.highlighted) + + if (callListener && mSelectionListener != null) { + if (!valuesToHighlight()) { + mSelectionListener!!.onNothingSelected() + } else { + // notify the listener + mSelectionListener!!.onValueSelected(e, high) + } + } + + // redraw the chart + invalidate() + } /** - * Additional data on top of dynamically generated description. This can be set by the user. + * Returns the Highlight object (contains x-index and DataSet index) of the + * selected value at the given touch point inside the Line-, Scatter-, or + * CandleStick-Chart. */ - private String accessibilitySummaryDescription = ""; - - /** - * default constructor for initialization in code - */ - public Chart(Context context) { - super(context); - init(); - } - - /** - * constructor for initialization in xml - */ - public Chart(Context context, AttributeSet attrs) { - super(context, attrs); - init(); - } - - /** - * even more awesome constructor - */ - public Chart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(); - } - - /** - * initialize all paints and stuff - */ - protected void init() { - - setWillNotDraw(false); - // setLayerType(View.LAYER_TYPE_HARDWARE, null); - - mAnimator = new ChartAnimator(new AnimatorUpdateListener() { - - @Override - public void onAnimationUpdate(ValueAnimator animation) { - // ViewCompat.postInvalidateOnAnimation(Chart.this); - postInvalidate(); - } - }); - - // initialize the utils - Utils.init(getContext()); - mMaxHighlightDistance = Utils.convertDpToPixel(500f); - - mDescription = new Description(); - mLegend = new Legend(); - - mLegendRenderer = new LegendRenderer(mViewPortHandler, mLegend); - - mXAxis = new XAxis(); - - mDescPaint = new Paint(Paint.ANTI_ALIAS_FLAG); - - mInfoPaint = new Paint(Paint.ANTI_ALIAS_FLAG); - mInfoPaint.setColor(Color.rgb(247, 189, 51)); // orange - mInfoPaint.setTextAlign(Align.CENTER); - mInfoPaint.setTextSize(Utils.convertDpToPixel(12f)); - - if (mLogEnabled) { - Log.i("", "Chart.init()"); - - // enable being detected by ScreenReader - setFocusable(true); - }} - - // public void initWithDummyData() { - // ColorTemplate template = new ColorTemplate(); - // template.addColorsForDataSets(ColorTemplate.COLORFUL_COLORS, - // getContext()); - // - // setColorTemplate(template); - // setDrawYValues(false); - // - // ArrayList xVals = new ArrayList(); - // Calendar calendar = Calendar.getInstance(); - // for (int i = 0; i < 12; i++) { - // xVals.add(calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, - // Locale.getDefault())); - // } - // - // ArrayList dataSets = new ArrayList(); - // for (int i = 0; i < 3; i++) { - // - // ArrayList yVals = new ArrayList(); - // - // for (int j = 0; j < 12; j++) { - // float val = (float) (Math.random() * 100); - // yVals.add(new Entry(val, j)); - // } - // - // DataSet set = new DataSet(yVals, "DataSet " + i); - // dataSets.add(set); // add the datasets - // } - // // create a data object with the datasets - // ChartData data = new ChartData(xVals, dataSets); - // setData(data); - // invalidate(); - // } - - /** - * Sets a new data object for the chart. The data object contains all values - * and information needed for displaying. - */ - public void setData(T data) { - - mData = data; - mOffsetsCalculated = false; - - if (data == null) { - return; - } - - // calculate how many digits are needed - setupDefaultFormatter(data.getYMin(), data.getYMax()); - - for (IDataSet set : mData.getDataSets()) { - if (set.needsFormatter() || set.getValueFormatter() == mDefaultValueFormatter) { - set.setValueFormatter(mDefaultValueFormatter); - } - } - - // let the chart know there is new data - notifyDataSetChanged(); - - if (mLogEnabled) { - Log.i(LOG_TAG, "Data is set."); - } - } - - /** - * Clears the chart from all data (sets it to null) and refreshes it (by - * calling invalidate()). - */ - public void clear() { - mData = null; - mOffsetsCalculated = false; - mIndicesToHighlight = null; - mChartTouchListener.setLastHighlighted(null); - invalidate(); - } - - /** - * Removes all DataSets (and thereby Entries) from the chart. Does not set the data object to null. Also refreshes the - * chart by calling invalidate(). - */ - public void clearValues() { - mData.clearValues(); - invalidate(); - } - - /** - * Returns true if the chart is empty (meaning it's data object is either - * null or contains no entries). - */ - public boolean isEmpty() { - if (mData == null) { - return true; - } else { - return mData.getEntryCount() <= 0; - } - } - - /** - * Lets the chart know its underlying data has changed and performs all - * necessary recalculations. It is crucial that this method is called - * everytime data is changed dynamically. Not calling this method can lead - * to crashes or unexpected behaviour. - */ - public abstract void notifyDataSetChanged(); - - /** - * Calculates the offsets of the chart to the border depending on the - * position of an eventual legend or depending on the length of the y-axis - * and x-axis labels and their position - */ - protected abstract void calculateOffsets(); - - /** - * Calculates the y-min and y-max value and the y-delta and x-delta value - */ - protected abstract void calcMinMax(); - - /** - * Calculates the required number of digits for the values that might be - * drawn in the chart (if enabled), and creates the default-value-formatter - */ - protected void setupDefaultFormatter(float min, float max) { - - float reference; - - if (mData == null || mData.getEntryCount() < 2) { - reference = Math.max(Math.abs(min), Math.abs(max)); - } else { - reference = Math.abs(max - min); - } - - int digits = Utils.getDecimals(reference); - - // setup the formatter with a new number of digits - mDefaultValueFormatter.setup(digits); - } - - /** - * flag that indicates if offsets calculation has already been done or not - */ - private boolean mOffsetsCalculated = false; - - @Override - protected void onDraw(Canvas canvas) { - // super.onDraw(canvas); - - if (mData == null) { - - boolean hasText = !TextUtils.isEmpty(mNoDataText); - - if (hasText) { - MPPointF pt = getCenter(); - - switch (mInfoPaint.getTextAlign()) { - case LEFT: - pt.x = 0; - canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint); - break; - - case RIGHT: - pt.x *= 2.0; - canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint); - break; - - default: - canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint); - break; - } - } - - return; - } - - if (!mOffsetsCalculated) { - - calculateOffsets(); - mOffsetsCalculated = true; - } - } - - /** - * Draws the description text in the bottom right corner of the chart (per default) - */ - protected void drawDescription(Canvas c) { - - // check if description should be drawn - if (mDescription != null && mDescription.isEnabled()) { - - MPPointF position = mDescription.getPosition(); - - mDescPaint.setTypeface(mDescription.getTypeface()); - mDescPaint.setTextSize(mDescription.getTextSize()); - mDescPaint.setColor(mDescription.getTextColor()); - mDescPaint.setTextAlign(mDescription.getTextAlign()); - - float x, y; - - // if no position specified, draw on default position - if (position == null) { - x = getWidth() - mViewPortHandler.offsetRight() - mDescription.getXOffset(); - y = getHeight() - mViewPortHandler.offsetBottom() - mDescription.getYOffset(); - } else { - x = position.x; - y = position.y; - } - - c.drawText(mDescription.getText(), x, y, mDescPaint); - } - } - - /** - * array of Highlight objects that reference the highlighted slices in the - * chart - */ - protected Highlight[] mIndicesToHighlight; - - /** - * The maximum distance in dp away from an entry causing it to highlight. - */ - protected float mMaxHighlightDistance = 0f; - - @Override - public float getMaxHighlightDistance() { - return mMaxHighlightDistance; - } - - /** - * Sets the maximum distance in screen dp a touch can be away from an entry to cause it to get highlighted. - * Default: 500dp - */ - public void setMaxHighlightDistance(float distDp) { - mMaxHighlightDistance = Utils.convertDpToPixel(distDp); - } - - /** - * Returns the array of currently highlighted values. This might a null or - * empty array if nothing is highlighted. - */ - public Highlight[] getHighlighted() { - return mIndicesToHighlight; - } - - /** - * Returns true if values can be highlighted via tap gesture, false if not. - */ - public boolean isHighlightPerTapEnabled() { - return mHighLightPerTapEnabled; - } - - /** - * Set this to false to prevent values from being highlighted by tap gesture. - * Values can still be highlighted via drag or programmatically. Default: true - */ - public void setHighlightPerTapEnabled(boolean enabled) { - mHighLightPerTapEnabled = enabled; - } - - /** - * Returns true if there are values to highlight, false if there are no - * values to highlight. Checks if the highlight array is null, has a length - * of zero or if the first object is null. - */ - public boolean valuesToHighlight() { - return mIndicesToHighlight != null && mIndicesToHighlight.length > 0 && mIndicesToHighlight[0] != null; - } - - /** - * Sets the last highlighted value for the touchlistener. - */ - protected void setLastHighlighted(Highlight[] highs) { - - if (highs == null || highs.length <= 0 || highs[0] == null) { - mChartTouchListener.setLastHighlighted(null); - } else { - mChartTouchListener.setLastHighlighted(highs[0]); - } - } - - /** - * Highlights the values at the given indices in the given DataSets. Provide - * null or an empty array to undo all highlighting. This should be used to - * programmatically highlight values. - * This method *will not* call the listener. - */ - public void highlightValues(Highlight[] highs) { - - // set the indices to highlight - mIndicesToHighlight = highs; - - setLastHighlighted(highs); - - // redraw the chart - invalidate(); - } - - public void highlightValues(List highs, List markers) { - if (highs.size() != markers.size()) throw new IllegalArgumentException("Markers and highs must be mutually corresponding. High size = " + highs.size() + " Markers size = " + markers.size()); - setMarkers(markers); - highlightValues(highs.toArray(new Highlight[0])); + open fun getHighlightByTouchPoint(x: Float, y: Float): Highlight? { + if (mData == null) { + Log.e(LOG_TAG, "Can't select by touch. No data set.") + return null + } else { + return this.highlighter!!.getHighlight(x, y) + } } + var onTouchListener: ChartTouchListener<*> + /** + * Returns an instance of the currently active touch listener. + */ + get() = mChartTouchListener!! + /** + * Set a new (e.g. custom) ChartTouchListener NOTE: make sure to + * setTouchEnabled(true); if you need touch gestures on the chart + */ + set(l) { + this.mChartTouchListener = l + } + /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * This method will call the listener. - ** @param x The x-value to highlight - * @param dataSetIndex The dataset index to search in - * @param dataIndex The data index to search in (only used in CombinedChartView currently) - */ - public void highlightValue(float x, int dataSetIndex, int dataIndex) { - highlightValue(x, dataSetIndex, dataIndex, true); - } - - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * This method will call the listener. - * - * @param x The x-value to highlight - * @param dataSetIndex The dataset index to search in - */ - public void highlightValue(float x, int dataSetIndex) { - highlightValue(x, dataSetIndex, -1, true); - } - - /** - * Highlights the value at the given x-value and y-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * This method will call the listener. - * - * @param x The x-value to highlight - * @param y The y-value to highlight. Supply `NaN` for "any" - * @param dataSetIndex The dataset index to search in - * @param dataIndex The data index to search in (only used in CombinedChartView currently) - */ - public void highlightValue(float x, float y, int dataSetIndex, int dataIndex) { - highlightValue(x, y, dataSetIndex, dataIndex, true); - } - - /** - * Highlights the value at the given x-value and y-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * This method will call the listener. - * - * @param x The x-value to highlight - * @param y The y-value to highlight. Supply `NaN` for "any" - * @param dataSetIndex The dataset index to search in - */ - public void highlightValue(float x, float y, int dataSetIndex) { - highlightValue(x, y, dataSetIndex, -1, true); - } - - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * - * @param x The x-value to highlight - * @param dataSetIndex The dataset index to search in - * @param dataIndex The data index to search in (only used in CombinedChartView currently) - * @param callListener Should the listener be called for this change - */ - public void highlightValue(float x, int dataSetIndex, int dataIndex, boolean callListener) { - highlightValue(x, Float.NaN, dataSetIndex, dataIndex, callListener); - } - - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * - * @param x The x-value to highlight - * @param dataSetIndex The dataset index to search in - * @param callListener Should the listener be called for this change - */ - public void highlightValue(float x, int dataSetIndex, boolean callListener) { - highlightValue(x, Float.NaN, dataSetIndex, -1, callListener); - } - - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * - * @param x The x-value to highlight - * @param y The y-value to highlight. Supply `NaN` for "any" - * @param dataSetIndex The dataset index to search in - * @param dataIndex The data index to search in (only used in CombinedChartView currently) - * @param callListener Should the listener be called for this change - */ - public void highlightValue(float x, float y, int dataSetIndex, int dataIndex, boolean callListener) { - - if (dataSetIndex < 0 || dataSetIndex >= mData.getDataSetCount()) { - highlightValue(null, callListener); - } else { - highlightValue(new Highlight(x, y, dataSetIndex, dataIndex), callListener); - } - } - - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * - * @param x The x-value to highlight - * @param y The y-value to highlight. Supply `NaN` for "any" - * @param dataSetIndex The dataset index to search in - * @param callListener Should the listener be called for this change - */ - public void highlightValue(float x, float y, int dataSetIndex, boolean callListener) { - highlightValue(x, y, dataSetIndex, -1, callListener); - } - - /** - * Highlights the values represented by the provided Highlight object - * This method *will not* call the listener. - * - * @param highlight contains information about which entry should be highlighted - */ - public void highlightValue(Highlight highlight) { - highlightValue(highlight, false); - } - - /** - * Highlights the value selected by touch gesture. Unlike - * highlightValues(...), this generates a callback to the - * OnChartValueSelectedListener. - * - * @param high - the highlight object - * @param callListener - call the listener - */ - public void highlightValue(Highlight high, boolean callListener) { - - Entry e = null; - - if (high == null) { - mIndicesToHighlight = null; - } else { - - if (mLogEnabled) { - Log.i(LOG_TAG, "Highlighted: " + high); - } - - e = mData.getEntryForHighlight(high); - if (e == null) { - mIndicesToHighlight = null; - high = null; - } else { - - // set the indices to highlight - mIndicesToHighlight = new Highlight[]{high}; - } - } - - setLastHighlighted(mIndicesToHighlight); - - if (callListener && mSelectionListener != null) { - - if (!valuesToHighlight()) { - mSelectionListener.onNothingSelected(); - } else { - // notify the listener - mSelectionListener.onValueSelected(e, high); - } - } - - // redraw the chart - invalidate(); - } - - /** - * Returns the Highlight object (contains x-index and DataSet index) of the - * selected value at the given touch point inside the Line-, Scatter-, or - * CandleStick-Chart. - */ - public Highlight getHighlightByTouchPoint(float x, float y) { - - if (mData == null) { - Log.e(LOG_TAG, "Can't select by touch. No data set."); - return null; - } else { - return getHighlighter().getHighlight(x, y); - } - } - - /** - * Set a new (e.g. custom) ChartTouchListener NOTE: make sure to - * setTouchEnabled(true); if you need touch gestures on the chart - */ - public void setOnTouchListener(ChartTouchListener l) { - this.mChartTouchListener = l; - } - - /** - * Returns an instance of the currently active touch listener. - */ - public ChartTouchListener getOnTouchListener() { - return mChartTouchListener; - } - - /** - * if set to true, the marker view is drawn when a value is clicked - */ - protected boolean mDrawMarkers = true; - - /** - * the view that represents the marker - */ - protected List mMarkers = new ArrayList<>(); - - /** - * draws all MarkerViews on the highlighted positions - */ - protected void drawMarkers(Canvas canvas) { - - // if there is no marker view or drawing marker is disabled - if (mMarkers == null || !isDrawMarkersEnabled() || !valuesToHighlight()) { - return; - } - - for (int i = 0; i < mIndicesToHighlight.length; i++) { - - Highlight highlight = mIndicesToHighlight[i]; - - // When changing data sets and calling animation functions, sometimes an erroneous highlight is generated - // on the dataset that is removed. Null check to prevent crash - IDataSet set = mData.getDataSetByIndex(highlight.getDataSetIndex()); - if (set == null || !set.isVisible()) { - continue; - } - - Entry e = mData.getEntryForHighlight(highlight); - - // make sure entry not null before using it - if (e == null) { - continue; - } - - int entryIndex = set.getEntryIndex(e); - - if (entryIndex > set.getEntryCount() * mAnimator.getPhaseX()) { - continue; - } - - float[] pos = getMarkerPosition(highlight); - - // check bounds - if (!mViewPortHandler.isInBounds(pos[0], pos[1])) { - continue; - } - - // callbacks to update the content - if (!mMarkers.isEmpty()) { - int markerIndex = i % mMarkers.size(); - IMarker markerItem = mMarkers.get(markerIndex); - markerItem.refreshContent(e, highlight); - - // draw the marker - markerItem.draw(canvas, pos[0], pos[1]); - } - } - } - - /** - * Returns the actual position in pixels of the MarkerView for the given - * Highlight object. - */ - protected float[] getMarkerPosition(Highlight high) { - return new float[]{high.getDrawX(), high.getDrawY()}; - } - - /** - * Returns the animator responsible for animating chart values. - */ - public ChartAnimator getAnimator() { - return mAnimator; - } - - /** - * If set to true, chart continues to scroll after touch up default: true - */ - public boolean isDragDecelerationEnabled() { - return mDragDecelerationEnabled; - } - - /** - * If set to true, chart continues to scroll after touch up. Default: true. - */ - public void setDragDecelerationEnabled(boolean enabled) { - mDragDecelerationEnabled = enabled; - } - - /** - * Returns drag deceleration friction coefficient - */ - public float getDragDecelerationFrictionCoef() { - return mDragDecelerationFrictionCoef; - } - - /** - * Deceleration friction coefficient in [0 ; 1] interval, higher values - * indicate that speed will decrease slowly, for example if it set to 0, it - * will stop immediately. 1 is an invalid value, and will be converted to - * 0.999f automatically. - */ - public void setDragDecelerationFrictionCoef(float newValue) { - - if (newValue < 0.f) { - newValue = 0.f; - } - - if (newValue >= 1f) { - newValue = 0.999f; - } - - mDragDecelerationFrictionCoef = newValue; - } - - /** - * Animates the drawing / rendering of the chart on both x- and y-axis with - * the specified animation time. If animate(...) is called, no further - * calling of invalidate() is necessary to refresh the chart. ANIMATIONS - * ONLY WORK FOR API LEVEL 11 (Android 3.0.x) AND HIGHER. - * - * @param easingX a custom easing function to be used on the animation phase - * @param easingY a custom easing function to be used on the animation phase - */ - public void animateXY(int durationMillisX, int durationMillisY, EasingFunction easingX, EasingFunction easingY) { - mAnimator.animateXY(durationMillisX, durationMillisY, easingX, easingY); - } - - /** - * Animates the drawing / rendering of the chart on both x- and y-axis with - * the specified animation time. If animate(...) is called, no further - * calling of invalidate() is necessary to refresh the chart. ANIMATIONS - * ONLY WORK FOR API LEVEL 11 (Android 3.0.x) AND HIGHER. - * - * @param easing a custom easing function to be used on the animation phase - */ - public void animateXY(int durationMillisX, int durationMillisY, EasingFunction easing) { - mAnimator.animateXY(durationMillisX, durationMillisY, easing); - } - - /** - * Animates the rendering of the chart on the x-axis with the specified - * animation time. If animate(...) is called, no further calling of - * invalidate() is necessary to refresh the chart. ANIMATIONS ONLY WORK FOR - * API LEVEL 11 (Android 3.0.x) AND HIGHER. - * - * @param easing a custom easing function to be used on the animation phase - */ - public void animateX(int durationMillis, EasingFunction easing) { - mAnimator.animateX(durationMillis, easing); - } - - /** - * Animates the rendering of the chart on the y-axis with the specified - * animation time. If animate(...) is called, no further calling of - * invalidate() is necessary to refresh the chart. ANIMATIONS ONLY WORK FOR - * API LEVEL 11 (Android 3.0.x) AND HIGHER. - * - * @param easing a custom easing function to be used on the animation phase - */ - public void animateY(int durationMillis, EasingFunction easing) { - mAnimator.animateY(durationMillis, easing); - } - - /** - * Animates the rendering of the chart on the x-axis with the specified - * animation time. If animate(...) is called, no further calling of - * invalidate() is necessary to refresh the chart. ANIMATIONS ONLY WORK FOR - * API LEVEL 11 (Android 3.0.x) AND HIGHER. - */ - public void animateX(int durationMillis) { - mAnimator.animateX(durationMillis); - } - - /** - * Animates the rendering of the chart on the y-axis with the specified - * animation time. If animate(...) is called, no further calling of - * invalidate() is necessary to refresh the chart. ANIMATIONS ONLY WORK FOR - * API LEVEL 11 (Android 3.0.x) AND HIGHER. - */ - public void animateY(int durationMillis) { - mAnimator.animateY(durationMillis); - } - - /** - * Animates the drawing / rendering of the chart on both x- and y-axis with - * the specified animation time. If animate(...) is called, no further - * calling of invalidate() is necessary to refresh the chart. ANIMATIONS - * ONLY WORK FOR API LEVEL 11 (Android 3.0.x) AND HIGHER. - */ - public void animateXY(int durationMillisX, int durationMillisY) { - mAnimator.animateXY(durationMillisX, durationMillisY); - } - - - /** - * Returns the object representing all x-labels, this method can be used to - * acquire the XAxis object and modify it (e.g. change the position of the - * labels, styling, etc.) - */ - public XAxis getXAxis() { - return mXAxis; - } - - /** - * Returns the default IValueFormatter that has been determined by the chart - * considering the provided minimum and maximum values. - */ - public IValueFormatter getDefaultValueFormatter() { - return mDefaultValueFormatter; - } - - /** - * set a selection listener for the chart - */ - public void setOnChartValueSelectedListener(OnChartValueSelectedListener l) { - this.mSelectionListener = l; - } - - /** - * Sets a gesture-listener for the chart for custom callbacks when executing - * gestures on the chart surface. - */ - public void setOnChartGestureListener(OnChartGestureListener l) { - this.mGestureListener = l; - } - - /** - * Returns the custom gesture listener. - */ - public OnChartGestureListener getOnChartGestureListener() { - return mGestureListener; - } - - /** - * returns the current y-max value across all DataSets - */ - public float getYMax() { - return mData.getYMax(); - } - - /** - * returns the current y-min value across all DataSets - */ - public float getYMin() { - return mData.getYMin(); - } - - @Override - public float getXChartMax() { - return mXAxis.mAxisMaximum; - } - - @Override - public float getXChartMin() { - return mXAxis.mAxisMinimum; - } - - @Override - public float getXRange() { - return mXAxis.mAxisRange; - } - - /** - * Returns a recyclable MPPointF instance. - * Returns the center point of the chart (the whole View) in pixels. - */ - public MPPointF getCenter() { - return MPPointF.getInstance(getWidth() / 2f, getHeight() / 2f); - } - - /** - * Returns a recyclable MPPointF instance. - * Returns the center of the chart taking offsets under consideration. - * (returns the center of the content rectangle) - */ - @Override - public MPPointF getCenterOffsets() { - return mViewPortHandler.getContentCenter(); - } - - /** - * Sets extra offsets (around the chart view) to be appended to the - * auto-calculated offsets. - */ - public void setExtraOffsets(float left, float top, float right, float bottom) { - setExtraLeftOffset(left); - setExtraTopOffset(top); - setExtraRightOffset(right); - setExtraBottomOffset(bottom); - } - - /** - * Set an extra offset to be appended to the viewport's top - */ - public void setExtraTopOffset(float offset) { - mExtraTopOffset = Utils.convertDpToPixel(offset); - } - - /** - * @return the extra offset to be appended to the viewport's top - */ - public float getExtraTopOffset() { - return mExtraTopOffset; - } - - /** - * Set an extra offset to be appended to the viewport's right - */ - public void setExtraRightOffset(float offset) { - mExtraRightOffset = Utils.convertDpToPixel(offset); - } - - /** - * @return the extra offset to be appended to the viewport's right - */ - public float getExtraRightOffset() { - return mExtraRightOffset; - } - - /** - * Set an extra offset to be appended to the viewport's bottom - */ - public void setExtraBottomOffset(float offset) { - mExtraBottomOffset = Utils.convertDpToPixel(offset); - } - - /** - * @return the extra offset to be appended to the viewport's bottom - */ - public float getExtraBottomOffset() { - return mExtraBottomOffset; - } - - /** - * Set an extra offset to be appended to the viewport's left - */ - public void setExtraLeftOffset(float offset) { - mExtraLeftOffset = Utils.convertDpToPixel(offset); - } - - /** - * @return the extra offset to be appended to the viewport's left - */ - public float getExtraLeftOffset() { - return mExtraLeftOffset; - } - - /** - * Set this to true to enable logcat outputs for the chart. Beware that - * logcat output decreases rendering performance. Default: disabled. - */ - public void setLogEnabled(boolean enabled) { - mLogEnabled = enabled; - } - - /** - * Returns true if log-output is enabled for the chart, fals if not. - */ - public boolean isLogEnabled() { - return mLogEnabled; - } - - /** - * Sets the text that informs the user that there is no data available with - * which to draw the chart. - */ - public void setNoDataText(String text) { - mNoDataText = text; - } - - /** - * Sets the color of the no data text. - */ - public void setNoDataTextColor(int color) { - mInfoPaint.setColor(color); - } - - /** - * Sets the typeface to be used for the no data text. - */ - public void setNoDataTextTypeface(Typeface tf) { - mInfoPaint.setTypeface(tf); - } - - /** - * alignment of the no data text - */ - public void setNoDataTextAlignment(Align align) { - mInfoPaint.setTextAlign(align); - } - - /** - * Set this to false to disable all gestures and touches on the chart, - * default: true - */ - public void setTouchEnabled(boolean enabled) { - this.mTouchEnabled = enabled; - } - - public void setMarkers(List marker) { - mMarkers = marker; + * returns true if drawing the marker is enabled when tapping on values + * (use the setMarker(IMarker marker) method to specify a marker) + */ + /** + * if set to true, the marker view is drawn when a value is clicked + */ + var isDrawMarkersEnabled: Boolean = true + protected set + + /** + * returns the marker that is set as a marker view for the chart + * + */ + /** + * the view that represents the marker + */ + var marker: MutableList? = ArrayList() + protected set + + /** + * draws all MarkerViews on the highlighted positions + */ + protected open fun drawMarkers(canvas: Canvas?) { + // if there is no marker view or drawing marker is disabled + + if (this.marker == null || !this.isDrawMarkersEnabled || !valuesToHighlight()) { + return + } + + for (i in highlighted!!.indices) { + val highlight = this.highlighted!![i] + + // When changing data sets and calling animation functions, sometimes an erroneous highlight is generated + // on the dataset that is removed. Null check to prevent crash + val set = mData!!.getDataSetByIndex(highlight.dataSetIndex) + if (set == null || !set.isVisible) { + continue + } + + val e = mData!!.getEntryForHighlight(highlight) + + // make sure entry not null before using it + if (e == null) { + continue + } + val entryIndex = set.getEntryIndex(e) + + if (entryIndex > set.entryCount * mAnimator!!.phaseX) { + continue + } + + val pos = getMarkerPosition(highlight) + + // check bounds + if (!viewPortHandler.isInBounds(pos[0], pos[1])) { + continue + } + + // callbacks to update the content + if (!marker!!.isEmpty()) { + val markerIndex = i % marker!!.size + val markerItem = marker!!.get(markerIndex) + markerItem.refreshContent(e, highlight) + + // draw the marker + markerItem.draw(canvas, pos[0], pos[1]) + } + } + } + + /** + * Returns the actual position in pixels of the MarkerView for the given + * Highlight object. + */ + protected open fun getMarkerPosition(high: Highlight): FloatArray { + return floatArrayOf(high.drawX, high.drawY) + } + + val animator: ChartAnimator + /** + * Returns the animator responsible for animating chart values. + */ + get() = mAnimator!! + + var dragDecelerationFrictionCoef: Float + /** + * Returns drag deceleration friction coefficient + */ + get() = mDragDecelerationFrictionCoef + /** + * Deceleration friction coefficient in [0 ; 1] interval, higher values + * indicate that speed will decrease slowly, for example if it set to 0, it + * will stop immediately. 1 is an invalid value, and will be converted to + * 0.999f automatically. + */ + set(newValue) { + var newValue = newValue + if (newValue < 0f) { + newValue = 0f + } + + if (newValue >= 1f) { + newValue = 0.999f + } + + mDragDecelerationFrictionCoef = newValue + } + + /** + * Animates the drawing / rendering of the chart on both x- and y-axis with + * the specified animation time. If animate(...) is called, no further + * calling of invalidate() is necessary to refresh the chart. ANIMATIONS + * ONLY WORK FOR API LEVEL 11 (Android 3.0.x) AND HIGHER. + * + * @param easingX a custom easing function to be used on the animation phase + * @param easingY a custom easing function to be used on the animation phase + */ + fun animateXY(durationMillisX: Int, durationMillisY: Int, easingX: EasingFunction?, easingY: EasingFunction?) { + mAnimator!!.animateXY(durationMillisX, durationMillisY, easingX, easingY) + } + + /** + * Animates the drawing / rendering of the chart on both x- and y-axis with + * the specified animation time. If animate(...) is called, no further + * calling of invalidate() is necessary to refresh the chart. ANIMATIONS + * ONLY WORK FOR API LEVEL 11 (Android 3.0.x) AND HIGHER. + * + * @param easing a custom easing function to be used on the animation phase + */ + fun animateXY(durationMillisX: Int, durationMillisY: Int, easing: EasingFunction?) { + mAnimator!!.animateXY(durationMillisX, durationMillisY, easing) + } + + /** + * Animates the rendering of the chart on the x-axis with the specified + * animation time. If animate(...) is called, no further calling of + * invalidate() is necessary to refresh the chart. ANIMATIONS ONLY WORK FOR + * API LEVEL 11 (Android 3.0.x) AND HIGHER. + * + * @param easing a custom easing function to be used on the animation phase + */ + fun animateX(durationMillis: Int, easing: EasingFunction?) { + mAnimator!!.animateX(durationMillis, easing) + } + + /** + * Animates the rendering of the chart on the y-axis with the specified + * animation time. If animate(...) is called, no further calling of + * invalidate() is necessary to refresh the chart. ANIMATIONS ONLY WORK FOR + * API LEVEL 11 (Android 3.0.x) AND HIGHER. + * + * @param easing a custom easing function to be used on the animation phase + */ + fun animateY(durationMillis: Int, easing: EasingFunction?) { + mAnimator!!.animateY(durationMillis, easing) + } + + /** + * Animates the rendering of the chart on the x-axis with the specified + * animation time. If animate(...) is called, no further calling of + * invalidate() is necessary to refresh the chart. ANIMATIONS ONLY WORK FOR + * API LEVEL 11 (Android 3.0.x) AND HIGHER. + */ + fun animateX(durationMillis: Int) { + mAnimator!!.animateX(durationMillis) + } + + /** + * Animates the rendering of the chart on the y-axis with the specified + * animation time. If animate(...) is called, no further calling of + * invalidate() is necessary to refresh the chart. ANIMATIONS ONLY WORK FOR + * API LEVEL 11 (Android 3.0.x) AND HIGHER. + */ + fun animateY(durationMillis: Int) { + mAnimator!!.animateY(durationMillis) + } + + /** + * Animates the drawing / rendering of the chart on both x- and y-axis with + * the specified animation time. If animate(...) is called, no further + * calling of invalidate() is necessary to refresh the chart. ANIMATIONS + * ONLY WORK FOR API LEVEL 11 (Android 3.0.x) AND HIGHER. + */ + fun animateXY(durationMillisX: Int, durationMillisY: Int) { + mAnimator!!.animateXY(durationMillisX, durationMillisY) + } + + + open val xAxis: XAxis + /** + * Returns the object representing all x-labels, this method can be used to + * acquire the XAxis object and modify it (e.g. change the position of the + * labels, styling, etc.) + */ + get() = mXAxis!! + + /** + * Returns the default IValueFormatter that has been determined by the chart + * considering the provided minimum and maximum values. + */ + override val defaultValueFormatter: IValueFormatter? + get() = mDefaultValueFormatter + + /** + * set a selection listener for the chart + */ + fun setOnChartValueSelectedListener(l: OnChartValueSelectedListener?) { + this.mSelectionListener = l + } + + val yMax: Float + /** + * returns the current y-max value across all DataSets + */ + get() = mData!!.yMax + + val yMin: Float + /** + * returns the current y-min value across all DataSets + */ + get() = mData!!.yMin + + override val xChartMax: Float + get() = mXAxis!!.mAxisMaximum + + override val xChartMin: Float + get() = mXAxis!!.axisMinimum + + override val xRange: Float + get() = mXAxis!!.mAxisRange + + val center: MPPointF + /** + * Returns a recyclable MPPointF instance. + * Returns the center point of the chart (the whole View) in pixels. + */ + get() = MPPointF.Companion.getInstance(width / 2f, height / 2f) + + /** + * Returns a recyclable MPPointF instance. + * Returns the center of the chart taking offsets under consideration. + * (returns the center of the content rectangle) + */ + override val centerOffsets: MPPointF + get() = viewPortHandler.contentCenter + + /** + * Sets extra offsets (around the chart view) to be appended to the + * auto-calculated offsets. + */ + fun setExtraOffsets(left: Float, top: Float, right: Float, bottom: Float) { + this.extraLeftOffset = left + this.extraTopOffset = top + this.extraRightOffset = right + this.extraBottomOffset = bottom + } + + var extraTopOffset: Float + /** + * @return the extra offset to be appended to the viewport's top + */ + get() = mExtraTopOffset + /** + * Set an extra offset to be appended to the viewport's top + */ + set(offset) { + mExtraTopOffset = Utils.convertDpToPixel(offset) + } + + var extraRightOffset: Float + /** + * @return the extra offset to be appended to the viewport's right + */ + get() = mExtraRightOffset + /** + * Set an extra offset to be appended to the viewport's right + */ + set(offset) { + mExtraRightOffset = Utils.convertDpToPixel(offset) + } + + var extraBottomOffset: Float + /** + * @return the extra offset to be appended to the viewport's bottom + */ + get() = mExtraBottomOffset + /** + * Set an extra offset to be appended to the viewport's bottom + */ + set(offset) { + mExtraBottomOffset = Utils.convertDpToPixel(offset) + } + + var extraLeftOffset: Float + /** + * @return the extra offset to be appended to the viewport's left + */ + get() = mExtraLeftOffset + /** + * Set an extra offset to be appended to the viewport's left + */ + set(offset) { + mExtraLeftOffset = Utils.convertDpToPixel(offset) + } + + /** + * Sets the text that informs the user that there is no data available with + * which to draw the chart. + */ + fun setNoDataText(text: String) { + mNoDataText = text + } + + /** + * Sets the color of the no data text. + */ + fun setNoDataTextColor(color: Int) { + mInfoPaint!!.setColor(color) + } + + /** + * Sets the typeface to be used for the no data text. + */ + fun setNoDataTextTypeface(tf: Typeface?) { + mInfoPaint!!.setTypeface(tf) + } + + /** + * alignment of the no data text + */ + fun setNoDataTextAlignment(align: Align?) { + mInfoPaint!!.textAlign = align + } + + /** + * Set this to false to disable all gestures and touches on the chart, + * default: true + */ + fun setTouchEnabled(enabled: Boolean) { + this.mTouchEnabled = enabled + } + + fun setMarkers(marker: MutableList?) { + this.marker = marker } /** * sets the marker that is displayed when a value is clicked on the chart */ - public void setMarker(IMarker marker) { - setMarkers(Collections.singletonList(marker)); - } - /** - * returns the marker that is set as a marker view for the chart - - */ - public List getMarker() { - return mMarkers; - } - - @Deprecated - public void setMarkerView(IMarker v) { - setMarker(v); - } - - @Deprecated - public List getMarkerView() { - return getMarker(); - } - - /** - * Sets a new Description object for the chart. - */ - public void setDescription(Description desc) { - this.mDescription = desc; - } - - /** - * Returns the Description object of the chart that is responsible for holding all information related - * to the description text that is displayed in the bottom right corner of the chart (by default). - */ - public Description getDescription() { - return mDescription; - } - - /** - * Returns the Legend object of the chart. This method can be used to get an - * instance of the legend in order to customize the automatically generated - * Legend. - */ - public Legend getLegend() { - return mLegend; - } - - /** - * Returns the renderer object responsible for rendering / drawing the - * Legend. - */ - public LegendRenderer getLegendRenderer() { - return mLegendRenderer; - } - - /** - * Returns the rectangle that defines the borders of the chart-value surface - * (into which the actual values are drawn). - */ - @Override - public RectF getContentRect() { - return mViewPortHandler.getContentRect(); - } - - /** - * disables intercept touchevents - */ - public void disableScroll() { - ViewParent parent = getParent(); - if (parent != null) { - parent.requestDisallowInterceptTouchEvent(true); - } - } - - /** - * enables intercept touchevents - */ - public void enableScroll() { - ViewParent parent = getParent(); - if (parent != null) { - parent.requestDisallowInterceptTouchEvent(false); - } - } - - /** - * paint for the grid background (only line and barchart) - */ - public static final int PAINT_GRID_BACKGROUND = 4; - - /** - * paint for the info text that is displayed when there are no values in the - * chart - */ - public static final int PAINT_INFO = 7; - - /** - * paint for the description text in the bottom right corner - */ - public static final int PAINT_DESCRIPTION = 11; - - /** - * paint for the hole in the middle of the pie chart - */ - public static final int PAINT_HOLE = 13; - - /** - * paint for the text in the middle of the pie chart - */ - public static final int PAINT_CENTER_TEXT = 14; - - /** - * paint used for the legend - */ - public static final int PAINT_LEGEND_LABEL = 18; - - /** - * set a new paint object for the specified parameter in the chart e.g. - * Chart.PAINT_VALUES - * - * @param p the new paint object - * @param which Chart.PAINT_VALUES, Chart.PAINT_GRID, Chart.PAINT_VALUES, - * ... - */ - public void setPaint(Paint p, int which) { - - switch (which) { - case PAINT_INFO: - mInfoPaint = p; - break; - case PAINT_DESCRIPTION: - mDescPaint = p; - break; - } - } - - /** - * Returns the paint object associated with the provided constant. - * - * @param which e.g. Chart.PAINT_LEGEND_LABEL - */ - public Paint getPaint(int which) { - switch (which) { - case PAINT_INFO: - return mInfoPaint; - case PAINT_DESCRIPTION: - return mDescPaint; - } - - return null; - } - - @Deprecated - public boolean isDrawMarkerViewsEnabled() { - return isDrawMarkersEnabled(); - } - - @Deprecated - public void setDrawMarkerViews(boolean enabled) { - setDrawMarkers(enabled); - } - - /** - * returns true if drawing the marker is enabled when tapping on values - * (use the setMarker(IMarker marker) method to specify a marker) - */ - public boolean isDrawMarkersEnabled() { - return mDrawMarkers; - } - - /** - * Set this to true to draw a user specified marker when tapping on - * chart values (use the setMarker(IMarker marker) method to specify a - * marker). Default: true - */ - public void setDrawMarkers(boolean enabled) { - mDrawMarkers = enabled; - } - - /** - * Returns the ChartData object that has been set for the chart. - */ - public T getData() { - return mData; - } - - /** - * Returns the ViewPortHandler of the chart that is responsible for the - * content area of the chart and its offsets and dimensions. - */ - public ViewPortHandler getViewPortHandler() { - return mViewPortHandler; - } - - /** - * Returns the Renderer object the chart uses for drawing data. - */ - public DataRenderer getRenderer() { - return mRenderer; - } - - /** - * Sets a new DataRenderer object for the chart. - */ - public void setRenderer(DataRenderer renderer) { - - if (renderer != null) { - mRenderer = renderer; - } - } - - public IHighlighter getHighlighter() { - return mHighlighter; - } - - /** - * Sets a custom highligher object for the chart that handles / processes - * all highlight touch events performed on the chart-view. - */ - public void setHighlighter(ChartHighlighter highlighter) { - mHighlighter = highlighter; - } - - /** - * Returns a recyclable MPPointF instance. - */ - @Override - public MPPointF getCenterOfView() { - return getCenter(); - } - - /** - * Returns the bitmap that represents the chart. - */ - public Bitmap getChartBitmap() { - // Define a bitmap with the same size as the view - Bitmap returnedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565); - // Bind a canvas to it - Canvas canvas = new Canvas(returnedBitmap); - // Get the view's background - Drawable bgDrawable = getBackground(); - if (bgDrawable != null) - // has background drawable, then draw it on the canvas - { - bgDrawable.draw(canvas); - } else - // does not have background drawable, then draw white background on - // the canvas - { - canvas.drawColor(Color.WHITE); - } - // draw the view on the canvas - draw(canvas); - // return the bitmap - return returnedBitmap; - } - - /** - * Saves the current chart state with the given name to the given path on - * the sdcard leaving the path empty "" will put the saved file directly on - * the SD card chart is saved as a PNG image, example: - * saveToPath("myfilename", "foldername1/foldername2"); - * - * @param pathOnSD e.g. "folder1/folder2/folder3" - * @return returns true on success, false on error - */ - public boolean saveToPath(String title, String pathOnSD) { - - - Bitmap b = getChartBitmap(); - - OutputStream stream; - try { - stream = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + pathOnSD + "/" + title + ".png"); - - /* + fun setMarker(marker: IMarker?) { + setMarkers(marker?.let { mutableListOf(marker) }) + } + + @Deprecated("") + fun setMarkerView(v: IMarker?) { + setMarker(v) + } + + @get:Deprecated("") + val markerView: MutableList? + get() = this.marker + + /** + * Returns the rectangle that defines the borders of the chart-value surface + * (into which the actual values are drawn). + */ + override val contentRect: RectF? + get() = viewPortHandler.contentRect + + /** + * disables intercept touchevents + */ + fun disableScroll() { + val parent = getParent() + parent?.requestDisallowInterceptTouchEvent(true) + } + + /** + * enables intercept touchevents + */ + fun enableScroll() { + val parent = getParent() + parent?.requestDisallowInterceptTouchEvent(false) + } + + /** + * set a new paint object for the specified parameter in the chart e.g. + * Chart.PAINT_VALUES + * + * @param p the new paint object + * @param which Chart.PAINT_VALUES, Chart.PAINT_GRID, Chart.PAINT_VALUES, + * ... + */ + open fun setPaint(p: Paint, which: Int) { + when (which) { + PAINT_INFO -> mInfoPaint = p + PAINT_DESCRIPTION -> mDescPaint = p + } + } + + /** + * Returns the paint object associated with the provided constant. + * + * @param which e.g. Chart.PAINT_LEGEND_LABEL + */ + open fun getPaint(which: Int): Paint? { + when (which) { + PAINT_INFO -> return mInfoPaint + PAINT_DESCRIPTION -> return mDescPaint + } + + return null + } + + @get:Deprecated("") + val isDrawMarkerViewsEnabled: Boolean + get() = this.isDrawMarkersEnabled + + @Deprecated("") + fun setDrawMarkerViews(enabled: Boolean) { + setDrawMarkers(enabled) + } + + /** + * Set this to true to draw a user specified marker when tapping on + * chart values (use the setMarker(IMarker marker) method to specify a + * marker). Default: true + */ + fun setDrawMarkers(enabled: Boolean) { + this.isDrawMarkersEnabled = enabled + } + + /** + * Returns the ChartData object that has been set for the chart. + */ + override val data: ChartData? + get() = mData + + var renderer: DataRenderer? + /** + * Returns the Renderer object the chart uses for drawing data. + */ + get() = mRenderer + /** + * Sets a new DataRenderer object for the chart. + */ + set(renderer) { + if (renderer != null) { + mRenderer = renderer + } + } + + /** + * Sets a custom highligher object for the chart that handles / processes + * all highlight touch events performed on the chart-view. + */ + fun setHighlighter(highlighter: ChartHighlighter<*>?) { + this.highlighter = highlighter + } + + /** + * Returns a recyclable MPPointF instance. + */ + override val centerOfView: MPPointF? + get() = this.center + + val chartBitmap: Bitmap + /** + * Returns the bitmap that represents the chart. + */ + get() { + // Define a bitmap with the same size as the view + val returnedBitmap = createBitmap(width, height, Bitmap.Config.RGB_565) + // Bind a canvas to it + val canvas = Canvas(returnedBitmap) + // Get the view's background + val bgDrawable = background + if (bgDrawable != null) // has background drawable, then draw it on the canvas + { + bgDrawable.draw(canvas) + } else // does not have background drawable, then draw white background on + // the canvas + { + canvas.drawColor(Color.WHITE) + } + // draw the view on the canvas + draw(canvas) + // return the bitmap + return returnedBitmap + } + + /** + * Saves the current chart state with the given name to the given path on + * the sdcard leaving the path empty "" will put the saved file directly on + * the SD card chart is saved as a PNG image, example: + * saveToPath("myfilename", "foldername1/foldername2"); + * + * @param pathOnSD e.g. "folder1/folder2/folder3" + * @return returns true on success, false on error + */ + fun saveToPath(title: String?, pathOnSD: String?): Boolean { + val b = this.chartBitmap + + val stream: OutputStream? + try { + stream = FileOutputStream(Environment.getExternalStorageDirectory().getPath() + pathOnSD + "/" + title + ".png") + + /* * Write bitmap to file using JPEG or PNG and 40% quality hint for * JPEG. */ - b.compress(CompressFormat.PNG, 40, stream); - - stream.close(); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - - return true; - } - - /** - * Saves the current state of the chart to the gallery as an image type. The - * compression must be set for JPEG only. 0 == maximum compression, 100 = low - * compression (high quality). NOTE: Needs permission WRITE_EXTERNAL_STORAGE - * - * @param fileName e.g. "my_image" - * @param subFolderPath e.g. "ChartPics" - * @param fileDescription e.g. "Chart details" - * @param format e.g. Bitmap.CompressFormat.PNG - * @param quality e.g. 50, min = 0, max = 100 - * @return returns true if saving was successful, false if not - */ - public boolean saveToGallery(String fileName, String subFolderPath, String fileDescription, Bitmap.CompressFormat format, int quality) { - // restrain quality - if (quality < 0 || quality > 100) { - quality = 50; - } - - long currentTime = System.currentTimeMillis(); - - File extBaseDir = Environment.getExternalStorageDirectory(); - File file = new File(extBaseDir.getAbsolutePath() + "/DCIM/" + subFolderPath); - if (!file.exists()) { - if (!file.mkdirs()) { - return false; - } - } - - String mimeType; - switch (format) { - case PNG: - mimeType = "image/png"; - if (!fileName.endsWith(".png")) { - fileName += ".png"; - } - break; - case WEBP: - mimeType = "image/webp"; - if (!fileName.endsWith(".webp")) { - fileName += ".webp"; - } - break; - case JPEG: - default: - mimeType = "image/jpeg"; - if (!(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))) { - fileName += ".jpg"; - } - break; - } - - String filePath = file.getAbsolutePath() + "/" + fileName; - FileOutputStream out; - try { - out = new FileOutputStream(filePath); - - Bitmap b = getChartBitmap(); - b.compress(format, quality, out); - - out.flush(); - out.close(); - - } catch (IOException e) { - e.printStackTrace(); - - return false; - } - - long size = new File(filePath).length(); - - ContentValues values = new ContentValues(8); - - // store the details - values.put(Images.Media.TITLE, fileName); - values.put(Images.Media.DISPLAY_NAME, fileName); - values.put(Images.Media.DATE_ADDED, currentTime); - values.put(Images.Media.MIME_TYPE, mimeType); - values.put(Images.Media.DESCRIPTION, fileDescription); - values.put(Images.Media.ORIENTATION, 0); - values.put(Images.Media.DATA, filePath); - values.put(Images.Media.SIZE, size); - - return getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values) != null; - } - - /** - * Saves the current state of the chart to the gallery as a JPEG image. The - * filename and compression can be set. 0 == maximum compression, 100 = low - * compression (high quality). NOTE: Needs permission WRITE_EXTERNAL_STORAGE - * - * @param fileName e.g. "my_image" - * @param quality e.g. 50, min = 0, max = 100 - * @return returns true if saving was successful, false if not - */ - public boolean saveToGallery(String fileName, int quality) { - return saveToGallery(fileName, "", "MPAndroidChart-Library Save", Bitmap.CompressFormat.PNG, quality); - } - - /** - * Saves the current state of the chart to the gallery as a PNG image. - * NOTE: Needs permission WRITE_EXTERNAL_STORAGE - * - * @param fileName e.g. "my_image" - * @return returns true if saving was successful, false if not - */ - public boolean saveToGallery(String fileName) { - return saveToGallery(fileName, "", "MPAndroidChart-Library Save", Bitmap.CompressFormat.PNG, 40); - } - - /** - * tasks to be done after the view is setup - */ - protected ArrayList mJobs = new ArrayList<>(); - - public void removeViewportJob(Runnable job) { - mJobs.remove(job); - } - - public void clearAllViewportJobs() { - mJobs.clear(); - } - - /** - * Either posts a job immediately if the chart has already setup it's - * dimensions or adds the job to the execution queue. - */ - public void addViewportJob(Runnable job) { - - if (mViewPortHandler.hasChartDimens()) { - post(job); - } else { - mJobs.add(job); - } - } - - /** - * Returns all jobs that are scheduled to be executed after - * onSizeChanged(...). - */ - public ArrayList getJobs() { - return mJobs; - } - - @Override - protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - - for (int i = 0; i < getChildCount(); i++) { - getChildAt(i).layout(left, top, right, bottom); - } - } - - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - int size = (int) Utils.convertDpToPixel(50f); - setMeasuredDimension(Math.max(getSuggestedMinimumWidth(), resolveSize(size, widthMeasureSpec)), Math.max(getSuggestedMinimumHeight(), resolveSize(size, heightMeasureSpec))); - } - - @Override - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - if (mLogEnabled) { - Log.i(LOG_TAG, "OnSizeChanged()"); - } - - if (w > 0 && h > 0 && w < 10000 && h < 10000) { - if (mLogEnabled) { - Log.i(LOG_TAG, "Setting chart dimens, width: " + w + ", height: " + h); - } - mViewPortHandler.setChartDimens(w, h); - } else { - if (mLogEnabled) { - Log.w(LOG_TAG, "*Avoiding* setting chart dimens! width: " + w + ", height: " + h); - } - } - - // This may cause the chart view to mutate properties affecting the view port -- - // lets do this before we try to run any pending jobs on the view port itself - notifyDataSetChanged(); - - for (Runnable r : mJobs) { - post(r); - } - - mJobs.clear(); - - super.onSizeChanged(w, h, oldw, oldh); - } - - /** - * Setting this to true will set the layer-type HARDWARE for the view, false - * will set layer-type SOFTWARE. - */ - public void setHardwareAccelerationEnabled(boolean enabled) { - - if (enabled) { - setLayerType(View.LAYER_TYPE_HARDWARE, null); - } else { - setLayerType(View.LAYER_TYPE_SOFTWARE, null); - } - } - - @Override - protected void onDetachedFromWindow() { - super.onDetachedFromWindow(); - - //Log.i(LOG_TAG, "Detaching..."); - - if (mUnbind) { - unbindDrawables(this); - } - } - - /** - * unbind flag - */ - private boolean mUnbind = false; - - /** - * Unbind all drawables to avoid memory leaks. - * Link: http://stackoverflow.com/a/6779164/1590502 - */ - private void unbindDrawables(View view) { - - if (view.getBackground() != null) { - view.getBackground().setCallback(null); - } - if (view instanceof ViewGroup) { - for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { - unbindDrawables(((ViewGroup) view).getChildAt(i)); - } - ((ViewGroup) view).removeAllViews(); - } - } - - /** - * Set this to true to enable "unbinding" of drawables. When a View is detached - * from a window. This helps avoid memory leaks. - * Default: false - * Link: http://stackoverflow.com/a/6779164/1590502 - */ - public void setUnbindEnabled(boolean enabled) { - this.mUnbind = enabled; - } + b.compress(CompressFormat.PNG, 40, stream) - // region accessibility + stream.close() + } catch (e: Exception) { + e.printStackTrace() + return false + } + + return true + } /** + * Saves the current state of the chart to the gallery as an image type. The + * compression must be set for JPEG only. 0 == maximum compression, 100 = low + * compression (high quality). NOTE: Needs permission WRITE_EXTERNAL_STORAGE * - * @return accessibility description must be created for each chart + * @param fileName e.g. "my_image" + * @param subFolderPath e.g. "ChartPics" + * @param fileDescription e.g. "Chart details" + * @param format e.g. Bitmap.CompressFormat.PNG + * @param quality e.g. 50, min = 0, max = 100 + * @return returns true if saving was successful, false if not + */ + /** + * Saves the current state of the chart to the gallery as a PNG image. + * NOTE: Needs permission WRITE_EXTERNAL_STORAGE + * + * @param fileName e.g. "my_image" + * @return returns true if saving was successful, false if not + */ + @JvmOverloads + fun saveToGallery( + fileName: String, + subFolderPath: String? = "", + fileDescription: String? = "MPAndroidChart-Library Save", + format: CompressFormat = CompressFormat.PNG, + quality: Int = 40 + ): Boolean { + // restrain quality + var fileName = fileName + var quality = quality + if (quality < 0 || quality > 100) { + quality = 50 + } + + val currentTime = System.currentTimeMillis() + + val extBaseDir = Environment.getExternalStorageDirectory() + val file = File(extBaseDir.absolutePath + "/DCIM/" + subFolderPath) + if (!file.exists()) { + if (!file.mkdirs()) { + return false + } + } + + val mimeType: String? + when (format) { + CompressFormat.PNG -> { + mimeType = "image/png" + if (!fileName.endsWith(".png")) { + fileName += ".png" + } + } + + CompressFormat.WEBP -> { + mimeType = "image/webp" + if (!fileName.endsWith(".webp")) { + fileName += ".webp" + } + } + + CompressFormat.JPEG -> { + mimeType = "image/jpeg" + if (!(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))) { + fileName += ".jpg" + } + } + + else -> { + mimeType = "image/jpeg" + if (!(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))) { + fileName += ".jpg" + } + } + } + + val filePath = file.absolutePath + "/" + fileName + val out: FileOutputStream? + try { + out = FileOutputStream(filePath) + + val b = this.chartBitmap + b.compress(format, quality, out) + + out.flush() + out.close() + } catch (e: IOException) { + e.printStackTrace() + + return false + } + + val size = File(filePath).length() + + val values = ContentValues(8) + + // store the details + values.put(MediaStore.Images.Media.TITLE, fileName) + values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName) + values.put(MediaStore.Images.Media.DATE_ADDED, currentTime) + values.put(MediaStore.Images.Media.MIME_TYPE, mimeType) + values.put(MediaStore.Images.Media.DESCRIPTION, fileDescription) + values.put(MediaStore.Images.Media.ORIENTATION, 0) + values.put(MediaStore.Images.Media.DATA, filePath) + values.put(MediaStore.Images.Media.SIZE, size) + + return getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) != null + } + + /** + * Saves the current state of the chart to the gallery as a JPEG image. The + * filename and compression can be set. 0 == maximum compression, 100 = low + * compression (high quality). NOTE: Needs permission WRITE_EXTERNAL_STORAGE + * + * @param fileName e.g. "my_image" + * @param quality e.g. 50, min = 0, max = 100 + * @return returns true if saving was successful, false if not + */ + fun saveToGallery(fileName: String, quality: Int): Boolean { + return saveToGallery(fileName, "", "MPAndroidChart-Library Save", CompressFormat.PNG, quality) + } + + /** + * Returns all jobs that are scheduled to be executed after + * onSizeChanged(...). + */ + /** + * tasks to be done after the view is setup */ - public abstract String getAccessibilityDescription(); + var jobs: ArrayList = ArrayList() + protected set - public String getAccessibilitySummaryDescription() { - return accessibilitySummaryDescription; + fun removeViewportJob(job: Runnable?) { + jobs.remove(job) } - public void setAccessibilitySummaryDescription(String accessibilitySummaryDescription) { - this.accessibilitySummaryDescription = accessibilitySummaryDescription; + fun clearAllViewportJobs() { + jobs.clear() } - @Override - public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { + /** + * Either posts a job immediately if the chart has already setup it's + * dimensions or adds the job to the execution queue. + */ + fun addViewportJob(job: Runnable?) { + if (viewPortHandler.hasChartDimens()) { + post(job) + } else { + jobs.add(job) + } + } - boolean completed = super.dispatchPopulateAccessibilityEvent(event); - Log.d(LOG_TAG, "Dispatch called for Chart and completed as " + completed); + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { + for (i in 0.. 0 && h > 0 && w < 10000 && h < 10000) { + if (this.isLogEnabled) { + Log.i(LOG_TAG, "Setting chart dimens, width: $w, height: $h") + } + viewPortHandler.setChartDimens(w.toFloat(), h.toFloat()) + } else { + if (this.isLogEnabled) { + Log.w(LOG_TAG, "*Avoiding* setting chart dimens! width: $w, height: $h") + } + } + + // This may cause the chart view to mutate properties affecting the view port -- + // lets do this before we try to run any pending jobs on the view port itself + notifyDataSetChanged() + + for (r in this.jobs) { + post(r) + } + + jobs.clear() + + super.onSizeChanged(w, h, oldw, oldh) + } + + /** + * Setting this to true will set the layer-type HARDWARE for the view, false + * will set layer-type SOFTWARE. + */ + fun setHardwareAccelerationEnabled(enabled: Boolean) { + if (enabled) { + setLayerType(LAYER_TYPE_HARDWARE, null) + } else { + setLayerType(LAYER_TYPE_SOFTWARE, null) + } + } + + override fun onDetachedFromWindow() { + super.onDetachedFromWindow() + + //Log.i(LOG_TAG, "Detaching..."); + if (mUnbind) { + unbindDrawables(this) + } + } + + /** + * unbind flag + */ + private var mUnbind = false + + /** + * Unbind all drawables to avoid memory leaks. + * Link: http://stackoverflow.com/a/6779164/1590502 + */ + private fun unbindDrawables(view: View) { + if (view.getBackground() != null) { + view.getBackground().setCallback(null) } + if (view is ViewGroup) { + for (i in 0.. and completed as $completed") + + event.text.add(this.accessibilityDescription) + + // Add the user generated summary after the dynamic summary is complete. + if (!TextUtils.isEmpty(this.accessibilitySummaryDescription)) { + event.text.add(this.accessibilitySummaryDescription) + } + + return true + } // endregion + + companion object { + const val LOG_TAG: String = "MPAndroidChart" + + /** + * paint for the grid background (only line and barchart) + */ + const val PAINT_GRID_BACKGROUND: Int = 4 + + /** + * paint for the info text that is displayed when there are no values in the + * chart + */ + const val PAINT_INFO: Int = 7 + + /** + * paint for the description text in the bottom right corner + */ + const val PAINT_DESCRIPTION: Int = 11 + + /** + * paint for the hole in the middle of the pie chart + */ + const val PAINT_HOLE: Int = 13 + + /** + * paint for the text in the middle of the pie chart + */ + const val PAINT_CENTER_TEXT: Int = 14 + + /** + * paint used for the legend + */ + const val PAINT_LEGEND_LABEL: Int = 18 + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt index 40b7d5b70a..900b52c23f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt @@ -1,24 +1,21 @@ - -package com.github.mikephil.charting.charts; - -import android.content.Context; -import android.graphics.Canvas; -import android.util.AttributeSet; -import android.util.Log; - -import com.github.mikephil.charting.components.IMarker; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BubbleData; -import com.github.mikephil.charting.data.CandleData; -import com.github.mikephil.charting.data.CombinedData; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.ScatterData; -import com.github.mikephil.charting.highlight.CombinedHighlighter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.renderer.CombinedChartRenderer; +package com.github.mikephil.charting.charts + +import android.content.Context +import android.graphics.Canvas +import android.util.AttributeSet +import android.util.Log +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BubbleData +import com.github.mikephil.charting.data.CandleData +import com.github.mikephil.charting.data.CombinedData +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.ScatterData +import com.github.mikephil.charting.highlight.CombinedHighlighter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider +import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet +import com.github.mikephil.charting.renderer.CombinedChartRenderer /** * This chart class allows the combination of lines, bars, scatter and candle @@ -26,253 +23,201 @@ import com.github.mikephil.charting.renderer.CombinedChartRenderer; * * @author Philipp Jahoda */ -@SuppressWarnings("unused") -public class CombinedChart extends BarLineChartBase implements CombinedDataProvider { - - /** - * if set to true, all values are drawn above their bars, instead of below - * their top - */ - private boolean mDrawValueAboveBar = true; - - - /** - * flag that indicates whether the highlight should be full-bar oriented, or single-value? - */ - protected boolean mHighlightFullBarEnabled = false; - - /** - * if set to true, a grey area is drawn behind each bar that indicates the - * maximum value - */ - private boolean mDrawBarShadow = false; - - protected DrawOrder[] mDrawOrder; - - /** - * enum that allows to specify the order in which the different data objects - * for the combined-chart are drawn - */ - public enum DrawOrder { - BAR, BUBBLE, LINE, CANDLE, SCATTER - } - - public CombinedChart(Context context) { - super(context); - } - - public CombinedChart(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public CombinedChart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - @Override - protected void init() { - super.init(); - - // Default values are not ready here yet - mDrawOrder = new DrawOrder[]{ - DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.LINE, DrawOrder.CANDLE, DrawOrder.SCATTER - }; - - setHighlighter(new CombinedHighlighter(this, this)); - - // Old default behaviour - setHighlightFullBarEnabled(true); - - mRenderer = new CombinedChartRenderer(this, mAnimator, mViewPortHandler); - } - - @Override - public CombinedData getCombinedData() { - return mData; - } - - @Override - public void setData(CombinedData data) { - super.setData(data); - setHighlighter(new CombinedHighlighter(this, this)); - ((CombinedChartRenderer) mRenderer).createRenderers(); - mRenderer.initBuffers(); - } - - /** - * Returns the Highlight object (contains x-index and DataSet index) of the selected value at the given touch - * point - * inside the CombinedChart. - */ - @Override - public Highlight getHighlightByTouchPoint(float x, float y) { - - if (mData == null) { - Log.e(LOG_TAG, "Can't select by touch. No data set."); - return null; - } else { - Highlight h = getHighlighter().getHighlight(x, y); - if (h == null || !isHighlightFullBarEnabled()) { - return h; - } - - // For isHighlightFullBarEnabled, remove stackIndex - return new Highlight(h.getX(), h.getY(), - h.getXPx(), h.getYPx(), - h.getDataSetIndex(), -1, h.getAxis()); - } - } - - @Override - public LineData getLineData() { - if (mData == null) { - return null; - } - return mData.getLineData(); - } - - @Override - public BarData getBarData() { - if (mData == null) { - return null; - } - return mData.getBarData(); - } - - @Override - public ScatterData getScatterData() { - if (mData == null) { - return null; - } - return mData.getScatterData(); - } - - @Override - public CandleData getCandleData() { - if (mData == null) { - return null; - } - return mData.getCandleData(); - } - - @Override - public BubbleData getBubbleData() { - if (mData == null) { - return null; - } - return mData.getBubbleData(); - } - - @Override - public boolean isDrawBarShadowEnabled() { - return mDrawBarShadow; - } - - @Override - public boolean isDrawValueAboveBarEnabled() { - return mDrawValueAboveBar; - } - - /** - * If set to true, all values are drawn above their bars, instead of below - * their top. - */ - public void setDrawValueAboveBar(boolean enabled) { - mDrawValueAboveBar = enabled; - } - - - /** - * If set to true, a grey area is drawn behind each bar that indicates the - * maximum value. Enabling his will reduce performance by about 50%. - */ - public void setDrawBarShadow(boolean enabled) { - mDrawBarShadow = enabled; - } - - /** - * Set this to true to make the highlight operation full-bar oriented, - * false to make it highlight single values (relevant only for stacked). - */ - public void setHighlightFullBarEnabled(boolean enabled) { - mHighlightFullBarEnabled = enabled; - } - - /** - * @return true the highlight operation is be full-bar oriented, false if single-value - */ - @Override - public boolean isHighlightFullBarEnabled() { - return mHighlightFullBarEnabled; - } - - /** - * Returns the currently set draw order. - */ - public DrawOrder[] getDrawOrder() { - return mDrawOrder; - } - - /** - * Sets the order in which the provided data objects should be drawn. The - * earlier you place them in the provided array, the further they will be in - * the background. e.g. if you provide new DrawOrer[] { DrawOrder.BAR, - * DrawOrder.LINE }, the bars will be drawn behind the lines. - */ - public void setDrawOrder(DrawOrder[] order) { - if (order == null || order.length <= 0) { - return; - } - mDrawOrder = order; - } - - /** - * draws all MarkerViews on the highlighted positions - */ - protected void drawMarkers(Canvas canvas) { - - // if there is no marker view or drawing marker is disabled - if (mMarkers == null || !isDrawMarkersEnabled() || !valuesToHighlight()) { - return; - } - - for (int i = 0; i < mIndicesToHighlight.length; i++) { - - Highlight highlight = mIndicesToHighlight[i]; - IDataSet set = mData.getDataSetByHighlight(highlight); - - Entry e = mData.getEntryForHighlight(highlight); - if (e == null || set == null) { - continue; - } - - int entryIndex = set.getEntryIndex(e); - - // make sure entry not null - if (entryIndex > set.getEntryCount() * mAnimator.getPhaseX()) { - continue; - } - - float[] pos = getMarkerPosition(highlight); - - // check bounds - if (!mViewPortHandler.isInBounds(pos[0], pos[1])) { - continue; - } - - // callbacks to update the content - if (!mMarkers.isEmpty()) { - IMarker markerItem = mMarkers.get(i % mMarkers.size()); - markerItem.refreshContent(e, highlight); - - // draw the marker - markerItem.draw(canvas, pos[0], pos[1]); - } - } - } - - @Override - public String getAccessibilityDescription() { - return "This is a combined chart"; - } +@Suppress("unused") +open class CombinedChart : BarLineChartBase, CombinedData>, CombinedDataProvider { + /** + * if set to true, all values are drawn above their bars, instead of below + * their top + */ + private var mDrawValueAboveBar = true + + + /** + * flag that indicates whether the highlight should be full-bar oriented, or single-value? + */ + protected var mHighlightFullBarEnabled: Boolean = false + + /** + * if set to true, a grey area is drawn behind each bar that indicates the + * maximum value + */ + private var mDrawBarShadow = false + + protected var mDrawOrder: Array = arrayOf( + DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.LINE, DrawOrder.CANDLE, DrawOrder.SCATTER + ) + + /** + * enum that allows to specify the order in which the different data objects + * for the combined-chart are drawn + */ + enum class DrawOrder { + BAR, BUBBLE, LINE, CANDLE, SCATTER + } + + constructor(context: Context) : super(context) + + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) + + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) + + override fun init() { + super.init() + + setHighlighter(CombinedHighlighter(this, this)) + + // Old default behaviour + setHighlightFullBarEnabled(true) + + mRenderer = CombinedChartRenderer(this, mAnimator!!, viewPortHandler) + } + + override val combinedData: CombinedData? + get() = mData + + override fun setData(data: CombinedData?) { + super.setData(data) + setHighlighter(CombinedHighlighter(this, this)) + (mRenderer as CombinedChartRenderer).createRenderers() + mRenderer!!.initBuffers() + } + + /** + * Returns the Highlight object (contains x-index and DataSet index) of the selected value at the given touch + * point + * inside the CombinedChart. + */ + override fun getHighlightByTouchPoint(x: Float, y: Float): Highlight? { + if (mData == null) { + Log.e(LOG_TAG, "Can't select by touch. No data set.") + return null + } else { + val h = highlighter!!.getHighlight(x, y) + if (h == null || !isHighlightFullBarEnabled) { + return h + } + + // For isHighlightFullBarEnabled, remove stackIndex + return Highlight( + h.x, h.y, + h.xPx, h.yPx, + h.dataSetIndex, -1, h.axis + ) + } + } + + override val lineData: LineData? + get() = mData?.lineData + + override val barData: BarData? + get() = mData?.barData + + override val scatterData: ScatterData? + get() = mData?.scatterData + + override val candleData: CandleData? + get() = mData?.candleData + + override val bubbleData: BubbleData? + get() = mData?.bubbleData + + override val isDrawBarShadowEnabled: Boolean + get() = mDrawBarShadow + + override val isDrawValueAboveBarEnabled: Boolean + get() = mDrawValueAboveBar + + /** + * If set to true, all values are drawn above their bars, instead of below + * their top. + */ + fun setDrawValueAboveBar(enabled: Boolean) { + mDrawValueAboveBar = enabled + } + + + /** + * If set to true, a grey area is drawn behind each bar that indicates the + * maximum value. Enabling his will reduce performance by about 50%. + */ + fun setDrawBarShadow(enabled: Boolean) { + mDrawBarShadow = enabled + } + + /** + * Set this to true to make the highlight operation full-bar oriented, + * false to make it highlight single values (relevant only for stacked). + */ + fun setHighlightFullBarEnabled(enabled: Boolean) { + mHighlightFullBarEnabled = enabled + } + + /** + * @return true the highlight operation is be full-bar oriented, false if single-value + */ + override val isHighlightFullBarEnabled: Boolean + get() = mHighlightFullBarEnabled + + var drawOrder: Array + /** + * Returns the currently set draw order. + */ + get() = mDrawOrder + /** + * Sets the order in which the provided data objects should be drawn. The + * earlier you place them in the provided array, the further they will be in + * the background. e.g. if you provide new DrawOrer[] { DrawOrder.BAR, + * DrawOrder.LINE }, the bars will be drawn behind the lines. + */ + set(order) { + if (order.isEmpty()) { + return + } + mDrawOrder = order + } + + /** + * draws all MarkerViews on the highlighted positions + */ + override fun drawMarkers(canvas: Canvas?) { + // if there is no marker view or drawing marker is disabled + + if (marker == null || !isDrawMarkersEnabled || !valuesToHighlight()) { + return + } + + for (i in highlighted!!.indices) { + val highlight = highlighted!![i] + val set = mData!!.getDataSetByHighlight(highlight) + + val e = mData!!.getEntryForHighlight(highlight) + if (e == null || set == null) { + continue + } + + val entryIndex = set.getEntryIndex(e) + + // make sure entry not null + if (entryIndex > set.entryCount * mAnimator.phaseX) { + continue + } + + val pos = getMarkerPosition(highlight) + + // check bounds + if (!viewPortHandler.isInBounds(pos[0], pos[1])) { + continue + } + + // callbacks to update the content + val markerItem = marker!![i % marker!!.size] + markerItem.refreshContent(e, highlight) + + // draw the marker + markerItem.draw(canvas, pos[0], pos[1]) + } + } + + override val accessibilityDescription: String? + get() = "This is a combined chart" } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.kt index 18d796b4c4..d47ba578c7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.kt @@ -1,24 +1,27 @@ -package com.github.mikephil.charting.charts; - -import android.content.Context; -import android.graphics.RectF; -import android.util.AttributeSet; -import android.util.Log; - -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.highlight.HorizontalBarHighlighter; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.renderer.HorizontalBarChartRenderer; -import com.github.mikephil.charting.renderer.XAxisRendererHorizontalBarChart; -import com.github.mikephil.charting.renderer.YAxisRendererHorizontalBarChart; -import com.github.mikephil.charting.utils.HorizontalViewPortHandler; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.TransformerHorizontalBarChart; -import com.github.mikephil.charting.utils.Utils; +package com.github.mikephil.charting.charts + +import android.content.Context +import android.graphics.RectF +import android.util.AttributeSet +import android.util.Log +import com.github.mikephil.charting.components.Legend.LegendHorizontalAlignment +import com.github.mikephil.charting.components.Legend.LegendOrientation +import com.github.mikephil.charting.components.Legend.LegendVerticalAlignment +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.highlight.HorizontalBarHighlighter +import com.github.mikephil.charting.renderer.HorizontalBarChartRenderer +import com.github.mikephil.charting.renderer.XAxisRendererHorizontalBarChart +import com.github.mikephil.charting.renderer.YAxisRendererHorizontalBarChart +import com.github.mikephil.charting.utils.HorizontalViewPortHandler +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.TransformerHorizontalBarChart +import com.github.mikephil.charting.utils.Utils +import kotlin.math.max +import kotlin.math.min /** * BarChart with horizontal bar orientation. In this implementation, x- and y-axis are switched, meaning the YAxis class @@ -26,225 +29,199 @@ import com.github.mikephil.charting.utils.Utils; * * @author Philipp Jahoda */ -public class HorizontalBarChart extends BarChart { +class HorizontalBarChart : BarChart { + constructor(context: Context) : super(context) - public HorizontalBarChart(Context context) { - super(context); - } - - public HorizontalBarChart(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public HorizontalBarChart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - @Override - protected void init() { + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - mViewPortHandler = new HorizontalViewPortHandler(); + override fun init() { + viewPortHandler = HorizontalViewPortHandler() - super.init(); + super.init() - mLeftAxisTransformer = new TransformerHorizontalBarChart(mViewPortHandler); - mRightAxisTransformer = new TransformerHorizontalBarChart(mViewPortHandler); + mLeftAxisTransformer = TransformerHorizontalBarChart(viewPortHandler) + mRightAxisTransformer = TransformerHorizontalBarChart(viewPortHandler) - mRenderer = new HorizontalBarChartRenderer(this, mAnimator, mViewPortHandler); - setHighlighter(new HorizontalBarHighlighter(this)); + mRenderer = HorizontalBarChartRenderer(this, mAnimator, viewPortHandler) + setHighlighter(HorizontalBarHighlighter(this)) - mAxisRendererLeft = new YAxisRendererHorizontalBarChart(mViewPortHandler, mAxisLeft, mLeftAxisTransformer); - mAxisRendererRight = new YAxisRendererHorizontalBarChart(mViewPortHandler, mAxisRight, mRightAxisTransformer); - mXAxisRenderer = new XAxisRendererHorizontalBarChart(mViewPortHandler, mXAxis, mLeftAxisTransformer); + mAxisRendererLeft = YAxisRendererHorizontalBarChart(viewPortHandler, mAxisLeft!!, mLeftAxisTransformer) + mAxisRendererRight = YAxisRendererHorizontalBarChart(viewPortHandler, mAxisRight!!, mRightAxisTransformer) + mXAxisRenderer = XAxisRendererHorizontalBarChart(viewPortHandler, mXAxis!!, mLeftAxisTransformer) } - private RectF mOffsetsBuffer = new RectF(); + private val mOffsetsBuffer = RectF() - protected void calculateLegendOffsets(RectF offsets) { + override fun calculateLegendOffsets(offsets: RectF) { + offsets.left = 0f + offsets.right = 0f + offsets.top = 0f + offsets.bottom = 0f - offsets.left = 0.f; - offsets.right = 0.f; - offsets.top = 0.f; - offsets.bottom = 0.f; + if (legend == null || !legend!!.isEnabled || legend!!.isDrawInsideEnabled) return - if (mLegend == null || !mLegend.isEnabled() || mLegend.isDrawInsideEnabled()) - return; + when (legend!!.orientation) { + LegendOrientation.VERTICAL -> when (legend!!.horizontalAlignment) { + LegendHorizontalAlignment.LEFT -> offsets.left += min( + legend!!.mNeededWidth, + viewPortHandler.chartWidth * legend!!.maxSizePercent + ) + legend!!.xOffset - switch (mLegend.getOrientation()) { - case VERTICAL: + LegendHorizontalAlignment.RIGHT -> offsets.right += min( + legend!!.mNeededWidth, + viewPortHandler.chartWidth * legend!!.maxSizePercent + ) + legend!!.xOffset - switch (mLegend.getHorizontalAlignment()) { - case LEFT: - offsets.left += Math.min(mLegend.mNeededWidth, - mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent()) - + mLegend.getXOffset(); - break; + LegendHorizontalAlignment.CENTER -> when (legend!!.verticalAlignment) { + LegendVerticalAlignment.TOP -> offsets.top += min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + legend!!.yOffset - case RIGHT: - offsets.right += Math.min(mLegend.mNeededWidth, - mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent()) - + mLegend.getXOffset(); - break; + LegendVerticalAlignment.BOTTOM -> offsets.bottom += min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + legend!!.yOffset - case CENTER: - - switch (mLegend.getVerticalAlignment()) { - case TOP: - offsets.top += Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) - + mLegend.getYOffset(); - break; - - case BOTTOM: - offsets.bottom += Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) - + mLegend.getYOffset(); - break; - - default: - break; - } + else -> {} } - break; - - case HORIZONTAL: - - switch (mLegend.getVerticalAlignment()) { - case TOP: - offsets.top += Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) - + mLegend.getYOffset(); + else -> {} + } - if (mAxisLeft.isEnabled() && mAxisLeft.isDrawLabelsEnabled()) - offsets.top += mAxisLeft.getRequiredHeightSpace( - mAxisRendererLeft.getPaintAxisLabels()); - break; + LegendOrientation.HORIZONTAL -> when (legend!!.verticalAlignment) { + LegendVerticalAlignment.TOP -> { + offsets.top += min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + legend!!.yOffset - case BOTTOM: - offsets.bottom += Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()) - + mLegend.getYOffset(); + if (mAxisLeft!!.isEnabled && mAxisLeft!!.isDrawLabelsEnabled) offsets.top += mAxisLeft!!.getRequiredHeightSpace( + mAxisRendererLeft!!.paintAxisLabels + ) + } - if (mAxisRight.isEnabled() && mAxisRight.isDrawLabelsEnabled()) - offsets.bottom += mAxisRight.getRequiredHeightSpace( - mAxisRendererRight.getPaintAxisLabels()); - break; + LegendVerticalAlignment.BOTTOM -> { + offsets.bottom += min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + legend!!.yOffset - default: - break; + if (mAxisRight!!.isEnabled && mAxisRight!!.isDrawLabelsEnabled) offsets.bottom += mAxisRight!!.getRequiredHeightSpace( + mAxisRendererRight!!.paintAxisLabels + ) } - break; + + else -> {} + } } } - @Override - public void calculateOffsets() { - - float offsetLeft = 0f, offsetRight = 0f, offsetTop = 0f, offsetBottom = 0f; + override fun calculateOffsets() { + var offsetLeft = 0f + var offsetRight = 0f + var offsetTop = 0f + var offsetBottom = 0f - calculateLegendOffsets(mOffsetsBuffer); + calculateLegendOffsets(mOffsetsBuffer) - offsetLeft += mOffsetsBuffer.left; - offsetTop += mOffsetsBuffer.top; - offsetRight += mOffsetsBuffer.right; - offsetBottom += mOffsetsBuffer.bottom; + offsetLeft += mOffsetsBuffer.left + offsetTop += mOffsetsBuffer.top + offsetRight += mOffsetsBuffer.right + offsetBottom += mOffsetsBuffer.bottom // offsets for y-labels - if (mAxisLeft.needsOffset()) { - offsetTop += mAxisLeft.getRequiredHeightSpace(mAxisRendererLeft.getPaintAxisLabels()); + if (mAxisLeft!!.needsOffset()) { + offsetTop += mAxisLeft!!.getRequiredHeightSpace(mAxisRendererLeft!!.paintAxisLabels) } - if (mAxisRight.needsOffset()) { - offsetBottom += mAxisRight.getRequiredHeightSpace(mAxisRendererRight.getPaintAxisLabels()); + if (mAxisRight!!.needsOffset()) { + offsetBottom += mAxisRight!!.getRequiredHeightSpace(mAxisRendererRight!!.paintAxisLabels) } - float xLabelWidth = mXAxis.mLabelWidth; - - if (mXAxis.isEnabled()) { + val xLabelWidth = mXAxis!!.mLabelWidth.toFloat() + if (mXAxis!!.isEnabled) { // offsets for x-labels - if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { - - offsetLeft += xLabelWidth; - } else if (mXAxis.getPosition() == XAxisPosition.TOP) { - - offsetRight += xLabelWidth; - - } else if (mXAxis.getPosition() == XAxisPosition.BOTH_SIDED) { - - offsetLeft += xLabelWidth; - offsetRight += xLabelWidth; + if (mXAxis!!.position == XAxisPosition.BOTTOM) { + offsetLeft += xLabelWidth + } else if (mXAxis!!.position == XAxisPosition.TOP) { + offsetRight += xLabelWidth + } else if (mXAxis!!.position == XAxisPosition.BOTH_SIDED) { + offsetLeft += xLabelWidth + offsetRight += xLabelWidth } } - offsetTop += getExtraTopOffset(); - offsetRight += getExtraRightOffset(); - offsetBottom += getExtraBottomOffset(); - offsetLeft += getExtraLeftOffset(); - - float minOffset = Utils.convertDpToPixel(mMinOffset); - - mViewPortHandler.restrainViewPort( - Math.max(minOffset, offsetLeft), - Math.max(minOffset, offsetTop), - Math.max(minOffset, offsetRight), - Math.max(minOffset, offsetBottom)); - - if (mLogEnabled) { - Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop + ", offsetRight: " + - offsetRight + ", offsetBottom: " - + offsetBottom); - Log.i(LOG_TAG, "Content: " + mViewPortHandler.getContentRect().toString()); + offsetTop += extraTopOffset + offsetRight += extraRightOffset + offsetBottom += extraBottomOffset + offsetLeft += extraLeftOffset + + val minOffset = Utils.convertDpToPixel(minOffset) + + viewPortHandler.restrainViewPort( + max(minOffset, offsetLeft), + max(minOffset, offsetTop), + max(minOffset, offsetRight), + max(minOffset, offsetBottom) + ) + + if (isLogEnabled) { + Log.i( + LOG_TAG, ("offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop + ", offsetRight: " + + offsetRight + ", offsetBottom: " + + offsetBottom) + ) + Log.i(LOG_TAG, "Content: " + viewPortHandler.contentRect.toString()) } - prepareOffsetMatrix(); - prepareValuePxMatrix(); + prepareOffsetMatrix() + prepareValuePxMatrix() } - @Override - protected void prepareValuePxMatrix() { - mRightAxisTransformer.prepareMatrixValuePx(mAxisRight.mAxisMinimum, mAxisRight.mAxisRange, mXAxis.mAxisRange, - mXAxis.mAxisMinimum); - mLeftAxisTransformer.prepareMatrixValuePx(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisRange, mXAxis.mAxisRange, - mXAxis.mAxisMinimum); + override fun prepareValuePxMatrix() { + mRightAxisTransformer!!.prepareMatrixValuePx( + mAxisRight!!.mAxisMinimum, mAxisRight!!.mAxisRange, mXAxis!!.mAxisRange, + mXAxis!!.mAxisMinimum + ) + mLeftAxisTransformer!!.prepareMatrixValuePx( + mAxisLeft!!.mAxisMinimum, mAxisLeft!!.mAxisRange, mXAxis!!.mAxisRange, + mXAxis!!.mAxisMinimum + ) } - @Override - protected float[] getMarkerPosition(Highlight high) { - return new float[]{high.getDrawY(), high.getDrawX()}; + override fun getMarkerPosition(high: Highlight): FloatArray { + return floatArrayOf(high.drawY, high.drawX) } - @Override - public void getBarBounds(BarEntry barEntry, RectF outputRect) { - - RectF bounds = outputRect; - IBarDataSet set = mData.getDataSetForEntry(barEntry); + override fun getBarBounds(barEntry: BarEntry, outputRect: RectF) { + val bounds = outputRect + val set = mData!!.getDataSetForEntry(barEntry) if (set == null) { - outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE); - return; + outputRect.set(Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE) + return } - float y = barEntry.getY(); - float x = barEntry.getX(); + val y = barEntry.y + val x = barEntry.x - float barWidth = mData.getBarWidth(); + val barWidth = mData!!.barWidth - float top = x - barWidth / 2f; - float bottom = x + barWidth / 2f; - float left = y >= 0 ? y : 0; - float right = y <= 0 ? y : 0; + val top = x - barWidth / 2f + val bottom = x + barWidth / 2f + val left = if (y >= 0) y else 0f + val right = if (y <= 0) y else 0f - bounds.set(left, top, right, bottom); - - getTransformer(set.getAxisDependency()).rectValueToPixel(bounds); + bounds.set(left, top, right, bottom) + getTransformer(set.axisDependency).rectValueToPixel(bounds) } - protected float[] mGetPositionBuffer = new float[2]; - /** * Returns a recyclable MPPointF instance. * @@ -252,19 +229,14 @@ public class HorizontalBarChart extends BarChart { * @param axis * @return */ - @Override - public MPPointF getPosition(Entry e, AxisDependency axis) { + override fun getPosition(e: Entry, axis: AxisDependency?): MPPointF? { + val vals = mGetPositionBuffer + vals[0] = e.y + vals[1] = e.x - if (e == null) - return null; + getTransformer(axis).pointValuesToPixel(vals) - float[] vals = mGetPositionBuffer; - vals[0] = e.getY(); - vals[1] = e.getX(); - - getTransformer(axis).pointValuesToPixel(vals); - - return MPPointF.getInstance(vals[0], vals[1]); + return MPPointF.Companion.getInstance(vals[0], vals[1]) } /** @@ -275,72 +247,65 @@ public class HorizontalBarChart extends BarChart { * @param y * @return */ - @Override - public Highlight getHighlightByTouchPoint(float x, float y) { - + override fun getHighlightByTouchPoint(x: Float, y: Float): Highlight? { if (mData == null) { - if (mLogEnabled) - Log.e(LOG_TAG, "Can't select by touch. No data set."); - return null; - } else - return getHighlighter().getHighlight(y, x); // switch x and y + if (isLogEnabled) Log.e(LOG_TAG, "Can't select by touch. No data set.") + return null + } else return highlighter!!.getHighlight(y, x) // switch x and y } - @Override - public float getLowestVisibleX() { - getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentLeft(), - mViewPortHandler.contentBottom(), posForGetLowestVisibleX); - float result = (float) Math.max(mXAxis.mAxisMinimum, posForGetLowestVisibleX.y); - return result; - } + override val lowestVisibleX: Float + get() { + getTransformer(AxisDependency.LEFT).getValuesByTouchPoint( + viewPortHandler.contentLeft(), + viewPortHandler.contentBottom(), posForGetLowestVisibleX + ) + val result = max(mXAxis!!.mAxisMinimum.toDouble(), posForGetLowestVisibleX.y).toFloat() + return result + } - @Override - public float getHighestVisibleX() { - getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentLeft(), - mViewPortHandler.contentTop(), posForGetHighestVisibleX); - float result = (float) Math.min(mXAxis.mAxisMaximum, posForGetHighestVisibleX.y); - return result; - } + override val highestVisibleX: Float + get() { + getTransformer(AxisDependency.LEFT).getValuesByTouchPoint( + viewPortHandler.contentLeft(), + viewPortHandler.contentTop(), posForGetHighestVisibleX + ) + val result = min(mXAxis!!.mAxisMaximum.toDouble(), posForGetHighestVisibleX.y).toFloat() + return result + } /** * ###### VIEWPORT METHODS BELOW THIS ###### */ - - @Override - public void setVisibleXRangeMaximum(float maxXRange) { - float xScale = mXAxis.mAxisRange / (maxXRange); - mViewPortHandler.setMinimumScaleY(xScale); + override fun setVisibleXRangeMaximum(maxXRange: Float) { + val xScale = mXAxis!!.mAxisRange / (maxXRange) + viewPortHandler.setMinimumScaleY(xScale) } - @Override - public void setVisibleXRangeMinimum(float minXRange) { - float xScale = mXAxis.mAxisRange / (minXRange); - mViewPortHandler.setMaximumScaleY(xScale); + override fun setVisibleXRangeMinimum(minXRange: Float) { + val xScale = mXAxis!!.mAxisRange / (minXRange) + viewPortHandler.setMaximumScaleY(xScale) } - @Override - public void setVisibleXRange(float minXRange, float maxXRange) { - float minScale = mXAxis.mAxisRange / minXRange; - float maxScale = mXAxis.mAxisRange / maxXRange; - mViewPortHandler.setMinMaxScaleY(minScale, maxScale); + override fun setVisibleXRange(minXRange: Float, maxXRange: Float) { + val minScale = mXAxis!!.mAxisRange / minXRange + val maxScale = mXAxis!!.mAxisRange / maxXRange + viewPortHandler.setMinMaxScaleY(minScale, maxScale) } - @Override - public void setVisibleYRangeMaximum(float maxYRange, AxisDependency axis) { - float yScale = getAxisRange(axis) / maxYRange; - mViewPortHandler.setMinimumScaleX(yScale); + override fun setVisibleYRangeMaximum(maxYRange: Float, axis: AxisDependency?) { + val yScale = getAxisRange(axis) / maxYRange + viewPortHandler.setMinimumScaleX(yScale) } - @Override - public void setVisibleYRangeMinimum(float minYRange, AxisDependency axis) { - float yScale = getAxisRange(axis) / minYRange; - mViewPortHandler.setMaximumScaleX(yScale); + override fun setVisibleYRangeMinimum(minYRange: Float, axis: AxisDependency?) { + val yScale = getAxisRange(axis) / minYRange + viewPortHandler.setMaximumScaleX(yScale) } - @Override - public void setVisibleYRange(float minYRange, float maxYRange, AxisDependency axis) { - float minScale = getAxisRange(axis) / minYRange; - float maxScale = getAxisRange(axis) / maxYRange; - mViewPortHandler.setMinMaxScaleX(minScale, maxScale); + override fun setVisibleYRange(minYRange: Float, maxYRange: Float, axis: AxisDependency?) { + val minScale = getAxisRange(axis) / minYRange + val maxScale = getAxisRange(axis) / maxYRange + viewPortHandler.setMinMaxScaleX(minScale, maxScale) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt index b2871320cd..04b82c098b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt @@ -2,30 +2,25 @@ package com.github.mikephil.charting.charts import android.content.Context import android.util.AttributeSet +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineData import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet import com.github.mikephil.charting.renderer.LineChartRenderer import java.util.Locale -open class LineChart : BarLineChartBase, LineDataProvider { - - constructor(context: Context?) : super(context) - constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) - constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) +open class LineChart : BarLineChartBase, LineDataProvider { + constructor(context: Context) : super(context) + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) override fun init() { super.init() - mRenderer = LineChartRenderer(this, mAnimator, mViewPortHandler) + mRenderer = LineChartRenderer(this, mAnimator, viewPortHandler) } override val lineData: LineData - get() { - mData?.let { - return it - } ?: run { - return LineData() - } - } + get() = mData ?: LineData() public override fun onDetachedFromWindow() { // releases the bitmap in the renderer to avoid oom error @@ -35,25 +30,26 @@ open class LineChart : BarLineChartBase, LineDataProvider { super.onDetachedFromWindow() } - override fun getAccessibilityDescription(): String { - val lineData = lineData - val numberOfPoints = lineData.entryCount - - // Min and max values... - val yAxisValueFormatter = axisLeft.valueFormatter - val minVal = yAxisValueFormatter.getFormattedValue(lineData.yMin, null) - val maxVal = yAxisValueFormatter.getFormattedValue(lineData.yMax, null) - - // Data range... - val xAxisValueFormatter = xAxis.valueFormatter - val minRange = xAxisValueFormatter.getFormattedValue(lineData.xMin, null) - val maxRange = xAxisValueFormatter.getFormattedValue(lineData.xMax, null) - val entries = if (numberOfPoints == 1) "entry" else "entries" - return String.format( - Locale.getDefault(), "The line chart has %d %s. " + - "The minimum value is %s and maximum value is %s." + - "Data ranges from %s to %s.", - numberOfPoints, entries, minVal, maxVal, minRange, maxRange - ) - } + override val accessibilityDescription: String? + get() { + val lineData = lineData + val numberOfPoints = lineData.entryCount + + // Min and max values... + val yAxisValueFormatter = axisLeft.valueFormatter + val minVal = yAxisValueFormatter!!.getFormattedValue(lineData.yMin, null) + val maxVal = yAxisValueFormatter.getFormattedValue(lineData.yMax, null) + + // Data range... + val xAxisValueFormatter = xAxis.valueFormatter + val minRange = xAxisValueFormatter!!.getFormattedValue(lineData.xMin, null) + val maxRange = xAxisValueFormatter.getFormattedValue(lineData.xMax, null) + val entries = if (numberOfPoints == 1) "entry" else "entries" + return String.format( + Locale.getDefault(), "The line chart has %d %s. " + + "The minimum value is %s and maximum value is %s." + + "Data ranges from %s to %s.", + numberOfPoints, entries, minVal, maxVal, minRange, maxRange + ) + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt index d029c2cb88..c68b6c852c 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt @@ -1,293 +1,379 @@ - -package com.github.mikephil.charting.charts; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.RectF; -import android.graphics.Typeface; -import android.text.TextUtils; -import android.util.AttributeSet; - -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.data.PieEntry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.highlight.PieHighlighter; -import com.github.mikephil.charting.interfaces.datasets.IPieDataSet; -import com.github.mikephil.charting.renderer.PieChartRenderer; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; - -import java.util.List; -import java.util.Locale; +package com.github.mikephil.charting.charts + +import android.content.Context +import android.graphics.Canvas +import android.graphics.RectF +import android.graphics.Typeface +import android.text.TextUtils +import android.util.AttributeSet +import com.github.mikephil.charting.components.XAxis +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.highlight.PieHighlighter +import com.github.mikephil.charting.interfaces.datasets.IPieDataSet +import com.github.mikephil.charting.renderer.PieChartRenderer +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Utils +import java.util.Locale +import kotlin.math.abs +import kotlin.math.cos +import kotlin.math.min +import kotlin.math.sin /** * View that represents a pie chart. Draws cake like slices. * * @author Philipp Jahoda */ -public class PieChart extends PieRadarChartBase { - +class PieChart : PieRadarChartBase { + /** + * returns the circlebox, the boundingbox of the pie-chart slices + * + * @return + */ /** * rect object that represents the bounds of the piechart, needed for * drawing the circle */ - private final RectF mCircleBox = new RectF(); + val circleBox: RectF = RectF() + + override val data: PieData? + get() = super.mData + /** + * Returns true if drawing the entry labels is enabled, false if not. + * + * @return + */ /** * flag indicating if entry labels should be drawn or not */ - private boolean mDrawEntryLabels = true; + var isDrawEntryLabelsEnabled: Boolean = true + private set + /** + * returns an integer array of all the different angles the chart slices + * have the angles in the returned array determine how much space (of 360°) + * each slice takes + * + * @return + */ /** * array that holds the width of each pie-slice in degrees */ - private float[] mDrawAngles = new float[1]; + var drawAngles: FloatArray = FloatArray(1) + private set + /** + * returns the absolute angles of the different chart slices (where the + * slices end) + * + * @return + */ /** * array that holds the absolute angle in degrees of each slice */ - private float[] mAbsoluteAngles = new float[1]; + var absoluteAngles: FloatArray = FloatArray(1) + private set + /** + * returns true if the hole in the center of the pie-chart is set to be + * visible, false if not + * + * @return + */ + /** + * set this to true to draw the pie center empty + * + * @param enabled + */ /** * if true, the white hole inside the chart will be drawn */ - private boolean mDrawHole = true; + var isDrawHoleEnabled: Boolean = true + /** + * Returns true if the inner tips of the slices are visible behind the hole, + * false if not. + * + * @return true if slices are visible behind the hole. + */ /** * if true, the hole will see-through to the inner tips of the slices */ - private boolean mDrawSlicesUnderHole = false; + var isDrawSlicesUnderHoleEnabled: Boolean = false + private set + /** + * Returns true if using percentage values is enabled for the chart. + * + * @return + */ /** * if true, the values inside the piechart are drawn as percent values */ - private boolean mUsePercentValues = false; + var isUsePercentValuesEnabled: Boolean = false + private set + /** + * Returns true if the chart is set to draw each end of a pie-slice + * "rounded". + * + * @return + */ /** * if true, the slices of the piechart are rounded */ - private boolean mDrawRoundedSlices = false; + var isDrawRoundedSlicesEnabled: Boolean = false + private set /** * variable for the text that is drawn in the center of the pie-chart */ - private CharSequence mCenterText = ""; + private var mCenterText: CharSequence? = "" - private final MPPointF mCenterTextOffset = MPPointF.getInstance(0, 0); + private val mCenterTextOffset: MPPointF = MPPointF.Companion.getInstance(0f, 0f) + /** + * Returns the size of the hole radius in percent of the total radius. + * + * @return + */ + /** + * sets the radius of the hole in the center of the piechart in percent of + * the maximum radius (max = the radius of the whole chart), default 50% + * + * @param percent + */ /** * indicates the size of the hole in the center of the piechart, default: * radius / 2 */ - private float mHoleRadiusPercent = 50f; + var holeRadius: Float = 50f + /** + * sets the radius of the transparent circle that is drawn next to the hole + * in the piechart in percent of the maximum radius (max = the radius of the + * whole chart), default 55% -> means 5% larger than the center-hole by + * default + * + * @param percent + */ /** * the radius of the transparent circle next to the chart-hole in the center */ - protected float mTransparentCircleRadiusPercent = 55f; + var transparentCircleRadius: Float = 55f + /** + * returns true if drawing the center text is enabled + * + * @return + */ /** * if enabled, centertext is drawn */ - private boolean mDrawCenterText = true; + var isDrawCenterTextEnabled: Boolean = true + private set - private float mCenterTextRadiusPercent = 100.f; + /** + * the rectangular radius of the bounding box for the center text, as a percentage of the pie + * hole + * default 1.f (100%) + */ + /** + * the rectangular radius of the bounding box for the center text, as a percentage of the pie + * hole + * default 1.f (100%) + */ + var centerTextRadiusPercent: Float = 100f - protected float mMaxAngle = 360f; + protected var mMaxAngle: Float = 360f /** * Minimum angle to draw slices, this only works if there is enough room for all slices to have * the minimum angle, default 0f. */ - private float mMinAngleForSlices = 0f; + private var mMinAngleForSlices = 0f - public PieChart(Context context) { - super(context); - } + constructor(context: Context) : super(context) - public PieChart(Context context, AttributeSet attrs) { - super(context, attrs); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - public PieChart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - @Override - protected void init() { - super.init(); + override fun init() { + super.init() - mRenderer = new PieChartRenderer(this, mAnimator, mViewPortHandler); - mXAxis = null; + mRenderer = PieChartRenderer(this, mAnimator!!, viewPortHandler) + mXAxis = null - mHighlighter = new PieHighlighter(this); + highlighter = PieHighlighter(this) } - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) - if (mData == null) - return; + if (mData == null) return - mRenderer.drawData(canvas); + mRenderer!!.drawData(canvas) - if (valuesToHighlight()) - mRenderer.drawHighlighted(canvas, mIndicesToHighlight); + if (valuesToHighlight()) mRenderer!!.drawHighlighted(canvas, highlighted!!) - mRenderer.drawExtras(canvas); + mRenderer!!.drawExtras(canvas) - mRenderer.drawValues(canvas); + mRenderer!!.drawValues(canvas) - mLegendRenderer.renderLegend(canvas); + legendRenderer!!.renderLegend(canvas) - drawDescription(canvas); + drawDescription(canvas) - drawMarkers(canvas); + drawMarkers(canvas) } - @Override - public void calculateOffsets() { - super.calculateOffsets(); + override fun calculateOffsets() { + super.calculateOffsets() // prevent nullpointer when no data set - if (mData == null) - return; + if (mData == null) return - float diameter = getDiameter(); - float radius = diameter / 2f; + val diameter = diameter + val radius = diameter / 2f - MPPointF c = getCenterOffsets(); + val c = centerOffsets - float shift = mData.getDataSet().getSelectionShift(); + val shift = mData!!.dataSet!!.selectionShift // create the circle box that will contain the pie-chart (the bounds of // the pie-chart) - mCircleBox.set(c.x - radius + shift, - c.y - radius + shift, - c.x + radius - shift, - c.y + radius - shift); + circleBox!!.set( + c!!.x - radius + shift, + c.y - radius + shift, + c.x + radius - shift, + c.y + radius - shift + ) - MPPointF.recycleInstance(c); + MPPointF.Companion.recycleInstance(c) } - @Override - protected void calcMinMax() { - calcAngles(); + override fun calcMinMax() { + calcAngles() } - @Override - protected float[] getMarkerPosition(Highlight highlight) { - - MPPointF center = getCenterCircleBox(); - float r = getRadius(); + override fun getMarkerPosition(high: Highlight): FloatArray { + val center = this.centerCircleBox + var r = radius - float off = r / 10f * 3.6f; + var off = r / 10f * 3.6f - if (isDrawHoleEnabled()) { - off = (r - (r / 100f * getHoleRadius())) / 2f; + if (this.isDrawHoleEnabled) { + off = (r - (r / 100f * this.holeRadius)) / 2f } - r -= off; // offset to keep things inside the chart + r -= off // offset to keep things inside the chart - float rotationAngle = getRotationAngle(); + val rotationAngle = rotationAngle - int entryIndex = (int) highlight.getX(); + val entryIndex = high.x.toInt() // offset needed to center the drawn text in the slice - float offset = mDrawAngles[entryIndex] / 2; + val offset = this.drawAngles[entryIndex] / 2 // calculate the text position - float x = (float) (r - * Math.cos(Math.toRadians((rotationAngle + mAbsoluteAngles[entryIndex] - offset) - * mAnimator.getPhaseY())) + center.x); - float y = (float) (r - * Math.sin(Math.toRadians((rotationAngle + mAbsoluteAngles[entryIndex] - offset) - * mAnimator.getPhaseY())) + center.y); - - MPPointF.recycleInstance(center); - return new float[]{x, y}; + val x = (r + * cos( + Math.toRadians( + ((rotationAngle + this.absoluteAngles[entryIndex] - offset) + * mAnimator!!.phaseY).toDouble() + ) + ) + center.x).toFloat() + val y = (r + * sin( + Math.toRadians( + ((rotationAngle + this.absoluteAngles[entryIndex] - offset) + * mAnimator!!.phaseY).toDouble() + ) + ) + center.y).toFloat() + + MPPointF.Companion.recycleInstance(center) + return floatArrayOf(x, y) } /** * calculates the needed angles for the chart slices */ - private void calcAngles() { + private fun calcAngles() { + val entryCount = mData!!.entryCount - int entryCount = mData.getEntryCount(); - - if (mDrawAngles.length != entryCount) { - mDrawAngles = new float[entryCount]; + if (drawAngles.size != entryCount) { + this.drawAngles = FloatArray(entryCount) } else { - for (int i = 0; i < entryCount; i++) { - mDrawAngles[i] = 0; + for (i in 0.. dataSets = mData.getDataSets(); + val yValueSum = mData!!.yValueSum - boolean hasMinAngle = mMinAngleForSlices != 0f && entryCount * mMinAngleForSlices <= mMaxAngle; - float[] minAngles = new float[entryCount]; + val dataSets = mData!!.dataSets - int cnt = 0; - float offset = 0f; - float diff = 0f; + val hasMinAngle = mMinAngleForSlices != 0f && entryCount * mMinAngleForSlices <= mMaxAngle + val minAngles = FloatArray(entryCount) - for (int i = 0; i < mData.getDataSetCount(); i++) { + var cnt = 0 + var offset = 0f + var diff = 0f - IPieDataSet set = dataSets.get(i); + for (i in 0.. { * @param index * @return */ - public boolean needsHighlight(int index) { - + fun needsHighlight(index: Int): Boolean { // no highlight - if (!valuesToHighlight()) - return false; + + if (!valuesToHighlight()) return false // check if the xvalue for the given dataset needs highlight - for (Highlight highlight : mIndicesToHighlight) - if ((int) highlight.getX() == index) - return true; + for (highlight in highlighted!!) if (highlight.x.toInt() == index) return true - return false; + return false } /** * calculates the needed angle for a given value * * @param value + * @param yValueSum * @return */ - private float calcAngle(float value) { - return calcAngle(value, mData.getYValueSum()); - } - /** * calculates the needed angle for a given value * * @param value - * @param yValueSum * @return */ - private float calcAngle(float value, float yValueSum) { - return value / yValueSum * mMaxAngle; + private fun calcAngle(value: Float, yValueSum: Float = mData!!.yValueSum): Float { + return value / yValueSum * mMaxAngle } /** @@ -337,24 +416,20 @@ public class PieChart extends PieRadarChartBase { * * @return */ - @Deprecated - @Override - public XAxis getXAxis() { - throw new RuntimeException("PieChart has no XAxis"); - } - - @Override - public int getIndexForAngle(float angle) { + @Deprecated("") + override val xAxis: XAxis + get() = throw RuntimeException("PieChart has no XAxis") + override fun getIndexForAngle(angle: Float): Int { // take the current angle of the chart into consideration - float a = Utils.getNormalizedAngle(angle - getRotationAngle()); - for (int i = 0; i < mAbsoluteAngles.length; i++) { - if (mAbsoluteAngles[i] > a) - return i; + val a = Utils.getNormalizedAngle(angle - rotationAngle) + + for (i in absoluteAngles.indices) { + if (this.absoluteAngles[i] > a) return i } - return -1; // return -1 if no index found + return -1 // return -1 if no index found } /** @@ -363,37 +438,14 @@ public class PieChart extends PieRadarChartBase { * @param xIndex * @return */ - public int getDataSetIndexForIndex(int xIndex) { - - List dataSets = mData.getDataSets(); + fun getDataSetIndexForIndex(xIndex: Int): Int { + val dataSets = mData!!.dataSets - for (int i = 0; i < dataSets.size(); i++) { - if (dataSets.get(i).getEntryForXValue(xIndex, Float.NaN) != null) - return i; + for (i in dataSets.indices) { + if (dataSets[i].getEntryForXValue(xIndex.toFloat(), Float.Companion.NaN) != null) return i } - return -1; - } - - /** - * returns an integer array of all the different angles the chart slices - * have the angles in the returned array determine how much space (of 360°) - * each slice takes - * - * @return - */ - public float[] getDrawAngles() { - return mDrawAngles; - } - - /** - * returns the absolute angles of the different chart slices (where the - * slices end) - * - * @return - */ - public float[] getAbsoluteAngles() { - return mAbsoluteAngles; + return -1 } /** @@ -402,66 +454,33 @@ public class PieChart extends PieRadarChartBase { * * @param color */ - public void setHoleColor(int color) { - ((PieChartRenderer) mRenderer).getPaintHole().setColor(color); + fun setHoleColor(color: Int) { + (mRenderer as PieChartRenderer).paintHole.setColor(color) } /** * Enable or disable the visibility of the inner tips of the slices behind the hole */ - public void setDrawSlicesUnderHole(boolean enable) { - mDrawSlicesUnderHole = enable; - } - - /** - * Returns true if the inner tips of the slices are visible behind the hole, - * false if not. - * - * @return true if slices are visible behind the hole. - */ - public boolean isDrawSlicesUnderHoleEnabled() { - return mDrawSlicesUnderHole; - } - - /** - * set this to true to draw the pie center empty - * - * @param enabled - */ - public void setDrawHoleEnabled(boolean enabled) { - this.mDrawHole = enabled; - } - - /** - * returns true if the hole in the center of the pie-chart is set to be - * visible, false if not - * - * @return - */ - public boolean isDrawHoleEnabled() { - return mDrawHole; - } - - /** - * Sets the text String that is displayed in the center of the PieChart. - * - * @param text - */ - public void setCenterText(CharSequence text) { - if (text == null) - mCenterText = ""; - else - mCenterText = text; - } - - /** - * returns the text that is drawn in the center of the pie-chart - * - * @return - */ - public CharSequence getCenterText() { - return mCenterText; - } + fun setDrawSlicesUnderHole(enable: Boolean) { + this.isDrawSlicesUnderHoleEnabled = enable + } + + var centerText: CharSequence? + /** + * returns the text that is drawn in the center of the pie-chart + * + * @return + */ + get() = mCenterText + /** + * Sets the text String that is displayed in the center of the PieChart. + * + * @param text + */ + set(text) { + if (text == null) mCenterText = "" + else mCenterText = text + } /** * set this to true to draw the text that is displayed in the center of the @@ -469,62 +488,35 @@ public class PieChart extends PieRadarChartBase { * * @param enabled */ - public void setDrawCenterText(boolean enabled) { - this.mDrawCenterText = enabled; + fun setDrawCenterText(enabled: Boolean) { + this.isDrawCenterTextEnabled = enabled } - /** - * returns true if drawing the center text is enabled - * - * @return - */ - public boolean isDrawCenterTextEnabled() { - return mDrawCenterText; - } - - @Override - protected float getRequiredLegendOffset() { - return mLegendRenderer.getLabelPaint().getTextSize() * 2.f; - } + override val requiredLegendOffset: Float + get() = legendRenderer!!.labelPaint.textSize * 2f - @Override - protected float getRequiredBaseOffset() { - return 0; - } - - @Override - public float getRadius() { - if (mCircleBox == null) - return 0; - else - return Math.min(mCircleBox.width() / 2f, mCircleBox.height() / 2f); - } + override val requiredBaseOffset: Float + get() = 0f - /** - * returns the circlebox, the boundingbox of the pie-chart slices - * - * @return - */ - public RectF getCircleBox() { - return mCircleBox; - } + override val radius: Float + get() = if (this.circleBox == null) 0f + else min(circleBox.width() / 2f, circleBox.height() / 2f) - /** - * returns the center of the circlebox - * - * @return - */ - public MPPointF getCenterCircleBox() { - return MPPointF.getInstance(mCircleBox.centerX(), mCircleBox.centerY()); - } + val centerCircleBox: MPPointF + /** + * returns the center of the circlebox + * + * @return + */ + get() = MPPointF.Companion.getInstance(circleBox!!.centerX(), circleBox.centerY()) /** * sets the typeface for the center-text paint * * @param t */ - public void setCenterTextTypeface(Typeface t) { - ((PieChartRenderer) mRenderer).getPaintCenterText().setTypeface(t); + fun setCenterTextTypeface(t: Typeface?) { + (mRenderer as PieChartRenderer).paintCenterText.setTypeface(t) } /** @@ -532,9 +524,10 @@ public class PieChart extends PieRadarChartBase { * * @param sizeDp */ - public void setCenterTextSize(float sizeDp) { - ((PieChartRenderer) mRenderer).getPaintCenterText().setTextSize( - Utils.convertDpToPixel(sizeDp)); + fun setCenterTextSize(sizeDp: Float) { + (mRenderer as PieChartRenderer).paintCenterText.setTextSize( + Utils.convertDpToPixel(sizeDp) + ) } /** @@ -542,8 +535,8 @@ public class PieChart extends PieRadarChartBase { * * @param sizePixels */ - public void setCenterTextSizePixels(float sizePixels) { - ((PieChartRenderer) mRenderer).getPaintCenterText().setTextSize(sizePixels); + fun setCenterTextSizePixels(sizePixels: Float) { + (mRenderer as PieChartRenderer).paintCenterText.setTextSize(sizePixels) } /** @@ -552,46 +545,26 @@ public class PieChart extends PieRadarChartBase { * @param x * @param y */ - public void setCenterTextOffset(float x, float y) { - mCenterTextOffset.x = Utils.convertDpToPixel(x); - mCenterTextOffset.y = Utils.convertDpToPixel(y); + fun setCenterTextOffset(x: Float, y: Float) { + mCenterTextOffset.x = Utils.convertDpToPixel(x) + mCenterTextOffset.y = Utils.convertDpToPixel(y) } - /** - * Returns the offset on the x- and y-axis the center text has in dp. - * - * @return - */ - public MPPointF getCenterTextOffset() { - return MPPointF.getInstance(mCenterTextOffset.x, mCenterTextOffset.y); - } + val centerTextOffset: MPPointF + /** + * Returns the offset on the x- and y-axis the center text has in dp. + * + * @return + */ + get() = MPPointF.Companion.getInstance(mCenterTextOffset.x, mCenterTextOffset.y) /** * Sets the color of the center text of the PieChart. * * @param color */ - public void setCenterTextColor(int color) { - ((PieChartRenderer) mRenderer).getPaintCenterText().setColor(color); - } - - /** - * sets the radius of the hole in the center of the piechart in percent of - * the maximum radius (max = the radius of the whole chart), default 50% - * - * @param percent - */ - public void setHoleRadius(final float percent) { - mHoleRadiusPercent = percent; - } - - /** - * Returns the size of the hole radius in percent of the total radius. - * - * @return - */ - public float getHoleRadius() { - return mHoleRadiusPercent; + fun setCenterTextColor(color: Int) { + (mRenderer as PieChartRenderer).paintCenterText.setColor(color) } /** @@ -599,28 +572,11 @@ public class PieChart extends PieRadarChartBase { * * @param color */ - public void setTransparentCircleColor(int color) { - - Paint p = ((PieChartRenderer) mRenderer).getPaintTransparentCircle(); - int alpha = p.getAlpha(); - p.setColor(color); - p.setAlpha(alpha); - } - - /** - * sets the radius of the transparent circle that is drawn next to the hole - * in the piechart in percent of the maximum radius (max = the radius of the - * whole chart), default 55% -> means 5% larger than the center-hole by - * default - * - * @param percent - */ - public void setTransparentCircleRadius(final float percent) { - mTransparentCircleRadiusPercent = percent; - } - - public float getTransparentCircleRadius() { - return mTransparentCircleRadiusPercent; + fun setTransparentCircleColor(color: Int) { + val p = (mRenderer as PieChartRenderer).paintTransparentCircle + val alpha = p.getAlpha() + p.setColor(color) + p.setAlpha(alpha) } /** @@ -630,8 +586,8 @@ public class PieChart extends PieRadarChartBase { * * @param alpha 0-255 */ - public void setTransparentCircleAlpha(int alpha) { - ((PieChartRenderer) mRenderer).getPaintTransparentCircle().setAlpha(alpha); + fun setTransparentCircleAlpha(alpha: Int) { + (mRenderer as PieChartRenderer).paintTransparentCircle.setAlpha(alpha) } /** @@ -640,9 +596,9 @@ public class PieChart extends PieRadarChartBase { * * @param enabled */ - @Deprecated - public void setDrawSliceText(boolean enabled) { - mDrawEntryLabels = enabled; + @Deprecated("") + fun setDrawSliceText(enabled: Boolean) { + this.isDrawEntryLabelsEnabled = enabled } /** @@ -650,17 +606,8 @@ public class PieChart extends PieRadarChartBase { * * @param enabled */ - public void setDrawEntryLabels(boolean enabled) { - mDrawEntryLabels = enabled; - } - - /** - * Returns true if drawing the entry labels is enabled, false if not. - * - * @return - */ - public boolean isDrawEntryLabelsEnabled() { - return mDrawEntryLabels; + fun setDrawEntryLabels(enabled: Boolean) { + this.isDrawEntryLabelsEnabled = enabled } /** @@ -668,8 +615,8 @@ public class PieChart extends PieRadarChartBase { * * @param color */ - public void setEntryLabelColor(int color) { - ((PieChartRenderer) mRenderer).getPaintEntryLabels().setColor(color); + fun setEntryLabelColor(color: Int) { + (mRenderer as PieChartRenderer).paintEntryLabels.setColor(color) } /** @@ -677,8 +624,8 @@ public class PieChart extends PieRadarChartBase { * * @param tf */ - public void setEntryLabelTypeface(Typeface tf) { - ((PieChartRenderer) mRenderer).getPaintEntryLabels().setTypeface(tf); + fun setEntryLabelTypeface(tf: Typeface?) { + (mRenderer as PieChartRenderer).paintEntryLabels.setTypeface(tf) } /** @@ -686,8 +633,8 @@ public class PieChart extends PieRadarChartBase { * * @param size */ - public void setEntryLabelTextSize(float size) { - ((PieChartRenderer) mRenderer).getPaintEntryLabels().setTextSize(Utils.convertDpToPixel(size)); + fun setEntryLabelTextSize(size: Float) { + (mRenderer as PieChartRenderer).paintEntryLabels.setTextSize(Utils.convertDpToPixel(size)) } /** @@ -696,18 +643,8 @@ public class PieChart extends PieRadarChartBase { * * @param enabled draw curved ends of slices */ - public void setDrawRoundedSlices(boolean enabled) { - mDrawRoundedSlices = enabled; - } - - /** - * Returns true if the chart is set to draw each end of a pie-slice - * "rounded". - * - * @return - */ - public boolean isDrawRoundedSlicesEnabled() { - return mDrawRoundedSlices; + fun setDrawRoundedSlices(enabled: Boolean) { + this.isDrawRoundedSlicesEnabled = enabled } /** @@ -717,114 +654,80 @@ public class PieChart extends PieRadarChartBase { * * @param enabled */ - public void setUsePercentValues(boolean enabled) { - mUsePercentValues = enabled; - } - - /** - * Returns true if using percentage values is enabled for the chart. - * - * @return - */ - public boolean isUsePercentValuesEnabled() { - return mUsePercentValues; - } - - /** - * the rectangular radius of the bounding box for the center text, as a percentage of the pie - * hole - * default 1.f (100%) - */ - public void setCenterTextRadiusPercent(float percent) { - mCenterTextRadiusPercent = percent; - } - - /** - * the rectangular radius of the bounding box for the center text, as a percentage of the pie - * hole - * default 1.f (100%) - */ - public float getCenterTextRadiusPercent() { - return mCenterTextRadiusPercent; - } - - public float getMaxAngle() { - return mMaxAngle; - } - - /** - * Sets the max angle that is used for calculating the pie-circle. 360f means - * it's a full PieChart, 180f results in a half-pie-chart. Default: 360f - * - * @param maxangle min 90, max 360 - */ - public void setMaxAngle(float maxangle) { - - if (maxangle > 360) - maxangle = 360f; - - if (maxangle < 90) - maxangle = 90f; - - this.mMaxAngle = maxangle; + fun setUsePercentValues(enabled: Boolean) { + this.isUsePercentValuesEnabled = enabled } - /** - * The minimum angle slices on the chart are rendered with, default is 0f. - * - * @return minimum angle for slices - */ - public float getMinAngleForSlices() { - return mMinAngleForSlices; - } + var maxAngle: Float + get() = mMaxAngle + /** + * Sets the max angle that is used for calculating the pie-circle. 360f means + * it's a full PieChart, 180f results in a half-pie-chart. Default: 360f + * + * @param maxangle min 90, max 360 + */ + set(maxangle) { + var maxangle = maxangle + if (maxangle > 360) maxangle = 360f - /** - * Set the angle to set minimum size for slices, you must call {@link #notifyDataSetChanged()} - * and {@link #invalidate()} when changing this, only works if there is enough room for all - * slices to have the minimum angle. - * - * @param minAngle minimum 0, maximum is half of {@link #setMaxAngle(float)} - */ - public void setMinAngleForSlices(float minAngle) { + if (maxangle < 90) maxangle = 90f - if (minAngle > (mMaxAngle / 2f)) - minAngle = mMaxAngle / 2f; - else if (minAngle < 0) - minAngle = 0f; + this.mMaxAngle = maxangle + } - this.mMinAngleForSlices = minAngle; - } + var minAngleForSlices: Float + /** + * The minimum angle slices on the chart are rendered with, default is 0f. + * + * @return minimum angle for slices + */ + get() = mMinAngleForSlices + /** + * Set the angle to set minimum size for slices, you must call [.notifyDataSetChanged] + * and [.invalidate] when changing this, only works if there is enough room for all + * slices to have the minimum angle. + * + * @param minAngle minimum 0, maximum is half of [.setMaxAngle] + */ + set(minAngle) { + var minAngle = minAngle + if (minAngle > (mMaxAngle / 2f)) minAngle = mMaxAngle / 2f + else if (minAngle < 0) minAngle = 0f + + this.mMinAngleForSlices = minAngle + } - @Override - protected void onDetachedFromWindow() { + protected override fun onDetachedFromWindow() { // releases the bitmap in the renderer to avoid oom error - if (mRenderer != null && mRenderer instanceof PieChartRenderer) { - ((PieChartRenderer) mRenderer).releaseBitmap(); + if (mRenderer != null && mRenderer is PieChartRenderer) { + (mRenderer as PieChartRenderer).releaseBitmap() } - super.onDetachedFromWindow(); + super.onDetachedFromWindow() } - @Override - public String getAccessibilityDescription() { + override val accessibilityDescription: String? + get() { + val pieData = mData - PieData pieData = getData(); + var entryCount = 0 + if (pieData != null) entryCount = pieData.entryCount - int entryCount = 0; - if (pieData != null) - entryCount = pieData.getEntryCount(); + val builder = StringBuilder() - StringBuilder builder = new StringBuilder(); + builder.append(String.format(Locale.getDefault(), "The pie chart has %d entries.", entryCount)) - builder.append(String.format(Locale.getDefault(), "The pie chart has %d entries.", entryCount)); + for (i in 0..>> - extends Chart { - +abstract class PieRadarChartBase, T : ChartData> + : Chart { /** * holds the normalized version of the current rotation angle of the chart */ - private float mRotationAngle = 270f; + private var mRotationAngle = 270f + /** + * gets the raw version of the current rotation angle of the pie chart the + * returned value could be any value, negative or positive, outside of the + * 360 degrees. this is used when working with rotation direction, mainly by + * gestures and animations. + * + * @return + */ /** * holds the raw version of the current rotation angle of the chart */ - private float mRawRotationAngle = 270f; + var rawRotationAngle: Float = 270f + private set + /** + * Returns true if rotation of the chart by touch is enabled, false if not. + * + * @return + */ + /** + * Set this to true to enable the rotation / spinning of the chart by touch. + * Set it to false to disable it. Default: true + * + * @param enabled + */ /** * flag that indicates if rotation is enabled or not */ - protected boolean mRotateEnabled = true; + var isRotationEnabled: Boolean = true + /** + * Gets the minimum offset (padding) around the chart, defaults to 0.f + */ /** * Sets the minimum offset (padding) around the chart, defaults to 0.f */ - protected float mMinOffset = 0.f; + /** + * Sets the minimum offset (padding) around the chart, defaults to 0.f + */ + var minOffset: Float = 0f - public PieRadarChartBase(Context context) { - super(context); - } + constructor(context: Context) : super(context) - public PieRadarChartBase(Context context, AttributeSet attrs) { - super(context, attrs); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - public PieRadarChartBase(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - @Override - protected void init() { - super.init(); + override fun init() { + super.init() - mChartTouchListener = new PieRadarChartTouchListener(this); + mChartTouchListener = PieRadarChartTouchListener(this) } - @Override - protected void calcMinMax() { + override fun calcMinMax() { //mXAxis.mAxisRange = mData.getXVals().size() - 1; } - @Override - public int getMaxVisibleCount() { - return mData.getEntryCount(); - } + override val maxVisibleCount: Int + get() = mData!!.entryCount - @Override - public boolean onTouchEvent(MotionEvent event) { + override fun onTouchEvent(event: MotionEvent?): Boolean { // use the pie- and radarchart listener own listener - if (mTouchEnabled && mChartTouchListener != null) - return mChartTouchListener.onTouch(this, event); - else - return super.onTouchEvent(event); + if (mTouchEnabled && mChartTouchListener != null) return mChartTouchListener!!.onTouch(this, event) + else return super.onTouchEvent(event) } - @Override - public void computeScroll() { - - if (mChartTouchListener instanceof PieRadarChartTouchListener) - ((PieRadarChartTouchListener) mChartTouchListener).computeScroll(); + override fun computeScroll() { + if (mChartTouchListener is PieRadarChartTouchListener) (mChartTouchListener as PieRadarChartTouchListener).computeScroll() } - @Override - public void notifyDataSetChanged() { - if (mData == null) - return; + override fun notifyDataSetChanged() { + if (mData == null) return - calcMinMax(); + calcMinMax() - if (mLegend != null) - mLegendRenderer.computeLegend(mData); + if (legend != null) legendRenderer?.computeLegend(mData!!) - calculateOffsets(); + calculateOffsets() } - @Override - public void calculateOffsets() { - - float legendLeft = 0f, legendRight = 0f, legendBottom = 0f, legendTop = 0f; - - if (mLegend != null && mLegend.isEnabled() && !mLegend.isDrawInsideEnabled()) { - - float fullLegendWidth = Math.min(mLegend.mNeededWidth, - mViewPortHandler.getChartWidth() * mLegend.getMaxSizePercent()); - - switch (mLegend.getOrientation()) { - case VERTICAL: { - float xLegendOffset = 0.f; - - if (mLegend.getHorizontalAlignment() == Legend.LegendHorizontalAlignment.LEFT - || mLegend.getHorizontalAlignment() == Legend.LegendHorizontalAlignment.RIGHT) { - if (mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.CENTER) { + public override fun calculateOffsets() { + var legendLeft = 0f + var legendRight = 0f + var legendBottom = 0f + var legendTop = 0f + + if (legend != null && legend!!.isEnabled && !legend!!.isDrawInsideEnabled) { + val fullLegendWidth = min( + legend!!.mNeededWidth, + viewPortHandler.chartWidth * legend!!.maxSizePercent + ) + + when (legend!!.orientation) { + LegendOrientation.VERTICAL -> { + var xLegendOffset = 0f + + if (legend!!.horizontalAlignment == LegendHorizontalAlignment.LEFT + || legend!!.horizontalAlignment == LegendHorizontalAlignment.RIGHT + ) { + if (legend!!.verticalAlignment == LegendVerticalAlignment.CENTER) { // this is the space between the legend and the chart - final float spacing = Utils.convertDpToPixel(13f); - - xLegendOffset = fullLegendWidth + spacing; + val spacing = Utils.convertDpToPixel(13f) + xLegendOffset = fullLegendWidth + spacing } else { // this is the space between the legend and the chart - float spacing = Utils.convertDpToPixel(8f); + val spacing = Utils.convertDpToPixel(8f) - float legendWidth = fullLegendWidth + spacing; - float legendHeight = mLegend.mNeededHeight + mLegend.mTextHeightMax; + val legendWidth = fullLegendWidth + spacing + val legendHeight = legend!!.mNeededHeight + legend!!.mTextHeightMax - MPPointF center = getCenter(); + val center = center - float bottomX = mLegend.getHorizontalAlignment() == - Legend.LegendHorizontalAlignment.RIGHT - ? getWidth() - legendWidth + 15.f - : legendWidth - 15.f; - float bottomY = legendHeight + 15.f; - float distLegend = distanceToCenter(bottomX, bottomY); + val bottomX = if (legend!!.horizontalAlignment == + LegendHorizontalAlignment.RIGHT + ) + width - legendWidth + 15f + else + legendWidth - 15f + val bottomY = legendHeight + 15f + val distLegend = distanceToCenter(bottomX, bottomY) - MPPointF reference = getPosition(center, getRadius(), - getAngleForPoint(bottomX, bottomY)); + val reference = getPosition( + center, this.radius, + getAngleForPoint(bottomX, bottomY) + ) - float distReference = distanceToCenter(reference.x, reference.y); - float minOffset = Utils.convertDpToPixel(5f); + val distReference = distanceToCenter(reference.x, reference.y) + val minOffset = Utils.convertDpToPixel(5f) - if (bottomY >= center.y && getHeight() - legendWidth > getWidth()) { - xLegendOffset = legendWidth; + if (bottomY >= center.y && height - legendWidth > width) { + xLegendOffset = legendWidth } else if (distLegend < distReference) { - - float diff = distReference - distLegend; - xLegendOffset = minOffset + diff; + val diff = distReference - distLegend + xLegendOffset = minOffset + diff } - MPPointF.recycleInstance(center); - MPPointF.recycleInstance(reference); + MPPointF.Companion.recycleInstance(center) + MPPointF.Companion.recycleInstance(reference) } } - switch (mLegend.getHorizontalAlignment()) { - case LEFT: - legendLeft = xLegendOffset; - break; - - case RIGHT: - legendRight = xLegendOffset; - break; - - case CENTER: - switch (mLegend.getVerticalAlignment()) { - case TOP: - legendTop = Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()); - break; - case BOTTOM: - legendBottom = Math.min(mLegend.mNeededHeight, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()); - break; - } - break; + when (legend!!.horizontalAlignment) { + LegendHorizontalAlignment.LEFT -> legendLeft = xLegendOffset + LegendHorizontalAlignment.RIGHT -> legendRight = xLegendOffset + LegendHorizontalAlignment.CENTER -> when (legend!!.verticalAlignment) { + LegendVerticalAlignment.TOP -> legendTop = min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + + LegendVerticalAlignment.BOTTOM -> legendBottom = min( + legend!!.mNeededHeight, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + + else -> {} + } + + else -> {} } } - break; - case HORIZONTAL: - float yLegendOffset = 0.f; - - if (mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.TOP || - mLegend.getVerticalAlignment() == Legend.LegendVerticalAlignment.BOTTOM) { + LegendOrientation.HORIZONTAL -> { + var yLegendOffset = 0f + if (legend!!.verticalAlignment == LegendVerticalAlignment.TOP || + legend!!.verticalAlignment == LegendVerticalAlignment.BOTTOM + ) { // It's possible that we do not need this offset anymore as it // is available through the extraOffsets, but changing it can mean // changing default visibility for existing apps. - float yOffset = getRequiredLegendOffset(); - - yLegendOffset = Math.min(mLegend.mNeededHeight + yOffset, - mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent()); - - switch (mLegend.getVerticalAlignment()) { - case TOP: - legendTop = yLegendOffset; - break; - case BOTTOM: - legendBottom = yLegendOffset; - break; + + val yOffset = this.requiredLegendOffset + + yLegendOffset = min( + legend!!.mNeededHeight + yOffset, + viewPortHandler.chartHeight * legend!!.maxSizePercent + ) + + when (legend!!.verticalAlignment) { + LegendVerticalAlignment.TOP -> legendTop = yLegendOffset + LegendVerticalAlignment.BOTTOM -> legendBottom = yLegendOffset + else -> {} } } - break; + } } - legendLeft += getRequiredBaseOffset(); - legendRight += getRequiredBaseOffset(); - legendTop += getRequiredBaseOffset(); - legendBottom += getRequiredBaseOffset(); + legendLeft += this.requiredBaseOffset + legendRight += this.requiredBaseOffset + legendTop += this.requiredBaseOffset + legendBottom += this.requiredBaseOffset } - float minOffset = Utils.convertDpToPixel(mMinOffset); + var minOffset = Utils.convertDpToPixel(this.minOffset) - if (this instanceof RadarChart) { - XAxis x = this.getXAxis(); + if (this is RadarChart) { + val x = this.xAxis - if (x.isEnabled() && x.isDrawLabelsEnabled()) { - minOffset = Math.max(minOffset, x.mLabelWidth); + if (x.isEnabled && x.isDrawLabelsEnabled) { + minOffset = max(minOffset, x.mLabelWidth.toFloat()) } } - legendTop += getExtraTopOffset(); - legendRight += getExtraRightOffset(); - legendBottom += getExtraBottomOffset(); - legendLeft += getExtraLeftOffset(); + legendTop += extraTopOffset + legendRight += extraRightOffset + legendBottom += extraBottomOffset + legendLeft += extraLeftOffset - float offsetLeft = Math.max(minOffset, legendLeft); - float offsetTop = Math.max(minOffset, legendTop); - float offsetRight = Math.max(minOffset, legendRight); - float offsetBottom = Math.max(minOffset, Math.max(getRequiredBaseOffset(), legendBottom)); + val offsetLeft = max(minOffset, legendLeft) + val offsetTop = max(minOffset, legendTop) + val offsetRight = max(minOffset, legendRight) + val offsetBottom = max(minOffset, max(this.requiredBaseOffset, legendBottom)) - mViewPortHandler.restrainViewPort(offsetLeft, offsetTop, offsetRight, offsetBottom); + viewPortHandler.restrainViewPort(offsetLeft, offsetTop, offsetRight, offsetBottom) - if (mLogEnabled) - Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop - + ", offsetRight: " + offsetRight + ", offsetBottom: " + offsetBottom); + if (isLogEnabled) Log.i( + LOG_TAG, ("offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop + + ", offsetRight: " + offsetRight + ", offsetBottom: " + offsetBottom) + ) } /** @@ -258,29 +272,27 @@ public abstract class PieRadarChartBase c.x) - angle = 360f - angle; + if (x > c.x) angle = 360f - angle // add 90° because chart starts EAST - angle = angle + 90f; + angle = angle + 90f // neutralize overflow - if (angle > 360f) - angle = angle - 360f; + if (angle > 360f) angle = angle - 360f - MPPointF.recycleInstance(c); + MPPointF.Companion.recycleInstance(c) - return angle; + return angle } /** @@ -293,16 +305,15 @@ public abstract class PieRadarChartBase c.x) { - xDist = x - c.x; + if (x > c!!.x) { + xDist = x - c.x } else { - xDist = c.x - x; + xDist = c.x - x } if (y > c.y) { - yDist = y - c.y; + yDist = y - c.y } else { - yDist = c.y - y; + yDist = c.y - y } // pythagoras - dist = (float) Math.sqrt(Math.pow(xDist, 2.0) + Math.pow(yDist, 2.0)); + dist = sqrt(xDist.toDouble().pow(2.0) + yDist.toDouble().pow(2.0)).toFloat() - MPPointF.recycleInstance(c); + MPPointF.Companion.recycleInstance(c) - return dist; + return dist } /** @@ -349,101 +359,55 @@ public abstract class PieRadarChartBase top (NORTH) - * - * @param angle - */ - public void setRotationAngle(float angle) { - mRawRotationAngle = angle; - mRotationAngle = Utils.getNormalizedAngle(mRawRotationAngle); - } - - /** - * gets the raw version of the current rotation angle of the pie chart the - * returned value could be any value, negative or positive, outside of the - * 360 degrees. this is used when working with rotation direction, mainly by - * gestures and animations. - * - * @return - */ - public float getRawRotationAngle() { - return mRawRotationAngle; - } - - /** - * gets a normalized version of the current rotation angle of the pie chart, - * which will always be between 0.0 < 360.0 - * - * @return - */ - public float getRotationAngle() { - return mRotationAngle; - } - - /** - * Set this to true to enable the rotation / spinning of the chart by touch. - * Set it to false to disable it. Default: true - * - * @param enabled - */ - public void setRotationEnabled(boolean enabled) { - mRotateEnabled = enabled; - } - - /** - * Returns true if rotation of the chart by touch is enabled, false if not. - * - * @return - */ - public boolean isRotationEnabled() { - return mRotateEnabled; - } - - /** - * Gets the minimum offset (padding) around the chart, defaults to 0.f - */ - public float getMinOffset() { - return mMinOffset; - } - - /** - * Sets the minimum offset (padding) around the chart, defaults to 0.f - */ - public void setMinOffset(float minOffset) { - mMinOffset = minOffset; - } + abstract fun getIndexForAngle(angle: Float): Int + + var rotationAngle: Float + /** + * gets a normalized version of the current rotation angle of the pie chart, + * which will always be between 0.0 < 360.0 + * + * @return + */ + get() = mRotationAngle + /** + * Set an offset for the rotation of the RadarChart in degrees. Default 270f + * --> top (NORTH) + * + * @param angle + */ + set(angle) { + this.rawRotationAngle = angle + mRotationAngle = Utils.getNormalizedAngle(this.rawRotationAngle) + } - /** - * returns the diameter of the pie- or radar-chart - * - * @return - */ - public float getDiameter() { - RectF content = mViewPortHandler.getContentRect(); - content.left += getExtraLeftOffset(); - content.top += getExtraTopOffset(); - content.right -= getExtraRightOffset(); - content.bottom -= getExtraBottomOffset(); - return Math.min(content.width(), content.height()); - } + val diameter: Float + /** + * returns the diameter of the pie- or radar-chart + * + * @return + */ + get() { + val content = viewPortHandler.contentRect + content.left += extraLeftOffset + content.top += extraTopOffset + content.right -= extraRightOffset + content.bottom -= extraBottomOffset + return min(content.width(), content.height()) + } /** * Returns the radius of the chart in pixels. * * @return */ - public abstract float getRadius(); + abstract val radius: Float /** * Returns the required offset for the chart legend. * * @return */ - protected abstract float getRequiredLegendOffset(); + protected abstract val requiredLegendOffset: Float /** * Returns the base offset needed for the chart without calculating the @@ -451,25 +415,18 @@ public abstract class PieRadarChartBase { - +class RadarChart : PieRadarChartBase { /** * width of the main web lines */ - private float mWebLineWidth = 2.5f; + private var mWebLineWidth = 2.5f /** * width of the inner web lines */ - private float mInnerWebLineWidth = 1.5f; + private var mInnerWebLineWidth = 1.5f + /** + * Sets the color for the web lines that come from the center. Don't forget + * to use getResources().getColor(...) when loading a color from the + * resources. Default: Color.rgb(122, 122, 122) + * + * @param color + */ /** * color for the main web lines */ - private int mWebColor = Color.rgb(122, 122, 122); + var webColor: Int = Color.rgb(122, 122, 122) + /** + * Sets the color for the web lines in between the lines that come from the + * center. Don't forget to use getResources().getColor(...) when loading a + * color from the resources. Default: Color.rgb(122, 122, 122) + * + * @param color + */ /** * color for the inner web */ - private int mWebColorInner = Color.rgb(122, 122, 122); + var webColorInner: Int = Color.rgb(122, 122, 122) + /** + * Returns the alpha value for all web lines. + * + * @return + */ + /** + * Sets the transparency (alpha) value for all web lines, default: 150, 255 + * = 100% opaque, 0 = 100% transparent + * + * @param alpha + */ /** * transparency the grid is drawn with (0-255) */ - private int mWebAlpha = 150; + var webAlpha: Int = 150 /** * flag indicating if the web lines should be drawn or not */ - private boolean mDrawWeb = true; + private var mDrawWeb = true /** * modulus that determines how many labels and web-lines are skipped before the next is drawn */ - private int mSkipWebLineCount = 0; + private var mSkipWebLineCount = 0 /** * the object reprsenting the y-axis labels */ - private YAxis mYAxis; + private var mYAxis: YAxis? = null - private List colorList; + private var colorList: MutableList = arrayListOf() - protected YAxisRendererRadarChart mYAxisRenderer; - protected XAxisRendererRadarChart mXAxisRenderer; + protected var mYAxisRenderer: YAxisRendererRadarChart? = null + protected var mXAxisRenderer: XAxisRendererRadarChart? = null - public RadarChart(Context context) { - super(context); - } + constructor(context: Context) : super(context) - public RadarChart(Context context, AttributeSet attrs) { - super(context, attrs); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - public RadarChart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - @Override - protected void init() { - super.init(); + override fun init() { + super.init() - mYAxis = new YAxis(AxisDependency.LEFT); - mYAxis.setLabelXOffset(10f); + mYAxis = YAxis(AxisDependency.LEFT) + mYAxis!!.labelXOffset = 10f - mWebLineWidth = Utils.convertDpToPixel(1.5f); - mInnerWebLineWidth = Utils.convertDpToPixel(0.75f); + mWebLineWidth = Utils.convertDpToPixel(1.5f) + mInnerWebLineWidth = Utils.convertDpToPixel(0.75f) - mRenderer = new RadarChartRenderer(this, mAnimator, mViewPortHandler); - mYAxisRenderer = new YAxisRendererRadarChart(mViewPortHandler, mYAxis, this); - mXAxisRenderer = new XAxisRendererRadarChart(mViewPortHandler, mXAxis, this); + mRenderer = RadarChartRenderer(this, mAnimator, viewPortHandler) + mYAxisRenderer = YAxisRendererRadarChart(viewPortHandler, mYAxis!!, this) + mXAxisRenderer = XAxisRendererRadarChart(viewPortHandler, mXAxis!!, this) - mHighlighter = new RadarHighlighter(this); + highlighter = RadarHighlighter(this) } - @Override - protected void calcMinMax() { - super.calcMinMax(); + override fun calcMinMax() { + super.calcMinMax() - mYAxis.calculate(mData.getYMin(AxisDependency.LEFT), mData.getYMax(AxisDependency.LEFT)); - mXAxis.calculate(0, mData.getMaxEntryCountSet().getEntryCount()); + mYAxis!!.calculate(mData!!.getYMin(AxisDependency.LEFT), mData!!.getYMax(AxisDependency.LEFT)) + mXAxis!!.calculate(0f, mData!!.maxEntryCountSet!!.entryCount.toFloat()) } - @Override - public void notifyDataSetChanged() { - if (mData == null) - return; + override fun notifyDataSetChanged() { + if (mData == null) return - calcMinMax(); + calcMinMax() - mYAxisRenderer.computeAxis(mYAxis.mAxisMinimum, mYAxis.mAxisMaximum, mYAxis.isInverted()); - mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false); + mYAxisRenderer!!.computeAxis(mYAxis!!.mAxisMinimum, mYAxis!!.mAxisMaximum, mYAxis!!.isInverted) + mXAxisRenderer!!.computeAxis(mXAxis!!.mAxisMinimum, mXAxis!!.mAxisMaximum, false) - if (mLegend != null && !mLegend.isLegendCustom()) - mLegendRenderer.computeLegend(mData); + if (legend != null && !legend!!.isLegendCustom) legendRenderer!!.computeLegend(mData!!) - calculateOffsets(); + calculateOffsets() } - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); + protected override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) - if (mData == null) - return; + if (mData == null) return -// if (mYAxis.isEnabled()) + // if (mYAxis.isEnabled()) // mYAxisRenderer.computeAxis(mYAxis.mAxisMinimum, mYAxis.mAxisMaximum, mYAxis.isInverted()); + if (mXAxis!!.isEnabled) mXAxisRenderer!!.computeAxis(mXAxis!!.mAxisMinimum, mXAxis!!.mAxisMaximum, false) - if (mXAxis.isEnabled()) - mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false); + mXAxisRenderer!!.renderAxisLabels(canvas) - mXAxisRenderer.renderAxisLabels(canvas); + if (mDrawWeb) mRenderer!!.drawExtras(canvas) - if (mDrawWeb) - mRenderer.drawExtras(canvas); + if (mYAxis!!.isEnabled && mYAxis!!.isDrawLimitLinesBehindDataEnabled) mYAxisRenderer!!.renderLimitLines(canvas) - if (mYAxis.isEnabled() && mYAxis.isDrawLimitLinesBehindDataEnabled()) - mYAxisRenderer.renderLimitLines(canvas); + mRenderer!!.drawData(canvas) - mRenderer.drawData(canvas); + if (valuesToHighlight()) mRenderer!!.drawHighlighted(canvas, highlighted!!) - if (valuesToHighlight()) - mRenderer.drawHighlighted(canvas, mIndicesToHighlight); + if (mYAxis!!.isEnabled && !mYAxis!!.isDrawLimitLinesBehindDataEnabled) mYAxisRenderer!!.renderLimitLines(canvas) - if (mYAxis.isEnabled() && !mYAxis.isDrawLimitLinesBehindDataEnabled()) - mYAxisRenderer.renderLimitLines(canvas); + mYAxisRenderer!!.renderAxisLabels(canvas) - mYAxisRenderer.renderAxisLabels(canvas); + mRenderer!!.drawValues(canvas) - mRenderer.drawValues(canvas); + legendRenderer!!.renderLegend(canvas) - mLegendRenderer.renderLegend(canvas); + drawDescription(canvas) - drawDescription(canvas); - - drawMarkers(canvas); + drawMarkers(canvas) } - /** - * Returns the factor that is needed to transform values into pixels. - * - * @return - */ - public float getFactor() { - RectF content = mViewPortHandler.getContentRect(); - return Math.min(content.width() / 2f, content.height() / 2f) / mYAxis.mAxisRange; - } + val factor: Float + /** + * Returns the factor that is needed to transform values into pixels. + * + * @return + */ + get() { + val content = viewPortHandler.contentRect + return min(content.width() / 2f, content.height() / 2f) / mYAxis!!.mAxisRange + } - /** - * Returns the angle that each slice in the radar chart occupies. - * - * @return - */ - public float getSliceAngle() { - return 360f / (float) mData.getMaxEntryCountSet().getEntryCount(); - } + val sliceAngle: Float + /** + * Returns the angle that each slice in the radar chart occupies. + * + * @return + */ + get() = 360f / mData!!.maxEntryCountSet!!.entryCount.toFloat() - public void setLayerColorList(List colorList) { - if (colorList == null || colorList.size() == 0) { - return; + val isCustomLayerColorEnable: Boolean + get() { + if (mData == null) { + return false + } + return colorList.size == this.yAxis.mEntryCount } - this.colorList = colorList; - } - public boolean isCustomLayerColorEnable() { - if (mData == null) { - return false; + var layerColorList: MutableList + get() = colorList + set(colorList) { + if (colorList.isEmpty()) { + return + } + this.colorList = colorList } - return colorList != null && colorList.size() == getYAxis().mEntryCount; - } - - public List getLayerColorList() { - return colorList; - } - - @Override - public int getIndexForAngle(float angle) { + override fun getIndexForAngle(angle: Float): Int { // take the current angle of the chart into consideration - float a = Utils.getNormalizedAngle(angle - getRotationAngle()); - float sliceangle = getSliceAngle(); + val a = Utils.getNormalizedAngle(angle - rotationAngle) - int max = mData.getMaxEntryCountSet().getEntryCount(); + val sliceangle = this.sliceAngle - int index = 0; + val max = mData!!.maxEntryCountSet!!.entryCount - for (int i = 0; i < max; i++) { + var index = 0 - float referenceAngle = sliceangle * (i + 1) - sliceangle / 2f; + for (i in 0.. a) { - index = i; - break; + index = i + break } } - return index; - } - - /** - * Returns the object that represents all y-labels of the RadarChart. - * - * @return - */ - public YAxis getYAxis() { - return mYAxis; - } - - /** - * Sets the width of the web lines that come from the center. - * - * @param width - */ - public void setWebLineWidth(float width) { - mWebLineWidth = Utils.convertDpToPixel(width); - } - - public float getWebLineWidth() { - return mWebLineWidth; - } - - /** - * Sets the width of the web lines that are in between the lines coming from - * the center. - * - * @param width - */ - public void setWebLineWidthInner(float width) { - mInnerWebLineWidth = Utils.convertDpToPixel(width); - } - - public float getWebLineWidthInner() { - return mInnerWebLineWidth; - } - - /** - * Sets the transparency (alpha) value for all web lines, default: 150, 255 - * = 100% opaque, 0 = 100% transparent - * - * @param alpha - */ - public void setWebAlpha(int alpha) { - mWebAlpha = alpha; - } - - /** - * Returns the alpha value for all web lines. - * - * @return - */ - public int getWebAlpha() { - return mWebAlpha; - } - - /** - * Sets the color for the web lines that come from the center. Don't forget - * to use getResources().getColor(...) when loading a color from the - * resources. Default: Color.rgb(122, 122, 122) - * - * @param color - */ - public void setWebColor(int color) { - mWebColor = color; - } - - public int getWebColor() { - return mWebColor; - } - - /** - * Sets the color for the web lines in between the lines that come from the - * center. Don't forget to use getResources().getColor(...) when loading a - * color from the resources. Default: Color.rgb(122, 122, 122) - * - * @param color - */ - public void setWebColorInner(int color) { - mWebColorInner = color; - } + return index + } + + val yAxis: YAxis + /** + * Returns the object that represents all y-labels of the RadarChart. + * + * @return + */ + get() = mYAxis!! + + var webLineWidth: Float + get() = mWebLineWidth + /** + * Sets the width of the web lines that come from the center. + * + * @param width + */ + set(width) { + mWebLineWidth = Utils.convertDpToPixel(width) + } - public int getWebColorInner() { - return mWebColorInner; - } + var webLineWidthInner: Float + get() = mInnerWebLineWidth + /** + * Sets the width of the web lines that are in between the lines coming from + * the center. + * + * @param width + */ + set(width) { + mInnerWebLineWidth = Utils.convertDpToPixel(width) + } /** * If set to true, drawing the web is enabled, if set to false, drawing the @@ -318,73 +265,59 @@ public class RadarChart extends PieRadarChartBase { * * @param enabled */ - public void setDrawWeb(boolean enabled) { - mDrawWeb = enabled; - } - - /** - * Sets the number of web-lines that should be skipped on chart web before the - * next one is drawn. This targets the lines that come from the center of the RadarChart. - * - * @param count if count = 1 -> 1 line is skipped in between - */ - public void setSkipWebLineCount(int count) { - - mSkipWebLineCount = Math.max(0, count); - } - - /** - * Returns the modulus that is used for skipping web-lines. - * - * @return - */ - public int getSkipWebLineCount() { - return mSkipWebLineCount; - } + fun setDrawWeb(enabled: Boolean) { + mDrawWeb = enabled + } + + var skipWebLineCount: Int + /** + * Returns the modulus that is used for skipping web-lines. + * + * @return + */ + get() = mSkipWebLineCount + /** + * Sets the number of web-lines that should be skipped on chart web before the + * next one is drawn. This targets the lines that come from the center of the RadarChart. + * + * @param count if count = 1 -> 1 line is skipped in between + */ + set(count) { + mSkipWebLineCount = max(0, count) + } - @Override - protected float getRequiredLegendOffset() { - return mLegendRenderer.getLabelPaint().getTextSize() * 4.f; - } + override val requiredLegendOffset: Float + get() = legendRenderer!!.labelPaint.textSize * 4f - @Override - protected float getRequiredBaseOffset() { - return mXAxis.isEnabled() && mXAxis.isDrawLabelsEnabled() ? - mXAxis.mLabelWidth : - Utils.convertDpToPixel(10f); - } + override val requiredBaseOffset: Float + get() = if (mXAxis!!.isEnabled && mXAxis!!.isDrawLabelsEnabled) mXAxis!!.mLabelWidth.toFloat() else Utils.convertDpToPixel(10f) - @Override - public float getRadius() { - RectF content = mViewPortHandler.getContentRect(); - return Math.min(content.width() / 2f, content.height() / 2f); - } + override val radius: Float + get() { + val content = viewPortHandler.contentRect + return min(content.width() / 2f, content.height() / 2f) + } /** * Returns the maximum value this chart can display on it's y-axis. */ - public float getYChartMax() { - return mYAxis.mAxisMaximum; - } + override val yChartMax: Float + get() = mYAxis!!.mAxisMaximum /** * Returns the minimum value this chart can display on it's y-axis. */ - public float getYChartMin() { - return mYAxis.mAxisMinimum; - } - - /** - * Returns the range of y-values this chart can display. - * - * @return - */ - public float getYRange() { - return mYAxis.mAxisRange; - } - - @Override - public String getAccessibilityDescription() { - return "This is a Radar chart"; - } + override val yChartMin: Float + get() = mYAxis!!.mAxisMinimum + + val yRange: Float + /** + * Returns the range of y-values this chart can display. + * + * @return + */ + get() = mYAxis!!.mAxisRange + + override val accessibilityDescription: String? + get() = "This is a Radar chart" } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt index 231ed0288b..0da7e85367 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt @@ -1,12 +1,12 @@ +package com.github.mikephil.charting.charts -package com.github.mikephil.charting.charts; - -import android.content.Context; -import android.util.AttributeSet; - -import com.github.mikephil.charting.data.ScatterData; -import com.github.mikephil.charting.interfaces.dataprovider.ScatterDataProvider; -import com.github.mikephil.charting.renderer.ScatterChartRenderer; +import android.content.Context +import android.util.AttributeSet +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.ScatterData +import com.github.mikephil.charting.interfaces.dataprovider.ScatterDataProvider +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.renderer.ScatterChartRenderer /** * The ScatterChart. Draws dots, triangles, squares and custom shapes into the @@ -15,42 +15,31 @@ import com.github.mikephil.charting.renderer.ScatterChartRenderer; * * @author Philipp Jahoda */ -public class ScatterChart extends BarLineChartBase implements ScatterDataProvider { - - public ScatterChart(Context context) { - super(context); - } +class ScatterChart : BarLineChartBase, ScatterDataProvider { + constructor(context: Context) : super(context) - public ScatterChart(Context context, AttributeSet attrs) { - super(context, attrs); - } + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - public ScatterChart(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } + constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - @Override - protected void init() { - super.init(); + override fun init() { + super.init() - mRenderer = new ScatterChartRenderer(this, mAnimator, mViewPortHandler); + mRenderer = ScatterChartRenderer(this, mAnimator, viewPortHandler) - getXAxis().setSpaceMin(0.5f); - getXAxis().setSpaceMax(0.5f); + xAxis.spaceMin = 0.5f + xAxis.spaceMax = 0.5f } - @Override - public ScatterData getScatterData() { - return mData; - } + override val scatterData: ScatterData? + get() = mData /** * Predefined ScatterShapes that allow the specification of a shape a ScatterDataSet should be drawn with. * If a ScatterShape is specified for a ScatterDataSet, the required renderer is set. */ - public enum ScatterShape { - + enum class ScatterShape(private val shapeIdentifier: String) { SQUARE("SQUARE"), CIRCLE("CIRCLE"), TRIANGLE("TRIANGLE"), @@ -59,24 +48,25 @@ public class ScatterChart extends BarLineChartBase implements Scatt CHEVRON_UP("CHEVRON_UP"), CHEVRON_DOWN("CHEVRON_DOWN"); - private final String shapeIdentifier; - - ScatterShape(final String shapeIdentifier) { - this.shapeIdentifier = shapeIdentifier; + override fun toString(): String { + return shapeIdentifier } - @Override - public String toString() { - return shapeIdentifier; - } - - public static ScatterShape[] getAllDefaultShapes() { - return new ScatterShape[]{SQUARE, CIRCLE, TRIANGLE, CROSS, X, CHEVRON_UP, CHEVRON_DOWN}; + companion object { + @JvmStatic + val allDefaultShapes: Array + get() = arrayOf( + SQUARE, + CIRCLE, + TRIANGLE, + CROSS, + X, + CHEVRON_UP, + CHEVRON_DOWN + ) } } - @Override - public String getAccessibilityDescription() { - return "This is scatter chart"; - } + override val accessibilityDescription: String? + get() = "This is scatter chart" } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt index 677def35be..6a81647608 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt @@ -1,377 +1,387 @@ +package com.github.mikephil.charting.components -package com.github.mikephil.charting.components; - -import android.graphics.Color; -import android.graphics.DashPathEffect; -import android.graphics.Paint; -import android.util.Log; - -import com.github.mikephil.charting.formatter.DefaultAxisValueFormatter; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.utils.Utils; - -import java.util.ArrayList; -import java.util.List; +import android.graphics.Color +import android.graphics.DashPathEffect +import android.graphics.Paint +import android.util.Log +import com.github.mikephil.charting.formatter.DefaultAxisValueFormatter +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.utils.Utils +import kotlin.math.abs /** * Base-class of all axes (previously called labels). * * @author Philipp Jahoda */ -public abstract class AxisBase extends ComponentBase { - +abstract class AxisBase : ComponentBase() { /** * custom formatter that is used instead of the auto-formatter if set */ - protected IAxisValueFormatter mAxisValueFormatter; + protected var mAxisValueFormatter: IAxisValueFormatter = DefaultAxisValueFormatter(1) - private int mGridColor = Color.GRAY; + /** + * Returns the color of the grid lines for this axis (the horizontal lines + * coming from each label). + * + * @return + */ + /** + * Sets the color of the grid lines for this axis (the horizontal lines + * coming from each label). + * + * @param color + */ + var gridColor: Int = Color.GRAY - private float mGridLineWidth = 1f; + private var mGridLineWidth = 1f - private int mAxisLineColor = Color.GRAY; + /** + * Returns the color of the axis line (line alongside the axis). + * + * @return + */ + /** + * Sets the color of the border surrounding the chart. + * + * @param color + */ + var axisLineColor: Int = Color.GRAY - private float mAxisLineWidth = 1f; + private var mAxisLineWidth = 1f /** * the actual array of entries */ - public float[] mEntries = new float[]{}; + var mEntries: FloatArray = floatArrayOf() /** * axis label entries only used for centered labels */ - public float[] mCenteredEntries = new float[]{}; + var mCenteredEntries: FloatArray = floatArrayOf() /** * the number of entries the legend contains */ - public int mEntryCount; + var mEntryCount: Int = 0 /** * the number of decimal digits to use */ - public int mDecimals; + var mDecimals: Int = 0 /** * the number of label entries the axis should have, default 6 */ - private int mLabelCount = 6; + private var mLabelCount = 6 /** * the minimum interval between axis values */ - protected float mGranularity = 1.0f; + protected var mGranularity: Float = 1.0f + /** + * @return true if granularity is enabled + */ + /** + * Enabled/disable granularity control on axis value intervals. If enabled, the axis + * interval is not allowed to go below a certain granularity. Default: false + * + * @param enabled + */ /** * When true, axis labels are controlled by the `granularity` property. * When false, axis values could possibly be repeated. * This could happen if two adjacent axis values are rounded to same value. * If using granularity this could be avoided by having fewer axis values visible. */ - protected boolean mGranularityEnabled = false; + var isGranularityEnabled: Boolean = false /** - * if true, the set number of y-labels will be forced + * Returns true if focing the y-label count is enabled. Default: false + * + * @return */ - protected boolean mForceLabels = false; - /** - * flag indicating if the grid lines for this axis should be drawn + * if true, the set number of y-labels will be forced */ - protected boolean mDrawGridLines = true; + var isForceLabelsEnabled: Boolean = false + protected set /** - * flag that indicates if the line alongside the axis is drawn or not + * Returns true if drawing grid lines is enabled for this axis. + * + * @return */ - protected boolean mDrawAxisLine = true; - /** - * flag that indicates of the labels of this axis should be drawn or not + * flag indicating if the grid lines for this axis should be drawn */ - protected boolean mDrawLabels = true; - - protected boolean mCenterAxisLabels = false; + var isDrawGridLinesEnabled: Boolean = true + protected set /** - * the path effect of the axis line that makes dashed lines possible + * Returns true if the line alongside the axis should be drawn. + * + * @return */ - private DashPathEffect mAxisLineDashPathEffect = null; - /** - * the path effect of the grid lines that makes dashed lines possible + * flag that indicates if the line alongside the axis is drawn or not */ - private DashPathEffect mGridDashPathEffect = null; + var isDrawAxisLineEnabled: Boolean = true + protected set /** - * array of limit lines that can be set for the axis + * Returns true if drawing the labels is enabled for this axis. + * + * @return */ - protected List mLimitLines; - /** - * array of limit ranges that can be set for the axis + * flag that indicates of the labels of this axis should be drawn or not */ - protected List mLimitRanges; + var isDrawLabelsEnabled: Boolean = true + protected set - /** - * flag indicating the limit lines layer depth - */ - protected boolean mDrawLimitLineBehindData = false; + protected var mCenterAxisLabels: Boolean = false /** - * flag indicating the grid lines layer depth + * returns the DashPathEffect that is set for axis line + * + * @return */ - protected boolean mDrawGridLinesBehindData = true; - /** - * Extra spacing for `axisMinimum` to be added to automatically calculated `axisMinimum` + * the path effect of the axis line that makes dashed lines possible */ - protected float mSpaceMin = 0.f; + var axisLineDashPathEffect: DashPathEffect? = null + private set /** - * Extra spacing for `axisMaximum` to be added to automatically calculated `axisMaximum` + * returns the DashPathEffect that is set for grid line + * + * @return */ - protected float mSpaceMax = 0.f; - /** - * flag indicating that the axis-min value has been customized + * the path effect of the grid lines that makes dashed lines possible */ - protected boolean mCustomAxisMin = false; + var gridDashPathEffect: DashPathEffect? = null + private set /** - * flag indicating that the axis-max value has been customized + * Returns the LimitLines of this axis. + * + * @return */ - protected boolean mCustomAxisMax = false; - /** - * don't touch this direclty, use setter + * array of limit lines that can be set for the axis */ - public float mAxisMaximum = 0f; + var limitLines: MutableList + protected set /** - * don't touch this directly, use setter + * Returns the LimitRanges of this axis. + * + * @return */ - public float mAxisMinimum = 0f; - /** - * the total range of values this axis covers + * array of limit ranges that can be set for the axis */ - public float mAxisRange = 0f; - - private int mAxisMinLabels = 2; - private int mAxisMaxLabels = 25; + var limitRanges: MutableList + protected set /** - * The minumum number of labels on the axis + * flag indicating the limit lines layer depth */ - public int getAxisMinLabels() { - return mAxisMinLabels; - } + var isDrawLimitLinesBehindDataEnabled: Boolean = false + protected set /** - * The minumum number of labels on the axis + * flag indicating the grid lines layer depth */ - public void setAxisMinLabels(int labels) { - if (labels > 0) - mAxisMinLabels = labels; - } + var isDrawGridLinesBehindDataEnabled: Boolean = true + protected set /** - * The maximum number of labels on the axis + * Gets extra spacing for `axisMinimum` to be added to automatically calculated `axisMinimum` */ - public int getAxisMaxLabels() { - return mAxisMaxLabels; - } - /** - * The maximum number of labels on the axis + * Sets extra spacing for `axisMinimum` to be added to automatically calculated `axisMinimum` */ - public void setAxisMaxLabels(int labels) { - if (labels > 0) - mAxisMaxLabels = labels; - } - /** - * if true, then labels and lines are displayed using specificPositions instead of computed ones + * Extra spacing for `axisMinimum` to be added to automatically calculated `axisMinimum` */ - private boolean showSpecificPositions = false; + var spaceMin: Float = 0f /** - * specify to which values labels and lines must be displayed. has no effect if not used showSpecificPositions set to true + * Gets extra spacing for `axisMaximum` to be added to automatically calculated `axisMaximum` */ - private float[] specificPositions = new float[]{}; - /** - * default constructor + * Sets extra spacing for `axisMaximum` to be added to automatically calculated `axisMaximum` */ - public AxisBase() { - this.mTextSize = Utils.convertDpToPixel(10f); - this.mXOffset = Utils.convertDpToPixel(5f); - this.mYOffset = Utils.convertDpToPixel(5f); - this.mLimitLines = new ArrayList(); - this.mLimitRanges = new ArrayList(); - } - /** - * Set this to true to enable drawing the grid lines for this axis. - * - * @param enabled + * Extra spacing for `axisMaximum` to be added to automatically calculated `axisMaximum` */ - public void setDrawGridLines(boolean enabled) { - mDrawGridLines = enabled; - } + var spaceMax: Float = 0f /** - * Returns true if drawing grid lines is enabled for this axis. + * Returns true if the axis min value has been customized (and is not calculated automatically) * * @return */ - public boolean isDrawGridLinesEnabled() { - return mDrawGridLines; - } - /** - * Set this to true if the line alongside the axis should be drawn or not. - * - * @param enabled + * flag indicating that the axis-min value has been customized */ - public void setDrawAxisLine(boolean enabled) { - mDrawAxisLine = enabled; - } + var isAxisMinCustom: Boolean = false + protected set /** - * Returns true if the line alongside the axis should be drawn. + * Returns true if the axis max value has been customized (and is not calculated automatically) * * @return */ - public boolean isDrawAxisLineEnabled() { - return mDrawAxisLine; - } - /** - * Centers the axis labels instead of drawing them at their original position. - * This is useful especially for grouped BarChart. - * - * @param enabled + * flag indicating that the axis-max value has been customized */ - public void setCenterAxisLabels(boolean enabled) { - mCenterAxisLabels = enabled; - } - - public boolean isCenterAxisLabelsEnabled() { - return mCenterAxisLabels && mEntryCount > 0; - } + var isAxisMaxCustom: Boolean = false + protected set /** - * Sets the color of the grid lines for this axis (the horizontal lines - * coming from each label). - * - * @param color + * don't touch this direclty, use setter */ - public void setGridColor(int color) { - mGridColor = color; - } + var mAxisMaximum: Float = 0f /** - * Returns the color of the grid lines for this axis (the horizontal lines - * coming from each label). - * - * @return + * don't touch this directly, use setter */ - public int getGridColor() { - return mGridColor; - } + var mAxisMinimum: Float = 0f /** - * Sets the width of the border surrounding the chart in dp. - * - * @param width + * the total range of values this axis covers */ - public void setAxisLineWidth(float width) { - mAxisLineWidth = Utils.convertDpToPixel(width); - } + var mAxisRange: Float = 0f + + private var mAxisMinLabels = 2 + private var mAxisMaxLabels = 25 + + var axisMinLabels: Int + /** + * The minumum number of labels on the axis + */ + get() = mAxisMinLabels + /** + * The minumum number of labels on the axis + */ + set(labels) { + if (labels > 0) mAxisMinLabels = labels + } + + var axisMaxLabels: Int + /** + * The maximum number of labels on the axis + */ + get() = mAxisMaxLabels + /** + * The maximum number of labels on the axis + */ + set(labels) { + if (labels > 0) mAxisMaxLabels = labels + } /** - * Returns the width of the axis line (line alongside the axis). - * - * @return + * if set to true, labels and lines will be displayed at the specific positions passed in via setSpecificPositions */ - public float getAxisLineWidth() { - return mAxisLineWidth; - } - /** - * Sets the width of the grid lines that are drawn away from each axis - * label. - * - * @param width + * if true, then labels and lines are displayed using specificPositions instead of computed ones */ - public void setGridLineWidth(float width) { - mGridLineWidth = Utils.convertDpToPixel(width); - } + var isShowSpecificPositions: Boolean = false /** - * Returns the width of the grid lines that are drawn away from each axis - * label. - * - * @return + * specify to which values labels and lines must be displayed. has no effect if not used showSpecificPositions set to true */ - public float getGridLineWidth() { - return mGridLineWidth; - } + var specificPositions: FloatArray = floatArrayOf() /** - * Sets the color of the border surrounding the chart. - * - * @param color + * default constructor */ - public void setAxisLineColor(int color) { - mAxisLineColor = color; + init { + this.mTextSize = Utils.convertDpToPixel(10f) + this.mXOffset = Utils.convertDpToPixel(5f) + this.mYOffset = Utils.convertDpToPixel(5f) + this.limitLines = ArrayList() + this.limitRanges = ArrayList() } /** - * Returns the color of the axis line (line alongside the axis). + * Set this to true to enable drawing the grid lines for this axis. * - * @return + * @param enabled */ - public int getAxisLineColor() { - return mAxisLineColor; + fun setDrawGridLines(enabled: Boolean) { + this.isDrawGridLinesEnabled = enabled } /** - * Set this to true to enable drawing the labels of this axis (this will not - * affect drawing the grid lines or axis lines). + * Set this to true if the line alongside the axis should be drawn or not. * * @param enabled */ - public void setDrawLabels(boolean enabled) { - mDrawLabels = enabled; + fun setDrawAxisLine(enabled: Boolean) { + this.isDrawAxisLineEnabled = enabled } /** - * Returns true if drawing the labels is enabled for this axis. + * Centers the axis labels instead of drawing them at their original position. + * This is useful especially for grouped BarChart. * - * @return + * @param enabled */ - public boolean isDrawLabelsEnabled() { - return mDrawLabels; - } + fun setCenterAxisLabels(enabled: Boolean) { + mCenterAxisLabels = enabled + } + + val isCenterAxisLabelsEnabled: Boolean + get() = mCenterAxisLabels && mEntryCount > 0 + + var axisLineWidth: Float + /** + * Returns the width of the axis line (line alongside the axis). + * + * @return + */ + get() = mAxisLineWidth + /** + * Sets the width of the border surrounding the chart in dp. + * + * @param width + */ + set(width) { + mAxisLineWidth = Utils.convertDpToPixel(width) + } + + var gridLineWidth: Float + /** + * Returns the width of the grid lines that are drawn away from each axis + * label. + * + * @return + */ + get() = mGridLineWidth + /** + * Sets the width of the grid lines that are drawn away from each axis + * label. + * + * @param width + */ + set(width) { + mGridLineWidth = Utils.convertDpToPixel(width) + } /** - * Sets the number of label entries for the y-axis max = 25, min = 2, default: 6, be aware - * that this number is not fixed. + * Set this to true to enable drawing the labels of this axis (this will not + * affect drawing the grid lines or axis lines). * - * @param count the number of y-axis labels that should be displayed + * @param enabled */ - public void setLabelCount(int count) { - - if (count > getAxisMaxLabels()) - count = getAxisMaxLabels(); - if (count < getAxisMinLabels()) - count = getAxisMinLabels(); - - mLabelCount = count; - mForceLabels = false; + fun setDrawLabels(enabled: Boolean) { + this.isDrawLabelsEnabled = enabled } /** @@ -381,93 +391,81 @@ public abstract class AxisBase extends ComponentBase { * * @param count the number of y-axis labels that should be displayed * @param force if enabled, the set label count will be forced, meaning that the exact - * specified count of labels will - * be drawn and evenly distributed alongside the axis - this might cause labels - * to have uneven values - */ - public void setLabelCount(int count, boolean force) { - - setLabelCount(count); - mForceLabels = force; - } - - /** - * Returns true if focing the y-label count is enabled. Default: false - * - * @return - */ - public boolean isForceLabelsEnabled() { - return mForceLabels; - } - - /** - * Returns the number of label entries the y-axis should have - * - * @return - */ - public int getLabelCount() { - return mLabelCount; - } - - /** - * @return true if granularity is enabled - */ - public boolean isGranularityEnabled() { - return mGranularityEnabled; - } - - /** - * Enabled/disable granularity control on axis value intervals. If enabled, the axis - * interval is not allowed to go below a certain granularity. Default: false - * - * @param enabled - */ - public void setGranularityEnabled(boolean enabled) { - mGranularityEnabled = enabled; - } - - /** - * @return the minimum interval between axis values - */ - public float getGranularity() { - return mGranularity; - } + * specified count of labels will + * be drawn and evenly distributed alongside the axis - this might cause labels + * to have uneven values + */ + fun setLabelCount(count: Int, force: Boolean) { + this.labelCount = count + this.isForceLabelsEnabled = force + } + + var labelCount: Int + /** + * Returns the number of label entries the y-axis should have + * + * @return + */ + get() = mLabelCount + /** + * Sets the number of label entries for the y-axis max = 25, min = 2, default: 6, be aware + * that this number is not fixed. + * + * @param count the number of y-axis labels that should be displayed + */ + set(count) { + var count = count + if (count > this.axisMaxLabels) count = this.axisMaxLabels + if (count < this.axisMinLabels) count = this.axisMinLabels + + mLabelCount = count + this.isForceLabelsEnabled = false + } - /** - * Set a minimum interval for the axis when zooming in. The axis is not allowed to go below - * that limit. This can be used to avoid label duplicating when zooming in. - * - * @param granularity - */ - public void setGranularity(float granularity) { - mGranularity = granularity; - // set this to true if it was disabled, as it makes no sense to call this method with granularity disabled - mGranularityEnabled = true; - } + var granularity: Float + /** + * @return the minimum interval between axis values + */ + get() = mGranularity + /** + * Set a minimum interval for the axis when zooming in. The axis is not allowed to go below + * that limit. This can be used to avoid label duplicating when zooming in. + * + * @param granularity + */ + set(granularity) { + mGranularity = granularity + // set this to true if it was disabled, as it makes no sense to call this method with granularity disabled + this.isGranularityEnabled = true + } /** * Adds a new LimitLine to this axis. * * @param l */ - public void addLimitLine(LimitLine l) { - mLimitLines.add(l); + fun addLimitLine(l: LimitLine) { + limitLines.add(l) - if (mLimitLines.size() > 6) { - Log.e("MPAndroiChart", - "Warning! You have more than 6 LimitLines on your axis, do you really want that?"); + if (limitLines.size > 6) { + Log.e( + "MPAndroiChart", + "Warning! You have more than 6 LimitLines on your axis, do you really want that?" + ) } } /** * Adds a new LimitLine to this axis. */ - public void addLimitRange(LimitRange l) { - mLimitRanges.add(l); + fun addLimitRange(l: LimitRange) { + limitRanges.add(l) - if (mLimitRanges.size() > 6) { - Log.e("MPAndroiChart", - "Warning! You have more than 6 LimitLines on your axis, do you really want that?"); + if (limitRanges.size > 6) { + Log.e( + "MPAndroiChart", + "Warning! You have more than 6 LimitLines on your axis, do you really want that?" + ) } } @@ -476,15 +474,15 @@ public abstract class AxisBase extends ComponentBase { * * @param l */ - public void removeLimitLine(LimitLine l) { - mLimitLines.remove(l); + fun removeLimitLine(l: LimitLine?) { + limitLines.remove(l) } /** * Removes all LimitLines from the axis. */ - public void removeAllLimitLines() { - mLimitLines.clear(); + fun removeAllLimitLines() { + limitLines.clear() } /** @@ -492,33 +490,15 @@ public abstract class AxisBase extends ComponentBase { * * @param l */ - public void removeLimitRange(LimitRange l) { - mLimitRanges.remove(l); + fun removeLimitRange(l: LimitRange?) { + limitRanges.remove(l) } /** * Removes all LimitLines from the axis. */ - public void removeAllLimitRanges() { - mLimitRanges.clear(); - } - - /** - * Returns the LimitLines of this axis. - * - * @return - */ - public List getLimitLines() { - return mLimitLines; - } - - /** - * Returns the LimitRanges of this axis. - * - * @return - */ - public List getLimitRanges() { - return mLimitRanges; + fun removeAllLimitRanges() { + limitRanges.clear() } /** @@ -527,12 +507,8 @@ public abstract class AxisBase extends ComponentBase { * * @param enabled */ - public void setDrawLimitLinesBehindData(boolean enabled) { - mDrawLimitLineBehindData = enabled; - } - - public boolean isDrawLimitLinesBehindDataEnabled() { - return mDrawLimitLineBehindData; + fun setDrawLimitLinesBehindData(enabled: Boolean) { + this.isDrawLimitLinesBehindDataEnabled = enabled } /** @@ -541,32 +517,29 @@ public abstract class AxisBase extends ComponentBase { * * @param enabled */ - public void setDrawGridLinesBehindData(boolean enabled) { mDrawGridLinesBehindData = enabled; } - - public boolean isDrawGridLinesBehindDataEnabled() { - return mDrawGridLinesBehindData; + fun setDrawGridLinesBehindData(enabled: Boolean) { + this.isDrawGridLinesBehindDataEnabled = enabled } - /** - * Returns the longest formatted label (in terms of characters), this axis - * contains. - * - * @return - */ - public String getLongestLabel() { + val longestLabel: String + /** + * Returns the longest formatted label (in terms of characters), this axis + * contains. + * + * @return + */ + get() { + var longest: String? = "" - String longest = ""; + for (i in mEntries.indices) { + val text = getFormattedLabel(i) - for (int i = 0; i < mEntries.length; i++) { - String text = getFormattedLabel(i); + if (text != null && longest!!.length < text.length) longest = text + } - if (text != null && longest.length() < text.length()) - longest = text; + return longest!! } - return longest; - } - /** * Returns the longest formatted label (in terms of px), this axis * contains. @@ -574,65 +547,56 @@ public abstract class AxisBase extends ComponentBase { * * @return */ - public String getLongestLabel(Paint p) { + fun getLongestLabel(p: Paint?): String { if (p == null) { - return getLongestLabel(); + return this.longestLabel } - String longest = ""; - float max = 0f; + var longest: String? = "" + val max = 0f - for (int i = 0; i < mEntries.length; i++) { - String text = getFormattedLabel(i); + for (i in mEntries.indices) { + val text = getFormattedLabel(i) if (text != null) { - float width = p.measureText(text); + val width = p.measureText(text) if (max < width) { - longest = text; + longest = text } } } - return longest; + return longest!! } - public String getFormattedLabel(int index) { - - if (index < 0 || index >= mEntries.length) - return ""; - else - return getValueFormatter().getFormattedValue(mEntries[index], this); + fun getFormattedLabel(index: Int): String { + return if (index < 0 || index >= mEntries.size) "" + else this.valueFormatter.getFormattedValue(mEntries[index], this) } - /** - * Sets the formatter to be used for formatting the axis labels. If no formatter is set, the - * chart will - * automatically determine a reasonable formatting (concerning decimals) for all the values - * that are drawn inside - * the chart. Use chart.getDefaultValueFormatter() to use the formatter calculated by the chart. - * - * @param f - */ - public void setValueFormatter(IAxisValueFormatter f) { - - if (f == null) - mAxisValueFormatter = new DefaultAxisValueFormatter(mDecimals); - else - mAxisValueFormatter = f; - } + var valueFormatter: IAxisValueFormatter + /** + * Returns the formatter used for formatting the axis labels. + * + * @return + */ + get() { + if (mAxisValueFormatter is DefaultAxisValueFormatter && + (mAxisValueFormatter as DefaultAxisValueFormatter).decimalDigits != mDecimals + ) mAxisValueFormatter = DefaultAxisValueFormatter(mDecimals) - /** - * Returns the formatter used for formatting the axis labels. - * - * @return - */ - public IAxisValueFormatter getValueFormatter() { - - if (mAxisValueFormatter == null || - (mAxisValueFormatter instanceof DefaultAxisValueFormatter && - ((DefaultAxisValueFormatter)mAxisValueFormatter).getDecimalDigits() != mDecimals)) - mAxisValueFormatter = new DefaultAxisValueFormatter(mDecimals); - - return mAxisValueFormatter; - } + return mAxisValueFormatter + } + /** + * Sets the formatter to be used for formatting the axis labels. If no formatter is set, the + * chart will + * automatically determine a reasonable formatting (concerning decimals) for all the values + * that are drawn inside + * the chart. Use chart.getDefaultValueFormatter() to use the formatter calculated by the chart. + * + * @param f + */ + set(f) { + mAxisValueFormatter = f + } /** * Enables the grid line to be drawn in dashed mode, e.g. like this @@ -643,10 +607,12 @@ public abstract class AxisBase extends ComponentBase { * @param spaceLength the length of space in between the pieces * @param phase offset, in degrees (normally, use 0) */ - public void enableGridDashedLine(float lineLength, float spaceLength, float phase) { - mGridDashPathEffect = new DashPathEffect(new float[]{ + fun enableGridDashedLine(lineLength: Float, spaceLength: Float, phase: Float) { + this.gridDashPathEffect = DashPathEffect( + floatArrayOf( lineLength, spaceLength - }, phase); + ), phase + ) } /** @@ -656,34 +622,24 @@ public abstract class AxisBase extends ComponentBase { * * @param effect the DashPathEffect */ - public void setGridDashedLine(DashPathEffect effect) { - mGridDashPathEffect = effect; + fun setGridDashedLine(effect: DashPathEffect?) { + this.gridDashPathEffect = effect } /** * Disables the grid line to be drawn in dashed mode. */ - public void disableGridDashedLine() { - mGridDashPathEffect = null; - } - - /** - * Returns true if the grid dashed-line effect is enabled, false if not. - * - * @return - */ - public boolean isGridDashedLineEnabled() { - return mGridDashPathEffect != null; + fun disableGridDashedLine() { + this.gridDashPathEffect = null } - /** - * returns the DashPathEffect that is set for grid line - * - * @return - */ - public DashPathEffect getGridDashPathEffect() { - return mGridDashPathEffect; - } + val isGridDashedLineEnabled: Boolean + /** + * Returns true if the grid dashed-line effect is enabled, false if not. + * + * @return + */ + get() = this.gridDashPathEffect != null /** @@ -695,10 +651,12 @@ public abstract class AxisBase extends ComponentBase { * @param spaceLength the length of space in between the pieces * @param phase offset, in degrees (normally, use 0) */ - public void enableAxisLineDashedLine(float lineLength, float spaceLength, float phase) { - mAxisLineDashPathEffect = new DashPathEffect(new float[]{ + fun enableAxisLineDashedLine(lineLength: Float, spaceLength: Float, phase: Float) { + this.axisLineDashPathEffect = DashPathEffect( + floatArrayOf( lineLength, spaceLength - }, phase); + ), phase + ) } /** @@ -708,63 +666,67 @@ public abstract class AxisBase extends ComponentBase { * * @param effect the DashPathEffect */ - public void setAxisLineDashedLine(DashPathEffect effect) { - mAxisLineDashPathEffect = effect; + fun setAxisLineDashedLine(effect: DashPathEffect?) { + this.axisLineDashPathEffect = effect } /** * Disables the axis line to be drawn in dashed mode. */ - public void disableAxisLineDashedLine() { - mAxisLineDashPathEffect = null; - } - - /** - * Returns true if the axis dashed-line effect is enabled, false if not. - * - * @return - */ - public boolean isAxisLineDashedLineEnabled() { - return mAxisLineDashPathEffect != null; - } - - /** - * returns the DashPathEffect that is set for axis line - * - * @return - */ - public DashPathEffect getAxisLineDashPathEffect() { - return mAxisLineDashPathEffect; - } - - /** - * ###### BELOW CODE RELATED TO CUSTOM AXIS VALUES ###### - */ - - public float getAxisMaximum() { - return mAxisMaximum; - } + fun disableAxisLineDashedLine() { + this.axisLineDashPathEffect = null + } + + val isAxisLineDashedLineEnabled: Boolean + /** + * Returns true if the axis dashed-line effect is enabled, false if not. + * + * @return + */ + get() = this.axisLineDashPathEffect != null + + var axisMaximum: Float + /** + * ###### BELOW CODE RELATED TO CUSTOM AXIS VALUES ###### + */ + get() = mAxisMaximum + /** + * Set a custom maximum value for this axis. If set, this value will not be calculated + * automatically depending on + * the provided data. Use resetAxisMaxValue() to undo this. + * + * @param max + */ + set(max) { + this.isAxisMaxCustom = true + mAxisMaximum = max + this.mAxisRange = abs(max - mAxisMinimum) + } - public float getAxisMinimum() { - return mAxisMinimum; - } + var axisMinimum: Float + get() = mAxisMinimum + /** + * Set a custom minimum value for this axis. If set, this value will not be calculated + * automatically depending on + * the provided data. Use resetAxisMinValue() to undo this. Do not forget to call + * setStartAtZero(false) if you use + * this method. Otherwise, the axis-minimum value will still be forced to 0. + * + * @param min + */ + set(min) { + this.isAxisMinCustom = true + mAxisMinimum = min + this.mAxisRange = abs(mAxisMaximum - min) + } /** * By calling this method, any custom maximum value that has been previously set is reseted, * and the calculation is * done automatically. */ - public void resetAxisMaximum() { - mCustomAxisMax = false; - } - - /** - * Returns true if the axis max value has been customized (and is not calculated automatically) - * - * @return - */ - public boolean isAxisMaxCustom() { - return mCustomAxisMax; + fun resetAxisMaximum() { + this.isAxisMaxCustom = false } /** @@ -772,32 +734,8 @@ public abstract class AxisBase extends ComponentBase { * and the calculation is * done automatically. */ - public void resetAxisMinimum() { - mCustomAxisMin = false; - } - - /** - * Returns true if the axis min value has been customized (and is not calculated automatically) - * - * @return - */ - public boolean isAxisMinCustom() { - return mCustomAxisMin; - } - - /** - * Set a custom minimum value for this axis. If set, this value will not be calculated - * automatically depending on - * the provided data. Use resetAxisMinValue() to undo this. Do not forget to call - * setStartAtZero(false) if you use - * this method. Otherwise, the axis-minimum value will still be forced to 0. - * - * @param min - */ - public void setAxisMinimum(float min) { - mCustomAxisMin = true; - mAxisMinimum = min; - this.mAxisRange = Math.abs(mAxisMaximum - min); + fun resetAxisMinimum() { + this.isAxisMinCustom = false } /** @@ -805,22 +743,9 @@ public abstract class AxisBase extends ComponentBase { * * @param min */ - @Deprecated - public void setAxisMinValue(float min) { - setAxisMinimum(min); - } - - /** - * Set a custom maximum value for this axis. If set, this value will not be calculated - * automatically depending on - * the provided data. Use resetAxisMaxValue() to undo this. - * - * @param max - */ - public void setAxisMaximum(float max) { - mCustomAxisMax = true; - mAxisMaximum = max; - this.mAxisRange = Math.abs(max - mAxisMinimum); + @Deprecated("") + fun setAxisMinValue(min: Float) { + this.axisMinimum = min } /** @@ -828,9 +753,9 @@ public abstract class AxisBase extends ComponentBase { * * @param max */ - @Deprecated - public void setAxisMaxValue(float max) { - setAxisMaximum(max); + @Deprecated("") + fun setAxisMaxValue(max: Float) { + this.axisMaximum = max } /** @@ -840,88 +765,25 @@ public abstract class AxisBase extends ComponentBase { * @param dataMin the min value according to chart data * @param dataMax the max value according to chart data */ - public void calculate(float dataMin, float dataMax) { - + open fun calculate(dataMin: Float, dataMax: Float) { // if custom, use value as is, else use data value - float min = mCustomAxisMin ? mAxisMinimum : (dataMin - mSpaceMin); - float max = mCustomAxisMax ? mAxisMaximum : (dataMax + mSpaceMax); + + var min = if (this.isAxisMinCustom) mAxisMinimum else (dataMin - this.spaceMin) + var max = if (this.isAxisMaxCustom) mAxisMaximum else (dataMax + this.spaceMax) // temporary range (before calculations) - float range = Math.abs(max - min); + val range = abs(max - min) // in case all values are equal if (range == 0f) { - max = max + 1f; - min = min - 1f; + max = max + 1f + min = min - 1f } - this.mAxisMinimum = min; - this.mAxisMaximum = max; + this.mAxisMinimum = min + this.mAxisMaximum = max // actual range - this.mAxisRange = Math.abs(max - min); - } - - /** - * Gets extra spacing for `axisMinimum` to be added to automatically calculated `axisMinimum` - */ - public float getSpaceMin() - { - return mSpaceMin; - } - - /** - * Sets extra spacing for `axisMinimum` to be added to automatically calculated `axisMinimum` - */ - public void setSpaceMin(float mSpaceMin) - { - this.mSpaceMin = mSpaceMin; - } - - /** - * Gets extra spacing for `axisMaximum` to be added to automatically calculated `axisMaximum` - */ - public float getSpaceMax() - { - return mSpaceMax; - } - - /** - * Sets extra spacing for `axisMaximum` to be added to automatically calculated `axisMaximum` - */ - public void setSpaceMax(float mSpaceMax) - { - this.mSpaceMax = mSpaceMax; - } - - /** - * if set to true, labels and lines will be displayed at the specific positions passed in via setSpecificPositions - */ - public void setShowSpecificPositions(boolean showSpecificPositions) - { - this.showSpecificPositions = showSpecificPositions; - } - - public boolean isShowSpecificPositions() - { - return showSpecificPositions; - } - - public void setSpecificPositions(float[] specificPositions) - { - this.specificPositions = specificPositions; - } - - public float[] getSpecificPositions() - { - return specificPositions; - } - - /** - * Sets the text color to use for the labels. Make sure to use - * getResources().getColor(...) when using a color from the resources. - */ - public void setTextColor(int color) { - super.setTextColor(color); + this.mAxisRange = abs(max - min) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt index 18294a3270..6bfad3f475 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt @@ -1,95 +1,72 @@ -package com.github.mikephil.charting.components; +package com.github.mikephil.charting.components -import android.graphics.Paint; - -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; +import android.graphics.Paint.Align +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Utils /** * Created by Philipp Jahoda on 17/09/16. */ -public class Description extends ComponentBase { - +class Description : ComponentBase() { /** - * the text used in the description - */ - private String text = "Description Label"; - - /** - * the custom position of the description text - */ - private MPPointF mPosition; - - /** - * the alignment of the description text + * Returns the description text. + * + * @return */ - private Paint.Align mTextAlign = Paint.Align.RIGHT; - - public Description() { - super(); - - // default size - mTextSize = Utils.convertDpToPixel(8f); - } - /** * Sets the text to be shown as the description. * Never set this to null as this will cause nullpointer exception when drawing with Android Canvas. * * @param text */ - public void setText(String text) { - this.text = text; - } + /** + * the text used in the description + */ + var text: String? = "Description Label" /** - * Returns the description text. + * Returns the customized position of the description, or null if none set. * * @return */ - public String getText() { - return text; - } - /** - * Sets a custom position for the description text in pixels on the screen. - * - * @param x - xcoordinate - * @param y - ycoordinate + * the custom position of the description text */ - public void setPosition(float x, float y) { - if (mPosition == null) { - mPosition = MPPointF.getInstance(x, y); - } else { - mPosition.x = x; - mPosition.y = y; - } - } + var position: MPPointF? = null + private set /** - * Returns the customized position of the description, or null if none set. + * Returns the text alignment of the description. * * @return */ - public MPPointF getPosition() { - return mPosition; - } - /** * Sets the text alignment of the description text. Default RIGHT. * * @param align */ - public void setTextAlign(Paint.Align align) { - this.mTextAlign = align; + /** + * the alignment of the description text + */ + var textAlign: Align? = Align.RIGHT + + init { + // default size + mTextSize = Utils.convertDpToPixel(8f) } /** - * Returns the text alignment of the description. + * Sets a custom position for the description text in pixels on the screen. * - * @return + * @param x - xcoordinate + * @param y - ycoordinate */ - public Paint.Align getTextAlign() { - return mTextAlign; + fun setPosition(x: Float, y: Float) { + if (this.position == null) { + this.position = MPPointF.Companion.getInstance(x, y) + } else { + position!!.x = x + position!!.y = y + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt index 3b8ca43c81..6a27df7c1f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt @@ -1,40 +1,38 @@ -package com.github.mikephil.charting.components; +package com.github.mikephil.charting.components -import android.graphics.Canvas; - -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.MPPointF; - -public interface IMarker { +import android.graphics.Canvas +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +interface IMarker { /** * @return The desired (general) offset you wish the IMarker to have on the x- and y-axis. - * By returning x: -(width / 2) you will center the IMarker horizontally. - * By returning y: -(height / 2) you will center the IMarker vertically. + * By returning x: -(width / 2) you will center the IMarker horizontally. + * By returning y: -(height / 2) you will center the IMarker vertically. */ - MPPointF getOffset(); + val offset: MPPointF? /** * @return The offset for drawing at the specific `point`. This allows conditional adjusting of the Marker position. - * If you have no adjustments to make, return getOffset(). + * If you have no adjustments to make, return getOffset(). * * @param posX This is the X position at which the marker wants to be drawn. - * You can adjust the offset conditionally based on this argument. + * You can adjust the offset conditionally based on this argument. * @param posY This is the X position at which the marker wants to be drawn. - * You can adjust the offset conditionally based on this argument. + * You can adjust the offset conditionally based on this argument. */ - MPPointF getOffsetForDrawingAtPoint(float posX, float posY); + fun getOffsetForDrawingAtPoint(posX: Float, posY: Float): MPPointF? /** * This method enables a specified custom IMarker to update it's content every time the IMarker is redrawn. * * @param e The Entry the IMarker belongs to. This can also be any subclass of Entry, like BarEntry or - * CandleEntry, simply cast it at runtime. + * CandleEntry, simply cast it at runtime. * @param highlight The highlight object contains information about the highlighted value such as it's dataset-index, the - * selected range or stack-index (only stacked bar entries). + * selected range or stack-index (only stacked bar entries). */ - void refreshContent(Entry e, Highlight highlight); + fun refreshContent(e: Entry?, highlight: Highlight?) /** * Draws the IMarker on the given position on the screen with the given Canvas object. @@ -43,5 +41,5 @@ public interface IMarker { * @param posX * @param posY */ - void draw(Canvas canvas, float posX, float posY); + fun draw(canvas: Canvas?, posX: Float, posY: Float) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt index 708129259b..d277a9c896 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt @@ -1,15 +1,13 @@ -package com.github.mikephil.charting.components; +package com.github.mikephil.charting.components -import android.graphics.DashPathEffect; -import android.graphics.Paint; - -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.FSize; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; - -import java.util.ArrayList; -import java.util.List; +import android.graphics.DashPathEffect +import android.graphics.Paint +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.FSize +import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.ViewPortHandler +import kotlin.math.max +import kotlin.math.min /** * Class representing the legend of the chart. The legend will contain one entry @@ -18,9 +16,8 @@ import java.util.List; * * @author Philipp Jahoda */ -public class Legend extends ComponentBase { - - public enum LegendForm { +class Legend() : ComponentBase() { + enum class LegendForm { /** * Avoid drawing a form */ @@ -52,531 +49,420 @@ public class Legend extends ComponentBase { LINE } - public enum LegendHorizontalAlignment { + enum class LegendHorizontalAlignment { LEFT, CENTER, RIGHT } - public enum LegendVerticalAlignment { + enum class LegendVerticalAlignment { TOP, CENTER, BOTTOM } - public enum LegendOrientation { + enum class LegendOrientation { HORIZONTAL, VERTICAL } - public enum LegendDirection { + enum class LegendDirection { LEFT_TO_RIGHT, RIGHT_TO_LEFT } /** * The legend entries array */ - private LegendEntry[] mEntries = new LegendEntry[]{}; + var entries: Array = arrayOf() + private set /** * Entries that will be appended to the end of the auto calculated entries after calculating the legend. * (if the legend has already been calculated, you will need to call notifyDataSetChanged() to let the changes take effect) */ - private LegendEntry[] mExtraEntries; + var extraEntries: Array = arrayOf() + private set + /** + * @return true if a custom legend entries has been set default + * false (automatic legend) + */ /** * Are the legend labels/colors a custom value or auto calculated? If false, * then it's auto, if true, then custom. default false (automatic legend) */ - private boolean mIsLegendCustom = false; - - private LegendHorizontalAlignment mHorizontalAlignment = LegendHorizontalAlignment.LEFT; - private LegendVerticalAlignment mVerticalAlignment = LegendVerticalAlignment.BOTTOM; - private LegendOrientation mOrientation = LegendOrientation.HORIZONTAL; - private boolean mDrawInside = false; + var isLegendCustom: Boolean = false + private set /** - * the text direction for the legend + * returns the horizontal alignment of the legend + * + * @return */ - private LegendDirection mDirection = LegendDirection.LEFT_TO_RIGHT; - /** - * the shape/form the legend colors are drawn in + * sets the horizontal alignment of the legend + * + * @param value */ - private LegendForm mShape = LegendForm.SQUARE; - + var horizontalAlignment: LegendHorizontalAlignment = LegendHorizontalAlignment.LEFT /** - * the size of the legend forms/shapes + * returns the vertical alignment of the legend + * + * @return */ - private float mFormSize = 8f; - /** - * the size of the legend forms/shapes + * sets the vertical alignment of the legend + * + * @param value */ - private float mFormLineWidth = 3f; - + var verticalAlignment: LegendVerticalAlignment = LegendVerticalAlignment.BOTTOM /** - * Line dash path effect used for shapes that consist of lines. + * returns the orientation of the legend + * + * @return */ - private DashPathEffect mFormLineDashEffect = null; - /** - * the space between the legend entries on a horizontal axis, default 6f + * sets the orientation of the legend + * + * @param value */ - private float mXEntrySpace = 6f; + var orientation: LegendOrientation = LegendOrientation.HORIZONTAL /** - * the space between the legend entries on a vertical axis, default 5f + * returns whether the legend will draw inside the chart or outside + * + * @return */ - private float mYEntrySpace = 0f; + var isDrawInsideEnabled: Boolean = false + private set /** - * the space between the legend entries on a vertical axis, default 2f - * private float mYEntrySpace = 2f; /** the space between the form and the - * actual label/text + * returns the text direction of the legend + * + * @return */ - private float mFormToTextSpace = 5f; - /** - * the space that should be left between stacked forms + * sets the text direction of the legend + * + * @param pos */ - private float mStackSpace = 3f; - /** - * the maximum relative size out of the whole chart view in percent + * the text direction for the legend */ - private float mMaxSizePercent = 0.95f; + var direction: LegendDirection? = LegendDirection.LEFT_TO_RIGHT /** - * default constructor + * returns the current form/shape that is set for the legend + * + * @return */ - public Legend() { - - this.mTextSize = Utils.convertDpToPixel(10f); - this.mXOffset = Utils.convertDpToPixel(5f); - this.mYOffset = Utils.convertDpToPixel(3f); // 2 - } - /** - * Constructor. Provide entries for the legend. + * sets the form/shape of the legend forms * - * @param entries + * @param shape */ - public Legend(LegendEntry[] entries) { - this(); - - if (entries == null) { - throw new IllegalArgumentException("entries array is NULL"); - } - - this.mEntries = entries; - } - /** - * This method sets the automatically computed colors for the legend. Use setCustom(...) to set custom colors. - * - * @param entries + * the shape/form the legend colors are drawn in */ - public void setEntries(List entries) { - mEntries = entries.toArray(new LegendEntry[entries.size()]); - } - - public LegendEntry[] getEntries() { - return mEntries; - } + var form: LegendForm? = LegendForm.SQUARE /** - * returns the maximum length in pixels across all legend labels + formsize - * + formtotextspace + * returns the size in dp of the legend forms * - * @param p the paint object used for rendering the text * @return */ - public float getMaximumEntryWidth(Paint p) { - - float max = 0f; - float maxFormSize = 0f; - float formToTextSpace = Utils.convertDpToPixel(mFormToTextSpace); - - for (LegendEntry entry : mEntries) { - final float formSize = Utils.convertDpToPixel( - Float.isNaN(entry.formSize) - ? mFormSize : entry.formSize); - if (formSize > maxFormSize) - maxFormSize = formSize; - - String label = entry.label; - if (label == null) continue; - - float length = (float) Utils.calcTextWidth(p, label); - - if (length > max) - max = length; - } - - return max + maxFormSize + formToTextSpace; - } - /** - * returns the maximum height in pixels across all legend labels + * sets the size in dp of the legend forms, default 8f * - * @param p the paint object used for rendering the text - * @return + * @param size */ - public float getMaximumEntryHeight(Paint p) { - - float max = 0f; - - for (LegendEntry entry : mEntries) { - String label = entry.label; - if (label == null) continue; - - float length = (float) Utils.calcTextHeight(p, label); - - if (length > max) - max = length; - } - - return max; - } - - public LegendEntry[] getExtraEntries() { - - return mExtraEntries; - } - - public void setExtra(List entries) { - mExtraEntries = entries.toArray(new LegendEntry[entries.size()]); - } - - public void setExtra(LegendEntry[] entries) { - if (entries == null) - entries = new LegendEntry[]{}; - mExtraEntries = entries; - } - /** - * Entries that will be appended to the end of the auto calculated - * entries after calculating the legend. - * (if the legend has already been calculated, you will need to call notifyDataSetChanged() - * to let the changes take effect) + * the size of the legend forms/shapes */ - public void setExtra(int[] colors, String[] labels) { - - List entries = new ArrayList<>(); - - for (int i = 0; i < Math.min(colors.length, labels.length); i++) { - final LegendEntry entry = new LegendEntry(); - entry.formColor = colors[i]; - entry.label = labels[i]; - - if (entry.formColor == ColorTemplate.COLOR_SKIP || - entry.formColor == 0) - entry.form = LegendForm.NONE; - else if (entry.formColor == ColorTemplate.COLOR_NONE) - entry.form = LegendForm.EMPTY; - - entries.add(entry); - } - - mExtraEntries = entries.toArray(new LegendEntry[entries.size()]); - } + var formSize: Float = 8f /** - * Sets a custom legend's entries array. - * * A null label will start a group. - * This will disable the feature that automatically calculates the legend - * entries from the datasets. - * Call resetCustom() to re-enable automatic calculation (and then - * notifyDataSetChanged() is needed to auto-calculate the legend again) + * returns the line width in dp for drawing forms that consist of lines + * + * @return */ - public void setCustom(LegendEntry[] entries) { - - mEntries = entries; - mIsLegendCustom = true; - } - /** - * Sets a custom legend's entries array. - * * A null label will start a group. - * This will disable the feature that automatically calculates the legend - * entries from the datasets. - * Call resetCustom() to re-enable automatic calculation (and then - * notifyDataSetChanged() is needed to auto-calculate the legend again) + * sets the line width in dp for forms that consist of lines, default 3f + * + * @param size */ - public void setCustom(List entries) { - - mEntries = entries.toArray(new LegendEntry[entries.size()]); - mIsLegendCustom = true; - } - /** - * Calling this will disable the custom legend entries (set by - * setCustom(...)). Instead, the entries will again be calculated - * automatically (after notifyDataSetChanged() is called). + * the size of the legend forms/shapes */ - public void resetCustom() { - mIsLegendCustom = false; - } + var formLineWidth: Float = 3f /** - * @return true if a custom legend entries has been set default - * false (automatic legend) + * @return The line dash path effect used for shapes that consist of lines. */ - public boolean isLegendCustom() { - return mIsLegendCustom; - } - /** - * returns the horizontal alignment of the legend + * Sets the line dash path effect used for shapes that consist of lines. * - * @return + * @param dashPathEffect */ - public LegendHorizontalAlignment getHorizontalAlignment() { - return mHorizontalAlignment; - } - /** - * sets the horizontal alignment of the legend - * - * @param value + * Line dash path effect used for shapes that consist of lines. */ - public void setHorizontalAlignment(LegendHorizontalAlignment value) { - mHorizontalAlignment = value; - } + var formLineDashEffect: DashPathEffect? = null /** - * returns the vertical alignment of the legend + * returns the space between the legend entries on a horizontal axis in + * pixels * * @return */ - public LegendVerticalAlignment getVerticalAlignment() { - return mVerticalAlignment; - } - /** - * sets the vertical alignment of the legend + * sets the space between the legend entries on a horizontal axis in pixels, + * converts to dp internally * - * @param value + * @param space */ - public void setVerticalAlignment(LegendVerticalAlignment value) { - mVerticalAlignment = value; - } + /** + * the space between the legend entries on a horizontal axis, default 6f + */ + var xEntrySpace: Float = 6f /** - * returns the orientation of the legend + * returns the space between the legend entries on a vertical axis in pixels * * @return */ - public LegendOrientation getOrientation() { - return mOrientation; - } - /** - * sets the orientation of the legend + * sets the space between the legend entries on a vertical axis in pixels, + * converts to dp internally * - * @param value + * @param space */ - public void setOrientation(LegendOrientation value) { - mOrientation = value; - } + /** + * the space between the legend entries on a vertical axis, default 5f + */ + var yEntrySpace: Float = 0f /** - * returns whether the legend will draw inside the chart or outside + * returns the space between the form and the actual label/text * * @return */ - public boolean isDrawInsideEnabled() { - return mDrawInside; - } - /** - * sets whether the legend will draw inside the chart or outside + * sets the space between the form and the actual label/text, converts to dp + * internally * - * @param value + * @param space */ - public void setDrawInside(boolean value) { - mDrawInside = value; - } + /** + * the space between the legend entries on a vertical axis, default 2f + * private float mYEntrySpace = 2f; / ** the space between the form and the + * actual label/text + */ + var formToTextSpace: Float = 5f /** - * returns the text direction of the legend + * returns the space that is left out between stacked forms (with no label) * * @return */ - public LegendDirection getDirection() { - return mDirection; - } - /** - * sets the text direction of the legend + * sets the space that is left out between stacked forms (with no label) * - * @param pos + * @param space */ - public void setDirection(LegendDirection pos) { - mDirection = pos; - } + /** + * the space that should be left between stacked forms + */ + var stackSpace: Float = 3f /** - * returns the current form/shape that is set for the legend + * The maximum relative size out of the whole chart view. / If the legend is + * to the right/left of the chart, then this affects the width of the + * legend. / If the legend is to the top/bottom of the chart, then this + * affects the height of the legend. / If the legend is the center of the + * piechart, then this defines the size of the rectangular bounds out of the + * size of the "hole". / default: 0.95f (95%) * * @return */ - public LegendForm getForm() { - return mShape; - } - /** - * sets the form/shape of the legend forms + * The maximum relative size out of the whole chart view. / If + * the legend is to the right/left of the chart, then this affects the width + * of the legend. / If the legend is to the top/bottom of the chart, then + * this affects the height of the legend. / default: 0.95f (95%) * - * @param shape + * @param maxSize */ - public void setForm(LegendForm shape) { - mShape = shape; - } - /** - * sets the size in dp of the legend forms, default 8f - * - * @param size + * the maximum relative size out of the whole chart view in percent */ - public void setFormSize(float size) { - mFormSize = size; - } + var maxSizePercent: Float = 0.95f /** - * returns the size in dp of the legend forms + * Constructor. Provide entries for the legend. * - * @return + * @param entries */ - public float getFormSize() { - return mFormSize; + constructor(entries: Array) : this() { + requireNotNull(entries) { "entries array is NULL" } + + this.entries = entries } /** - * sets the line width in dp for forms that consist of lines, default 3f + * This method sets the automatically computed colors for the legend. Use setCustom(...) to set custom colors. * - * @param size + * @param entries */ - public void setFormLineWidth(float size) { - mFormLineWidth = size; + fun setEntries(entries: MutableList) { + this.entries = entries.toTypedArray() } /** - * returns the line width in dp for drawing forms that consist of lines + * returns the maximum length in pixels across all legend labels + formsize + * + formtotextspace * + * @param p the paint object used for rendering the text * @return */ - public float getFormLineWidth() { - return mFormLineWidth; - } + fun getMaximumEntryWidth(p: Paint): Float { + var max = 0f + var maxFormSize = 0f + val formToTextSpace = Utils.convertDpToPixel(this.formToTextSpace) - /** - * Sets the line dash path effect used for shapes that consist of lines. - * - * @param dashPathEffect - */ - public void setFormLineDashEffect(DashPathEffect dashPathEffect) { - mFormLineDashEffect = dashPathEffect; - } + for (entry in this.entries) { + val formSize = Utils.convertDpToPixel( + if (entry.formSize.isNaN()) + this.formSize + else + entry.formSize + ) + if (formSize > maxFormSize) maxFormSize = formSize - /** - * @return The line dash path effect used for shapes that consist of lines. - */ - public DashPathEffect getFormLineDashEffect() { - return mFormLineDashEffect; + val label = entry.label + if (label == null) continue + + val length = Utils.calcTextWidth(p, label).toFloat() + + if (length > max) max = length + } + + return max + maxFormSize + formToTextSpace } /** - * returns the space between the legend entries on a horizontal axis in - * pixels + * returns the maximum height in pixels across all legend labels * + * @param p the paint object used for rendering the text * @return */ - public float getXEntrySpace() { - return mXEntrySpace; + fun getMaximumEntryHeight(p: Paint): Float { + var max = 0f + + for (entry in this.entries) { + val label = entry.label + if (label == null) continue + + val length = Utils.calcTextHeight(p, label).toFloat() + + if (length > max) max = length + } + + return max } - /** - * sets the space between the legend entries on a horizontal axis in pixels, - * converts to dp internally - * - * @param space - */ - public void setXEntrySpace(float space) { - mXEntrySpace = space; + fun setExtra(entries: MutableList) { + this.extraEntries = entries.toTypedArray() } - /** - * returns the space between the legend entries on a vertical axis in pixels - * - * @return - */ - public float getYEntrySpace() { - return mYEntrySpace; + fun setExtra(entries: Array) { + this.extraEntries = entries } /** - * sets the space between the legend entries on a vertical axis in pixels, - * converts to dp internally - * - * @param space + * Entries that will be appended to the end of the auto calculated + * entries after calculating the legend. + * (if the legend has already been calculated, you will need to call notifyDataSetChanged() + * to let the changes take effect) */ - public void setYEntrySpace(float space) { - mYEntrySpace = space; + fun setExtra(colors: IntArray, labels: Array) { + val entries: MutableList = ArrayList() + + for (i in 0..() } /** - * returns the space between the form and the actual label/text - * - * @return + * Sets a custom legend's entries array. + * * A null label will start a group. + * This will disable the feature that automatically calculates the legend + * entries from the datasets. + * Call resetCustom() to re-enable automatic calculation (and then + * notifyDataSetChanged() is needed to auto-calculate the legend again) */ - public float getFormToTextSpace() { - return mFormToTextSpace; + fun setCustom(entries: Array) { + this.entries = entries + this.isLegendCustom = true } /** - * sets the space between the form and the actual label/text, converts to dp - * internally - * - * @param space + * Sets a custom legend's entries array. + * * A null label will start a group. + * This will disable the feature that automatically calculates the legend + * entries from the datasets. + * Call resetCustom() to re-enable automatic calculation (and then + * notifyDataSetChanged() is needed to auto-calculate the legend again) */ - public void setFormToTextSpace(float space) { - this.mFormToTextSpace = space; + fun setCustom(entries: MutableList) { + this.entries = entries.toTypedArray() + this.isLegendCustom = true } /** - * returns the space that is left out between stacked forms (with no label) - * - * @return + * Calling this will disable the custom legend entries (set by + * setCustom(...)). Instead, the entries will again be calculated + * automatically (after notifyDataSetChanged() is called). */ - public float getStackSpace() { - return mStackSpace; + fun resetCustom() { + this.isLegendCustom = false } /** - * sets the space that is left out between stacked forms (with no label) + * sets whether the legend will draw inside the chart or outside * - * @param space + * @param value */ - public void setStackSpace(float space) { - mStackSpace = space; + fun setDrawInside(value: Boolean) { + this.isDrawInsideEnabled = value } /** * the total width of the legend (needed width space) */ - public float mNeededWidth = 0f; + var mNeededWidth: kotlin.Float = 0f /** * the total height of the legend (needed height space) */ - public float mNeededHeight = 0f; + var mNeededHeight: kotlin.Float = 0f - public float mTextHeightMax = 0f; + var mTextHeightMax: kotlin.Float = 0f - public float mTextWidthMax = 0f; + var mTextWidthMax: kotlin.Float = 0f /** - * flag that indicates if word wrapping is enabled + * If this is set, then word wrapping the legend is enabled. This means the + * legend will not be cut off if too long. + * + * @return */ - private boolean mWordWrapEnabled = false; - /** * Should the legend word wrap? / this is currently supported only for: * BelowChartLeft, BelowChartRight, BelowChartCenter. / note that word @@ -586,60 +472,22 @@ public class Legend extends ComponentBase { * * @param enabled */ - public void setWordWrapEnabled(boolean enabled) { - mWordWrapEnabled = enabled; - } - /** - * If this is set, then word wrapping the legend is enabled. This means the - * legend will not be cut off if too long. - * - * @return + * flag that indicates if word wrapping is enabled */ - public boolean isWordWrapEnabled() { - return mWordWrapEnabled; - } + var isWordWrapEnabled: Boolean = false - /** - * The maximum relative size out of the whole chart view. / If the legend is - * to the right/left of the chart, then this affects the width of the - * legend. / If the legend is to the top/bottom of the chart, then this - * affects the height of the legend. / If the legend is the center of the - * piechart, then this defines the size of the rectangular bounds out of the - * size of the "hole". / default: 0.95f (95%) - * - * @return - */ - public float getMaxSizePercent() { - return mMaxSizePercent; - } + val calculatedLabelSizes: MutableList = ArrayList(16) + val calculatedLabelBreakPoints: MutableList = ArrayList(16) + val calculatedLineSizes: MutableList = ArrayList(16) /** - * The maximum relative size out of the whole chart view. / If - * the legend is to the right/left of the chart, then this affects the width - * of the legend. / If the legend is to the top/bottom of the chart, then - * this affects the height of the legend. / default: 0.95f (95%) - * - * @param maxSize + * default constructor */ - public void setMaxSizePercent(float maxSize) { - mMaxSizePercent = maxSize; - } - - private List mCalculatedLabelSizes = new ArrayList<>(16); - private List mCalculatedLabelBreakPoints = new ArrayList<>(16); - private List mCalculatedLineSizes = new ArrayList<>(16); - - public List getCalculatedLabelSizes() { - return mCalculatedLabelSizes; - } - - public List getCalculatedLabelBreakPoints() { - return mCalculatedLabelBreakPoints; - } - - public List getCalculatedLineSizes() { - return mCalculatedLineSizes; + init { + this.mTextSize = Utils.convertDpToPixel(10f) + this.mXOffset = Utils.convertDpToPixel(5f) + this.mYOffset = Utils.convertDpToPixel(3f) // 2 } /** @@ -649,177 +497,174 @@ public class Legend extends ComponentBase { * * @param labelpaint */ - public void calculateDimensions(Paint labelpaint, ViewPortHandler viewPortHandler) { - - float defaultFormSize = Utils.convertDpToPixel(mFormSize); - float stackSpace = Utils.convertDpToPixel(mStackSpace); - float formToTextSpace = Utils.convertDpToPixel(mFormToTextSpace); - float xEntrySpace = Utils.convertDpToPixel(mXEntrySpace); - float yEntrySpace = Utils.convertDpToPixel(mYEntrySpace); - boolean wordWrapEnabled = mWordWrapEnabled; - LegendEntry[] entries = mEntries; - int entryCount = entries.length; - - mTextWidthMax = getMaximumEntryWidth(labelpaint); - mTextHeightMax = getMaximumEntryHeight(labelpaint); - - switch (mOrientation) { - case VERTICAL: { - - float maxWidth = 0f, maxHeight = 0f, width = 0f; - float labelLineHeight = Utils.getLineHeight(labelpaint); - boolean wasStacked = false; - - for (int i = 0; i < entryCount; i++) { - - LegendEntry e = entries[i]; - boolean drawingForm = e.form != LegendForm.NONE; - float formSize = Float.isNaN(e.formSize) - ? defaultFormSize - : Utils.convertDpToPixel(e.formSize); - String label = e.label; - - if (!wasStacked) - width = 0.f; + fun calculateDimensions(labelpaint: Paint, viewPortHandler: ViewPortHandler) { + val defaultFormSize = Utils.convertDpToPixel(this.formSize) + val stackSpace = Utils.convertDpToPixel(this.stackSpace) + val formToTextSpace = Utils.convertDpToPixel(this.formToTextSpace) + val xEntrySpace = Utils.convertDpToPixel(this.xEntrySpace) + val yEntrySpace = Utils.convertDpToPixel(this.yEntrySpace) + val wordWrapEnabled = this.isWordWrapEnabled + val entries = this.entries + val entryCount = entries.size + + mTextWidthMax = getMaximumEntryWidth(labelpaint) + mTextHeightMax = getMaximumEntryHeight(labelpaint) + + when (this.orientation) { + LegendOrientation.VERTICAL -> { + var maxWidth = 0f + var maxHeight = 0f + var width = 0f + val labelLineHeight = Utils.getLineHeight(labelpaint) + var wasStacked = false + + var i = 0 + while (i < entryCount) { + val e = entries[i] + val drawingForm = e.form != LegendForm.NONE + val formSize = if (e.formSize.isNaN()) + defaultFormSize + else + Utils.convertDpToPixel(e.formSize) + val label = e.label + + if (!wasStacked) width = 0f if (drawingForm) { - if (wasStacked) - width += stackSpace; - width += formSize; + if (wasStacked) width += stackSpace + width += formSize } // grouped forms have null labels if (label != null) { - // make a step to the left - if (drawingForm && !wasStacked) - width += formToTextSpace; + + if (drawingForm && !wasStacked) width += formToTextSpace else if (wasStacked) { - maxWidth = Math.max(maxWidth, width); - maxHeight += labelLineHeight + yEntrySpace; - width = 0.f; - wasStacked = false; + maxWidth = max(maxWidth, width) + maxHeight += labelLineHeight + yEntrySpace + width = 0f + wasStacked = false } - width += Utils.calcTextWidth(labelpaint, label); + width += Utils.calcTextWidth(labelpaint, label).toFloat() - maxHeight += labelLineHeight + yEntrySpace; + maxHeight += labelLineHeight + yEntrySpace } else { - wasStacked = true; - width += formSize; - if (i < entryCount - 1) - width += stackSpace; + wasStacked = true + width += formSize + if (i < entryCount - 1) width += stackSpace } - maxWidth = Math.max(maxWidth, width); + maxWidth = max(maxWidth, width) + i++ } - mNeededWidth = maxWidth; - mNeededHeight = maxHeight; - - break; + mNeededWidth = maxWidth + mNeededHeight = maxHeight } - case HORIZONTAL: { - float labelLineHeight = Utils.getLineHeight(labelpaint); - float labelLineSpacing = Utils.getLineSpacing(labelpaint) + yEntrySpace; - float contentWidth = viewPortHandler.contentWidth() * mMaxSizePercent; + LegendOrientation.HORIZONTAL -> { + val labelLineHeight = Utils.getLineHeight(labelpaint) + val labelLineSpacing = Utils.getLineSpacing(labelpaint) + yEntrySpace + val contentWidth = viewPortHandler.contentWidth() * this.maxSizePercent // Start calculating layout - float maxLineWidth = 0.f; - float currentLineWidth = 0.f; - float requiredWidth = 0.f; - int stackedStartIndex = -1; - - mCalculatedLabelBreakPoints.clear(); - mCalculatedLabelSizes.clear(); - mCalculatedLineSizes.clear(); - - for (int i = 0; i < entryCount; i++) { - - LegendEntry e = entries[i]; - boolean drawingForm = e.form != LegendForm.NONE; - float formSize = Float.isNaN(e.formSize) - ? defaultFormSize - : Utils.convertDpToPixel(e.formSize); - String label = e.label; - - mCalculatedLabelBreakPoints.add(false); + var maxLineWidth = 0f + var currentLineWidth = 0f + var requiredWidth = 0f + var stackedStartIndex = -1 + + calculatedLabelBreakPoints.clear() + calculatedLabelSizes.clear() + calculatedLineSizes.clear() + + var i = 0 + while (i < entryCount) { + val e = entries[i] + val drawingForm = e.form != LegendForm.NONE + val formSize = if (e.formSize.isNaN()) + defaultFormSize + else + Utils.convertDpToPixel(e.formSize) + val label = e.label + + calculatedLabelBreakPoints.add(false) if (stackedStartIndex == -1) { // we are not stacking, so required width is for this label // only - requiredWidth = 0.f; + requiredWidth = 0f } else { // add the spacing appropriate for stacked labels/forms - requiredWidth += stackSpace; + requiredWidth += stackSpace } // grouped forms have null labels if (label != null) { - - mCalculatedLabelSizes.add(Utils.calcTextSize(labelpaint, label)); - requiredWidth += drawingForm ? formToTextSpace + formSize : 0.f; - requiredWidth += mCalculatedLabelSizes.get(i).width; + calculatedLabelSizes.add(Utils.calcTextSize(labelpaint, label)) + requiredWidth += if (drawingForm) formToTextSpace + formSize else 0f + requiredWidth += calculatedLabelSizes[i]!!.width } else { - - mCalculatedLabelSizes.add(FSize.getInstance(0.f, 0.f)); - requiredWidth += drawingForm ? formSize : 0.f; + calculatedLabelSizes.add(FSize.Companion.getInstance(0f, 0f)) + requiredWidth += if (drawingForm) formSize else 0f if (stackedStartIndex == -1) { // mark this index as we might want to break here later - stackedStartIndex = i; + stackedStartIndex = i } } if (label != null || i == entryCount - 1) { - - float requiredSpacing = currentLineWidth == 0.f ? 0.f : xEntrySpace; + val requiredSpacing = if (currentLineWidth == 0f) 0f else xEntrySpace if (!wordWrapEnabled // No word wrapping, it must fit. - // The line is empty, it must fit - || currentLineWidth == 0.f - // It simply fits - || (contentWidth - currentLineWidth >= - requiredSpacing + requiredWidth)) { + // The line is empty, it must fit + || currentLineWidth == 0f // It simply fits + || (contentWidth - currentLineWidth >= + requiredSpacing + requiredWidth) + ) { // Expand current line - currentLineWidth += requiredSpacing + requiredWidth; + currentLineWidth += requiredSpacing + requiredWidth } else { // It doesn't fit, we need to wrap a line // Add current line size to array - mCalculatedLineSizes.add(FSize.getInstance(currentLineWidth, labelLineHeight)); - maxLineWidth = Math.max(maxLineWidth, currentLineWidth); + + calculatedLineSizes.add(FSize.Companion.getInstance(currentLineWidth, labelLineHeight)) + maxLineWidth = max(maxLineWidth, currentLineWidth) // Start a new line - mCalculatedLabelBreakPoints.set( - stackedStartIndex > -1 ? stackedStartIndex - : i, true); - currentLineWidth = requiredWidth; + calculatedLabelBreakPoints.set( + if (stackedStartIndex > -1) + stackedStartIndex + else + i, true + ) + currentLineWidth = requiredWidth } if (i == entryCount - 1) { // Add last line size to array - mCalculatedLineSizes.add(FSize.getInstance(currentLineWidth, labelLineHeight)); - maxLineWidth = Math.max(maxLineWidth, currentLineWidth); + calculatedLineSizes.add(FSize.Companion.getInstance(currentLineWidth, labelLineHeight)) + maxLineWidth = max(maxLineWidth, currentLineWidth) } } - stackedStartIndex = label != null ? -1 : stackedStartIndex; + stackedStartIndex = if (label != null) -1 else stackedStartIndex + i++ } - mNeededWidth = maxLineWidth; - mNeededHeight = labelLineHeight - * (float) (mCalculatedLineSizes.size()) - + labelLineSpacing * - (float) (mCalculatedLineSizes.size() == 0 - ? 0 - : (mCalculatedLineSizes.size() - 1)); - - break; + mNeededWidth = maxLineWidth + mNeededHeight = (labelLineHeight + * (calculatedLineSizes.size).toFloat() + + labelLineSpacing * (if (calculatedLineSizes.size == 0) + 0 + else + (calculatedLineSizes.size - 1)).toFloat()) } } - mNeededHeight += mYOffset; - mNeededWidth += mXOffset; + mNeededHeight += mYOffset + mNeededWidth += mXOffset } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt index 3acec0f461..b429168437 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt @@ -1,14 +1,12 @@ -package com.github.mikephil.charting.components; +package com.github.mikephil.charting.components +import android.graphics.DashPathEffect +import com.github.mikephil.charting.components.Legend.LegendForm +import com.github.mikephil.charting.utils.ColorTemplate -import android.graphics.DashPathEffect; -import com.github.mikephil.charting.utils.ColorTemplate; - -public class LegendEntry { - public LegendEntry() { - - } +class LegendEntry { + constructor() /** * @@ -19,26 +17,27 @@ public class LegendEntry { * @param formLineDashEffect Set to nil to use the legend's default. * @param formColor The color for drawing the form. */ - public LegendEntry(String label, - Legend.LegendForm form, - float formSize, - float formLineWidth, - DashPathEffect formLineDashEffect, - int formColor) - { - this.label = label; - this.form = form; - this.formSize = formSize; - this.formLineWidth = formLineWidth; - this.formLineDashEffect = formLineDashEffect; - this.formColor = formColor; + constructor( + label: String?, + form: LegendForm?, + formSize: Float, + formLineWidth: Float, + formLineDashEffect: DashPathEffect?, + formColor: Int + ) { + this.label = label + this.form = form + this.formSize = formSize + this.formLineWidth = formLineWidth + this.formLineDashEffect = formLineDashEffect + this.formColor = formColor } /** * The legend entry text. * A `null` label will start a group. */ - public String label; + var label: String? = null /** * The form to draw for this entry. @@ -47,32 +46,31 @@ public class LegendEntry { * `EMPTY` will avoid drawing a form, but keep its space. * `DEFAULT` will use the Legend's default. */ - public Legend.LegendForm form = Legend.LegendForm.DEFAULT; + var form: LegendForm? = LegendForm.DEFAULT /** * Form size will be considered except for when .None is used * * Set as NaN to use the legend's default */ - public float formSize = Float.NaN; + var formSize: Float = Float.Companion.NaN /** * Line width used for shapes that consist of lines. * * Set as NaN to use the legend's default */ - public float formLineWidth = Float.NaN; + var formLineWidth: Float = Float.Companion.NaN /** * Line dash path effect used for shapes that consist of lines. * * Set to null to use the legend's default */ - public DashPathEffect formLineDashEffect = null; + var formLineDashEffect: DashPathEffect? = null /** * The color for drawing the form */ - public int formColor = ColorTemplate.COLOR_NONE; - + var formColor: Int = ColorTemplate.COLOR_NONE } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt index 8fcdee8fc1..8e6234e53b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt @@ -1,215 +1,170 @@ +package com.github.mikephil.charting.components -package com.github.mikephil.charting.components; - -import android.graphics.Color; -import android.graphics.DashPathEffect; -import android.graphics.Paint; -import android.graphics.Typeface; - -import com.github.mikephil.charting.utils.Utils; +import android.graphics.Color +import android.graphics.DashPathEffect +import android.graphics.Paint +import com.github.mikephil.charting.utils.Utils /** * The limit line is an additional feature for all Line-, Bar- and * ScatterCharts. It allows the displaying of an additional line in the chart * that marks a certain maximum / limit on the specified axis (x- or y-axis). - * + * * @author Philipp Jahoda */ -public class LimitLine extends ComponentBase { - - /** limit / maximum (the y-value or xIndex) */ - private float mLimit = 0f; - - /** the width of the limit line */ - private float mLineWidth = 2f; - - /** the color of the limit line */ - private int mLineColor = Color.rgb(237, 91, 91); - - /** the style of the label text */ - private Paint.Style mTextStyle = Paint.Style.FILL_AND_STROKE; - - /** label string that is drawn next to the limit line */ - private String mLabel = ""; - - /** the path effect of this LimitLine that makes dashed lines possible */ - private DashPathEffect mDashPathEffect = null; - - /** indicates the position of the LimitLine label */ - private LimitLabelPosition mLabelPosition = LimitLabelPosition.RIGHT_TOP; - - /** enum that indicates the position of the LimitLine label */ - public enum LimitLabelPosition { - LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM - } - - /** - * Constructor with limit. - * - * @param limit - the position (the value) on the y-axis (y-value) or x-axis - * (xIndex) where this line should appear - */ - public LimitLine(float limit) { - mLimit = limit; - } - - /** - * Constructor with limit and label. - * - * @param limit - the position (the value) on the y-axis (y-value) or x-axis - * (xIndex) where this line should appear - * @param label - provide "" if no label is required - */ - public LimitLine(float limit, String label) { - mLimit = limit; - mLabel = label; - } - +class LimitLine : ComponentBase { /** * Returns the limit that is set for this line. - * + * * @return */ - public float getLimit() { - return mLimit; - } + /** limit / maximum (the y-value or xIndex) */ + var limit: Float = 0f + private set - /** - * set the line width of the chart (min = 0.2f, max = 12f); default 2f NOTE: - * thinner line == better performance, thicker line == worse performance - * - * @param width - */ - public void setLineWidth(float width) { - - if (width < 0.2f) - width = 0.2f; - if (width > 12.0f) - width = 12.0f; - mLineWidth = Utils.convertDpToPixel(width); - } + /** the width of the limit line */ + private var mLineWidth = 2f /** - * returns the width of limit line - * + * Returns the color that is used for this LimitLine + * * @return */ - public float getLineWidth() { - return mLineWidth; - } - /** * Sets the linecolor for this LimitLine. Make sure to use * getResources().getColor(...) - * + * * @param color */ - public void setLineColor(int color) { - mLineColor = color; - } + /** the color of the limit line */ + var lineColor: Int = Color.rgb(237, 91, 91) /** - * Returns the color that is used for this LimitLine - * + * Returns the color of the value-text that is drawn next to the LimitLine. + * * @return */ - public int getLineColor() { - return mLineColor; - } - /** - * Enables the line to be drawn in dashed mode, e.g. like this "- - - - - -" - * - * @param lineLength the length of the line pieces - * @param spaceLength the length of space inbetween the pieces - * @param phase offset, in degrees (normally, use 0) - */ - public void enableDashedLine(float lineLength, float spaceLength, float phase) { - mDashPathEffect = new DashPathEffect(new float[] { - lineLength, spaceLength - }, phase); - } - - /** - * Disables the line to be drawn in dashed mode. + * Sets the color of the value-text that is drawn next to the LimitLine. + * Default: Paint.Style.FILL_AND_STROKE + * + * @param style */ - public void disableDashedLine() { - mDashPathEffect = null; - } + /** the style of the label text */ + var textStyle: Paint.Style? = Paint.Style.FILL_AND_STROKE /** - * Returns true if the dashed-line effect is enabled, false if not. Default: - * disabled - * + * Returns the label that is drawn next to the limit line. + * * @return */ - public boolean isDashedLineEnabled() { - return mDashPathEffect == null ? false : true; - } - /** - * returns the DashPathEffect that is set for this LimitLine - * - * @return + * Sets the label that is drawn next to the limit line. Provide "" if no + * label is required. + * + * @param label */ - public DashPathEffect getDashPathEffect() { - return mDashPathEffect; - } + /** label string that is drawn next to the limit line */ + var label: String? = "" /** - * Sets the color of the value-text that is drawn next to the LimitLine. - * Default: Paint.Style.FILL_AND_STROKE + * returns the DashPathEffect that is set for this LimitLine * - * @param style + * @return */ - public void setTextStyle(Paint.Style style) { - this.mTextStyle = style; - } + /** the path effect of this LimitLine that makes dashed lines possible */ + var dashPathEffect: DashPathEffect? = null + private set /** - * Returns the color of the value-text that is drawn next to the LimitLine. + * Returns the position of the LimitLine label (value). * * @return */ - public Paint.Style getTextStyle() { - return mTextStyle; - } - /** * Sets the position of the LimitLine value label (either on the right or on * the left edge of the chart). Not supported for RadarChart. - * + * * @param pos */ - public void setLabelPosition(LimitLabelPosition pos) { - mLabelPosition = pos; + /** indicates the position of the LimitLine label */ + var labelPosition: LimitLabelPosition? = LimitLabelPosition.RIGHT_TOP + + /** enum that indicates the position of the LimitLine label */ + enum class LimitLabelPosition { + LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM } /** - * Returns the position of the LimitLine label (value). - * - * @return + * Constructor with limit. + * + * @param limit - the position (the value) on the y-axis (y-value) or x-axis + * (xIndex) where this line should appear */ - public LimitLabelPosition getLabelPosition() { - return mLabelPosition; + constructor(limit: Float) { + this.limit = limit } /** - * Sets the label that is drawn next to the limit line. Provide "" if no - * label is required. - * - * @param label + * Constructor with limit and label. + * + * @param limit - the position (the value) on the y-axis (y-value) or x-axis + * (xIndex) where this line should appear + * @param label - provide "" if no label is required */ - public void setLabel(String label) { - mLabel = label; + constructor(limit: Float, label: String?) { + this.limit = limit + this.label = label + } + + var lineWidth: Float + /** + * returns the width of limit line + * + * @return + */ + get() = mLineWidth + /** + * set the line width of the chart (min = 0.2f, max = 12f); default 2f NOTE: + * thinner line == better performance, thicker line == worse performance + * + * @param width + */ + set(width) { + var width = width + if (width < 0.2f) width = 0.2f + if (width > 12.0f) width = 12.0f + mLineWidth = Utils.convertDpToPixel(width) + } + + /** + * Enables the line to be drawn in dashed mode, e.g. like this "- - - - - -" + * + * @param lineLength the length of the line pieces + * @param spaceLength the length of space inbetween the pieces + * @param phase offset, in degrees (normally, use 0) + */ + fun enableDashedLine(lineLength: Float, spaceLength: Float, phase: Float) { + this.dashPathEffect = DashPathEffect( + floatArrayOf( + lineLength, spaceLength + ), phase + ) } /** - * Returns the label that is drawn next to the limit line. - * - * @return + * Disables the line to be drawn in dashed mode. */ - public String getLabel() { - return mLabel; + fun disableDashedLine() { + this.dashPathEffect = null } + + val isDashedLineEnabled: Boolean + /** + * Returns true if the dashed-line effect is enabled, false if not. Default: + * disabled + * + * @return + */ + get() = if (this.dashPathEffect == null) false else true } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt index 926c844998..06dc3d98ae 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt @@ -1,270 +1,213 @@ +package com.github.mikephil.charting.components -package com.github.mikephil.charting.components; - -import android.graphics.Color; -import android.graphics.DashPathEffect; -import android.graphics.Paint; - -import com.github.mikephil.charting.utils.Utils; +import android.graphics.Color +import android.graphics.DashPathEffect +import android.graphics.Paint +import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition +import com.github.mikephil.charting.utils.Utils /** * The limit line is an additional feature for all Line-, Bar- and * ScatterCharts. It allows the displaying of an additional line in the chart * that marks a certain maximum / limit on the specified axis (x- or y-axis). */ -public class LimitRange extends ComponentBase { - - public static class Range { - private final float mLow; - private final float mHigh; - - Range(float r1, float r2) { - if (r1 < r2) { - mLow = r1; - mHigh = r2; - } else { - mLow = r2; - mHigh = r1; - } - } - - public float getLow() { - return mLow; - } - - public float getHigh() { - return mHigh; - } - } - - /** - * limit / maximum (the y-value or xIndex) - */ - private Range mLimit; - - /** - * the width of the limit line - */ - private float mLineWidth = 0f; - - /** - * the color of the limit line - */ - private int mLineColor = Color.rgb(237, 91, 91); - - /** - * the color of the Range - */ - private int mRangeColor = Color.rgb(128, 128, 128); - - /** - * the style of the label text - */ - private Paint.Style mTextStyle = Paint.Style.FILL; - - /** - * label string that is drawn next to the limit line - */ - private String mLabel = ""; - - /** - * the path effect of this LimitLine that makes dashed lines possible - */ - private DashPathEffect mDashPathEffect = null; - - /** - * indicates the position of the LimitLine label - */ - private LimitLine.LimitLabelPosition mLabelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP; - - /** - * Constructor with limit. - * - * @param firstLimit - the position (the value) on the y-axis (y-value) or x-axis - * (xIndex) where this line should appear - * @param secondLimit - the position (the value) on the y-axis (y-value) or x-axis - * (xIndex) where this line should appear - */ - public LimitRange(float firstLimit, float secondLimit) { - mLimit = new Range(firstLimit, secondLimit); - } - - /** - * Constructor with limit and label. - * - * @param firstLimit - the position (the value) on the y-axis (y-value) or x-axis - * (xIndex) where this line should appear - * @param secondLimit - the position (the value) on the y-axis (y-value) or x-axis - * (xIndex) where this line should appear - * @param label - provide "" if no label is required - */ - public LimitRange(float firstLimit, float secondLimit, String label) { - mLimit = new Range(firstLimit, secondLimit); - mLabel = label; - } - - /** - * Returns the limit that is set for this line. - * - * @return - */ - public Range getLimit() { - return mLimit; - } - - /** - * set the line width of the chart (min = 0.2f, max = 12f); default 2f NOTE: - * thinner line == better performance, thicker line == worse performance - * - * @param width - */ - public void setLineWidth(float width) { - if (width > 12.0f) { - width = 12.0f; +class LimitRange : ComponentBase { + class Range internal constructor(r1: Float, r2: Float) { + val low: Float + val high: Float + + init { + if (r1 < r2) { + this.low = r1 + this.high = r2 + } else { + this.low = r2 + this.high = r1 + } + } + } + + /** + * Returns the limit that is set for this line. + * + * @return + */ + /** + * limit / maximum (the y-value or xIndex) + */ + val limit: Range + + /** + * the width of the limit line + */ + private var mLineWidth = 0f + + /** + * Returns the color that is used for this LimitLine + * + * @return + */ + /** + * Sets the linecolor for this LimitLine. Make sure to use + * getResources().getColor(...) + * + * @param color + */ + /** + * the color of the limit line + */ + var lineColor: Int = Color.rgb(237, 91, 91) + + /** + * Returns the color that is used for this LimitRange + * + * @return + */ + /** + * Sets the range color for this LimitRange. Make sure to use + * getResources().getColor(...) + * + * @param color + */ + /** + * the color of the Range + */ + var rangeColor: Int = Color.rgb(128, 128, 128) + + /** + * Returns the color of the value-text that is drawn next to the LimitLine. + * + * @return + */ + /** + * Sets the color of the value-text that is drawn next to the LimitLine. + * Default: Paint.Style.FILL_AND_STROKE + * + * @param style + */ + /** + * the style of the label text + */ + var textStyle: Paint.Style? = Paint.Style.FILL + + /** + * Returns the label that is drawn next to the limit line. + * + * @return + */ + /** + * Sets the label that is drawn next to the limit line. Provide "" if no + * label is required. + * + * @param label + */ + /** + * label string that is drawn next to the limit line + */ + var label: String? = "" + + /** + * returns the DashPathEffect that is set for this LimitLine + * + * @return + */ + /** + * the path effect of this LimitLine that makes dashed lines possible + */ + var dashPathEffect: DashPathEffect? = null + private set + + /** + * Returns the position of the LimitLine label (value). + * + * @return + */ + /** + * Sets the position of the LimitLine value label (either on the right or on + * the left edge of the chart). Not supported for RadarChart. + * + * @param pos + */ + /** + * indicates the position of the LimitLine label + */ + var labelPosition: LimitLabelPosition? = LimitLabelPosition.RIGHT_TOP + + /** + * Constructor with limit. + * + * @param firstLimit - the position (the value) on the y-axis (y-value) or x-axis + * (xIndex) where this line should appear + * @param secondLimit - the position (the value) on the y-axis (y-value) or x-axis + * (xIndex) where this line should appear + */ + constructor(firstLimit: Float, secondLimit: Float) { + this.limit = Range(firstLimit, secondLimit) + } + + /** + * Constructor with limit and label. + * + * @param firstLimit - the position (the value) on the y-axis (y-value) or x-axis + * (xIndex) where this line should appear + * @param secondLimit - the position (the value) on the y-axis (y-value) or x-axis + * (xIndex) where this line should appear + * @param label - provide "" if no label is required + */ + constructor(firstLimit: Float, secondLimit: Float, label: String?) { + this.limit = Range(firstLimit, secondLimit) + this.label = label + } + + var lineWidth: Float + /** + * returns the width of limit line + * + * @return + */ + get() = mLineWidth + /** + * set the line width of the chart (min = 0.2f, max = 12f); default 2f NOTE: + * thinner line == better performance, thicker line == worse performance + * + * @param width + */ + set(width) { + var width = width + if (width > 12.0f) { + width = 12.0f + } + mLineWidth = Utils.convertDpToPixel(width) } - mLineWidth = Utils.convertDpToPixel(width); - } - - /** - * returns the width of limit line - * - * @return - */ - public float getLineWidth() { - return mLineWidth; - } - - /** - * Sets the linecolor for this LimitLine. Make sure to use - * getResources().getColor(...) - * - * @param color - */ - public void setLineColor(int color) { - mLineColor = color; - } - - /** - * Sets the range color for this LimitRange. Make sure to use - * getResources().getColor(...) - * - * @param color - */ - public void setRangeColor(int color) { - mRangeColor = color; - } - - /** - * Returns the color that is used for this LimitLine - * - * @return - */ - public int getLineColor() { - return mLineColor; - } - - /** - * Returns the color that is used for this LimitRange - * - * @return - */ - public int getRangeColor() { - return mRangeColor; - } - - /** - * Enables the line to be drawn in dashed mode, e.g. like this "- - - - - -" - * - * @param lineLength the length of the line pieces - * @param spaceLength the length of space inbetween the pieces - * @param phase offset, in degrees (normally, use 0) - */ - public void enableDashedLine(float lineLength, float spaceLength, float phase) { - mDashPathEffect = new DashPathEffect(new float[]{ - lineLength, spaceLength - }, phase); - } - - /** - * Disables the line to be drawn in dashed mode. - */ - public void disableDashedLine() { - mDashPathEffect = null; - } - - /** - * Returns true if the dashed-line effect is enabled, false if not. Default: - * disabled - * - * @return - */ - public boolean isDashedLineEnabled() { - return mDashPathEffect == null ? false : true; - } - - /** - * returns the DashPathEffect that is set for this LimitLine - * - * @return - */ - public DashPathEffect getDashPathEffect() { - return mDashPathEffect; - } - - /** - * Sets the color of the value-text that is drawn next to the LimitLine. - * Default: Paint.Style.FILL_AND_STROKE - * - * @param style - */ - public void setTextStyle(Paint.Style style) { - this.mTextStyle = style; - } - - /** - * Returns the color of the value-text that is drawn next to the LimitLine. - * - * @return - */ - public Paint.Style getTextStyle() { - return mTextStyle; - } - - /** - * Sets the position of the LimitLine value label (either on the right or on - * the left edge of the chart). Not supported for RadarChart. - * - * @param pos - */ - public void setLabelPosition(LimitLine.LimitLabelPosition pos) { - mLabelPosition = pos; - } - - /** - * Returns the position of the LimitLine label (value). - * - * @return - */ - public LimitLine.LimitLabelPosition getLabelPosition() { - return mLabelPosition; - } - - /** - * Sets the label that is drawn next to the limit line. Provide "" if no - * label is required. - * - * @param label - */ - public void setLabel(String label) { - mLabel = label; - } - /** - * Returns the label that is drawn next to the limit line. - * - * @return - */ - public String getLabel() { - return mLabel; - } + /** + * Enables the line to be drawn in dashed mode, e.g. like this "- - - - - -" + * + * @param lineLength the length of the line pieces + * @param spaceLength the length of space inbetween the pieces + * @param phase offset, in degrees (normally, use 0) + */ + fun enableDashedLine(lineLength: Float, spaceLength: Float, phase: Float) { + this.dashPathEffect = DashPathEffect( + floatArrayOf( + lineLength, spaceLength + ), phase + ) + } + + /** + * Disables the line to be drawn in dashed mode. + */ + fun disableDashedLine() { + this.dashPathEffect = null + } + + val isDashedLineEnabled: Boolean + /** + * Returns true if the dashed-line effect is enabled, false if not. Default: + * disabled + * + * @return + */ + get() = if (this.dashPathEffect == null) false else true } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt index 7bd7b8e6c3..2098798d20 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt @@ -1,22 +1,17 @@ -package com.github.mikephil.charting.components; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Rect; -import android.graphics.drawable.Drawable; -import android.os.Build; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.RelativeLayout; - -import com.github.mikephil.charting.charts.Chart; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.FSize; -import com.github.mikephil.charting.utils.MPPointF; - -import java.lang.ref.WeakReference; +package com.github.mikephil.charting.components + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Rect +import android.graphics.drawable.Drawable +import android.os.Build +import com.github.mikephil.charting.charts.Chart +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.FSize +import com.github.mikephil.charting.utils.MPPointF +import java.lang.ref.WeakReference +import androidx.core.graphics.withTranslation /** * View that can be displayed when selecting values in the chart. Extend this class to provide custom layouts for your @@ -24,144 +19,125 @@ import java.lang.ref.WeakReference; * * @author Philipp Jahoda */ -public class MarkerImage implements IMarker { +class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMarker { + private var mDrawable: Drawable? = null - private Context mContext; - private Drawable mDrawable; + private var mOffset: MPPointF? = MPPointF() + private val mOffset2 = MPPointF() + private var mWeakChart: WeakReference?>? = null - private MPPointF mOffset = new MPPointF(); - private MPPointF mOffset2 = new MPPointF(); - private WeakReference mWeakChart; - - private FSize mSize = new FSize(); - private Rect mDrawableBoundsCache = new Rect(); + private var mSize: FSize? = FSize() + private val mDrawableBoundsCache = Rect() /** * Constructor. Sets up the MarkerView with a custom layout resource. * - * @param context + * @param mContext * @param drawableResourceId the drawable resource to render */ - public MarkerImage(Context context, int drawableResourceId) { - mContext = context; - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) - { - mDrawable = mContext.getResources().getDrawable(drawableResourceId, null); - } - else - { - mDrawable = mContext.getResources().getDrawable(drawableResourceId); + init { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + mDrawable = mContext.getResources().getDrawable(drawableResourceId, null) + } else { + mDrawable = mContext.getResources().getDrawable(drawableResourceId) } } - public void setOffset(MPPointF offset) { - mOffset = offset; + fun setOffset(offset: MPPointF?) { + mOffset = offset if (mOffset == null) { - mOffset = new MPPointF(); + mOffset = MPPointF() } } - public void setOffset(float offsetX, float offsetY) { - mOffset.x = offsetX; - mOffset.y = offsetY; + fun setOffset(offsetX: Float, offsetY: Float) { + mOffset!!.x = offsetX + mOffset!!.y = offsetY } - @Override - public MPPointF getOffset() { - return mOffset; - } + override val offset: MPPointF? + get() = mOffset - public void setSize(FSize size) { - mSize = size; + var size: FSize? + get() = mSize + set(size) { + mSize = size - if (mSize == null) { - mSize = new FSize(); + if (mSize == null) { + mSize = FSize() + } } - } - - public FSize getSize() { - return mSize; - } - - public void setChartView(Chart chart) { - mWeakChart = new WeakReference<>(chart); - } - - public Chart getChartView() { - return mWeakChart == null ? null : mWeakChart.get(); - } - @Override - public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) { + var chartView: Chart<*, *, *>? + get() = if (mWeakChart == null) null else mWeakChart!!.get() + set(chart) { + mWeakChart = WeakReference?>(chart) + } - MPPointF offset = getOffset(); - mOffset2.x = offset.x; - mOffset2.y = offset.y; + override fun getOffsetForDrawingAtPoint(posX: Float, posY: Float): MPPointF { + val offset = offset!! + mOffset2.x = offset.x + mOffset2.y = offset.y - Chart chart = getChartView(); + val chart = this.chartView - float width = mSize.width; - float height = mSize.height; + var width = mSize!!.width + var height = mSize!!.height - if (width == 0.f && mDrawable != null) { - width = mDrawable.getIntrinsicWidth(); + if (width == 0f && mDrawable != null) { + width = mDrawable!!.getIntrinsicWidth().toFloat() } - if (height == 0.f && mDrawable != null) { - height = mDrawable.getIntrinsicHeight(); + if (height == 0f && mDrawable != null) { + height = mDrawable!!.getIntrinsicHeight().toFloat() } if (posX + mOffset2.x < 0) { - mOffset2.x = - posX; + mOffset2.x = -posX } else if (chart != null && posX + width + mOffset2.x > chart.getWidth()) { - mOffset2.x = chart.getWidth() - posX - width; + mOffset2.x = chart.getWidth() - posX - width } if (posY + mOffset2.y < 0) { - mOffset2.y = - posY; + mOffset2.y = -posY } else if (chart != null && posY + height + mOffset2.y > chart.getHeight()) { - mOffset2.y = chart.getHeight() - posY - height; + mOffset2.y = chart.getHeight() - posY - height } - return mOffset2; + return mOffset2 } - @Override - public void refreshContent(Entry e, Highlight highlight) { - + override fun refreshContent(e: Entry?, highlight: Highlight?) { } - @Override - public void draw(Canvas canvas, float posX, float posY) { - - if (mDrawable == null) return; + override fun draw(canvas: Canvas?, posX: Float, posY: Float) { + if (mDrawable == null) return - MPPointF offset = getOffsetForDrawingAtPoint(posX, posY); + val offset = getOffsetForDrawingAtPoint(posX, posY) - float width = mSize.width; - float height = mSize.height; + var width = mSize!!.width + var height = mSize!!.height - if (width == 0.f) { - width = mDrawable.getIntrinsicWidth(); + if (width == 0f) { + width = mDrawable!!.getIntrinsicWidth().toFloat() } - if (height == 0.f) { - height = mDrawable.getIntrinsicHeight(); + if (height == 0f) { + height = mDrawable!!.getIntrinsicHeight().toFloat() } - mDrawable.copyBounds(mDrawableBoundsCache); - mDrawable.setBounds( - mDrawableBoundsCache.left, - mDrawableBoundsCache.top, - mDrawableBoundsCache.left + (int)width, - mDrawableBoundsCache.top + (int)height); - - int saveId = canvas.save(); - // translate to the correct position and draw - canvas.translate(posX + offset.x, posY + offset.y); - mDrawable.draw(canvas); - canvas.restoreToCount(saveId); + mDrawable!!.copyBounds(mDrawableBoundsCache) + mDrawable!!.setBounds( + mDrawableBoundsCache.left, + mDrawableBoundsCache.top, + mDrawableBoundsCache.left + width.toInt(), + mDrawableBoundsCache.top + height.toInt() + ) + + canvas?.withTranslation(posX + offset.x, posY + offset.y) { + // translate to the correct position and draw + mDrawable!!.draw(canvas) + } - mDrawable.setBounds(mDrawableBoundsCache); + mDrawable!!.bounds = mDrawableBoundsCache } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt index 162e88e33c..a09634beec 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt @@ -1,18 +1,15 @@ -package com.github.mikephil.charting.components; - -import android.content.Context; -import android.graphics.Canvas; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.RelativeLayout; - -import com.github.mikephil.charting.charts.Chart; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.FSize; -import com.github.mikephil.charting.utils.MPPointF; - -import java.lang.ref.WeakReference; +package com.github.mikephil.charting.components + +import android.content.Context +import android.graphics.Canvas +import android.view.LayoutInflater +import android.widget.RelativeLayout +import com.github.mikephil.charting.charts.Chart +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.lang.ref.WeakReference +import androidx.core.graphics.withTranslation /** * View that can be displayed when selecting values in the chart. Extend this class to provide custom layouts for your @@ -20,11 +17,10 @@ import java.lang.ref.WeakReference; * * @author Philipp Jahoda */ -public class MarkerView extends RelativeLayout implements IMarker { - - private MPPointF mOffset = new MPPointF(); - private MPPointF mOffset2 = new MPPointF(); - private WeakReference mWeakChart; +open class MarkerView(context: Context?, layoutResource: Int) : RelativeLayout(context), IMarker { + private var mOffset: MPPointF? = MPPointF() + private val mOffset2 = MPPointF() + private var mWeakChart: WeakReference?>? = null /** * Constructor. Sets up the MarkerView with a custom layout resource. @@ -32,9 +28,8 @@ public class MarkerView extends RelativeLayout implements IMarker { * @param context * @param layoutResource the layout resource to use for the MarkerView */ - public MarkerView(Context context, int layoutResource) { - super(context); - setupLayoutResource(layoutResource); + init { + setupLayoutResource(layoutResource) } /** @@ -42,88 +37,77 @@ public class MarkerView extends RelativeLayout implements IMarker { * * @param layoutResource */ - private void setupLayoutResource(int layoutResource) { + private fun setupLayoutResource(layoutResource: Int) { + val inflated = LayoutInflater.from(context).inflate(layoutResource, this) - View inflated = LayoutInflater.from(getContext()).inflate(layoutResource, this); - - inflated.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); - inflated.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); + inflated.setLayoutParams(LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) + inflated.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)) // measure(getWidth(), getHeight()); - inflated.layout(0, 0, inflated.getMeasuredWidth(), inflated.getMeasuredHeight()); + inflated.layout(0, 0, inflated.measuredWidth, inflated.measuredHeight) } - public void setOffset(MPPointF offset) { - mOffset = offset; + fun setOffset(offset: MPPointF?) { + mOffset = offset if (mOffset == null) { - mOffset = new MPPointF(); + mOffset = MPPointF() } } - public void setOffset(float offsetX, float offsetY) { - mOffset.x = offsetX; - mOffset.y = offsetY; - } - - @Override - public MPPointF getOffset() { - return mOffset; + fun setOffset(offsetX: Float, offsetY: Float) { + mOffset!!.x = offsetX + mOffset!!.y = offsetY } - public void setChartView(Chart chart) { - mWeakChart = new WeakReference<>(chart); - } + override val offset: MPPointF? + get() = mOffset - public Chart getChartView() { - return mWeakChart == null ? null : mWeakChart.get(); - } - - @Override - public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) { + var chartView: Chart<*, *, *>? + get() = if (mWeakChart == null) null else mWeakChart!!.get() + set(chart) { + mWeakChart = WeakReference?>(chart) + } - MPPointF offset = getOffset(); - mOffset2.x = offset.x; - mOffset2.y = offset.y; + override fun getOffsetForDrawingAtPoint(posX: Float, posY: Float): MPPointF { + val offset = offset!! + mOffset2.x = offset.x + mOffset2.y = offset.y - Chart chart = getChartView(); + val chart = this.chartView - float width = getWidth(); - float height = getHeight(); + val width = width.toFloat() + val height = height.toFloat() if (posX + mOffset2.x < 0) { - mOffset2.x = - posX; - } else if (chart != null && posX + width + mOffset2.x > chart.getWidth()) { - mOffset2.x = chart.getWidth() - posX - width; + mOffset2.x = -posX + } else if (chart != null && posX + width + mOffset2.x > chart.width) { + mOffset2.x = chart.width - posX - width } if (posY + mOffset2.y < 0) { - mOffset2.y = - posY; - } else if (chart != null && posY + height + mOffset2.y > chart.getHeight()) { - mOffset2.y = chart.getHeight() - posY - height; + mOffset2.y = -posY + } else if (chart != null && posY + height + mOffset2.y > chart.height) { + mOffset2.y = chart.height - posY - height } - return mOffset2; + return mOffset2 } - @Override - public void refreshContent(Entry e, Highlight highlight) { - - measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), - MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); - layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); - + override fun refreshContent(e: Entry?, highlight: Highlight?) { + measure( + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED) + ) + layout(0, 0, measuredWidth, measuredHeight) } - @Override - public void draw(Canvas canvas, float posX, float posY) { - - MPPointF offset = getOffsetForDrawingAtPoint(posX, posY); + override fun draw(canvas: Canvas?, posX: Float, posY: Float) { + val offset = getOffsetForDrawingAtPoint(posX, posY) - int saveId = canvas.save(); - // translate to the correct position and draw - canvas.translate(posX + offset.x, posY + offset.y); - draw(canvas); - canvas.restoreToCount(saveId); + canvas?.withTranslation(posX + offset.x, posY + offset.y) { + // translate to the correct position and draw + draw(canvas) + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/XAxis.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/XAxis.kt index 42ec5a8792..3353501e47 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/XAxis.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/XAxis.kt @@ -1,7 +1,6 @@ +package com.github.mikephil.charting.components -package com.github.mikephil.charting.components; - -import com.github.mikephil.charting.utils.Utils; +import com.github.mikephil.charting.utils.Utils /** * Class representing the x-axis labels settings. Only use the setter methods to @@ -10,79 +9,66 @@ import com.github.mikephil.charting.utils.Utils; * * @author Philipp Jahoda */ -public class XAxis extends AxisBase { - +class XAxis : AxisBase() { /** * width of the x-axis labels in pixels - this is automatically * calculated by the computeSize() methods in the renderers */ - public int mLabelWidth = 1; + var mLabelWidth: Int = 1 /** * height of the x-axis labels in pixels - this is automatically * calculated by the computeSize() methods in the renderers */ - public int mLabelHeight = 1; + var mLabelHeight: Int = 1 /** - * This is the angle for drawing the X axis labels (in degrees) + * returns the angle for drawing the X axis labels (in degrees) */ - protected float mLabelRotationAngle = 0f; - /** - * 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 + * sets the angle for drawing the X axis labels (in degrees) + * + * @param angle the angle in degrees */ - private boolean mAvoidFirstLastClipping = false; - /** - * the position of the x-labels relative to the chart + * This is the angle for drawing the X axis labels (in degrees) */ - private XAxisPosition mPosition = XAxisPosition.TOP; + var labelRotationAngle: Float = 0f /** - * enum for the position of the x-labels relative to the chart + * returns true if avoid-first-lastclipping is enabled, false if not + * + * @return */ - public enum XAxisPosition { - TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE - } - - public XAxis() { - super(); - - mYOffset = Utils.convertDpToPixel(4.f); // -3 - } + /** + * 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 + */ + var isAvoidFirstLastClippingEnabled: Boolean = false + private set /** * returns the position of the x-labels */ - public XAxisPosition getPosition() { - return mPosition; - } - /** * sets the position of the x-labels * * @param pos */ - public void setPosition(XAxisPosition pos) { - mPosition = pos; - } - /** - * returns the angle for drawing the X axis labels (in degrees) + * the position of the x-labels relative to the chart */ - public float getLabelRotationAngle() { - return mLabelRotationAngle; - } + var position: XAxisPosition? = XAxisPosition.TOP /** - * sets the angle for drawing the X axis labels (in degrees) - * - * @param angle the angle in degrees + * enum for the position of the x-labels relative to the chart */ - public void setLabelRotationAngle(float angle) { - mLabelRotationAngle = angle; + enum class XAxisPosition { + TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE + } + + init { + mYOffset = Utils.convertDpToPixel(4f) // -3 } /** @@ -91,16 +77,7 @@ public class XAxis extends AxisBase { * * @param enabled */ - public void setAvoidFirstLastClipping(boolean enabled) { - mAvoidFirstLastClipping = enabled; - } - - /** - * returns true if avoid-first-lastclipping is enabled, false if not - * - * @return - */ - public boolean isAvoidFirstLastClippingEnabled() { - return mAvoidFirstLastClipping; + fun setAvoidFirstLastClipping(enabled: Boolean) { + this.isAvoidFirstLastClippingEnabled = enabled } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt index 872bb721a9..733772e7b6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt @@ -1,9 +1,11 @@ -package com.github.mikephil.charting.components; +package com.github.mikephil.charting.components -import android.graphics.Color; -import android.graphics.Paint; - -import com.github.mikephil.charting.utils.Utils; +import android.graphics.Color +import android.graphics.Paint +import com.github.mikephil.charting.utils.Utils +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min /** * Class representing the y-axis labels settings and its entries. Only use the setter methods to @@ -16,349 +18,282 @@ import com.github.mikephil.charting.utils.Utils; * * @author Philipp Jahoda */ -public class YAxis extends AxisBase { - +class YAxis : AxisBase { + /** + * returns true if drawing the bottom y-axis label entry is enabled + * + * @return + */ /** * indicates if the bottom y-label entry is drawn or not */ - private boolean mDrawBottomYLabelEntry = true; + val isDrawBottomYLabelEntryEnabled: Boolean = true + /** + * returns true if drawing the top y-axis label entry is enabled + * + * @return + */ /** * indicates if the top y-label entry is drawn or not */ - private boolean mDrawTopYLabelEntry = true; + var isDrawTopYLabelEntryEnabled: Boolean = true + private set /** - * flag that indicates if the axis is inverted or not + * If this returns true, the y-axis is inverted. + * + * @return */ - protected boolean mInverted = false; - /** - * flag that indicates if the zero-line should be drawn regardless of other grid lines + * If this is set to true, the y-axis is inverted which means that low values are on top of + * the chart, high values + * on bottom. + * + * @param enabled */ - protected boolean mDrawZeroLine = false; - /** - * flag indicating that auto scale min restriction should be used + * flag that indicates if the axis is inverted or not */ - private boolean mUseAutoScaleRestrictionMin = false; + var isInverted: Boolean = false /** - * flag indicating that auto scale max restriction should be used + * flag that indicates if the zero-line should be drawn regardless of other grid lines */ - private boolean mUseAutoScaleRestrictionMax = false; + var isDrawZeroLineEnabled: Boolean = false + protected set /** - * Color of the zero line + * Returns true if autoscale restriction for axis min value is enabled */ - protected int mZeroLineColor = Color.GRAY; - /** - * Width of the zero line in pixels + * Sets autoscale restriction for axis min value as enabled/disabled */ - protected float mZeroLineWidth = 1f; - /** - * axis space from the largest value to the top in percent of the total axis range + * flag indicating that auto scale min restriction should be used */ - protected float mSpacePercentTop = 10f; + @get:Deprecated("") + @set:Deprecated("") + var isUseAutoScaleMinRestriction: Boolean = false /** - * axis space from the smallest value to the bottom in percent of the total axis range + * Returns true if autoscale restriction for axis max value is enabled */ - protected float mSpacePercentBottom = 10f; - /** - * the position of the y-labels relative to the chart + * Sets autoscale restriction for axis max value as enabled/disabled */ - private YAxisLabelPosition mPosition = YAxisLabelPosition.OUTSIDE_CHART; - /** - * the horizontal offset of the y-label + * flag indicating that auto scale max restriction should be used */ - private float mXLabelOffset = 0.0f; + @get:Deprecated("") + @set:Deprecated("") + var isUseAutoScaleMaxRestriction: Boolean = false /** - * enum for the position of the y-labels relative to the chart + * Sets the color of the zero line + * + * @param color */ - public enum YAxisLabelPosition { - OUTSIDE_CHART, INSIDE_CHART - } - /** - * the side this axis object represents + * Color of the zero line */ - private AxisDependency mAxisDependency; + var zeroLineColor: Int = Color.GRAY /** - * the minimum width that the axis should take (in dp). - *

- * default: 0.0 + * Width of the zero line in pixels */ - protected float mMinWidth = 0.f; + protected var mZeroLineWidth: Float = 1f /** - * the maximum width that the axis can take (in dp). - * use Inifinity for disabling the maximum - * default: Float.POSITIVE_INFINITY (no maximum specified) + * Returns the top axis space in percent of the full range. Default 10f + * + * @return */ - protected float mMaxWidth = Float.POSITIVE_INFINITY; - /** - * Enum that specifies the axis a DataSet should be plotted against, either LEFT or RIGHT. + * Sets the top axis space in percent of the full range. Default 10f * - * @author Philipp Jahoda + * @param percent */ - public enum AxisDependency { - LEFT, RIGHT - } - - public YAxis() { - super(); - - // default left - this.mAxisDependency = AxisDependency.LEFT; - this.mYOffset = 0f; - } - - public YAxis(AxisDependency position) { - super(); - this.mAxisDependency = position; - this.mYOffset = 0f; - } - - public AxisDependency getAxisDependency() { - return mAxisDependency; - } - /** - * @return the minimum width that the axis should take (in dp). + * axis space from the largest value to the top in percent of the total axis range */ - public float getMinWidth() { - return mMinWidth; - } + var spaceTop: Float = 10f /** - * Sets the minimum width that the axis should take (in dp). + * Returns the bottom axis space in percent of the full range. Default 10f * - * @param minWidth + * @return */ - public void setMinWidth(float minWidth) { - mMinWidth = minWidth; - } - /** - * @return the maximum width that the axis can take (in dp). + * Sets the bottom axis space in percent of the full range. Default 10f + * + * @param percent */ - public float getMaxWidth() { - return mMaxWidth; - } - /** - * Sets the maximum width that the axis can take (in dp). - * - * @param maxWidth + * axis space from the smallest value to the bottom in percent of the total axis range */ - public void setMaxWidth(float maxWidth) { - mMaxWidth = maxWidth; - } + var spaceBottom: Float = 10f /** * returns the position of the y-labels */ - public YAxisLabelPosition getLabelPosition() { - return mPosition; - } - /** - * sets the position of the y-labels - * - * @param pos + * the position of the y-labels relative to the chart */ - public void setPosition(YAxisLabelPosition pos) { - mPosition = pos; - } + var labelPosition: YAxisLabelPosition = YAxisLabelPosition.OUTSIDE_CHART + private set /** * returns the horizontal offset of the y-label */ - public float getLabelXOffset() { - return mXLabelOffset; - } - /** * sets the horizontal offset of the y-label * * @param xOffset */ - public void setLabelXOffset(float xOffset) { - mXLabelOffset = xOffset; - } - /** - * returns true if drawing the top y-axis label entry is enabled - * - * @return + * the horizontal offset of the y-label */ - public boolean isDrawTopYLabelEntryEnabled() { - return mDrawTopYLabelEntry; - } + var labelXOffset: Float = 0.0f /** - * returns true if drawing the bottom y-axis label entry is enabled - * - * @return + * enum for the position of the y-labels relative to the chart */ - public boolean isDrawBottomYLabelEntryEnabled() { - return mDrawBottomYLabelEntry; + enum class YAxisLabelPosition { + OUTSIDE_CHART, INSIDE_CHART } /** - * set this to true to enable drawing the top y-label entry. Disabling this can be helpful - * when the top y-label and - * left x-label interfere with each other. default: true - * - * @param enabled + * the side this axis object represents */ - public void setDrawTopYLabelEntry(boolean enabled) { - mDrawTopYLabelEntry = enabled; - } + val axisDependency: AxisDependency /** - * If this is set to true, the y-axis is inverted which means that low values are on top of - * the chart, high values - * on bottom. - * - * @param enabled + * @return the minimum width that the axis should take (in dp). */ - public void setInverted(boolean enabled) { - mInverted = enabled; - } - /** - * If this returns true, the y-axis is inverted. + * Sets the minimum width that the axis should take (in dp). * - * @return + * @param minWidth */ - public boolean isInverted() { - return mInverted; - } - /** - * This method is deprecated. - * Use setAxisMinimum(...) / setAxisMaximum(...) instead. + * the minimum width that the axis should take (in dp). * - * @param startAtZero + * + * default: 0.0 */ - @Deprecated - public void setStartAtZero(boolean startAtZero) { - if (startAtZero) - setAxisMinimum(0f); - else - resetAxisMinimum(); - } + var minWidth: Float = 0f /** - * Sets the top axis space in percent of the full range. Default 10f - * - * @param percent + * @return the maximum width that the axis can take (in dp). */ - public void setSpaceTop(float percent) { - mSpacePercentTop = percent; - } - /** - * Returns the top axis space in percent of the full range. Default 10f + * Sets the maximum width that the axis can take (in dp). * - * @return + * @param maxWidth */ - public float getSpaceTop() { - return mSpacePercentTop; - } - /** - * Sets the bottom axis space in percent of the full range. Default 10f - * - * @param percent + * the maximum width that the axis can take (in dp). + * use Inifinity for disabling the maximum + * default: Float.POSITIVE_INFINITY (no maximum specified) */ - public void setSpaceBottom(float percent) { - mSpacePercentBottom = percent; - } + var maxWidth: Float = Float.Companion.POSITIVE_INFINITY /** - * Returns the bottom axis space in percent of the full range. Default 10f + * Enum that specifies the axis a DataSet should be plotted against, either LEFT or RIGHT. * - * @return + * @author Philipp Jahoda */ - public float getSpaceBottom() { - return mSpacePercentBottom; + enum class AxisDependency { + LEFT, RIGHT } - public boolean isDrawZeroLineEnabled() { - return mDrawZeroLine; + constructor() : super() { + // default left + this.axisDependency = AxisDependency.LEFT + this.mYOffset = 0f + } + + constructor(position: AxisDependency) : super() { + this.axisDependency = position + this.mYOffset = 0f } /** - * Set this to true to draw the zero-line regardless of weather other - * grid-lines are enabled or not. Default: false + * sets the position of the y-labels * - * @param mDrawZeroLine + * @param pos */ - public void setDrawZeroLine(boolean mDrawZeroLine) { - this.mDrawZeroLine = mDrawZeroLine; - } - - public int getZeroLineColor() { - return mZeroLineColor; + fun setPosition(pos: YAxisLabelPosition) { + this.labelPosition = pos } /** - * Sets the color of the zero line + * set this to true to enable drawing the top y-label entry. Disabling this can be helpful + * when the top y-label and + * left x-label interfere with each other. default: true * - * @param color + * @param enabled */ - public void setZeroLineColor(int color) { - mZeroLineColor = color; + fun setDrawTopYLabelEntry(enabled: Boolean) { + this.isDrawTopYLabelEntryEnabled = enabled } - public float getZeroLineWidth() { - return mZeroLineWidth; + /** + * This method is deprecated. + * Use setAxisMinimum(...) / setAxisMaximum(...) instead. + * + * @param startAtZero + */ + @Deprecated("") + fun setStartAtZero(startAtZero: Boolean) { + if (startAtZero) axisMinimum = 0f + else resetAxisMinimum() } /** - * Sets the width of the zero line in dp + * Set this to true to draw the zero-line regardless of weather other + * grid-lines are enabled or not. Default: false * - * @param width + * @param mDrawZeroLine */ - public void setZeroLineWidth(float width) { - this.mZeroLineWidth = Utils.convertDpToPixel(width); + fun setDrawZeroLine(mDrawZeroLine: Boolean) { + this.isDrawZeroLineEnabled = mDrawZeroLine } + var zeroLineWidth: Float + get() = mZeroLineWidth + /** + * Sets the width of the zero line in dp + * + * @param width + */ + set(width) { + this.mZeroLineWidth = Utils.convertDpToPixel(width) + } + /** * This is for normal (not horizontal) charts horizontal spacing. * * @param p * @return */ - public float getRequiredWidthSpace(Paint p) { + fun getRequiredWidthSpace(p: Paint): Float { + p.setTextSize(mTextSize) - p.setTextSize(mTextSize); + val label = getLongestLabel(p) + var width = Utils.calcTextWidth(p, label).toFloat() + xOffset * 2f - String label = getLongestLabel(p); - float width = (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f; + var minWidth = this.minWidth + var maxWidth = this.maxWidth - float minWidth = getMinWidth(); - float maxWidth = getMaxWidth(); + if (minWidth > 0f) minWidth = Utils.convertDpToPixel(minWidth) - if (minWidth > 0.f) - minWidth = Utils.convertDpToPixel(minWidth); + if (maxWidth > 0f && maxWidth != Float.Companion.POSITIVE_INFINITY) maxWidth = Utils.convertDpToPixel(maxWidth) - if (maxWidth > 0.f && maxWidth != Float.POSITIVE_INFINITY) - maxWidth = Utils.convertDpToPixel(maxWidth); + width = max(minWidth, min(width, if (maxWidth > 0.0) maxWidth else width)) - width = Math.max(minWidth, Math.min(width, maxWidth > 0.0 ? maxWidth : width)); - - return width; + return width } /** @@ -367,12 +302,11 @@ public class YAxis extends AxisBase { * @param p * @return */ - public float getRequiredHeightSpace(Paint p) { - - p.setTextSize(mTextSize); + fun getRequiredHeightSpace(p: Paint): Float { + p.setTextSize(mTextSize) - String label = getLongestLabel(p); - return (float) Utils.calcTextHeight(p, label) + getYOffset() * 2f; + val label = getLongestLabel(p) + return Utils.calcTextHeight(p, label).toFloat() + yOffset * 2f } /** @@ -380,88 +314,45 @@ public class YAxis extends AxisBase { * * @return */ - public boolean needsOffset() { - if (isEnabled() && isDrawLabelsEnabled() && getLabelPosition() == YAxisLabelPosition - .OUTSIDE_CHART) - return true; - else - return false; + fun needsOffset(): Boolean { + if (isEnabled && isDrawLabelsEnabled && this.labelPosition == YAxisLabelPosition.OUTSIDE_CHART) return true + else return false } - /** - * Returns true if autoscale restriction for axis min value is enabled - */ - @Deprecated - public boolean isUseAutoScaleMinRestriction( ) { - return mUseAutoScaleRestrictionMin; - } - /** - * Sets autoscale restriction for axis min value as enabled/disabled - */ - @Deprecated - public void setUseAutoScaleMinRestriction( boolean isEnabled ) { - mUseAutoScaleRestrictionMin = isEnabled; - } - - /** - * Returns true if autoscale restriction for axis max value is enabled - */ - @Deprecated - public boolean isUseAutoScaleMaxRestriction() { - return mUseAutoScaleRestrictionMax; - } - - /** - * Sets autoscale restriction for axis max value as enabled/disabled - */ - @Deprecated - public void setUseAutoScaleMaxRestriction( boolean isEnabled ) { - mUseAutoScaleRestrictionMax = isEnabled; - } - - - @Override - public void calculate(float dataMin, float dataMax) { - - float min = dataMin; - float max = dataMax; + override fun calculate(dataMin: Float, dataMax: Float) { + var min = dataMin + var max = dataMax // Make sure max is greater than min // Discussion: https://github.com/danielgindi/Charts/pull/3650#discussion_r221409991 - if (min > max) - { - if (mCustomAxisMax && mCustomAxisMin) - { - float t = min; - min = max; - max = t; - } - else if (mCustomAxisMax) - { - min = max < 0f ? max * 1.5f : max * 0.5f; - } - else if (mCustomAxisMin) - { - max = min < 0f ? min * 0.5f : min * 1.5f; + if (min > max) { + if (isAxisMaxCustom && isAxisMinCustom) { + val t = min + min = max + max = t + } else if (isAxisMaxCustom) { + min = if (max < 0f) max * 1.5f else max * 0.5f + } else if (isAxisMinCustom) { + max = if (min < 0f) min * 0.5f else min * 1.5f } } - float range = Math.abs(max - min); + var range = abs(max - min) // in case all values are equal if (range == 0f) { - max = max + 1f; - min = min - 1f; + max = max + 1f + min = min - 1f } // recalculate - range = Math.abs(max - min); + range = abs(max - min) // calc extra spacing - this.mAxisMinimum = mCustomAxisMin ? this.mAxisMinimum : min - (range / 100f) * getSpaceBottom(); - this.mAxisMaximum = mCustomAxisMax ? this.mAxisMaximum : max + (range / 100f) * getSpaceTop(); + this.mAxisMinimum = if (isAxisMinCustom) this.mAxisMinimum else min - (range / 100f) * this.spaceBottom + this.mAxisMaximum = if (isAxisMaxCustom) this.mAxisMaximum else max + (range / 100f) * this.spaceTop - this.mAxisRange = Math.abs(this.mAxisMinimum - this.mAxisMaximum); + this.mAxisRange = abs(this.mAxisMinimum - this.mAxisMaximum) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt index 16d60f6f9c..f0bd4f7132 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt @@ -1,47 +1,29 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; - -import java.util.List; +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet /** * Data object that represents all data for the BarChart. * * @author Philipp Jahoda */ -public class BarData extends BarLineScatterCandleBubbleData { - - /** - * the width of the bars on the x-axis, in values (not pixels) - */ - private float mBarWidth = 0.85f; - - public BarData() { - super(); - } - - public BarData(IBarDataSet... dataSets) { - super(dataSets); - } - - public BarData(List dataSets) { - super(dataSets); - } - +class BarData : BarLineScatterCandleBubbleData { /** * Sets the width each bar should have on the x-axis (in values, not pixels). * Default 0.85f * - * @param mBarWidth + * @param this.barWidth */ - public void setBarWidth(float mBarWidth) { - this.mBarWidth = mBarWidth; - } + /** + * the width of the bars on the x-axis, in values (not pixels) + */ + var barWidth: Float = 0.85f - public float getBarWidth() { - return mBarWidth; - } + constructor() : super() + + constructor(vararg dataSets: IBarDataSet) : super(*dataSets) + + constructor(dataSets: MutableList) : super(dataSets) /** * Groups all BarDataSet objects this data object holds together by modifying the x-value of their entries. @@ -53,57 +35,52 @@ public class BarData extends BarLineScatterCandleBubbleData { * @param groupSpace the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f * @param barSpace the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f */ - public void groupBars(float fromX, float groupSpace, float barSpace) { - - int setCount = mDataSets.size(); + fun groupBars(fromX: Float, groupSpace: Float, barSpace: Float) { + var fromX = fromX + val setCount = dataSets.size if (setCount <= 1) { - throw new RuntimeException("BarData needs to hold at least 2 BarDataSets to allow grouping."); + throw RuntimeException("BarData needs to hold at least 2 BarDataSets to allow grouping.") } - IBarDataSet max = getMaxEntryCountSet(); - int maxEntryCount = max.getEntryCount(); - - float groupSpaceWidthHalf = groupSpace / 2f; - float barSpaceHalf = barSpace / 2f; - float barWidthHalf = mBarWidth / 2f; - - float interval = getGroupWidth(groupSpace, barSpace); - - for (int i = 0; i < maxEntryCount; i++) { + val max = maxEntryCountSet + val maxEntryCount = max!!.entryCount - float start = fromX; - fromX += groupSpaceWidthHalf; + val groupSpaceWidthHalf = groupSpace / 2f + val barSpaceHalf = barSpace / 2f + val barWidthHalf = this.barWidth / 2f - for (IBarDataSet set : mDataSets) { + val interval = getGroupWidth(groupSpace, barSpace) - fromX += barSpaceHalf; - fromX += barWidthHalf; + for (i in 0.. 0 || diff < 0) { - fromX += diff; + fromX += diff } } - notifyDataChanged(); + notifyDataChanged() } /** @@ -113,7 +90,7 @@ public class BarData extends BarLineScatterCandleBubbleData { * @param barSpace * @return */ - public float getGroupWidth(float groupSpace, float barSpace) { - return mDataSets.size() * (mBarWidth + barSpace) + groupSpace; + fun getGroupWidth(groupSpace: Float, barSpace: Float): Float { + return dataSets.size * (this.barWidth + barSpace) + groupSpace } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt index 3f2c66dbe3..6d839cdd95 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt @@ -1,94 +1,92 @@ - -package com.github.mikephil.charting.data; - -import android.graphics.Color; - -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.utils.Fill; - -import java.util.ArrayList; -import java.util.List; - -public class BarDataSet extends BarLineScatterCandleBubbleDataSet implements IBarDataSet { - +package com.github.mikephil.charting.data + +import android.graphics.Color +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.utils.Fill +import kotlin.Array +import kotlin.Boolean +import kotlin.Deprecated +import kotlin.Int +import kotlin.String +import kotlin.arrayOf + +open class BarDataSet(yVals: MutableList, label: String) : BarLineScatterCandleBubbleDataSet(yVals, label), IBarDataSet { /** * the maximum number of bars that are stacked upon each other, this value * is calculated from the Entries that are added to the DataSet */ - private int mStackSize = 1; + private var mStackSize = 1 /** * the color used for drawing the bar shadows */ - private int mBarShadowColor = Color.rgb(215, 215, 215); + private var mBarShadowColor = Color.rgb(215, 215, 215) - private float mBarBorderWidth = 0.0f; + private var mBarBorderWidth = 0.0f - private int mBarBorderColor = Color.BLACK; + private var mBarBorderColor = Color.BLACK /** * the alpha value used to draw the highlight indicator bar */ - private int mHighLightAlpha = 120; + private var mHighLightAlpha = 120 + /** + * returns the overall entry count, including counting each stack-value + * individually + * + * @return + */ /** * the overall entry count, including counting each stack-value individually */ - private int mEntryCountStacks = 0; + var entryCountStacks: Int = 0 + private set /** * array of labels used to describe the different values of the stacked bars */ - private String[] mStackLabels = new String[]{}; + private var mStackLabels: Array? = arrayOf() - protected List mFills = null; - - public BarDataSet(List yVals, String label) { - super(yVals, label); + /** + * This method is deprecated. + * Use getFills() instead. + */ + @get:Deprecated("") + var gradients: MutableList? = null + protected set - mHighLightColor = Color.rgb(0, 0, 0); + init { + highLightColor = Color.rgb(0, 0, 0) - calcStackSize(yVals); - calcEntryCountIncludingStacks(yVals); + calcStackSize(yVals) + calcEntryCountIncludingStacks(yVals) } - @Override - public DataSet copy() { - List entries = new ArrayList(); - for (int i = 0; i < mEntries.size(); i++) { - entries.add(mEntries.get(i).copy()); + override fun copy(): DataSet { + val entries: MutableList = ArrayList() + for (i in mEntries.indices) { + entries.add(mEntries[i].copy()) } - BarDataSet copied = new BarDataSet(entries, getLabel()); - copy(copied); - return copied; - } - - protected void copy(BarDataSet barDataSet) { - super.copy((BaseDataSet) barDataSet); - barDataSet.mStackSize = mStackSize; - barDataSet.mBarShadowColor = mBarShadowColor; - barDataSet.mBarBorderWidth = mBarBorderWidth; - barDataSet.mStackLabels = mStackLabels; - barDataSet.mHighLightAlpha = mHighLightAlpha; + val copied = BarDataSet(entries, label) + copy(copied) + return copied } - @Override - public List getFills() { - return mFills; + protected fun copy(barDataSet: BarDataSet) { + super.copy((barDataSet as BaseDataSet<*>?)!!) + barDataSet.mStackSize = mStackSize + barDataSet.mBarShadowColor = mBarShadowColor + barDataSet.mBarBorderWidth = mBarBorderWidth + barDataSet.mStackLabels = mStackLabels + barDataSet.mHighLightAlpha = mHighLightAlpha } - @Override - public Fill getFill(int index) { - return mFills.get(index % mFills.size()); - } + override val fills: MutableList? + get() = gradients - /** - * This method is deprecated. - * Use getFills() instead. - */ - @Deprecated - public List getGradients() { - return mFills; + override fun getFill(index: Int): Fill? { + return gradients!!.get(index % gradients!!.size) } /** @@ -97,9 +95,9 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * * @param index */ - @Deprecated - public Fill getGradient(int index) { - return getFill(index); + @Deprecated("") + fun getGradient(index: Int): Fill? { + return getFill(index) } /** @@ -108,9 +106,9 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * @param startColor * @param endColor */ - public void setGradientColor(int startColor, int endColor) { - mFills.clear(); - mFills.add(new Fill(startColor, endColor)); + fun setGradientColor(startColor: Int, endColor: Int) { + gradients!!.clear() + gradients!!.add(Fill(startColor, endColor)) } /** @@ -119,9 +117,9 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * * @param gradientColors */ - @Deprecated - public void setGradientColors(List gradientColors) { - this.mFills = gradientColors; + @Deprecated("") + fun setGradientColors(gradientColors: MutableList?) { + this.gradients = gradientColors } /** @@ -129,26 +127,22 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * * @param fills */ - public void setFills(List fills) { - this.mFills = fills; + fun setFills(fills: MutableList?) { + this.gradients = fills } /** * Calculates the total number of entries this DataSet represents, including * stacks. All values belonging to a stack are calculated separately. */ - private void calcEntryCountIncludingStacks(List yVals) { + private fun calcEntryCountIncludingStacks(yVals: MutableList) { + this.entryCountStacks = 0 - mEntryCountStacks = 0; + for (i in yVals.indices) { + val vals = yVals[i].yVals - for (int i = 0; i < yVals.size(); i++) { - - float[] vals = yVals.get(i).getYVals(); - - if (vals == null) - mEntryCountStacks++; - else - mEntryCountStacks += vals.length; + if (vals == null) this.entryCountStacks++ + else this.entryCountStacks += vals.size } } @@ -156,61 +150,35 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * calculates the maximum stacksize that occurs in the Entries array of this * DataSet */ - private void calcStackSize(List yVals) { - - for (int i = 0; i < yVals.size(); i++) { + private fun calcStackSize(yVals: MutableList) { + for (i in yVals.indices) { + val vals = yVals[i].yVals - float[] vals = yVals.get(i).getYVals(); - - if (vals != null && vals.length > mStackSize) - mStackSize = vals.length; + if (vals != null && vals.size > mStackSize) mStackSize = vals.size } } - @Override - protected void calcMinMax(BarEntry e) { - - if (e != null && !Float.isNaN(e.getY())) { - - if (e.getYVals() == null) { + override fun calcMinMax(e: BarEntry) { + if (!e.y.isNaN()) { + if (e.yVals == null) { + if (e.y < mYMin) mYMin = e.y - if (e.getY() < mYMin) - mYMin = e.getY(); - - if (e.getY() > mYMax) - mYMax = e.getY(); + if (e.y > mYMax) mYMax = e.y } else { + if (-e.negativeSum < mYMin) mYMin = -e.negativeSum - if (-e.getNegativeSum() < mYMin) - mYMin = -e.getNegativeSum(); - - if (e.getPositiveSum() > mYMax) - mYMax = e.getPositiveSum(); + if (e.positiveSum > mYMax) mYMax = e.positiveSum } - calcMinMaxX(e); + calcMinMaxX(e) } } - @Override - public int getStackSize() { - return mStackSize; - } + override val stackSize: Int + get() = mStackSize - @Override - public boolean isStacked() { - return mStackSize > 1 ? true : false; - } - - /** - * returns the overall entry count, including counting each stack-value - * individually - * - * @return - */ - public int getEntryCountStacks() { - return mEntryCountStacks; - } + override val isStacked: Boolean + get() = mStackSize > 1 /** * Sets the color used for drawing the bar-shadows. The bar shadows is a @@ -219,14 +187,12 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * * @param color */ - public void setBarShadowColor(int color) { - mBarShadowColor = color; + fun setBarShadowColor(color: Int) { + mBarShadowColor = color } - @Override - public int getBarShadowColor() { - return mBarShadowColor; - } + override val barShadowColor: Int + get() = mBarShadowColor /** * Sets the width used for drawing borders around the bars. @@ -234,8 +200,8 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * * @return */ - public void setBarBorderWidth(float width) { - mBarBorderWidth = width; + fun setBarBorderWidth(width: Float) { + mBarBorderWidth = width } /** @@ -244,18 +210,16 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * * @return */ - @Override - public float getBarBorderWidth() { - return mBarBorderWidth; - } + override val barBorderWidth: Float + get() = mBarBorderWidth /** * Sets the color drawing borders around the bars. * * @return */ - public void setBarBorderColor(int color) { - mBarBorderColor = color; + fun setBarBorderColor(color: Int) { + mBarBorderColor = color } /** @@ -263,10 +227,8 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * * @return */ - @Override - public int getBarBorderColor() { - return mBarBorderColor; - } + override val barBorderColor: Int + get() = mBarBorderColor /** * Set the alpha value (transparency) that is used for drawing the highlight @@ -274,26 +236,22 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet impl * * @param alpha */ - public void setHighLightAlpha(int alpha) { - mHighLightAlpha = alpha; + fun setHighLightAlpha(alpha: Int) { + mHighLightAlpha = alpha } - @Override - public int getHighLightAlpha() { - return mHighLightAlpha; - } + override val highLightAlpha: Int + get() = mHighLightAlpha /** * Sets labels for different values of bar-stacks, in case there are one. * * @param labels */ - public void setStackLabels(String[] labels) { - mStackLabels = labels; + fun setStackLabels(labels: Array?) { + mStackLabels = labels } - @Override - public String[] getStackLabels() { - return mStackLabels; - } + override val stackLabels: Array? + get() = mStackLabels } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt index 365ef51a2d..043cabdc0f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt @@ -1,9 +1,9 @@ -package com.github.mikephil.charting.data; +package com.github.mikephil.charting.data -import android.annotation.SuppressLint; -import android.graphics.drawable.Drawable; - -import com.github.mikephil.charting.highlight.Range; +import android.annotation.SuppressLint +import android.graphics.drawable.Drawable +import com.github.mikephil.charting.highlight.Range +import kotlin.math.abs /** * Entry class for the BarChart. (especially stacked bars) @@ -11,27 +11,51 @@ import com.github.mikephil.charting.highlight.Range; * @author Philipp Jahoda */ @SuppressLint("ParcelCreator") -public class BarEntry extends Entry { - +class BarEntry : Entry { + /** + * Returns the stacked values this BarEntry represents, or null, if only a single value is represented (then, use + * getY()). + * + * @return + */ /** * the values the stacked barchart holds */ - private float[] mYVals; + var yVals: FloatArray? = null + private set + /** + * Returns the ranges of the individual stack-entries. Will return null if this entry is not stacked. + * + * @return + */ /** * the ranges for the individual stack values - automatically calculated */ - private Range[] mRanges; + var ranges: Array = arrayOf() + private set + /** + * Returns the sum of all negative values this entry (if stacked) contains. (this is a positive number) + * + * @return + */ /** * the sum of all negative values this entry (if stacked) contains */ - private float mNegativeSum; + var negativeSum: Float = 0f + private set + /** + * Reuturns the sum of all positive values this entry (if stacked) contains. + * + * @return + */ /** * the sum of all positive values this entry (if stacked) contains */ - private float mPositiveSum; + var positiveSum: Float = 0f + private set /** * Constructor for normal bars (not stacked). @@ -39,9 +63,7 @@ public class BarEntry extends Entry { * @param x * @param y */ - public BarEntry(float x, float y) { - super(x, y); - } + constructor(x: Float, y: Float) : super(x, y) /** * Constructor for normal bars (not stacked). @@ -50,9 +72,7 @@ public class BarEntry extends Entry { * @param y * @param data - Spot for additional data this Entry represents. */ - public BarEntry(float x, float y, Object data) { - super(x, y, data); - } + constructor(x: Float, y: Float, data: Any?) : super(x, y, data) /** * Constructor for normal bars (not stacked). @@ -61,9 +81,7 @@ public class BarEntry extends Entry { * @param y * @param icon - icon image */ - public BarEntry(float x, float y, Drawable icon) { - super(x, y, icon); - } + constructor(x: Float, y: Float, icon: Drawable?) : super(x, y, icon) /** * Constructor for normal bars (not stacked). @@ -73,9 +91,7 @@ public class BarEntry extends Entry { * @param icon - icon image * @param data - Spot for additional data this Entry represents. */ - public BarEntry(float x, float y, Drawable icon, Object data) { - super(x, y, icon, data); - } + constructor(x: Float, y: Float, icon: Drawable?, data: Any?) : super(x, y, icon, data) /** * Constructor for stacked bar entries. One data object for whole stack @@ -83,12 +99,10 @@ public class BarEntry extends Entry { * @param x * @param vals - the stack values, use at least 2 */ - public BarEntry(float x, float[] vals) { - super(x, calcSum(vals)); - - this.mYVals = vals; - calcPosNegSum(); - calcRanges(); + constructor(x: Float, vals: FloatArray?) : super(x, calcSum(vals)) { + this.yVals = vals + calcPosNegSum() + calcRanges() } /** @@ -98,12 +112,10 @@ public class BarEntry extends Entry { * @param vals - the stack values, use at least 2 * @param data - Spot for additional data this Entry represents. */ - public BarEntry(float x, float[] vals, Object data) { - super(x, calcSum(vals), data); - - this.mYVals = vals; - calcPosNegSum(); - calcRanges(); + constructor(x: Float, vals: FloatArray?, data: Any?) : super(x, calcSum(vals), data) { + this.yVals = vals + calcPosNegSum() + calcRanges() } /** @@ -113,12 +125,10 @@ public class BarEntry extends Entry { * @param vals - the stack values, use at least 2 * @param icon - icon image */ - public BarEntry(float x, float[] vals, Drawable icon) { - super(x, calcSum(vals), icon); - - this.mYVals = vals; - calcPosNegSum(); - calcRanges(); + constructor(x: Float, vals: FloatArray?, icon: Drawable?) : super(x, calcSum(vals), icon) { + this.yVals = vals + calcPosNegSum() + calcRanges() } /** @@ -129,32 +139,19 @@ public class BarEntry extends Entry { * @param icon - icon image * @param data - Spot for additional data this Entry represents. */ - public BarEntry(float x, float[] vals, Drawable icon, Object data) { - super(x, calcSum(vals), icon, data); - - this.mYVals = vals; - calcPosNegSum(); - calcRanges(); + constructor(x: Float, vals: FloatArray?, icon: Drawable?, data: Any?) : super(x, calcSum(vals), icon, data) { + this.yVals = vals + calcPosNegSum() + calcRanges() } /** * Returns an exact copy of the BarEntry. */ - public BarEntry copy() { - - BarEntry copied = new BarEntry(getX(), getY(), getData()); - copied.setVals(mYVals); - return copied; - } - - /** - * Returns the stacked values this BarEntry represents, or null, if only a single value is represented (then, use - * getY()). - * - * @return - */ - public float[] getYVals() { - return mYVals; + public override fun copy(): BarEntry { + val copied = BarEntry(x, y, data) + copied.setVals(this.yVals) + return copied } /** @@ -162,147 +159,100 @@ public class BarEntry extends Entry { * * @param vals */ - public void setVals(float[] vals) { - setY(calcSum(vals)); - mYVals = vals; - calcPosNegSum(); - calcRanges(); - } - - /** - * Returns the value of this BarEntry. If the entry is stacked, it returns the positive sum of all values. - * - * @return - */ - @Override - public float getY() { - return super.getY(); + fun setVals(vals: FloatArray?) { + y = calcSum(vals) + this.yVals = vals + calcPosNegSum() + calcRanges() } - /** - * Returns the ranges of the individual stack-entries. Will return null if this entry is not stacked. - * - * @return - */ - public Range[] getRanges() { - return mRanges; - } - - /** - * Returns true if this BarEntry is stacked (has a values array), false if not. - * - * @return - */ - public boolean isStacked() { - return mYVals != null; - } + val isStacked: Boolean + /** + * Returns true if this BarEntry is stacked (has a values array), false if not. + * + * @return + */ + get() = this.yVals != null /** * Use `getSumBelow(stackIndex)` instead. */ - @Deprecated - public float getBelowSum(int stackIndex) { - return getSumBelow(stackIndex); + @Deprecated("") + fun getBelowSum(stackIndex: Int): Float { + return getSumBelow(stackIndex) } - public float getSumBelow(int stackIndex) { - - if (mYVals == null) - return 0; + fun getSumBelow(stackIndex: Int): Float { + if (this.yVals == null) return 0f - float remainder = 0f; - int index = mYVals.length - 1; + var remainder = 0f + var index = yVals!!.size - 1 while (index > stackIndex && index >= 0) { - remainder += mYVals[index]; - index--; + remainder += this.yVals!![index] + index-- } - return remainder; + return remainder } - /** - * Reuturns the sum of all positive values this entry (if stacked) contains. - * - * @return - */ - public float getPositiveSum() { - return mPositiveSum; - } - - /** - * Returns the sum of all negative values this entry (if stacked) contains. (this is a positive number) - * - * @return - */ - public float getNegativeSum() { - return mNegativeSum; - } - - private void calcPosNegSum() { - - if (mYVals == null) { - mNegativeSum = 0; - mPositiveSum = 0; - return; + private fun calcPosNegSum() { + if (this.yVals == null) { + this.negativeSum = 0f + this.positiveSum = 0f + return } - float sumNeg = 0f; - float sumPos = 0f; + var sumNeg = 0f + var sumPos = 0f - for (float f : mYVals) { - if (f <= 0f) - sumNeg += Math.abs(f); - else - sumPos += f; + for (f in this.yVals) { + if (f <= 0f) sumNeg += abs(f) + else sumPos += f } - mNegativeSum = sumNeg; - mPositiveSum = sumPos; + this.negativeSum = sumNeg + this.positiveSum = sumPos } - /** - * Calculates the sum across all values of the given stack. - * - * @param vals - * @return - */ - private static float calcSum(float[] vals) { + protected fun calcRanges() { + val values = this.yVals - if (vals == null) - return 0f; + if (values == null || values.isEmpty()) return - float sum = 0f; + var negRemain = -this.negativeSum + var posRemain = 0f - for (float f : vals) - sum += f; + this.ranges = Array(values.size) { i -> + val value = values[i] - return sum; + if (value < 0) { + Range(negRemain, negRemain - value).also { + negRemain -= value + } + } else { + Range(posRemain, posRemain + value).also { + posRemain += value + } + } + } } - protected void calcRanges() { - - float[] values = getYVals(); - - if (values == null || values.length == 0) - return; + companion object { + /** + * Calculates the sum across all values of the given stack. + * + * @param vals + * @return + */ + private fun calcSum(vals: FloatArray?): Float { + if (vals == null) return 0f - mRanges = new Range[values.length]; + var sum = 0f - float negRemain = -getNegativeSum(); - float posRemain = 0f; + for (f in vals) sum += f - for (int i = 0; i < mRanges.length; i++) { - - float value = values[i]; - - if (value < 0) { - mRanges[i] = new Range(negRemain, negRemain - value); - negRemain -= value; - } else { - mRanges[i] = new Range(posRemain, posRemain + value); - posRemain += value; - } + return sum } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleData.kt index c09eadec9b..f6cc95ba1b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleData.kt @@ -1,27 +1,17 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet; - -import java.util.List; +import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet /** * Baseclass for all Line, Bar, Scatter, Candle and Bubble data. - * + * * @author Philipp Jahoda */ -public abstract class BarLineScatterCandleBubbleData> - extends ChartData { - - public BarLineScatterCandleBubbleData() { - super(); - } +abstract class BarLineScatterCandleBubbleData> + : ChartData { + constructor() : super() - public BarLineScatterCandleBubbleData(T... sets) { - super(sets); - } + constructor(vararg sets: T) : super(*sets) - public BarLineScatterCandleBubbleData(List sets) { - super(sets); - } + constructor(sets: MutableList) : super(sets) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt index fc8cc36e06..51dedbdc00 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt @@ -1,30 +1,15 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import android.graphics.Color; - -import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet; - -import java.util.List; +import android.graphics.Color +import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet /** * Baseclass of all DataSets for Bar-, Line-, Scatter- and CandleStickChart. * * @author Philipp Jahoda */ -public abstract class BarLineScatterCandleBubbleDataSet - extends DataSet - implements IBarLineScatterCandleBubbleDataSet { - - /** - * default highlight color - */ - protected int mHighLightColor = Color.rgb(255, 187, 115); - - public BarLineScatterCandleBubbleDataSet(List yVals, String label) { - super(yVals, label); - } - +abstract class BarLineScatterCandleBubbleDataSet + (yVals: MutableList, label: String) : DataSet(yVals, label), IBarLineScatterCandleBubbleDataSet { /** * Sets the color that is used for drawing the highlight indicators. Dont * forget to resolve the color using getResources().getColor(...) or @@ -32,17 +17,13 @@ public abstract class BarLineScatterCandleBubbleDataSet * * @param color */ - public void setHighLightColor(int color) { - mHighLightColor = color; - } - - @Override - public int getHighLightColor() { - return mHighLightColor; - } + /** + * default highlight color + */ + override var highLightColor: Int = Color.rgb(255, 187, 115) - protected void copy(BarLineScatterCandleBubbleDataSet barLineScatterCandleBubbleDataSet) { - super.copy((BaseDataSet) barLineScatterCandleBubbleDataSet); - barLineScatterCandleBubbleDataSet.mHighLightColor = mHighLightColor; + protected fun copy(barLineScatterCandleBubbleDataSet: BarLineScatterCandleBubbleDataSet<*>) { + super.copy((barLineScatterCandleBubbleDataSet as BaseDataSet<*>?)!!) + barLineScatterCandleBubbleDataSet.highLightColor = this.highLightColor } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt index dd37ddb55c..d431e7a6ef 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt @@ -17,7 +17,7 @@ import com.github.mikephil.charting.utils.Utils * This is the base dataset of all DataSets. It's purpose is to implement critical methods * provided by the IDataSet interface. */ -abstract class BaseDataSet() : IDataSet { +abstract class BaseDataSet() : IDataSet { /** * List representing all colors that are used for this DataSet */ @@ -28,62 +28,11 @@ abstract class BaseDataSet() : IDataSet { */ protected var mValueColors: MutableList - /** - * label that describes the DataSet or the data the DataSet represents - */ - private var mLabel = "DataSet" - /** * this specifies which axis this DataSet should be plotted against */ protected var mAxisDependency: AxisDependency = AxisDependency.LEFT - /** - * if true, value highlightning is enabled - */ - protected var mHighlightEnabled: Boolean = true - - /** - * custom formatter that is used instead of the auto-formatter if set - */ - @Transient - protected var mValueFormatter: IValueFormatter? = null - - /** - * the typeface used for the value text - */ - protected var mValueTypeface: Typeface? = null - - private var mForm = LegendForm.DEFAULT - private var mFormSize = Float.NaN - private var mFormLineWidth = Float.NaN - private var mFormLineDashEffect: DashPathEffect? = null - - /** - * if true, y-values are drawn on the chart - */ - protected var mDrawValues: Boolean = true - - /** - * if true, y-icons are drawn on the chart - */ - protected var mDrawIcons: Boolean = true - - /** - * the offset for drawing icons (in dp) - */ - protected var mIconsOffset: MPPointF = MPPointF() - - /** - * the size of the value-text labels - */ - protected var mValueTextSize: Float = 17f - - /** - * flag that indicates if the DataSet is visible or not - */ - protected var mVisible: Boolean = true - /** * Default constructor. */ @@ -102,7 +51,7 @@ abstract class BaseDataSet() : IDataSet { * @param label */ constructor(label: String) : this() { - this.mLabel = label + this.label = label } /** @@ -112,16 +61,14 @@ abstract class BaseDataSet() : IDataSet { calcMinMax() } - override fun getColors(): List { - return mColors - } + override val colors: MutableList + get() = mColors val valueColors: List get() = mValueColors - override fun getColor(): Int { - return mColors[0] - } + override val color: Int + get() = mColors[0] override fun getColor(index: Int): Int { return mColors[index % mColors.size] @@ -227,142 +174,52 @@ abstract class BaseDataSet() : IDataSet { /** * ###### ###### OTHER STYLING RELATED METHODS ##### ###### */ - override fun setLabel(label: String) { - mLabel = label - } - - override fun getLabel(): String { - return mLabel - } - - override fun setHighlightEnabled(enabled: Boolean) { - mHighlightEnabled = enabled - } + override var label: String = "DataSet" - override fun isHighlightEnabled(): Boolean { - return mHighlightEnabled - } - - override fun setValueFormatter(f: IValueFormatter?) { - if (f == null) return - else mValueFormatter = f - } - - override fun getValueFormatter(): IValueFormatter { - if (needsFormatter()) return Utils.getDefaultValueFormatter() - return mValueFormatter!! - } + override var isHighlightEnabled: Boolean = true - override fun needsFormatter(): Boolean { - return mValueFormatter == null - } + @Transient + override var valueFormatter: IValueFormatter = Utils.defaultValueFormatter - override fun setValueTextColor(color: Int) { - mValueColors.clear() - mValueColors.add(color) - } + override var valueTextColor: Int + get() = mValueColors[0] + set(value) { + mValueColors.clear() + mValueColors.add(value) + } override fun setValueTextColors(colors: MutableList) { mValueColors = colors } - override fun setValueTypeface(tf: Typeface?) { - mValueTypeface = tf - } + override var valueTypeface: Typeface? = null - override fun setValueTextSize(size: Float) { - mValueTextSize = Utils.convertDpToPixel(size) - } - - override fun getValueTextColor(): Int { - return mValueColors[0] - } + override var valueTextSize: Float = 17f + set(value) { + field = Utils.convertDpToPixel(value) + } override fun getValueTextColor(index: Int): Int { return mValueColors[index % mValueColors.size] } - override fun getValueTypeface(): Typeface? { - return mValueTypeface - } + override var form: LegendForm? = LegendForm.DEFAULT - override fun getValueTextSize(): Float { - return mValueTextSize - } + override var formSize: Float = Float.NaN - fun setForm(form: LegendForm) { - mForm = form - } - - override fun getForm(): LegendForm { - return mForm - } - - fun setFormSize(formSize: Float) { - mFormSize = formSize - } + override var formLineWidth: Float = Float.NaN - override fun getFormSize(): Float { - return mFormSize - } + override var formLineDashEffect: DashPathEffect? = null - fun setFormLineWidth(formLineWidth: Float) { - mFormLineWidth = formLineWidth - } + override var isDrawValuesEnabled: Boolean = true - override fun getFormLineWidth(): Float { - return mFormLineWidth - } + override var isDrawIconsEnabled: Boolean = true - fun setFormLineDashEffect(dashPathEffect: DashPathEffect?) { - mFormLineDashEffect = dashPathEffect - } + override var iconsOffset: MPPointF = MPPointF() - override fun getFormLineDashEffect(): DashPathEffect? { - return mFormLineDashEffect - } - - override fun setDrawValues(enabled: Boolean) { - this.mDrawValues = enabled - } - - override fun isDrawValuesEnabled(): Boolean { - return mDrawValues - } - - override fun setDrawIcons(enabled: Boolean) { - mDrawIcons = enabled - } - - override fun isDrawIconsEnabled(): Boolean { - return mDrawIcons - } - - override fun setIconsOffset(offsetDp: MPPointF) { - mIconsOffset.x = offsetDp.x - mIconsOffset.y = offsetDp.y - } - - override fun getIconsOffset(): MPPointF { - return mIconsOffset - } - - override fun setVisible(visible: Boolean) { - mVisible = visible - } - - override fun isVisible(): Boolean { - return mVisible - } - - override fun getAxisDependency(): AxisDependency { - return mAxisDependency - } - - override fun setAxisDependency(dependency: AxisDependency) { - mAxisDependency = dependency - } + override var isVisible: Boolean = true + override var axisDependency: AxisDependency? = AxisDependency.LEFT /** * ###### ###### DATA RELATED METHODS ###### ###### @@ -399,9 +256,9 @@ abstract class BaseDataSet() : IDataSet { return removeEntry(e) } - override fun contains(e: T): Boolean { + override fun contains(entry: T?): Boolean { for (i in 0..() : IDataSet { protected fun copy(baseDataSet: BaseDataSet<*>) { baseDataSet.mAxisDependency = mAxisDependency baseDataSet.mColors = mColors - baseDataSet.mDrawIcons = mDrawIcons - baseDataSet.mDrawValues = mDrawValues - baseDataSet.mForm = mForm - baseDataSet.mFormLineDashEffect = mFormLineDashEffect - baseDataSet.mFormLineWidth = mFormLineWidth - baseDataSet.mFormSize = mFormSize - baseDataSet.mHighlightEnabled = mHighlightEnabled - baseDataSet.mIconsOffset = mIconsOffset + baseDataSet.isDrawIconsEnabled = isDrawIconsEnabled + baseDataSet.isDrawValuesEnabled = isDrawValuesEnabled + baseDataSet.form = form + baseDataSet.formLineDashEffect = formLineDashEffect + baseDataSet.formLineWidth = formLineWidth + baseDataSet.formSize = formSize + baseDataSet.isHighlightEnabled = isHighlightEnabled + baseDataSet.iconsOffset = iconsOffset baseDataSet.mValueColors = mValueColors - baseDataSet.mValueFormatter = mValueFormatter - baseDataSet.mValueTextSize = mValueTextSize - baseDataSet.mVisible = mVisible + baseDataSet.valueFormatter = valueFormatter + baseDataSet.valueTextSize = valueTextSize + baseDataSet.isVisible = isVisible } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleData.kt index 8fb72f17c5..7e6eae80fe 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleData.kt @@ -1,34 +1,24 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; +import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet -import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet; +class BubbleData : BarLineScatterCandleBubbleData { + constructor() : super() -import java.util.List; + constructor(vararg dataSets: IBubbleDataSet) : super(*dataSets) -public class BubbleData extends BarLineScatterCandleBubbleData { - - public BubbleData() { - super(); - } - - public BubbleData(IBubbleDataSet... dataSets) { - super(dataSets); - } - - public BubbleData(List dataSets) { - super(dataSets); - } + constructor(dataSets: MutableList) : super(dataSets) /** * Sets the width of the circle that surrounds the bubble when highlighted * for all DataSet objects this data object contains, in dp. - * + * * @param width */ - public void setHighlightCircleWidth(float width) { - for (IBubbleDataSet set : mDataSets) { - set.setHighlightCircleWidth(width); + fun setHighlightCircleWidth(width: Float) { + for (set in dataSets) { + set?.highlightCircleWidth = width } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt index 9ef87fb2d5..c79e7001ec 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt @@ -1,71 +1,46 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; +import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet +import com.github.mikephil.charting.utils.Utils -import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet; -import com.github.mikephil.charting.utils.Utils; +open class BubbleDataSet(yVals: MutableList, label: String) : BarLineScatterCandleBubbleDataSet(yVals, label), IBubbleDataSet { + protected var mMaxSize: Float = 0f + protected var mNormalizeSize: Boolean = true -import java.util.ArrayList; -import java.util.List; + override var highlightCircleWidth: Float = 2.5f -public class BubbleDataSet extends BarLineScatterCandleBubbleDataSet implements IBubbleDataSet { + override fun calcMinMax(e: BubbleEntry) { + super.calcMinMax(e) - protected float mMaxSize; - protected boolean mNormalizeSize = true; - - private float mHighlightCircleWidth = 2.5f; - - public BubbleDataSet(List yVals, String label) { - super(yVals, label); - } - - @Override - public void setHighlightCircleWidth(float width) { - mHighlightCircleWidth = Utils.convertDpToPixel(width); - } - - @Override - public float getHighlightCircleWidth() { - return mHighlightCircleWidth; - } - - @Override - protected void calcMinMax(BubbleEntry e) { - super.calcMinMax(e); - - final float size = e.getSize(); + val size = e.size if (size > mMaxSize) { - mMaxSize = size; + mMaxSize = size } } - @Override - public DataSet copy() { - List entries = new ArrayList(); - for (int i = 0; i < mEntries.size(); i++) { - entries.add(mEntries.get(i).copy()); + override fun copy(): DataSet { + val entries: MutableList = ArrayList() + for (i in mEntries.indices) { + entries.add(mEntries[i].copy()) } - BubbleDataSet copied = new BubbleDataSet(entries, getLabel()); - copy(copied); - return copied; + val copied = BubbleDataSet(entries, label) + copy(copied) + return copied } - protected void copy(BubbleDataSet bubbleDataSet) { - bubbleDataSet.mHighlightCircleWidth = mHighlightCircleWidth; - bubbleDataSet.mNormalizeSize = mNormalizeSize; + protected fun copy(bubbleDataSet: BubbleDataSet) { + bubbleDataSet.highlightCircleWidth = highlightCircleWidth + bubbleDataSet.mNormalizeSize = mNormalizeSize } - @Override - public float getMaxSize() { - return mMaxSize; - } + override val maxSize: Float + get() = mMaxSize - @Override - public boolean isNormalizeSizeEnabled() { - return mNormalizeSize; - } + override val isNormalizeSizeEnabled: Boolean + get() = mNormalizeSize - public void setNormalizeSizeEnabled(boolean normalizeSize) { - mNormalizeSize = normalizeSize; + fun setNormalizeSizeEnabled(normalizeSize: Boolean) { + mNormalizeSize = normalizeSize } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt index 35d8330aaa..4c26e571dc 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt @@ -1,8 +1,7 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import android.annotation.SuppressLint; -import android.graphics.drawable.Drawable; +import android.annotation.SuppressLint +import android.graphics.drawable.Drawable /** * Subclass of Entry that holds a value for one entry in a BubbleChart. Bubble @@ -12,10 +11,14 @@ import android.graphics.drawable.Drawable; * @author Philipp Jahoda */ @SuppressLint("ParcelCreator") -public class BubbleEntry extends Entry { - - /** size value */ - private float mSize = 0f; +class BubbleEntry : Entry { + /** + * Returns the size of this entry (the size of the bubble). + * + * @return + */ + /** size value */ + var size: Float = 0f /** * Constructor. @@ -24,9 +27,8 @@ public class BubbleEntry extends Entry { * @param y The value on the y-axis. * @param size The size of the bubble. */ - public BubbleEntry(float x, float y, float size) { - super(x, y); - this.mSize = size; + constructor(x: Float, y: Float, size: Float) : super(x, y) { + this.size = size } /** @@ -37,9 +39,8 @@ public class BubbleEntry extends Entry { * @param size The size of the bubble. * @param data Spot for additional data this Entry represents. */ - public BubbleEntry(float x, float y, float size, Object data) { - super(x, y, data); - this.mSize = size; + constructor(x: Float, y: Float, size: Float, data: Any?) : super(x, y, data) { + this.size = size } /** @@ -50,9 +51,8 @@ public class BubbleEntry extends Entry { * @param size The size of the bubble. * @param icon Icon image */ - public BubbleEntry(float x, float y, float size, Drawable icon) { - super(x, y, icon); - this.mSize = size; + constructor(x: Float, y: Float, size: Float, icon: Drawable?) : super(x, y, icon) { + this.size = size } /** @@ -64,28 +64,12 @@ public class BubbleEntry extends Entry { * @param icon Icon image * @param data Spot for additional data this Entry represents. */ - public BubbleEntry(float x, float y, float size, Drawable icon, Object data) { - super(x, y, icon, data); - this.mSize = size; - } - - public BubbleEntry copy() { - - BubbleEntry c = new BubbleEntry(getX(), getY(), mSize, getData()); - return c; - } - - /** - * Returns the size of this entry (the size of the bubble). - * - * @return - */ - public float getSize() { - return mSize; + constructor(x: Float, y: Float, size: Float, icon: Drawable?, data: Any?) : super(x, y, icon, data) { + this.size = size } - public void setSize(float size) { - this.mSize = size; + public override fun copy(): BubbleEntry { + val c = BubbleEntry(x, y, this.size, data) + return c } - } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.kt index 1e90db2ba5..ce6197da3d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleData.kt @@ -1,21 +1,11 @@ -package com.github.mikephil.charting.data; +package com.github.mikephil.charting.data -import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet; +import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet -import java.util.ArrayList; -import java.util.List; +class CandleData : BarLineScatterCandleBubbleData { + constructor() : super() -public class CandleData extends BarLineScatterCandleBubbleData { + constructor(dataSets: MutableList) : super(dataSets) - public CandleData() { - super(); - } - - public CandleData(List dataSets) { - super(dataSets); - } - - public CandleData(ICandleDataSet... dataSets) { - super(dataSets); - } + constructor(vararg dataSets: ICandleDataSet) : super(*dataSets) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt index a9a016bcad..2919121340 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt @@ -1,134 +1,114 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import android.graphics.Paint; - -import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.Utils; - -import java.util.ArrayList; -import java.util.List; +import android.graphics.Paint +import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.Utils /** * DataSet for the CandleStickChart. * * @author Philipp Jahoda */ -public class CandleDataSet extends LineScatterCandleRadarDataSet implements ICandleDataSet { - +class CandleDataSet(yVals: MutableList, label: String) : LineScatterCandleRadarDataSet(yVals, label), ICandleDataSet { /** * the width of the shadow of the candle */ - private float mShadowWidth = 3f; + private var mShadowWidth = 3f /** * should the candle bars show? * when false, only "ticks" will show - *

+ * + * * - default: true */ - private boolean mShowCandleBar = true; + private var mShowCandleBar = true /** * the space between the candle entries, default 0.1f (10%) */ - private float mBarSpace = 0.1f; + private var mBarSpace = 0.1f /** * use candle color for the shadow */ - private boolean mShadowColorSameAsCandle = false; + private var mShadowColorSameAsCandle = false /** * paint style when open < close * increasing candlesticks are traditionally hollow */ - protected Paint.Style mIncreasingPaintStyle = Paint.Style.STROKE; + protected var mIncreasingPaintStyle: Paint.Style? = Paint.Style.STROKE /** * paint style when open > close * descreasing candlesticks are traditionally filled */ - protected Paint.Style mDecreasingPaintStyle = Paint.Style.FILL; + protected var mDecreasingPaintStyle: Paint.Style? = Paint.Style.FILL /** * color for open == close */ - protected int mNeutralColor = ColorTemplate.COLOR_SKIP; + protected var mNeutralColor: Int = ColorTemplate.COLOR_SKIP /** * color for open < close */ - protected int mIncreasingColor = ColorTemplate.COLOR_SKIP; + protected var mIncreasingColor: Int = ColorTemplate.COLOR_SKIP /** * color for open > close */ - protected int mDecreasingColor = ColorTemplate.COLOR_SKIP; + protected var mDecreasingColor: Int = ColorTemplate.COLOR_SKIP /** * shadow line color, set -1 for backward compatibility and uses default * color */ - protected int mShadowColor = ColorTemplate.COLOR_SKIP; + protected var mShadowColor: Int = ColorTemplate.COLOR_SKIP - public CandleDataSet(List yVals, String label) { - super(yVals, label); - } - - @Override - public DataSet copy() { - List entries = new ArrayList(); - for (int i = 0; i < mEntries.size(); i++) { - entries.add(mEntries.get(i).copy()); + override fun copy(): DataSet { + val entries: MutableList = ArrayList() + for (i in mEntries.indices) { + entries.add(mEntries[i].copy()) } - CandleDataSet copied = new CandleDataSet(entries, getLabel()); - copy(copied); - return copied; + val copied = CandleDataSet(entries, label) + copy(copied) + return copied } - protected void copy(CandleDataSet candleDataSet) { - super.copy((BaseDataSet) candleDataSet); - candleDataSet.mShadowWidth = mShadowWidth; - candleDataSet.mShowCandleBar = mShowCandleBar; - candleDataSet.mBarSpace = mBarSpace; - candleDataSet.mShadowColorSameAsCandle = mShadowColorSameAsCandle; - candleDataSet.mHighLightColor = mHighLightColor; - candleDataSet.mIncreasingPaintStyle = mIncreasingPaintStyle; - candleDataSet.mDecreasingPaintStyle = mDecreasingPaintStyle; - candleDataSet.mNeutralColor = mNeutralColor; - candleDataSet.mIncreasingColor = mIncreasingColor; - candleDataSet.mDecreasingColor = mDecreasingColor; - candleDataSet.mShadowColor = mShadowColor; + protected fun copy(candleDataSet: CandleDataSet) { + super.copy((candleDataSet as BaseDataSet<*>?)!!) + candleDataSet.mShadowWidth = mShadowWidth + candleDataSet.mShowCandleBar = mShowCandleBar + candleDataSet.mBarSpace = mBarSpace + candleDataSet.mShadowColorSameAsCandle = mShadowColorSameAsCandle + candleDataSet.highLightColor = highLightColor + candleDataSet.mIncreasingPaintStyle = mIncreasingPaintStyle + candleDataSet.mDecreasingPaintStyle = mDecreasingPaintStyle + candleDataSet.mNeutralColor = mNeutralColor + candleDataSet.mIncreasingColor = mIncreasingColor + candleDataSet.mDecreasingColor = mDecreasingColor + candleDataSet.mShadowColor = mShadowColor } - @Override - protected void calcMinMax(CandleEntry e) { - - if (e.getLow() < mYMin) - mYMin = e.getLow(); + override fun calcMinMax(e: CandleEntry) { + if (e.low < mYMin) mYMin = e.low - if (e.getHigh() > mYMax) - mYMax = e.getHigh(); + if (e.high > mYMax) mYMax = e.high - calcMinMaxX(e); + calcMinMaxX(e) } - @Override - protected void calcMinMaxY(CandleEntry e) { + override fun calcMinMaxY(e: CandleEntry) { + if (e.high < mYMin) mYMin = e.high - if (e.getHigh() < mYMin) - mYMin = e.getHigh(); + if (e.high > mYMax) mYMax = e.high - if (e.getHigh() > mYMax) - mYMax = e.getHigh(); + if (e.low < mYMin) mYMin = e.low - if (e.getLow() < mYMin) - mYMin = e.getLow(); - - if (e.getLow() > mYMax) - mYMax = e.getLow(); + if (e.low > mYMax) mYMax = e.low } /** @@ -137,48 +117,40 @@ public class CandleDataSet extends LineScatterCandleRadarDataSet im * * @param space */ - public void setBarSpace(float space) { - - if (space < 0f) - space = 0f; - if (space > 0.45f) - space = 0.45f; + fun setBarSpace(space: Float) { + var space = space + if (space < 0f) space = 0f + if (space > 0.45f) space = 0.45f - mBarSpace = space; + mBarSpace = space } - @Override - public float getBarSpace() { - return mBarSpace; - } + override val barSpace: Float + get() = mBarSpace /** * Sets the width of the candle-shadow-line in pixels. Default 3f. * * @param width */ - public void setShadowWidth(float width) { - mShadowWidth = Utils.convertDpToPixel(width); + fun setShadowWidth(width: Float) { + mShadowWidth = Utils.convertDpToPixel(width) } - @Override - public float getShadowWidth() { - return mShadowWidth; - } + override val shadowWidth: Float + get() = mShadowWidth /** * Sets whether the candle bars should show? * * @param showCandleBar */ - public void setShowCandleBar(boolean showCandleBar) { - mShowCandleBar = showCandleBar; + fun setShowCandleBar(showCandleBar: Boolean) { + mShowCandleBar = showCandleBar } - @Override - public boolean getShowCandleBar() { - return mShowCandleBar; - } + override val showCandleBar: Boolean + get() = mShowCandleBar // TODO /** @@ -189,23 +161,19 @@ public class CandleDataSet extends LineScatterCandleRadarDataSet im * * @author Mesrop */ - - /** BELOW THIS COLOR HANDLING */ - + /** BELOW THIS COLOR HANDLING */ /** * Sets the one and ONLY color that should be used for this DataSet when * open == close. * * @param color */ - public void setNeutralColor(int color) { - mNeutralColor = color; + fun setNeutralColor(color: Int) { + mNeutralColor = color } - @Override - public int getNeutralColor() { - return mNeutralColor; - } + override val neutralColor: Int + get() = mNeutralColor /** * Sets the one and ONLY color that should be used for this DataSet when @@ -213,14 +181,12 @@ public class CandleDataSet extends LineScatterCandleRadarDataSet im * * @param color */ - public void setIncreasingColor(int color) { - mIncreasingColor = color; + fun setIncreasingColor(color: Int) { + mIncreasingColor = color } - @Override - public int getIncreasingColor() { - return mIncreasingColor; - } + override val increasingColor: Int + get() = mIncreasingColor /** * Sets the one and ONLY color that should be used for this DataSet when @@ -228,68 +194,58 @@ public class CandleDataSet extends LineScatterCandleRadarDataSet im * * @param color */ - public void setDecreasingColor(int color) { - mDecreasingColor = color; + fun setDecreasingColor(color: Int) { + mDecreasingColor = color } - @Override - public int getDecreasingColor() { - return mDecreasingColor; - } + override val decreasingColor: Int + get() = mDecreasingColor - @Override - public Paint.Style getIncreasingPaintStyle() { - return mIncreasingPaintStyle; - } + override val increasingPaintStyle: Paint.Style? + get() = mIncreasingPaintStyle /** * Sets paint style when open < close * * @param paintStyle */ - public void setIncreasingPaintStyle(Paint.Style paintStyle) { - this.mIncreasingPaintStyle = paintStyle; + fun setIncreasingPaintStyle(paintStyle: Paint.Style?) { + this.mIncreasingPaintStyle = paintStyle } - @Override - public Paint.Style getDecreasingPaintStyle() { - return mDecreasingPaintStyle; - } + override val decreasingPaintStyle: Paint.Style? + get() = mDecreasingPaintStyle /** * Sets paint style when open > close * * @param decreasingPaintStyle */ - public void setDecreasingPaintStyle(Paint.Style decreasingPaintStyle) { - this.mDecreasingPaintStyle = decreasingPaintStyle; + fun setDecreasingPaintStyle(decreasingPaintStyle: Paint.Style?) { + this.mDecreasingPaintStyle = decreasingPaintStyle } - @Override - public int getShadowColor() { - return mShadowColor; - } + override val shadowColor: Int + get() = mShadowColor /** * Sets shadow color for all entries * * @param shadowColor */ - public void setShadowColor(int shadowColor) { - this.mShadowColor = shadowColor; + fun setShadowColor(shadowColor: Int) { + this.mShadowColor = shadowColor } - @Override - public boolean getShadowColorSameAsCandle() { - return mShadowColorSameAsCandle; - } + override val shadowColorSameAsCandle: Boolean + get() = mShadowColorSameAsCandle /** * Sets shadow color to be the same color as the candle color * * @param shadowColorSameAsCandle */ - public void setShadowColorSameAsCandle(boolean shadowColorSameAsCandle) { - this.mShadowColorSameAsCandle = shadowColorSameAsCandle; + fun setShadowColorSameAsCandle(shadowColorSameAsCandle: Boolean) { + this.mShadowColorSameAsCandle = shadowColorSameAsCandle } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt index 5a049957a2..5c7be61834 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt @@ -1,45 +1,62 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import android.annotation.SuppressLint; -import android.graphics.drawable.Drawable; +import android.annotation.SuppressLint +import android.graphics.drawable.Drawable +import kotlin.math.abs /** * Subclass of Entry that holds all values for one entry in a CandleStickChart. - * + * * @author Philipp Jahoda */ @SuppressLint("ParcelCreator") -public class CandleEntry extends Entry { - - /** shadow-high value */ - private float mShadowHigh = 0f; +class CandleEntry : Entry { + /** + * Returns the upper shadows highest value. + * + * @return + */ + /** shadow-high value */ + var high: Float = 0f - /** shadow-low value */ - private float mShadowLow = 0f; + /** + * Returns the lower shadows lowest value. + * + * @return + */ + /** shadow-low value */ + var low: Float = 0f - /** close value */ - private float mClose = 0f; + /** + * Returns the bodys close value. + * + * @return + */ + /** close value */ + var close: Float = 0f - /** open value */ - private float mOpen = 0f; + /** + * Returns the bodys open value. + * + * @return + */ + /** open value */ + var open: Float = 0f /** * Constructor. - * + * * @param x The value on the x-axis * @param shadowH The (shadow) high value * @param shadowL The (shadow) low value * @param open The open value * @param close The close value */ - public CandleEntry(float x, float shadowH, float shadowL, float open, float close) { - super(x, (shadowH + shadowL) / 2f); - - this.mShadowHigh = shadowH; - this.mShadowLow = shadowL; - this.mOpen = open; - this.mClose = close; + constructor(x: Float, shadowH: Float, shadowL: Float, open: Float, close: Float) : super(x, (shadowH + shadowL) / 2f) { + this.high = shadowH + this.low = shadowL + this.open = open + this.close = close } /** @@ -52,14 +69,14 @@ public class CandleEntry extends Entry { * @param close * @param data Spot for additional data this Entry represents */ - public CandleEntry(float x, float shadowH, float shadowL, float open, float close, - Object data) { - super(x, (shadowH + shadowL) / 2f, data); - - this.mShadowHigh = shadowH; - this.mShadowLow = shadowL; - this.mOpen = open; - this.mClose = close; + constructor( + x: Float, shadowH: Float, shadowL: Float, open: Float, close: Float, + data: Any? + ) : super(x, (shadowH + shadowL) / 2f, data) { + this.high = shadowH + this.low = shadowL + this.open = open + this.close = close } /** @@ -72,14 +89,14 @@ public class CandleEntry extends Entry { * @param close * @param icon Icon image */ - public CandleEntry(float x, float shadowH, float shadowL, float open, float close, - Drawable icon) { - super(x, (shadowH + shadowL) / 2f, icon); - - this.mShadowHigh = shadowH; - this.mShadowLow = shadowL; - this.mOpen = open; - this.mClose = close; + constructor( + x: Float, shadowH: Float, shadowL: Float, open: Float, close: Float, + icon: Drawable? + ) : super(x, (shadowH + shadowL) / 2f, icon) { + this.high = shadowH + this.low = shadowL + this.open = open + this.close = close } /** @@ -93,101 +110,39 @@ public class CandleEntry extends Entry { * @param icon Icon image * @param data Spot for additional data this Entry represents */ - public CandleEntry(float x, float shadowH, float shadowL, float open, float close, - Drawable icon, Object data) { - super(x, (shadowH + shadowL) / 2f, icon, data); - - this.mShadowHigh = shadowH; - this.mShadowLow = shadowL; - this.mOpen = open; - this.mClose = close; - } - - /** - * Returns the overall range (difference) between shadow-high and - * shadow-low. - * - * @return - */ - public float getShadowRange() { - return Math.abs(mShadowHigh - mShadowLow); - } - - /** - * Returns the body size (difference between open and close). - * - * @return - */ - public float getBodyRange() { - return Math.abs(mOpen - mClose); - } - - /** - * Returns the center value of the candle. (Middle value between high and - * low) - */ - @Override - public float getY() { - return super.getY(); - } - - public CandleEntry copy() { - - CandleEntry c = new CandleEntry(getX(), mShadowHigh, mShadowLow, mOpen, - mClose, getData()); - - return c; - } - - /** - * Returns the upper shadows highest value. - * - * @return - */ - public float getHigh() { - return mShadowHigh; - } - - public void setHigh(float mShadowHigh) { - this.mShadowHigh = mShadowHigh; - } - - /** - * Returns the lower shadows lowest value. - * - * @return - */ - public float getLow() { - return mShadowLow; - } - - public void setLow(float mShadowLow) { - this.mShadowLow = mShadowLow; - } - - /** - * Returns the bodys close value. - * - * @return - */ - public float getClose() { - return mClose; - } - - public void setClose(float mClose) { - this.mClose = mClose; - } - - /** - * Returns the bodys open value. - * - * @return - */ - public float getOpen() { - return mOpen; - } - - public void setOpen(float mOpen) { - this.mOpen = mOpen; + constructor( + x: Float, shadowH: Float, shadowL: Float, open: Float, close: Float, + icon: Drawable?, data: Any? + ) : super(x, (shadowH + shadowL) / 2f, icon, data) { + this.high = shadowH + this.low = shadowL + this.open = open + this.close = close + } + + val shadowRange: Float + /** + * Returns the overall range (difference) between shadow-high and + * shadow-low. + * + * @return + */ + get() = abs(this.high - this.low) + + val bodyRange: Float + /** + * Returns the body size (difference between open and close). + * + * @return + */ + get() = abs(this.open - this.close) + + public override fun copy(): CandleEntry { + val c = CandleEntry( + x, this.high, this.low, this.open, + this.close, data + ) + + return c } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt index fa6dc27573..255a129faf 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt @@ -1,17 +1,12 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import android.graphics.Typeface; -import android.util.Log; - -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.formatter.IValueFormatter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; +import android.graphics.Typeface +import android.util.Log +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.formatter.IValueFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IDataSet +import java.io.Serializable /** * Class that holds all relevant data that represents the chart. That involves @@ -19,57 +14,81 @@ import java.util.List; * * @author Philipp Jahoda */ -public abstract class ChartData> implements Serializable { - +abstract class ChartData>( + /** + * Returns all DataSet objects this ChartData object holds. + * + * @return + */ + /** + * array that holds all DataSets the ChartData object represents + */ + open val dataSets: MutableList = arrayListOf(), +) : Serializable { + /** + * Returns the greatest y-value the data object contains. + * + * @return + */ /** * maximum y-value in the value array across all axes */ - protected float mYMax = -Float.MAX_VALUE; + var yMax: Float = -Float.Companion.MAX_VALUE + protected set + /** + * Returns the smallest y-value the data object contains. + * + * @return + */ /** * the minimum y-value in the value array across all axes */ - protected float mYMin = Float.MAX_VALUE; + var yMin: Float = Float.Companion.MAX_VALUE + protected set + /** + * Returns the maximum x-value this data object contains. + * + * @return + */ /** * maximum x-value in the value array */ - protected float mXMax = -Float.MAX_VALUE; + var xMax: Float = -Float.Companion.MAX_VALUE + protected set + /** + * Returns the minimum x-value this data object contains. + * + * @return + */ /** * minimum x-value in the value array */ - protected float mXMin = Float.MAX_VALUE; - - - protected float mLeftAxisMax = -Float.MAX_VALUE; + var xMin: Float = Float.Companion.MAX_VALUE + protected set - protected float mLeftAxisMin = Float.MAX_VALUE; - protected float mRightAxisMax = -Float.MAX_VALUE; + protected var mLeftAxisMax: Float = -Float.Companion.MAX_VALUE - protected float mRightAxisMin = Float.MAX_VALUE; + protected var mLeftAxisMin: Float = Float.Companion.MAX_VALUE - /** - * array that holds all DataSets the ChartData object represents - */ - protected List mDataSets; + protected var mRightAxisMax: Float = -Float.Companion.MAX_VALUE - /** - * Default constructor. - */ - public ChartData() { - mDataSets = new ArrayList(); - } + protected var mRightAxisMin: Float = Float.Companion.MAX_VALUE /** * Constructor taking single or multiple DataSet objects. * * @param dataSets */ - public ChartData(T... dataSets) { - mDataSets = arrayToList(dataSets); - notifyDataChanged(); + constructor(vararg dataSets: T): this(dataSets = dataSets.toMutableList()) + + init { + if (dataSets.isNotEmpty()) { + notifyDataChanged() + } } /** @@ -78,25 +97,14 @@ public abstract class ChartData> implements * @param array * @return */ - private List arrayToList(T[] array) { - - List list = new ArrayList<>(); + private fun arrayToList(array: Array): MutableList { + val list: MutableList = ArrayList() - for (T set : array) { - list.add(set); + for (set in array) { + list.add(set) } - return list; - } - - /** - * constructor for chart data - * - * @param sets the dataset array - */ - public ChartData(List sets) { - this.mDataSets = sets; - notifyDataChanged(); + return list } /** @@ -104,8 +112,8 @@ public abstract class ChartData> implements * changed. Calling this performs all necessary recalculations needed when * the contained data has changed. */ - public void notifyDataChanged() { - calcMinMax(); + open fun notifyDataChanged() { + calcMinMax() } /** @@ -115,98 +123,76 @@ public abstract class ChartData> implements * @param fromX the x-value to start the calculation from * @param toX the x-value to which the calculation should be performed */ - public void calcMinMaxY(float fromX, float toX) { - - for (T set : mDataSets) { - set.calcMinMaxY(fromX, toX); + fun calcMinMaxY(fromX: Float, toX: Float) { + for (set in this.dataSets) { + set!!.calcMinMaxY(fromX, toX) } // apply the new data - calcMinMax(); + calcMinMax() } /** * Calc minimum and maximum values (both x and y) over all DataSets. */ - protected void calcMinMax() { - - if (mDataSets == null) - return; + open fun calcMinMax() { + this.yMax = -Float.Companion.MAX_VALUE + this.yMin = Float.Companion.MAX_VALUE + this.xMax = -Float.Companion.MAX_VALUE + this.xMin = Float.Companion.MAX_VALUE - mYMax = -Float.MAX_VALUE; - mYMin = Float.MAX_VALUE; - mXMax = -Float.MAX_VALUE; - mXMin = Float.MAX_VALUE; - - for (T set : mDataSets) { - calcMinMax(set); + for (set in this.dataSets) { + calcMinMax(set) } - mLeftAxisMax = -Float.MAX_VALUE; - mLeftAxisMin = Float.MAX_VALUE; - mRightAxisMax = -Float.MAX_VALUE; - mRightAxisMin = Float.MAX_VALUE; + mLeftAxisMax = -Float.Companion.MAX_VALUE + mLeftAxisMin = Float.Companion.MAX_VALUE + mRightAxisMax = -Float.Companion.MAX_VALUE + mRightAxisMin = Float.Companion.MAX_VALUE // left axis - T firstLeft = getFirstLeft(mDataSets); + val firstLeft = getFirstLeft(this.dataSets) if (firstLeft != null) { + mLeftAxisMax = firstLeft.yMax + mLeftAxisMin = firstLeft.yMin - mLeftAxisMax = firstLeft.getYMax(); - mLeftAxisMin = firstLeft.getYMin(); - - for (T dataSet : mDataSets) { - if (dataSet.getAxisDependency() == AxisDependency.LEFT) { - if (dataSet.getYMin() < mLeftAxisMin) - mLeftAxisMin = dataSet.getYMin(); + for (dataSet in this.dataSets) { + if (dataSet!!.axisDependency == AxisDependency.LEFT) { + if (dataSet.yMin < mLeftAxisMin) mLeftAxisMin = dataSet.yMin - if (dataSet.getYMax() > mLeftAxisMax) - mLeftAxisMax = dataSet.getYMax(); + if (dataSet.yMax > mLeftAxisMax) mLeftAxisMax = dataSet.yMax } } } // right axis - T firstRight = getFirstRight(mDataSets); + val firstRight = getFirstRight(this.dataSets) if (firstRight != null) { + mRightAxisMax = firstRight.yMax + mRightAxisMin = firstRight.yMin - mRightAxisMax = firstRight.getYMax(); - mRightAxisMin = firstRight.getYMin(); + for (dataSet in this.dataSets) { + if (dataSet!!.axisDependency == AxisDependency.RIGHT) { + if (dataSet.yMin < mRightAxisMin) mRightAxisMin = dataSet.yMin - for (T dataSet : mDataSets) { - if (dataSet.getAxisDependency() == AxisDependency.RIGHT) { - if (dataSet.getYMin() < mRightAxisMin) - mRightAxisMin = dataSet.getYMin(); - - if (dataSet.getYMax() > mRightAxisMax) - mRightAxisMax = dataSet.getYMax(); + if (dataSet.yMax > mRightAxisMax) mRightAxisMax = dataSet.yMax } } } } - /** ONLY GETTERS AND SETTERS BELOW THIS */ - - /** - * returns the number of LineDataSets this object contains - * - * @return - */ - public int getDataSetCount() { - if (mDataSets == null) - return 0; - return mDataSets.size(); - } - - /** - * Returns the smallest y-value the data object contains. - * - * @return - */ - public float getYMin() { - return mYMin; - } + /** ONLY GETTERS AND SETTERS BELOW THIS */ + val dataSetCount: Int + /** + * returns the number of LineDataSets this object contains + * + * @return + */ + get() { + return dataSets.size + } /** * Returns the minimum y-value for the specified axis. @@ -214,78 +200,36 @@ public abstract class ChartData> implements * @param axis * @return */ - public float getYMin(AxisDependency axis) { - if (axis == AxisDependency.LEFT) { - - if (mLeftAxisMin == Float.MAX_VALUE) { - return mRightAxisMin; - } else - return mLeftAxisMin; + fun getYMin(axis: AxisDependency?): Float { + return if (axis == AxisDependency.LEFT) { + if (mLeftAxisMin == Float.Companion.MAX_VALUE) { + mRightAxisMin + } else mLeftAxisMin } else { - if (mRightAxisMin == Float.MAX_VALUE) { - return mLeftAxisMin; - } else - return mRightAxisMin; + if (mRightAxisMin == Float.Companion.MAX_VALUE) { + mLeftAxisMin + } else mRightAxisMin } } - /** - * Returns the greatest y-value the data object contains. - * - * @return - */ - public float getYMax() { - return mYMax; - } - /** * Returns the maximum y-value for the specified axis. * * @param axis * @return */ - public float getYMax(AxisDependency axis) { + fun getYMax(axis: AxisDependency?): Float { if (axis == AxisDependency.LEFT) { - - if (mLeftAxisMax == -Float.MAX_VALUE) { - return mRightAxisMax; - } else - return mLeftAxisMax; + if (mLeftAxisMax == -Float.Companion.MAX_VALUE) { + return mRightAxisMax + } else return mLeftAxisMax } else { - if (mRightAxisMax == -Float.MAX_VALUE) { - return mLeftAxisMax; - } else - return mRightAxisMax; + if (mRightAxisMax == -Float.Companion.MAX_VALUE) { + return mLeftAxisMax + } else return mRightAxisMax } } - /** - * Returns the minimum x-value this data object contains. - * - * @return - */ - public float getXMin() { - return mXMin; - } - - /** - * Returns the maximum x-value this data object contains. - * - * @return - */ - public float getXMax() { - return mXMax; - } - - /** - * Returns all DataSet objects this ChartData object holds. - * - * @return - */ - public List getDataSets() { - return mDataSets; - } - /** * Retrieve the index of a DataSet with a specific label from the ChartData. * Search can be case sensitive or not. IMPORTANT: This method does @@ -297,49 +241,45 @@ public abstract class ChartData> implements * @param ignorecase if true, the search is not case-sensitive * @return */ - protected int getDataSetIndexByLabel(List dataSets, String label, - boolean ignorecase) { - + protected fun getDataSetIndexByLabel( + dataSets: MutableList, label: String, + ignorecase: Boolean + ): Int { if (ignorecase) { - for (int i = 0; i < dataSets.size(); i++) - if (label.equalsIgnoreCase(dataSets.get(i).getLabel())) - return i; + for (i in dataSets.indices) if (label.equals(dataSets[i]!!.label, ignoreCase = true)) return i } else { - for (int i = 0; i < dataSets.size(); i++) - if (label.equals(dataSets.get(i).getLabel())) - return i; + for (i in dataSets.indices) if (label == dataSets[i]!!.label) return i } - return -1; + return -1 } - /** - * Returns the labels of all DataSets as a string array. - * - * @return - */ - public String[] getDataSetLabels() { + val dataSetLabels: Array + /** + * Returns the labels of all DataSets as a string array. + * + * @return + */ + get() { + val types = arrayOfNulls(dataSets.size) - String[] types = new String[mDataSets.size()]; + for (i in dataSets.indices) { + types[i] = dataSets[i]!!.label + } - for (int i = 0; i < mDataSets.size(); i++) { - types[i] = mDataSets.get(i).getLabel(); + return types } - return types; - } - /** * Get the Entry for a corresponding highlight object * * @param highlight * @return the entry that is highlighted */ - public Entry getEntryForHighlight(Highlight highlight) { - if (highlight.getDataSetIndex() >= mDataSets.size()) - return null; + open fun getEntryForHighlight(highlight: Highlight): E? { + return if (highlight.dataSetIndex >= dataSets.size) null else { - return mDataSets.get(highlight.getDataSetIndex()).getEntryForXValue(highlight.getX(), highlight.getY()); + dataSets[highlight.dataSetIndex]!!.getEntryForXValue(highlight.x, highlight.y) } } @@ -352,22 +292,17 @@ public abstract class ChartData> implements * @param ignorecase * @return */ - public T getDataSetByLabel(String label, boolean ignorecase) { + open fun getDataSetByLabel(label: String, ignorecase: Boolean): T? { + val index = getDataSetIndexByLabel(this.dataSets, label, ignorecase) - int index = getDataSetIndexByLabel(mDataSets, label, ignorecase); - - if (index < 0 || index >= mDataSets.size()) - return null; - else - return mDataSets.get(index); + return if (index < 0 || index >= dataSets.size) null + else dataSets[index] } - public T getDataSetByIndex(int index) { - - if (mDataSets == null || index < 0 || index >= mDataSets.size()) - return null; + open fun getDataSetByIndex(index: Int): T { + if (index < 0 || index >= dataSets.size) throw ArrayIndexOutOfBoundsException(index) - return mDataSets.get(index); + return dataSets[index] } /** @@ -375,14 +310,12 @@ public abstract class ChartData> implements * * @param d */ - public void addDataSet(T d) { + fun addDataSet(d: T?) { + if (d == null) return - if (d == null) - return; + calcMinMax(d) - calcMinMax(d); - - mDataSets.add(d); + dataSets.add(d) } /** @@ -392,19 +325,17 @@ public abstract class ChartData> implements * * @param d */ - public boolean removeDataSet(T d) { - - if (d == null) - return false; + open fun removeDataSet(d: IDataSet?): Boolean { + if (d == null) return false - boolean removed = mDataSets.remove(d); + val removed = dataSets.remove(d) // if a DataSet was removed if (removed) { - notifyDataChanged(); + notifyDataChanged() } - return removed; + return removed } /** @@ -414,13 +345,11 @@ public abstract class ChartData> implements * * @param index */ - public boolean removeDataSet(int index) { - - if (index >= mDataSets.size() || index < 0) - return false; + open fun removeDataSet(index: Int): Boolean { + if (index >= dataSets.size || index < 0) return false - T set = mDataSets.get(index); - return removeDataSet(set); + val set = dataSets[index] + return removeDataSet(set) } /** @@ -430,19 +359,15 @@ public abstract class ChartData> implements * @param e * @param dataSetIndex */ - public void addEntry(Entry e, int dataSetIndex) { - - if (mDataSets.size() > dataSetIndex && dataSetIndex >= 0) { - - IDataSet set = mDataSets.get(dataSetIndex); + fun addEntry(e: E, dataSetIndex: Int) { + if (dataSets.size > dataSetIndex && dataSetIndex >= 0) { + val set = dataSets[dataSetIndex] // add the entry to the dataset - if (!set.addEntry(e)) - return; - - calcMinMax(e, set.getAxisDependency()); + if (!set.addEntry(e)) return + calcMinMax(e, set.axisDependency) } else { - Log.e("addEntry", "Cannot add Entry because dataSetIndex too high or too low."); + Log.e("addEntry", "Cannot add Entry because dataSetIndex too high or too low.") } } @@ -452,29 +377,19 @@ public abstract class ChartData> implements * @param e * @param axis */ - protected void calcMinMax(Entry e, AxisDependency axis) { - - if (mYMax < e.getY()) - mYMax = e.getY(); - if (mYMin > e.getY()) - mYMin = e.getY(); + protected fun calcMinMax(e: E, axis: AxisDependency?) { + if (this.yMax < e.y) this.yMax = e.y + if (this.yMin > e.y) this.yMin = e.y - if (mXMax < e.getX()) - mXMax = e.getX(); - if (mXMin > e.getX()) - mXMin = e.getX(); + if (this.xMax < e.x) this.xMax = e.x + if (this.xMin > e.x) this.xMin = e.x if (axis == AxisDependency.LEFT) { - - if (mLeftAxisMax < e.getY()) - mLeftAxisMax = e.getY(); - if (mLeftAxisMin > e.getY()) - mLeftAxisMin = e.getY(); + if (mLeftAxisMax < e.y) mLeftAxisMax = e.y + if (mLeftAxisMin > e.y) mLeftAxisMin = e.y } else { - if (mRightAxisMax < e.getY()) - mRightAxisMax = e.getY(); - if (mRightAxisMin > e.getY()) - mRightAxisMin = e.getY(); + if (mRightAxisMax < e.y) mRightAxisMax = e.y + if (mRightAxisMin > e.y) mRightAxisMin = e.y } } @@ -483,29 +398,19 @@ public abstract class ChartData> implements * * @param d */ - protected void calcMinMax(T d) { - - if (mYMax < d.getYMax()) - mYMax = d.getYMax(); - if (mYMin > d.getYMin()) - mYMin = d.getYMin(); - - if (mXMax < d.getXMax()) - mXMax = d.getXMax(); - if (mXMin > d.getXMin()) - mXMin = d.getXMin(); + protected fun calcMinMax(d: T?) { + if (this.yMax < d!!.yMax) this.yMax = d.yMax + if (this.yMin > d.yMin) this.yMin = d.yMin - if (d.getAxisDependency() == AxisDependency.LEFT) { + if (this.xMax < d.xMax) this.xMax = d.xMax + if (this.xMin > d.xMin) this.xMin = d.xMin - if (mLeftAxisMax < d.getYMax()) - mLeftAxisMax = d.getYMax(); - if (mLeftAxisMin > d.getYMin()) - mLeftAxisMin = d.getYMin(); + if (d.axisDependency == AxisDependency.LEFT) { + if (mLeftAxisMax < d.yMax) mLeftAxisMax = d.yMax + if (mLeftAxisMin > d.yMin) mLeftAxisMin = d.yMin } else { - if (mRightAxisMax < d.getYMax()) - mRightAxisMax = d.getYMax(); - if (mRightAxisMin > d.getYMin()) - mRightAxisMin = d.getYMin(); + if (mRightAxisMax < d.yMax) mRightAxisMax = d.yMax + if (mRightAxisMin > d.yMin) mRightAxisMin = d.yMin } } @@ -515,25 +420,23 @@ public abstract class ChartData> implements * @param e * @param dataSetIndex */ - public boolean removeEntry(Entry e, int dataSetIndex) { - + open fun removeEntry(e: E?, dataSetIndex: Int): Boolean { // entry null, outofbounds - if (e == null || dataSetIndex >= mDataSets.size()) - return false; - IDataSet set = mDataSets.get(dataSetIndex); + if (e == null || dataSetIndex >= dataSets.size) return false + + val set = dataSets[dataSetIndex] if (set != null) { // remove the entry from the dataset - boolean removed = set.removeEntry(e); + val removed: Boolean = set.removeEntry(e) if (removed) { - notifyDataChanged(); + notifyDataChanged() } - return removed; - } else - return false; + return removed + } else return false } /** @@ -545,18 +448,15 @@ public abstract class ChartData> implements * @param dataSetIndex * @return */ - public boolean removeEntry(float xValue, int dataSetIndex) { - - if (dataSetIndex >= mDataSets.size()) - return false; + open fun removeEntry(xValue: Float, dataSetIndex: Int): Boolean { + if (dataSetIndex >= dataSets.size) return false - IDataSet dataSet = mDataSets.get(dataSetIndex); - Entry e = dataSet.getEntryForXValue(xValue, Float.NaN); + val dataSet = dataSets[dataSetIndex]!! + val e = dataSet.getEntryForXValue(xValue, Float.Companion.NaN) - if (e == null) - return false; + if (e == null) return false - return removeEntry(e, dataSetIndex); + return removeEntry(e, dataSetIndex) } /** @@ -566,56 +466,48 @@ public abstract class ChartData> implements * @param e * @return */ - public T getDataSetForEntry(Entry e) { - - if (e == null) - return null; - - for (int i = 0; i < mDataSets.size(); i++) { + fun getDataSetForEntry(e: E?): T? { + if (e == null) return null - T set = mDataSets.get(i); + for (i in dataSets.indices) { + val set = dataSets[i] - for (int j = 0; j < set.getEntryCount(); j++) { - if (e.equalTo(set.getEntryForXValue(e.getX(), e.getY()))) - return set; + for (j in 0.. clrs = mDataSets.get(i).getColors(); + for (i in dataSets.indices) { + val clrs = dataSets[i]!!.colors!! - for (Integer clr : clrs) { - colors[cnt] = clr; - cnt++; + for (clr in clrs) { + colors[cnt] = clr!! + cnt++ + } } - } - return colors; - } + return colors + } /** * Returns the index of the provided DataSet in the DataSet array of this data object, or -1 if it does not exist. @@ -623,8 +515,8 @@ public abstract class ChartData> implements * @param dataSet * @return */ - public int getIndexOfDataSet(T dataSet) { - return mDataSets.indexOf(dataSet); + fun getIndexOfDataSet(dataSet: T?): Int { + return dataSets.indexOf(dataSet) } /** @@ -633,12 +525,11 @@ public abstract class ChartData> implements * * @return */ - protected T getFirstLeft(List sets) { - for (T dataSet : sets) { - if (dataSet.getAxisDependency() == AxisDependency.LEFT) - return dataSet; + protected fun getFirstLeft(sets: MutableList): T? { + for (dataSet in sets) { + if (dataSet!!.axisDependency == AxisDependency.LEFT) return dataSet } - return null; + return null } /** @@ -647,12 +538,11 @@ public abstract class ChartData> implements * * @return */ - public T getFirstRight(List sets) { - for (T dataSet : sets) { - if (dataSet.getAxisDependency() == AxisDependency.RIGHT) - return dataSet; + fun getFirstRight(sets: MutableList): T? { + for (dataSet in sets) { + if (dataSet!!.axisDependency == AxisDependency.RIGHT) return dataSet } - return null; + return null } /** @@ -660,12 +550,11 @@ public abstract class ChartData> implements * * @param f */ - public void setValueFormatter(IValueFormatter f) { - if (f == null) - return; + fun setValueFormatter(f: IValueFormatter?) { + if (f == null) return else { - for (IDataSet set : mDataSets) { - set.setValueFormatter(f); + for (set in this.dataSets) { + set!!.valueFormatter = f } } } @@ -676,9 +565,9 @@ public abstract class ChartData> implements * * @param color */ - public void setValueTextColor(int color) { - for (IDataSet set : mDataSets) { - set.setValueTextColor(color); + fun setValueTextColor(color: Int) { + for (set in this.dataSets) { + set!!.valueTextColor = color } } @@ -688,9 +577,9 @@ public abstract class ChartData> implements * * @param colors */ - public void setValueTextColors(List colors) { - for (IDataSet set : mDataSets) { - set.setValueTextColors(colors); + fun setValueTextColors(colors: MutableList) { + for (set in this.dataSets) { + set.setValueTextColors(colors) } } @@ -700,9 +589,9 @@ public abstract class ChartData> implements * * @param tf */ - public void setValueTypeface(Typeface tf) { - for (IDataSet set : mDataSets) { - set.setValueTypeface(tf); + fun setValueTypeface(tf: Typeface?) { + for (set in this.dataSets) { + set.valueTypeface = tf } } @@ -712,9 +601,9 @@ public abstract class ChartData> implements * * @param size */ - public void setValueTextSize(float size) { - for (IDataSet set : mDataSets) { - set.setValueTextSize(size); + fun setValueTextSize(size: Float) { + for (set in this.dataSets) { + set.valueTextSize = size } } @@ -724,46 +613,43 @@ public abstract class ChartData> implements * * @param enabled */ - public void setDrawValues(boolean enabled) { - for (IDataSet set : mDataSets) { - set.setDrawValues(enabled); + fun setDrawValues(enabled: Boolean) { + for (set in this.dataSets) { + set.isDrawValuesEnabled = enabled } } - /** - * Enables / disables highlighting values for all DataSets this data object - * contains. If set to true, this means that values can - * be highlighted programmatically or by touch gesture. - */ - public void setHighlightEnabled(boolean enabled) { - for (IDataSet set : mDataSets) { - set.setHighlightEnabled(enabled); + var isHighlightEnabled: Boolean + /** + * Returns true if highlighting of all underlying values is enabled, false + * if not. + * + * @return + */ + get() { + for (set in this.dataSets) { + if (!set!!.isHighlightEnabled) return false + } + return true } - } - - /** - * Returns true if highlighting of all underlying values is enabled, false - * if not. - * - * @return - */ - public boolean isHighlightEnabled() { - for (IDataSet set : mDataSets) { - if (!set.isHighlightEnabled()) - return false; + /** + * Enables / disables highlighting values for all DataSets this data object + * contains. If set to true, this means that values can + * be highlighted programmatically or by touch gesture. + */ + set(enabled) { + for (set in this.dataSets) { + set!!.isHighlightEnabled = enabled + } } - return true; - } /** * Clears this data object from all DataSets and removes all Entries. Don't * forget to invalidate the chart after this. */ - public void clearValues() { - if (mDataSets != null) { - mDataSets.clear(); - } - notifyDataChanged(); + fun clearValues() { + dataSets.clear() + notifyDataChanged() } /** @@ -773,50 +659,45 @@ public abstract class ChartData> implements * @param dataSet * @return */ - public boolean contains(T dataSet) { - - for (T set : mDataSets) { - if (set.equals(dataSet)) - return true; + fun contains(dataSet: T?): Boolean { + for (set in this.dataSets) { + if (set == dataSet) return true } - return false; + return false } - /** - * Returns the total entry count across all DataSet objects this data object contains. - * - * @return - */ - public int getEntryCount() { + val entryCount: Int + /** + * Returns the total entry count across all DataSet objects this data object contains. + * + * @return + */ + get() { + var count = 0 - int count = 0; + for (set in this.dataSets) { + count += set!!.entryCount + } - for (T set : mDataSets) { - count += set.getEntryCount(); + return count } - return count; - } - - /** - * Returns the DataSet object with the maximum number of entries or null if there are no DataSets. - * - * @return - */ - public T getMaxEntryCountSet() { - - if (mDataSets == null || mDataSets.isEmpty()) - return null; + val maxEntryCountSet: T? + /** + * Returns the DataSet object with the maximum number of entries or null if there are no DataSets. + * + * @return + */ + get() { + if (dataSets.isEmpty()) return null - T max = mDataSets.get(0); + var max = dataSets[0] - for (T set : mDataSets) { + for (set in this.dataSets) { + if (set!!.entryCount > max!!.entryCount) max = set + } - if (set.getEntryCount() > max.getEntryCount()) - max = set; + return max } - - return max; - } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt index 0b36aa3bef..dfb78e4d51 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt @@ -1,14 +1,15 @@ - -package com.github.mikephil.charting.data; - -import android.util.Log; - -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet; - -import java.util.ArrayList; -import java.util.List; +package com.github.mikephil.charting.data + +import android.util.Log +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet +import com.github.mikephil.charting.interfaces.datasets.IDataSet +import java.lang.Float +import kotlin.Boolean +import kotlin.Deprecated +import kotlin.Int +import kotlin.unaryMinus /** * Data object that allows the combination of Line-, Bar-, Scatter-, Bubble- and @@ -16,165 +17,117 @@ import java.util.List; * * @author Philipp Jahoda */ -public class CombinedData extends BarLineScatterCandleBubbleData> { +class CombinedData : BarLineScatterCandleBubbleData>() { + var lineData: LineData? = null + private set + var barData: BarData? = null + private set + var scatterData: ScatterData? = null + private set + var candleData: CandleData? = null + private set + var bubbleData: BubbleData? = null + private set - private LineData mLineData; - private BarData mBarData; - private ScatterData mScatterData; - private CandleData mCandleData; - private BubbleData mBubbleData; - - public CombinedData() { - super(); - } - - public void setData(LineData data) { - mLineData = data; - notifyDataChanged(); + fun setData(data: LineData?) { + this.lineData = data + notifyDataChanged() } - public void setData(BarData data) { - mBarData = data; - notifyDataChanged(); + fun setData(data: BarData?) { + this.barData = data + notifyDataChanged() } - public void setData(ScatterData data) { - mScatterData = data; - notifyDataChanged(); + fun setData(data: ScatterData?) { + this.scatterData = data + notifyDataChanged() } - public void setData(CandleData data) { - mCandleData = data; - notifyDataChanged(); + fun setData(data: CandleData?) { + this.candleData = data + notifyDataChanged() } - public void setData(BubbleData data) { - mBubbleData = data; - notifyDataChanged(); + fun setData(data: BubbleData?) { + this.bubbleData = data + notifyDataChanged() } - @Override - public void calcMinMax() { + override fun calcMinMax() { + dataSets.clear() - if(mDataSets == null){ - mDataSets = new ArrayList<>(); - } - mDataSets.clear(); + yMax = -Float.MAX_VALUE + yMin = Float.MAX_VALUE + xMax = -Float.MAX_VALUE + xMin = Float.MAX_VALUE - mYMax = -Float.MAX_VALUE; - mYMin = Float.MAX_VALUE; - mXMax = -Float.MAX_VALUE; - mXMin = Float.MAX_VALUE; + mLeftAxisMax = -Float.MAX_VALUE + mLeftAxisMin = Float.MAX_VALUE + mRightAxisMax = -Float.MAX_VALUE + mRightAxisMin = Float.MAX_VALUE - mLeftAxisMax = -Float.MAX_VALUE; - mLeftAxisMin = Float.MAX_VALUE; - mRightAxisMax = -Float.MAX_VALUE; - mRightAxisMin = Float.MAX_VALUE; + val allData = this.allData - List allData = getAllData(); + for (data in allData) { + data.calcMinMax() - for (ChartData data : allData) { + dataSets.addAll(data.dataSets as Collection>) - data.calcMinMax(); + if (data.yMax > yMax) yMax = data.yMax - List> sets = data.getDataSets(); - mDataSets.addAll(sets); + if (data.yMin < yMin) yMin = data.yMin - if (data.getYMax() > mYMax) - mYMax = data.getYMax(); + if (data.xMax > xMax) xMax = data.xMax - if (data.getYMin() < mYMin) - mYMin = data.getYMin(); + if (data.xMin < xMin) xMin = data.xMin - if (data.getXMax() > mXMax) - mXMax = data.getXMax(); - - if (data.getXMin() < mXMin) - mXMin = data.getXMin(); - - for (IBarLineScatterCandleBubbleDataSet dataset : sets) { - if (dataset.getAxisDependency() == YAxis.AxisDependency.LEFT) { - if (dataset.getYMax() > mLeftAxisMax) { - mLeftAxisMax = dataset.getYMax(); + for (dataset in dataSets) { + if (dataset.axisDependency == AxisDependency.LEFT) { + if (dataset.yMax > mLeftAxisMax) { + mLeftAxisMax = dataset.yMax } - if (dataset.getYMin() < mLeftAxisMin) { - mLeftAxisMin = dataset.getYMin(); + if (dataset.yMin < mLeftAxisMin) { + mLeftAxisMin = dataset.yMin } - } - else { - if (dataset.getYMax() > mRightAxisMax) { - mRightAxisMax = dataset.getYMax(); + } else { + if (dataset.yMax > mRightAxisMax) { + mRightAxisMax = dataset.yMax } - if (dataset.getYMin() < mRightAxisMin) { - mRightAxisMin = dataset.getYMin(); + if (dataset.yMin < mRightAxisMin) { + mRightAxisMin = dataset.yMin } } } } } - public BubbleData getBubbleData() { - return mBubbleData; - } + val allData + /** + * Returns all data objects in row: line-bar-scatter-candle-bubble if not null. + * + * @return + */ + get() = listOfNotNull( + this.lineData, + this.barData, + this.scatterData, + this.candleData, + this.bubbleData, + ) - public LineData getLineData() { - return mLineData; - } + fun getDataByIndex(index: Int) = this.allData[index] - public BarData getBarData() { - return mBarData; - } + override fun notifyDataChanged() { + if (this.lineData != null) lineData!!.notifyDataChanged() + if (this.barData != null) barData!!.notifyDataChanged() + if (this.candleData != null) candleData!!.notifyDataChanged() + if (this.scatterData != null) scatterData!!.notifyDataChanged() + if (this.bubbleData != null) bubbleData!!.notifyDataChanged() - public ScatterData getScatterData() { - return mScatterData; - } - - public CandleData getCandleData() { - return mCandleData; - } - - /** - * Returns all data objects in row: line-bar-scatter-candle-bubble if not null. - * - * @return - */ - public List getAllData() { - - List data = new ArrayList(); - if (mLineData != null) - data.add(mLineData); - if (mBarData != null) - data.add(mBarData); - if (mScatterData != null) - data.add(mScatterData); - if (mCandleData != null) - data.add(mCandleData); - if (mBubbleData != null) - data.add(mBubbleData); - - return data; - } - - public BarLineScatterCandleBubbleData getDataByIndex(int index) { - return getAllData().get(index); - } - - @Override - public void notifyDataChanged() { - if (mLineData != null) - mLineData.notifyDataChanged(); - if (mBarData != null) - mBarData.notifyDataChanged(); - if (mCandleData != null) - mCandleData.notifyDataChanged(); - if (mScatterData != null) - mScatterData.notifyDataChanged(); - if (mBubbleData != null) - mBubbleData.notifyDataChanged(); - - calcMinMax(); // recalculate everything + calcMinMax() // recalculate everything } /** @@ -183,28 +136,21 @@ public class CombinedData extends BarLineScatterCandleBubbleData= getAllData().size()) - return null; + override fun getEntryForHighlight(highlight: Highlight): Entry? { + if (highlight.dataIndex >= this.allData.size) return null - ChartData data = getDataByIndex(highlight.getDataIndex()); + val data: ChartData<*, *> = getDataByIndex(highlight.dataIndex) - if (highlight.getDataSetIndex() >= data.getDataSetCount()) - return null; + if (highlight.dataSetIndex >= data.dataSetCount) return null // The value of the highlighted entry could be NaN - // if we are not interested in highlighting a specific value. + val entries = data.getDataSetByIndex(highlight.dataSetIndex)!!.getEntriesForXValue(highlight.x) + for (entry in entries) if (entry.y == highlight.y || + Float.isNaN(highlight.y) + ) return entry - List entries = data.getDataSetByIndex(highlight.getDataSetIndex()) - .getEntriesForXValue(highlight.getX()); - for (Entry entry : entries) - if (entry.getY() == highlight.getY() || - Float.isNaN(highlight.getY())) - return entry; - - return null; + return null } /** @@ -213,60 +159,51 @@ public class CombinedData extends BarLineScatterCandleBubbleData getDataSetByHighlight(Highlight highlight) { - if (highlight.getDataIndex() >= getAllData().size()) - return null; + fun getDataSetByHighlight(highlight: Highlight): IBarLineScatterCandleBubbleDataSet? { + if (highlight.dataIndex >= this.allData.size) return null - BarLineScatterCandleBubbleData data = getDataByIndex(highlight.getDataIndex()); + val data = getDataByIndex(highlight.dataIndex) - if (highlight.getDataSetIndex() >= data.getDataSetCount()) - return null; + if (highlight.dataSetIndex >= data.dataSetCount) return null - return (IBarLineScatterCandleBubbleDataSet) - data.getDataSets().get(highlight.getDataSetIndex()); + return data.dataSets[highlight.dataSetIndex] } - public int getDataIndex(ChartData data) { - return getAllData().indexOf(data); + fun getDataIndex(data: BarLineScatterCandleBubbleData>): Int { + return this.allData.indexOf(data) } - @Override - public boolean removeDataSet(IBarLineScatterCandleBubbleDataSet d) { - - List datas = getAllData(); - - boolean success = false; + override fun removeDataSet(d: IDataSet?): Boolean { + val datas = this.allData - for (ChartData data : datas) { + var success = false - success = data.removeDataSet(d); + for (data in datas) { + success = data.removeDataSet(d) if (success) { - break; + break } } - return success; + return success } - @Deprecated - @Override - public boolean removeDataSet(int index) { - Log.e("MPAndroidChart", "removeDataSet(int index) not supported for CombinedData"); - return false; + @Deprecated("") + override fun removeDataSet(index: Int): Boolean { + Log.e("MPAndroidChart", "removeDataSet(int index) not supported for CombinedData") + return false } - @Deprecated - @Override - public boolean removeEntry(Entry e, int dataSetIndex) { - Log.e("MPAndroidChart", "removeEntry(...) not supported for CombinedData"); - return false; + @Deprecated("") + override fun removeEntry(e: Entry?, dataSetIndex: Int): Boolean { + Log.e("MPAndroidChart", "removeEntry(...) not supported for CombinedData") + return false } - @Deprecated - @Override - public boolean removeEntry(float xValue, int dataSetIndex) { - Log.e("MPAndroidChart", "removeEntry(...) not supported for CombinedData"); - return false; + @Deprecated("") + override fun removeEntry(xValue: kotlin.Float, dataSetIndex: Int): Boolean { + Log.e("MPAndroidChart", "removeEntry(...) not supported for CombinedData") + return false } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt index 56fb0d924f..5d2e291b04 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt @@ -1,9 +1,7 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; +import java.io.Serializable +import kotlin.math.abs /** * The DataSet class represents one group or type of entries (Entry) in the @@ -13,32 +11,31 @@ import java.util.List; * * @author Philipp Jahoda */ -public abstract class DataSet extends BaseDataSet implements Serializable { - +abstract class DataSet( /** * the entries that this DataSet represents / holds together */ - protected List mEntries; - + protected var mEntries: MutableList, label: String +) : BaseDataSet(label), Serializable { /** * maximum y-value in the value array */ - protected float mYMax = -Float.MAX_VALUE; + protected var mYMax: Float = -Float.MAX_VALUE /** * minimum y-value in the value array */ - protected float mYMin = Float.MAX_VALUE; + protected var mYMin: Float = Float.MAX_VALUE /** * maximum x-value in the value array */ - protected float mXMax = -Float.MAX_VALUE; + protected var mXMax: Float = -Float.Companion.MAX_VALUE /** * minimum x-value in the value array */ - protected float mXMin = Float.MAX_VALUE; + protected var mXMin: Float = Float.Companion.MAX_VALUE /** @@ -46,52 +43,41 @@ public abstract class DataSet extends BaseDataSet implements * label that describes the DataSet can be specified. The label can also be * used to retrieve the DataSet from a ChartData object. * - * @param entries + * @param mEntries * @param label */ - public DataSet(List entries, String label) { - super(label); - this.mEntries = entries; - - if (mEntries == null) - mEntries = new ArrayList(); - - calcMinMax(); + init { + calcMinMax() } - @Override - public void calcMinMax() { - - mYMax = -Float.MAX_VALUE; - mYMin = Float.MAX_VALUE; - mXMax = -Float.MAX_VALUE; - mXMin = Float.MAX_VALUE; + override fun calcMinMax() { + mYMax = -Float.Companion.MAX_VALUE + mYMin = Float.Companion.MAX_VALUE + mXMax = -Float.Companion.MAX_VALUE + mXMin = Float.Companion.MAX_VALUE - if (mEntries == null || mEntries.isEmpty()) - return; + if (mEntries.isEmpty()) return - for (T e : mEntries) { - calcMinMax(e); + for (e in mEntries) { + calcMinMax(e) } } - @Override - public void calcMinMaxY(float fromX, float toX) { - mYMax = -Float.MAX_VALUE; - mYMin = Float.MAX_VALUE; - - if (mEntries == null || mEntries.isEmpty()) - return; + override fun calcMinMaxY(fromX: Float, toX: Float) { + mYMax = -Float.Companion.MAX_VALUE + mYMin = Float.Companion.MAX_VALUE - int indexFrom = getEntryIndex(fromX, Float.NaN, Rounding.DOWN); - int indexTo = getEntryIndex(toX, Float.NaN, Rounding.UP); + if (mEntries.isEmpty()) return - if (indexTo < indexFrom) return; + val indexFrom = getEntryIndex(fromX, Float.Companion.NaN, Rounding.DOWN) + val indexTo = getEntryIndex(toX, Float.Companion.NaN, Rounding.UP) - for (int i = indexFrom; i <= indexTo; i++) { + if (indexTo < indexFrom) return + for (i in indexFrom..indexTo) { // only recalculate y - calcMinMaxY(mEntries.get(i)); + + calcMinMaxY(mEntries[i]) } } @@ -100,102 +86,85 @@ public abstract class DataSet extends BaseDataSet implements * * @param e */ - protected void calcMinMax(T e) { - - if (e == null) - return; - - calcMinMaxX(e); - - calcMinMaxY(e); - } - - protected void calcMinMaxX(T e) { + protected open fun calcMinMax(e: T) { + calcMinMaxX(e) - if (e.getX() < mXMin) - mXMin = e.getX(); - - if (e.getX() > mXMax) - mXMax = e.getX(); + calcMinMaxY(e) } - protected void calcMinMaxY(T e) { - - if (e.getY() < mYMin) - mYMin = e.getY(); + protected fun calcMinMaxX(e: T) { + if (e.x < mXMin) mXMin = e.x - if (e.getY() > mYMax) - mYMax = e.getY(); + if (e.x > mXMax) mXMax = e.x } - @Override - public int getEntryCount() { - return mEntries.size(); - } + protected open fun calcMinMaxY(e: T) { + if (e.y < mYMin) mYMin = e.y - /** - * This method is deprecated. - * Use getEntries() instead. - * - * @return - */ - @Deprecated - public List getValues() { - return mEntries; - } - - /** - * Returns the array of entries that this DataSet represents. - * - * @return - */ - public List getEntries() { - return mEntries; + if (e.y > mYMax) mYMax = e.y } - /** - * This method is deprecated. - * Use setEntries(...) instead. - * - * @param values - */ - @Deprecated - public void setValues(List values) { - setEntries(values); - } + override val entryCount: Int + get() = mEntries.size + + @get:Deprecated("") + @set:Deprecated("") + var values: MutableList + /** + * This method is deprecated. + * Use getEntries() instead. + * + * @return + */ + get() = mEntries + /** + * This method is deprecated. + * Use setEntries(...) instead. + * + * @param values + */ + set(values) { + this.entries = values + } - /** - * Sets the array of entries that this DataSet represents, and calls notifyDataSetChanged() - * - * @return - */ - public void setEntries(List entries) { - mEntries = entries; - notifyDataSetChanged(); - } + var entries: MutableList + /** + * Returns the array of entries that this DataSet represents. + * + * @return + */ + get() = mEntries + /** + * Sets the array of entries that this DataSet represents, and calls notifyDataSetChanged() + * + * @return + */ + set(entries) { + mEntries = entries + notifyDataSetChanged() + } /** * Provides an exact copy of the DataSet this method is used on. * * @return */ - public abstract DataSet copy(); + abstract fun copy(): DataSet /** * @param dataSet */ - protected void copy(DataSet dataSet) { - super.copy(dataSet); + protected fun copy(dataSet: DataSet<*>) { + super.copy(dataSet) } - @Override - public String toString() { - StringBuffer buffer = new StringBuffer(); - buffer.append(toSimpleString()); - for (int i = 0; i < mEntries.size(); i++) { - buffer.append(mEntries.get(i).toString() + " "); + override fun toString(): String { + val buffer = StringBuffer() + buffer.append(toSimpleString()) + for (i in mEntries.indices) { + buffer.append(mEntries[i].toString() + " ") } - return buffer.toString(); + return buffer.toString() } /** @@ -204,270 +173,223 @@ public abstract class DataSet extends BaseDataSet implements * * @return */ - public String toSimpleString() { - StringBuffer buffer = new StringBuffer(); - buffer.append("DataSet, label: " + (getLabel() == null ? "" : getLabel()) + ", entries: " + mEntries.size() + - "\n"); - return buffer.toString(); - } - - @Override - public float getYMin() { - return mYMin; + fun toSimpleString(): String { + val buffer = StringBuffer() + buffer.append( + "DataSet, label: " + (label) + ", entries: " + mEntries.size + + "\n" + ) + return buffer.toString() } - @Override - public float getYMax() { - return mYMax; - } + override val yMin: Float + get() = mYMin - @Override - public float getXMin() { - return mXMin; - } + override val yMax: Float + get() = mYMax - @Override - public float getXMax() { - return mXMax; - } + override val xMin: Float + get() = mXMin - @Override - public void addEntryOrdered(T e) { + override val xMax: Float + get() = mXMax - if (e == null) - return; + override fun addEntryOrdered(e: T) { + calcMinMax(e) - if (mEntries == null) { - mEntries = new ArrayList(); - } - - calcMinMax(e); - - if (mEntries.size() > 0 && mEntries.get(mEntries.size() - 1).getX() > e.getX()) { - int closestIndex = getEntryIndex(e.getX(), e.getY(), Rounding.UP); - mEntries.add(closestIndex, e); + if (mEntries.isNotEmpty() && mEntries[mEntries.size - 1].x > e.x) { + val closestIndex = getEntryIndex(e.x, e.y, Rounding.UP) + mEntries.add(closestIndex, e) } else { - mEntries.add(e); + mEntries.add(e) } } - @Override - public void clear() { - mEntries.clear(); - notifyDataSetChanged(); + override fun clear() { + mEntries.clear() + notifyDataSetChanged() } - @Override - public boolean addEntry(T e) { + override fun addEntry(e: T): Boolean { - if (e == null) - return false; + val values = this.entries - List values = getEntries(); - if (values == null) { - values = new ArrayList<>(); - } - - calcMinMax(e); + calcMinMax(e) // add the entry - return values.add(e); + return values.add(e) } - @Override - public boolean removeEntry(T e) { - - if (e == null) - return false; - - if (mEntries == null) - return false; + override fun removeEntry(e: T?): Boolean { + if (e == null) return false // remove the entry - boolean removed = mEntries.remove(e); + val removed = mEntries.remove(e) if (removed) { - calcMinMax(); + calcMinMax() } - return removed; + return removed } - @Override - public int getEntryIndex(Entry e) { - return mEntries.indexOf(e); + override fun getEntryIndex(e: Entry): Int { + return mEntries.indexOf(e) } - @Override - public T getEntryForXValue(float xValue, float closestToY, Rounding rounding) { - - int index = getEntryIndex(xValue, closestToY, rounding); - if (index > -1) - return mEntries.get(index); - return null; + override fun getEntryForXValue(xValue: Float, closestToY: Float, rounding: Rounding?): T? { + val index = getEntryIndex(xValue, closestToY, rounding) + if (index > -1) return mEntries[index] + return null } - @Override - public T getEntryForXValue(float xValue, float closestToY) { - return getEntryForXValue(xValue, closestToY, Rounding.CLOSEST); + override fun getEntryForXValue(xValue: Float, closestToY: Float): T? { + return getEntryForXValue(xValue, closestToY, Rounding.CLOSEST) } - @Override - public T getEntryForIndex(int index) { - if (index < 0) - return null; - if (index >= mEntries.size()) - return null; - return mEntries.get(index); + override fun getEntryForIndex(index: Int): T { + if (index < 0) throw ArrayIndexOutOfBoundsException(index) + if (index >= mEntries.size) throw ArrayIndexOutOfBoundsException(index) + return mEntries[index] } - @Override - public int getEntryIndex(float xValue, float closestToY, Rounding rounding) { - - if (mEntries == null || mEntries.isEmpty()) - return -1; + override fun getEntryIndex(xValue: Float, closestToY: Float, rounding: Rounding?): Int { + if (mEntries.isEmpty()) return -1 - int low = 0; - int high = mEntries.size() - 1; - int closest = high; + var low = 0 + var high = mEntries.size - 1 + var closest = high while (low < high) { - int m = low + (high - low) / 2; + val m = low + (high - low) / 2 - Entry currentEntry = mEntries.get(m); + val currentEntry: Entry? = mEntries[m] if (currentEntry != null) { - Entry nextEntry = mEntries.get(m + 1); + val nextEntry: Entry? = mEntries[m + 1] if (nextEntry != null) { - final float d1 = currentEntry.getX() - xValue, - d2 = nextEntry.getX() - xValue, - ad1 = Math.abs(d1), - ad2 = Math.abs(d2); + val d1 = currentEntry.x - xValue + val d2 = nextEntry.x - xValue + val ad1 = abs(d1) + val ad2 = abs(d2) if (ad2 < ad1) { // [m + 1] is closer to xValue // Search in an higher place - low = m + 1; + low = m + 1 } else if (ad1 < ad2) { // [m] is closer to xValue // Search in a lower place - high = m; + high = m } else { // We have multiple sequential x-value with same distance if (d1 >= 0.0) { // Search in a lower place - high = m; + high = m } else if (d1 < 0.0) { // Search in an higher place - low = m + 1; + low = m + 1 } } - closest = high; + closest = high } } } if (closest != -1) { - Entry closestEntry = mEntries.get(closest); + val closestEntry: Entry? = mEntries[closest] if (closestEntry != null) { - float closestXValue = closestEntry.getX(); + val closestXValue = closestEntry.x if (rounding == Rounding.UP) { // If rounding up, and found x-value is lower than specified x, and we can go upper... - if (closestXValue < xValue && closest < mEntries.size() - 1) { - ++closest; + if (closestXValue < xValue && closest < mEntries.size - 1) { + ++closest } } else if (rounding == Rounding.DOWN) { // If rounding down, and found x-value is upper than specified x, and we can go lower... if (closestXValue > xValue && closest > 0) { - --closest; + --closest } } // Search by closest to y-value - if (!Float.isNaN(closestToY)) { - while (closest > 0 && mEntries.get(closest - 1).getX() == closestXValue) - closest -= 1; + if (!closestToY.isNaN()) { + while (closest > 0 && mEntries[closest - 1].x == closestXValue) closest -= 1 - float closestYValue = closestEntry.getY(); - int closestYIndex = closest; + var closestYValue = closestEntry.y + var closestYIndex = closest while (true) { - closest += 1; - if (closest >= mEntries.size()) - break; + closest += 1 + if (closest >= mEntries.size) break - final Entry value = mEntries.get(closest); + val value: Entry? = mEntries[closest] if (value == null) { - continue; + continue } - if (value.getX() != closestXValue) - break; + if (value.x != closestXValue) break - if (Math.abs(value.getY() - closestToY) <= Math.abs(closestYValue - closestToY)) { - closestYValue = closestToY; - closestYIndex = closest; + if (abs(value.y - closestToY) <= abs(closestYValue - closestToY)) { + closestYValue = closestToY + closestYIndex = closest } } - closest = closestYIndex; + closest = closestYIndex } } } - return closest; + return closest } - @Override - public List getEntriesForXValue(float xValue) { - - List entries = new ArrayList(); + override fun getEntriesForXValue(xValue: Float): MutableList { + val entries: MutableList = ArrayList() - int low = 0; - int high = mEntries.size() - 1; + var low = 0 + var high = mEntries.size - 1 while (low <= high) { - int m = (high + low) / 2; - T entry = mEntries.get(m); + var m = (high + low) / 2 + var entry = mEntries[m] // if we have a match - if (xValue == entry.getX()) { - while (m > 0 && mEntries.get(m - 1).getX() == xValue) - m--; + if (xValue == entry.x) { + while (m > 0 && mEntries[m - 1].x == xValue) m-- - high = mEntries.size(); + high = mEntries.size // loop over all "equal" entries - for (; m < high; m++) { - entry = mEntries.get(m); - if (entry.getX() == xValue) { - entries.add(entry); + while (m < high) { + entry = mEntries[m] + if (entry.x == xValue) { + entries.add(entry) } else { - break; + break } + m++ } - break; + break } else { - if (xValue > entry.getX()) - low = m + 1; - else - high = m - 1; + if (xValue > entry.x) low = m + 1 + else high = m - 1 } } - return entries; + return entries } /** * Determines how to round DataSet index values for - * {@link DataSet#getEntryIndex(float, float, Rounding)} DataSet.getEntryIndex()} + * [DataSet.getEntryIndex] DataSet.getEntryIndex()} * when an exact x-index is not found. */ - public enum Rounding { + enum class Rounding { UP, DOWN, CLOSEST, diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/Entry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/Entry.kt index b02f4c10f5..482de2d8be 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/Entry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/Entry.kt @@ -72,7 +72,7 @@ open class Entry : BaseEntry, Parcelable, Serializable { * * @return */ - open fun copy(): Entry? { + open fun copy(): Entry { val e = Entry(x, y, data) return e } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineData.kt index 4cf544874b..afc96cb7d9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineData.kt @@ -1,27 +1,16 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; - -import java.util.ArrayList; -import java.util.List; +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** * Data object that encapsulates all data associated with a LineChart. - * + * * @author Philipp Jahoda */ -public class LineData extends BarLineScatterCandleBubbleData { - - public LineData() { - super(); - } +class LineData : BarLineScatterCandleBubbleData { + constructor() : super() - public LineData(ILineDataSet... dataSets) { - super(dataSets); - } + constructor(vararg dataSets: ILineDataSet) : super(*dataSets) - public LineData(List dataSets) { - super(dataSets); - } + constructor(dataSets: MutableList) : super(dataSets) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt index 6590d5800e..0bc6b77523 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt @@ -1,110 +1,111 @@ - -package com.github.mikephil.charting.data; - -import android.content.Context; -import android.graphics.Color; -import android.graphics.DashPathEffect; -import android.util.Log; - -import com.github.mikephil.charting.formatter.DefaultFillFormatter; -import com.github.mikephil.charting.formatter.IFillFormatter; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.Utils; - -import java.util.ArrayList; -import java.util.List; - -public class LineDataSet extends LineRadarDataSet implements ILineDataSet { - +package com.github.mikephil.charting.data + +import android.content.Context +import android.graphics.Color +import android.graphics.DashPathEffect +import android.util.Log +import com.github.mikephil.charting.formatter.DefaultFillFormatter +import com.github.mikephil.charting.formatter.IFillFormatter +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.Utils + +class LineDataSet(yVals: MutableList, label: String) : LineRadarDataSet(yVals, label), ILineDataSet { /** * Drawing mode for this line dataset - **/ - private LineDataSet.Mode mMode = Mode.LINEAR; + */ + private var mMode: Mode? = Mode.LINEAR + /** + * returns all colors specified for the circles + * + * @return + */ + /** + * Sets the colors that should be used for the circles of this DataSet. + * Colors are reused as soon as the number of Entries the DataSet represents + * is higher than the size of the colors array. Make sure that the colors + * are already prepared (by calling getResources().getColor(...)) before + * adding them to the DataSet. + * + * @param colors + */ /** * List representing all colors that are used for the circles */ - private List mCircleColors = null; + var circleColors: MutableList = mutableListOf() /** * the color of the inner circles */ - private int mCircleHoleColor = Color.WHITE; + private var mCircleHoleColor = Color.WHITE /** * the radius of the circle-shaped value indicators */ - private float mCircleRadius = 8f; + private var mCircleRadius = 8f /** * the hole radius of the circle-shaped value indicators */ - private float mCircleHoleRadius = 4f; + private var mCircleHoleRadius = 4f /** * sets the intensity of the cubic lines */ - private float mCubicIntensity = 0.2f; + private var mCubicIntensity = 0.2f /** * the path effect of this DataSet that makes dashed lines possible */ - private DashPathEffect mDashPathEffect = null; + private var mDashPathEffect: DashPathEffect? = null /** * formatter for customizing the position of the fill-line */ - private IFillFormatter mFillFormatter = new DefaultFillFormatter(); + private var mFillFormatter: IFillFormatter = DefaultFillFormatter() /** * if true, drawing circles is enabled */ - private boolean mDrawCircles = true; + private var mDrawCircles = true - private boolean mDrawCircleHole = true; + private var mDrawCircleHole = true - public LineDataSet(List yVals, String label) { - super(yVals, label); - + init { // mCircleRadius = Utils.convertDpToPixel(4f); // mLineWidth = Utils.convertDpToPixel(1f); - - if (mCircleColors == null) { - mCircleColors = new ArrayList(); - } - mCircleColors.clear(); + circleColors.clear() // default colors // mColors.add(Color.rgb(192, 255, 140)); // mColors.add(Color.rgb(255, 247, 140)); - mCircleColors.add(Color.rgb(140, 234, 255)); + circleColors.add(Color.rgb(140, 234, 255)) } - @Override - public DataSet copy() { - List entries = new ArrayList(); - for (int i = 0; i < mEntries.size(); i++) { - entries.add(mEntries.get(i).copy()); + override fun copy(): DataSet { + val entries: MutableList = ArrayList() + for (i in mEntries.indices) { + entries.add(mEntries[i].copy()) } - LineDataSet copied = new LineDataSet(entries, getLabel()); - copy(copied); - return copied; + val copied = LineDataSet(entries, label) + copy(copied) + return copied } - protected void copy(LineDataSet lineDataSet) { - super.copy((BaseDataSet) lineDataSet); - lineDataSet.mCircleColors = mCircleColors; - lineDataSet.mCircleHoleColor = mCircleHoleColor; - lineDataSet.mCircleHoleRadius = mCircleHoleRadius; - lineDataSet.mCircleRadius = mCircleRadius; - lineDataSet.mCubicIntensity = mCubicIntensity; - lineDataSet.mDashPathEffect = mDashPathEffect; - lineDataSet.mDrawCircleHole = mDrawCircleHole; - lineDataSet.mDrawCircles = mDrawCircleHole; - lineDataSet.mFillFormatter = mFillFormatter; - lineDataSet.mMode = mMode; + protected fun copy(lineDataSet: LineDataSet) { + super.copy((lineDataSet as BaseDataSet<*>?)!!) + lineDataSet.circleColors = this.circleColors + lineDataSet.mCircleHoleColor = mCircleHoleColor + lineDataSet.mCircleHoleRadius = mCircleHoleRadius + lineDataSet.mCircleRadius = mCircleRadius + lineDataSet.mCubicIntensity = mCubicIntensity + lineDataSet.mDashPathEffect = mDashPathEffect + lineDataSet.mDrawCircleHole = mDrawCircleHole + lineDataSet.mDrawCircles = mDrawCircleHole + lineDataSet.mFillFormatter = mFillFormatter + lineDataSet.mMode = mMode } /** @@ -112,18 +113,16 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @return */ - @Override - public LineDataSet.Mode getMode() { - return mMode; - } + override val mode: Mode? + get() = mMode /** * Returns the drawing mode for this LineDataSet * * @return */ - public void setMode(LineDataSet.Mode mode) { - mMode = mode; + fun setMode(mode: Mode?) { + mMode = mode } /** @@ -132,20 +131,16 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param intensity */ - public void setCubicIntensity(float intensity) { + fun setCubicIntensity(intensity: Float) { + var intensity = intensity + if (intensity > 1f) intensity = 1f + if (intensity < 0.05f) intensity = 0.05f - if (intensity > 1f) - intensity = 1f; - if (intensity < 0.05f) - intensity = 0.05f; - - mCubicIntensity = intensity; + mCubicIntensity = intensity } - @Override - public float getCubicIntensity() { - return mCubicIntensity; - } + override val cubicIntensity: Float + get() = mCubicIntensity /** @@ -154,19 +149,16 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param radius */ - public void setCircleRadius(float radius) { - + fun setCircleRadius(radius: Float) { if (radius >= 1f) { - mCircleRadius = Utils.convertDpToPixel(radius); + mCircleRadius = Utils.convertDpToPixel(radius) } else { - Log.e("LineDataSet", "Circle radius cannot be < 1"); + Log.e("LineDataSet", "Circle radius cannot be < 1") } } - @Override - public float getCircleRadius() { - return mCircleRadius; - } + override val circleRadius: Float + get() = mCircleRadius /** * Sets the hole radius of the drawn circles. @@ -174,40 +166,36 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param holeRadius */ - public void setCircleHoleRadius(float holeRadius) { - + fun setCircleHoleRadius(holeRadius: Float) { if (holeRadius >= 0.5f) { - mCircleHoleRadius = Utils.convertDpToPixel(holeRadius); + mCircleHoleRadius = Utils.convertDpToPixel(holeRadius) } else { - Log.e("LineDataSet", "Circle radius cannot be < 0.5"); + Log.e("LineDataSet", "Circle radius cannot be < 0.5") } } - @Override - public float getCircleHoleRadius() { - return mCircleHoleRadius; - } - - /** - * sets the size (radius) of the circle shpaed value indicators, - * default size = 4f - *

- * This method is deprecated because of unclarity. Use setCircleRadius instead. - * - * @param size - */ - @Deprecated - public void setCircleSize(float size) { - setCircleRadius(size); - } - - /** - * This function is deprecated because of unclarity. Use getCircleRadius instead. - */ - @Deprecated - public float getCircleSize() { - return getCircleRadius(); - } + override val circleHoleRadius: Float + get() = mCircleHoleRadius + + @get:Deprecated("") + @set:Deprecated("") + var circleSize: Float + /** + * This function is deprecated because of unclarity. Use getCircleRadius instead. + */ + get() = circleRadius + /** + * sets the size (radius) of the circle shpaed value indicators, + * default size = 4f + * + * + * This method is deprecated because of unclarity. Use setCircleRadius instead. + * + * @param size + */ + set(size) { + setCircleRadius(size) + } /** * Enables the line to be drawn in dashed mode, e.g. like this @@ -218,28 +206,26 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * @param spaceLength the length of space in between the pieces * @param phase offset, in degrees (normally, use 0) */ - public void enableDashedLine(float lineLength, float spaceLength, float phase) { - mDashPathEffect = new DashPathEffect(new float[]{ + fun enableDashedLine(lineLength: Float, spaceLength: Float, phase: Float) { + mDashPathEffect = DashPathEffect( + floatArrayOf( lineLength, spaceLength - }, phase); + ), phase + ) } /** * Disables the line to be drawn in dashed mode. */ - public void disableDashedLine() { - mDashPathEffect = null; + fun disableDashedLine() { + mDashPathEffect = null } - @Override - public boolean isDashedLineEnabled() { - return mDashPathEffect == null ? false : true; - } + override val isDashedLineEnabled: Boolean + get() = mDashPathEffect != null - @Override - public DashPathEffect getDashPathEffect() { - return mDashPathEffect; - } + override val dashPathEffect: DashPathEffect? + get() = mDashPathEffect /** * set this to true to enable the drawing of circle indicators for this @@ -247,60 +233,29 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param enabled */ - public void setDrawCircles(boolean enabled) { - this.mDrawCircles = enabled; - } - - @Override - public boolean isDrawCirclesEnabled() { - return mDrawCircles; + fun setDrawCircles(enabled: Boolean) { + this.mDrawCircles = enabled } - @Deprecated - @Override - public boolean isDrawCubicEnabled() { - return mMode == Mode.CUBIC_BEZIER; - } - - @Deprecated - @Override - public boolean isDrawSteppedEnabled() { - return mMode == Mode.STEPPED; - } + override val isDrawCirclesEnabled: Boolean + get() = mDrawCircles - /** ALL CODE BELOW RELATED TO CIRCLE-COLORS */ + @Deprecated("") + override val isDrawCubicEnabled: Boolean + get() = mMode == Mode.CUBIC_BEZIER - /** - * returns all colors specified for the circles - * - * @return - */ - public List getCircleColors() { - return mCircleColors; - } + @Deprecated("") + override val isDrawSteppedEnabled: Boolean + get() = mMode == Mode.STEPPED - @Override - public int getCircleColor(int index) { - return mCircleColors.get(index); - } + /** ALL CODE BELOW RELATED TO CIRCLE-COLORS */ - @Override - public int getCircleColorCount() { - return mCircleColors.size(); + override fun getCircleColor(index: Int): Int { + return circleColors[index] } - /** - * Sets the colors that should be used for the circles of this DataSet. - * Colors are reused as soon as the number of Entries the DataSet represents - * is higher than the size of the colors array. Make sure that the colors - * are already prepared (by calling getResources().getColor(...)) before - * adding them to the DataSet. - * - * @param colors - */ - public void setCircleColors(List colors) { - mCircleColors = colors; - } + override val circleColorCount: Int + get() = circleColors.size /** * Sets the colors that should be used for the circles of this DataSet. @@ -311,8 +266,8 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param colors */ - public void setCircleColors(int... colors) { - this.mCircleColors = ColorTemplate.createColors(colors); + fun setCircleColors(vararg colors: Int) { + this.circleColors = ColorTemplate.createColors(colors) } /** @@ -325,19 +280,15 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param colors */ - public void setCircleColors(int[] colors, Context c) { + fun setCircleColors(colors: IntArray, c: Context) { + val clrs = this.circleColors + clrs.clear() - List clrs = mCircleColors; - if (clrs == null) { - clrs = new ArrayList<>(); + for (color in colors) { + clrs.add(c.resources.getColor(color)) } - clrs.clear(); - for (int color : colors) { - clrs.add(c.getResources().getColor(color)); - } - - mCircleColors = clrs; + this.circleColors = clrs } /** @@ -346,19 +297,16 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param color */ - public void setCircleColor(int color) { - resetCircleColors(); - mCircleColors.add(color); + fun setCircleColor(color: Int) { + resetCircleColors() + circleColors.add(color) } /** * resets the circle-colors array and creates a new one */ - public void resetCircleColors() { - if (mCircleColors == null) { - mCircleColors = new ArrayList(); - } - mCircleColors.clear(); + fun resetCircleColors() { + circleColors.clear() } /** @@ -366,28 +314,24 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param color */ - public void setCircleHoleColor(int color) { - mCircleHoleColor = color; + fun setCircleHoleColor(color: Int) { + mCircleHoleColor = color } - @Override - public int getCircleHoleColor() { - return mCircleHoleColor; - } + override val circleHoleColor: Int + get() = mCircleHoleColor /** * Set this to true to allow drawing a hole in each data circle. * * @param enabled */ - public void setDrawCircleHole(boolean enabled) { - mDrawCircleHole = enabled; + fun setDrawCircleHole(enabled: Boolean) { + mDrawCircleHole = enabled } - @Override - public boolean isDrawCircleHoleEnabled() { - return mDrawCircleHole; - } + override val isDrawCircleHoleEnabled: Boolean + get() = mDrawCircleHole /** * Sets a custom IFillFormatter to the chart that handles the position of the @@ -395,20 +339,15 @@ public class LineDataSet extends LineRadarDataSet implements ILineDataSet * * @param formatter */ - public void setFillFormatter(IFillFormatter formatter) { - - if (formatter == null) - mFillFormatter = new DefaultFillFormatter(); - else - mFillFormatter = formatter; + fun setFillFormatter(formatter: IFillFormatter?) { + if (formatter == null) mFillFormatter = DefaultFillFormatter() + else mFillFormatter = formatter } - @Override - public IFillFormatter getFillFormatter() { - return mFillFormatter; - } + override val fillFormatter: IFillFormatter + get() = mFillFormatter - public enum Mode { + enum class Mode { LINEAR, STEPPED, CUBIC_BEZIER, diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt index df422666e3..8010324def 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt @@ -1,57 +1,44 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import android.annotation.TargetApi; -import android.graphics.Color; -import android.graphics.drawable.Drawable; - -import com.github.mikephil.charting.interfaces.datasets.ILineRadarDataSet; -import com.github.mikephil.charting.utils.Utils; - -import java.util.List; +import android.graphics.Color +import android.graphics.drawable.Drawable +import com.github.mikephil.charting.interfaces.datasets.ILineRadarDataSet +import com.github.mikephil.charting.utils.Utils /** * Base dataset for line and radar DataSets. * * @author Philipp Jahoda */ -public abstract class LineRadarDataSet extends LineScatterCandleRadarDataSet implements ILineRadarDataSet { - +abstract class LineRadarDataSet(yVals: MutableList, label: String) : LineScatterCandleRadarDataSet(yVals, label), ILineRadarDataSet { // TODO: Move to using `Fill` class /** * the color that is used for filling the line surface */ - private int mFillColor = Color.rgb(140, 234, 255); + private var mFillColor = Color.rgb(140, 234, 255) /** * the drawable to be used for filling the line surface */ - protected Drawable mFillDrawable; + protected var mFillDrawable: Drawable? = null /** * transparency used for filling line surface */ - private int mFillAlpha = 85; + private var mFillAlpha = 85 /** * the width of the drawn data lines */ - private float mLineWidth = 2.5f; + private var mLineWidth = 2.5f /** * if true, the data will also be drawn filled */ - private boolean mDrawFilled = false; + private var mDrawFilled = false - - public LineRadarDataSet(List yVals, String label) { - super(yVals, label); - } - - @Override - public int getFillColor() { - return mFillColor; - } + override val fillColor: Int + get() = mFillColor /** * Sets the color that is used for filling the area below the line. @@ -59,30 +46,25 @@ public abstract class LineRadarDataSet extends LineScatterCandl * * @param color */ - public void setFillColor(int color) { - mFillColor = color; - mFillDrawable = null; + fun setFillColor(color: Int) { + mFillColor = color + mFillDrawable = null } - @Override - public Drawable getFillDrawable() { - return mFillDrawable; - } + override val fillDrawable: Drawable? + get() = mFillDrawable /** * Sets the drawable to be used to fill the area below the line. * * @param drawable */ - @TargetApi(18) - public void setFillDrawable(Drawable drawable) { - this.mFillDrawable = drawable; + fun setFillDrawable(drawable: Drawable?) { + this.mFillDrawable = drawable } - @Override - public int getFillAlpha() { - return mFillAlpha; - } + override val fillAlpha: Int + get() = mFillAlpha /** * sets the alpha value (transparency) that is used for filling the line @@ -90,8 +72,8 @@ public abstract class LineRadarDataSet extends LineScatterCandl * * @param alpha */ - public void setFillAlpha(int alpha) { - mFillAlpha = alpha; + fun setFillAlpha(alpha: Int) { + mFillAlpha = alpha } /** @@ -100,36 +82,29 @@ public abstract class LineRadarDataSet extends LineScatterCandl * * @param width */ - public void setLineWidth(float width) { - - if (width < 0.0f) - width = 0.0f; - if (width > 10.0f) - width = 10.0f; - mLineWidth = Utils.convertDpToPixel(width); + fun setLineWidth(width: Float) { + var width = width + if (width < 0.0f) width = 0.0f + if (width > 10.0f) width = 10.0f + mLineWidth = Utils.convertDpToPixel(width) } - @Override - public float getLineWidth() { - return mLineWidth; - } + override val lineWidth: Float + get() = mLineWidth - @Override - public void setDrawFilled(boolean filled) { - mDrawFilled = filled; + override fun setDrawFilled(enabled: Boolean) { + mDrawFilled = enabled } - @Override - public boolean isDrawFilledEnabled() { - return mDrawFilled; - } + override val isDrawFilledEnabled: Boolean + get() = mDrawFilled - protected void copy(LineRadarDataSet lineRadarDataSet) { - super.copy((BaseDataSet) lineRadarDataSet); - lineRadarDataSet.mDrawFilled = mDrawFilled; - lineRadarDataSet.mFillAlpha = mFillAlpha; - lineRadarDataSet.mFillColor = mFillColor; - lineRadarDataSet.mFillDrawable = mFillDrawable; - lineRadarDataSet.mLineWidth = mLineWidth; + protected fun copy(lineRadarDataSet: LineRadarDataSet<*>) { + super.copy((lineRadarDataSet as BaseDataSet<*>?)!!) + lineRadarDataSet.mDrawFilled = mDrawFilled + lineRadarDataSet.mFillAlpha = mFillAlpha + lineRadarDataSet.mFillColor = mFillColor + lineRadarDataSet.mFillDrawable = mFillDrawable + lineRadarDataSet.mLineWidth = mLineWidth } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt index 151294630d..18dae3ef45 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt @@ -1,79 +1,65 @@ -package com.github.mikephil.charting.data; +package com.github.mikephil.charting.data -import android.graphics.DashPathEffect; - -import com.github.mikephil.charting.interfaces.datasets.ILineScatterCandleRadarDataSet; -import com.github.mikephil.charting.utils.Utils; - -import java.util.List; +import android.graphics.DashPathEffect +import com.github.mikephil.charting.interfaces.datasets.ILineScatterCandleRadarDataSet +import com.github.mikephil.charting.utils.Utils /** * Created by Philipp Jahoda on 11/07/15. */ -public abstract class LineScatterCandleRadarDataSet extends BarLineScatterCandleBubbleDataSet implements ILineScatterCandleRadarDataSet { - - protected boolean mDrawVerticalHighlightIndicator = true; - protected boolean mDrawHorizontalHighlightIndicator = true; +abstract class LineScatterCandleRadarDataSet(yVals: MutableList, label: String) : BarLineScatterCandleBubbleDataSet(yVals, label), + ILineScatterCandleRadarDataSet { + override var isVerticalHighlightIndicatorEnabled: Boolean = true + protected set + override var isHorizontalHighlightIndicatorEnabled: Boolean = true + protected set - /** the width of the highlight indicator lines */ - protected float mHighlightLineWidth = 0.5f; + /** the width of the highlight indicator lines */ + protected var mHighlightLineWidth: Float = 0.5f - /** the path effect for dashed highlight-lines */ - protected DashPathEffect mHighlightDashPathEffect = null; + /** the path effect for dashed highlight-lines */ + override var dashPathEffectHighlight: DashPathEffect? = null + protected set - public LineScatterCandleRadarDataSet(List yVals, String label) { - super(yVals, label); - mHighlightLineWidth = Utils.convertDpToPixel(0.5f); + init { + mHighlightLineWidth = Utils.convertDpToPixel(0.5f) } /** * Enables / disables the horizontal highlight-indicator. If disabled, the indicator is not drawn. * @param enabled */ - public void setDrawHorizontalHighlightIndicator(boolean enabled) { - this.mDrawHorizontalHighlightIndicator = enabled; + fun setDrawHorizontalHighlightIndicator(enabled: Boolean) { + this.isHorizontalHighlightIndicatorEnabled = enabled } /** * Enables / disables the vertical highlight-indicator. If disabled, the indicator is not drawn. * @param enabled */ - public void setDrawVerticalHighlightIndicator(boolean enabled) { - this.mDrawVerticalHighlightIndicator = enabled; + fun setDrawVerticalHighlightIndicator(enabled: Boolean) { + this.isVerticalHighlightIndicatorEnabled = enabled } /** * Enables / disables both vertical and horizontal highlight-indicators. * @param enabled */ - public void setDrawHighlightIndicators(boolean enabled) { - setDrawVerticalHighlightIndicator(enabled); - setDrawHorizontalHighlightIndicator(enabled); - } - - @Override - public boolean isVerticalHighlightIndicatorEnabled() { - return mDrawVerticalHighlightIndicator; + fun setDrawHighlightIndicators(enabled: Boolean) { + setDrawVerticalHighlightIndicator(enabled) + setDrawHorizontalHighlightIndicator(enabled) } - @Override - public boolean isHorizontalHighlightIndicatorEnabled() { - return mDrawHorizontalHighlightIndicator; - } - - /** - * Sets the width of the highlight line in dp. - * @param width - */ - public void setHighlightLineWidth(float width) { - mHighlightLineWidth = Utils.convertDpToPixel(width); - } - - @Override - public float getHighlightLineWidth() { - return mHighlightLineWidth; - } + override var highlightLineWidth: Float + get() = mHighlightLineWidth + /** + * Sets the width of the highlight line in dp. + * @param width + */ + set(width) { + mHighlightLineWidth = Utils.convertDpToPixel(width) + } /** * Enables the highlight-line to be drawn in dashed mode, e.g. like this "- - - - - -" @@ -82,39 +68,35 @@ public abstract class LineScatterCandleRadarDataSet extends Bar * @param spaceLength the length of space inbetween the line-pieces * @param phase offset, in degrees (normally, use 0) */ - public void enableDashedHighlightLine(float lineLength, float spaceLength, float phase) { - mHighlightDashPathEffect = new DashPathEffect(new float[] { + fun enableDashedHighlightLine(lineLength: Float, spaceLength: Float, phase: Float) { + this.dashPathEffectHighlight = DashPathEffect( + floatArrayOf( lineLength, spaceLength - }, phase); + ), phase + ) } /** * Disables the highlight-line to be drawn in dashed mode. */ - public void disableDashedHighlightLine() { - mHighlightDashPathEffect = null; - } - - /** - * Returns true if the dashed-line effect is enabled for highlight lines, false if not. - * Default: disabled - * - * @return - */ - public boolean isDashedHighlightLineEnabled() { - return mHighlightDashPathEffect == null ? false : true; - } - - @Override - public DashPathEffect getDashPathEffectHighlight() { - return mHighlightDashPathEffect; + fun disableDashedHighlightLine() { + this.dashPathEffectHighlight = null } - protected void copy(LineScatterCandleRadarDataSet lineScatterCandleRadarDataSet) { - super.copy((BaseDataSet) lineScatterCandleRadarDataSet); - lineScatterCandleRadarDataSet.mDrawHorizontalHighlightIndicator = mDrawHorizontalHighlightIndicator; - lineScatterCandleRadarDataSet.mDrawVerticalHighlightIndicator = mDrawVerticalHighlightIndicator; - lineScatterCandleRadarDataSet.mHighlightLineWidth = mHighlightLineWidth; - lineScatterCandleRadarDataSet.mHighlightDashPathEffect = mHighlightDashPathEffect; + val isDashedHighlightLineEnabled: Boolean + /** + * Returns true if the dashed-line effect is enabled for highlight lines, false if not. + * Default: disabled + * + * @return + */ + get() = if (this.dashPathEffectHighlight == null) false else true + + protected fun copy(lineScatterCandleRadarDataSet: LineScatterCandleRadarDataSet<*>) { + super.copy((lineScatterCandleRadarDataSet as BaseDataSet<*>?)!!) + lineScatterCandleRadarDataSet.isHorizontalHighlightIndicatorEnabled = this.isHorizontalHighlightIndicatorEnabled + lineScatterCandleRadarDataSet.isVerticalHighlightIndicatorEnabled = this.isVerticalHighlightIndicatorEnabled + lineScatterCandleRadarDataSet.mHighlightLineWidth = mHighlightLineWidth + lineScatterCandleRadarDataSet.dashPathEffectHighlight = this.dashPathEffectHighlight } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt index 423ce19b16..743e1b880a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt @@ -1,13 +1,8 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import android.util.Log; - -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IPieDataSet; - -import java.util.ArrayList; -import java.util.List; +import android.util.Log +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IPieDataSet /** * A PieData object can only represent one DataSet. Unlike all other charts, the @@ -17,84 +12,77 @@ import java.util.List; * * @author Philipp Jahoda */ -public class PieData extends ChartData { - - public PieData() { - super(); - } +class PieData : ChartData { + constructor() : super() + + constructor(dataSet: IPieDataSet) : super(dataSet) + + var dataSet: IPieDataSet + /** + * Returns the DataSet this PieData object represents. A PieData object can + * only contain one DataSet. + * + * @return + */ + get() = dataSets[0] + /** + * Sets the PieDataSet this data object should represent. + * + * @param dataSet + */ + set(dataSet) { + dataSets.clear() + dataSets.add(dataSet) + notifyDataChanged() + } - public PieData(IPieDataSet dataSet) { - super(dataSet); - } + override val dataSets: MutableList + get() { + val dataSets = super.dataSets - /** - * Sets the PieDataSet this data object should represent. - * - * @param dataSet - */ - public void setDataSet(IPieDataSet dataSet) { - mDataSets.clear(); - mDataSets.add(dataSet); - notifyDataChanged(); - } + if (dataSets.size > 1) { + Log.e( + "MPAndroidChart", + "Found multiple data sets while pie chart only allows one" + ) + } - /** - * Returns the DataSet this PieData object represents. A PieData object can - * only contain one DataSet. - * - * @return - */ - public IPieDataSet getDataSet() { - return mDataSets.get(0); - } - - @Override - public List getDataSets() { - List dataSets = super.getDataSets(); - - if (dataSets.size() < 1) { - Log.e("MPAndroidChart", - "Found multiple data sets while pie chart only allows one"); + return dataSets } - return dataSets; - } - /** * The PieData object can only have one DataSet. Use getDataSet() method instead. * * @param index * @return */ - @Override - public IPieDataSet getDataSetByIndex(int index) { - return index == 0 ? getDataSet() : null; + override fun getDataSetByIndex(index: Int): IPieDataSet { + return if (index == 0) this.dataSet else throw ArrayIndexOutOfBoundsException(index) } - @Override - public IPieDataSet getDataSetByLabel(String label, boolean ignorecase) { - return ignorecase ? label.equalsIgnoreCase(mDataSets.get(0).getLabel()) ? mDataSets.get(0) - : null : label.equals(mDataSets.get(0).getLabel()) ? mDataSets.get(0) : null; + override fun getDataSetByLabel(label: String, ignorecase: Boolean): IPieDataSet? { + return if (ignorecase) if (label.equals(dataSets[0].label, ignoreCase = true)) + dataSets[0] + else + null else if (label == dataSets[0].label) dataSets[0] else null } - @Override - public Entry getEntryForHighlight(Highlight highlight) { - return getDataSet().getEntryForIndex((int) highlight.getX()); + override fun getEntryForHighlight(highlight: Highlight): PieEntry? { + return this.dataSet.getEntryForIndex(highlight.x.toInt()) } - /** - * Returns the sum of all values in this PieData object. - * - * @return - */ - public float getYValueSum() { + val yValueSum: Float + /** + * Returns the sum of all values in this PieData object. + * + * @return + */ + get() { + var sum = 0f - float sum = 0; + for (i in 0.. implements IPieDataSet { +import com.github.mikephil.charting.interfaces.datasets.IPieDataSet +import com.github.mikephil.charting.utils.Utils +open class PieDataSet(yVals: MutableList, label: String) : DataSet(yVals, label), IPieDataSet { /** * the space in pixels between the chart-slices, default 0f */ - private float mSliceSpace = 0f; - private boolean mAutomaticallyDisableSliceSpacing; + private var mSliceSpace = 0f + private var mAutomaticallyDisableSliceSpacing = false /** * indicates the selection distance of a pie slice */ - private float mShift = 18f; - - private ValuePosition mXValuePosition = ValuePosition.INSIDE_SLICE; - private ValuePosition mYValuePosition = ValuePosition.INSIDE_SLICE; - private int mValueLineColor = 0xff000000; - private boolean mUseValueColorForLine = false; - private float mValueLineWidth = 1.0f; - private float mValueLinePart1OffsetPercentage = 75.f; - private float mValueLinePart1Length = 0.3f; - private float mValueLinePart2Length = 0.4f; - private boolean mValueLineVariableLength = true; - private Integer mHighlightColor = null; - - public PieDataSet(List yVals, String label) { - super(yVals, label); -// mShift = Utils.convertDpToPixel(12f); - } - - @Override - public DataSet copy() { - List entries = new ArrayList<>(); - for (int i = 0; i < mEntries.size(); i++) { - entries.add(mEntries.get(i).copy()); + private var mShift = 18f + + private var mXValuePosition: ValuePosition? = ValuePosition.INSIDE_SLICE + private var mYValuePosition: ValuePosition? = ValuePosition.INSIDE_SLICE + private var mValueLineColor = -0x1000000 + private var mUseValueColorForLine = false + private var mValueLineWidth = 1.0f + private var mValueLinePart1OffsetPercentage = 75f + private var mValueLinePart1Length = 0.3f + private var mValueLinePart2Length = 0.4f + private var mValueLineVariableLength = true + private var mHighlightColor: Int? = null + + override fun copy(): DataSet { + val entries: MutableList = ArrayList() + for (i in mEntries.indices) { + entries.add(mEntries[i].copy()) } - PieDataSet copied = new PieDataSet(entries, getLabel()); - copy(copied); - return copied; + val copied = PieDataSet(entries, label) + copy(copied) + return copied } - protected void copy(PieDataSet pieDataSet) { - super.copy((BaseDataSet) pieDataSet); + protected fun copy(pieDataSet: PieDataSet) { + super.copy((pieDataSet as BaseDataSet<*>?)!!) } - @Override - protected void calcMinMax(PieEntry e) { - - if (e == null) - return; - - calcMinMaxY(e); + override fun calcMinMax(e: PieEntry) { + calcMinMaxY(e) } /** @@ -68,20 +50,16 @@ public class PieDataSet extends DataSet implements IPieDataSet { * * @param spaceDp */ - public void setSliceSpace(float spaceDp) { + fun setSliceSpace(spaceDp: Float) { + var spaceDp = spaceDp + if (spaceDp > 20) spaceDp = 20f + if (spaceDp < 0) spaceDp = 0f - if (spaceDp > 20) - spaceDp = 20f; - if (spaceDp < 0) - spaceDp = 0f; - - mSliceSpace = Utils.convertDpToPixel(spaceDp); + mSliceSpace = Utils.convertDpToPixel(spaceDp) } - @Override - public float getSliceSpace() { - return mSliceSpace; - } + override val sliceSpace: Float + get() = mSliceSpace /** * When enabled, slice spacing will be 0.0 when the smallest value is going to be @@ -89,8 +67,8 @@ public class PieDataSet extends DataSet implements IPieDataSet { * * @param autoDisable */ - public void setAutomaticallyDisableSliceSpacing(boolean autoDisable) { - mAutomaticallyDisableSliceSpacing = autoDisable; + fun setAutomaticallyDisableSliceSpacing(autoDisable: Boolean) { + mAutomaticallyDisableSliceSpacing = autoDisable } /** @@ -99,10 +77,8 @@ public class PieDataSet extends DataSet implements IPieDataSet { * * @return */ - @Override - public boolean isAutomaticallyDisableSliceSpacingEnabled() { - return mAutomaticallyDisableSliceSpacing; - } + override val isAutomaticallyDisableSliceSpacingEnabled: Boolean + get() = mAutomaticallyDisableSliceSpacing /** * sets the distance the highlighted piechart-slice of this DataSet is @@ -110,152 +86,123 @@ public class PieDataSet extends DataSet implements IPieDataSet { * * @param shift */ - public void setSelectionShift(float shift) { - mShift = Utils.convertDpToPixel(shift); + fun setSelectionShift(shift: Float) { + mShift = Utils.convertDpToPixel(shift) } - @Override - public float getSelectionShift() { - return mShift; - } + override val selectionShift: Float + get() = mShift - @Override - public ValuePosition getXValuePosition() { - return mXValuePosition; - } + override val xValuePosition: ValuePosition? + get() = mXValuePosition - public void setXValuePosition(ValuePosition xValuePosition) { - this.mXValuePosition = xValuePosition; + fun setXValuePosition(xValuePosition: ValuePosition?) { + this.mXValuePosition = xValuePosition } - @Override - public ValuePosition getYValuePosition() { - return mYValuePosition; - } + override val yValuePosition: ValuePosition? + get() = mYValuePosition - public void setYValuePosition(ValuePosition yValuePosition) { - this.mYValuePosition = yValuePosition; + fun setYValuePosition(yValuePosition: ValuePosition?) { + this.mYValuePosition = yValuePosition } - /** - * This method is deprecated. - * Use isUseValueColorForLineEnabled() instead. - */ - @Deprecated - public boolean isUsingSliceColorAsValueLineColor() { - return isUseValueColorForLineEnabled(); - } - - /** - * This method is deprecated. - * Use setUseValueColorForLine(...) instead. - * - * @param enabled - */ - @Deprecated - public void setUsingSliceColorAsValueLineColor(boolean enabled) { - setUseValueColorForLine(enabled); - } + @get:Deprecated("") + @set:Deprecated("") + var isUsingSliceColorAsValueLineColor: Boolean + /** + * This method is deprecated. + * Use isUseValueColorForLineEnabled() instead. + */ + get() = isUseValueColorForLineEnabled + /** + * This method is deprecated. + * Use setUseValueColorForLine(...) instead. + * + * @param enabled + */ + set(enabled) { + setUseValueColorForLine(enabled) + } /** * When valuePosition is OutsideSlice, indicates line color */ - @Override - public int getValueLineColor() { - return mValueLineColor; - } + override val valueLineColor: Int + get() = mValueLineColor - public void setValueLineColor(int valueLineColor) { - this.mValueLineColor = valueLineColor; + fun setValueLineColor(valueLineColor: Int) { + this.mValueLineColor = valueLineColor } - @Override - public boolean isUseValueColorForLineEnabled() - { - return mUseValueColorForLine; - } + override val isUseValueColorForLineEnabled: Boolean + get() = mUseValueColorForLine - public void setUseValueColorForLine(boolean enabled) - { - mUseValueColorForLine = enabled; + fun setUseValueColorForLine(enabled: Boolean) { + mUseValueColorForLine = enabled } /** * When valuePosition is OutsideSlice, indicates line width */ - @Override - public float getValueLineWidth() { - return mValueLineWidth; - } + override val valueLineWidth: Float + get() = mValueLineWidth - public void setValueLineWidth(float valueLineWidth) { - this.mValueLineWidth = valueLineWidth; + fun setValueLineWidth(valueLineWidth: Float) { + this.mValueLineWidth = valueLineWidth } /** * When valuePosition is OutsideSlice, indicates offset as percentage out of the slice size */ - @Override - public float getValueLinePart1OffsetPercentage() { - return mValueLinePart1OffsetPercentage; - } + override val valueLinePart1OffsetPercentage: Float + get() = mValueLinePart1OffsetPercentage - public void setValueLinePart1OffsetPercentage(float valueLinePart1OffsetPercentage) { - this.mValueLinePart1OffsetPercentage = valueLinePart1OffsetPercentage; + fun setValueLinePart1OffsetPercentage(valueLinePart1OffsetPercentage: Float) { + this.mValueLinePart1OffsetPercentage = valueLinePart1OffsetPercentage } /** * When valuePosition is OutsideSlice, indicates length of first half of the line */ - @Override - public float getValueLinePart1Length() { - return mValueLinePart1Length; - } + override val valueLinePart1Length: Float + get() = mValueLinePart1Length - public void setValueLinePart1Length(float valueLinePart1Length) { - this.mValueLinePart1Length = valueLinePart1Length; + fun setValueLinePart1Length(valueLinePart1Length: Float) { + this.mValueLinePart1Length = valueLinePart1Length } /** * When valuePosition is OutsideSlice, indicates length of second half of the line */ - @Override - public float getValueLinePart2Length() { - return mValueLinePart2Length; - } + override val valueLinePart2Length: Float + get() = mValueLinePart2Length - public void setValueLinePart2Length(float valueLinePart2Length) { - this.mValueLinePart2Length = valueLinePart2Length; + fun setValueLinePart2Length(valueLinePart2Length: Float) { + this.mValueLinePart2Length = valueLinePart2Length } /** * When valuePosition is OutsideSlice, this allows variable line length */ - @Override - public boolean isValueLineVariableLength() { - return mValueLineVariableLength; - } + override val isValueLineVariableLength: Boolean + get() = mValueLineVariableLength - public void setValueLineVariableLength(boolean valueLineVariableLength) { - this.mValueLineVariableLength = valueLineVariableLength; + fun setValueLineVariableLength(valueLineVariableLength: Boolean) { + this.mValueLineVariableLength = valueLineVariableLength } - /** Gets the color for the highlighted sector */ - @Override - @Nullable - public Integer getHighlightColor() - { - return mHighlightColor; - } + /** Gets the color for the highlighted sector */ + override val highlightColor: Int? + get() = mHighlightColor - /** Sets the color for the highlighted sector (null for using entry color) */ - public void setHighlightColor(@Nullable Integer color) - { - this.mHighlightColor = color; + /** Sets the color for the highlighted sector (null for using entry color) */ + fun setHighlightColor(color: Int?) { + this.mHighlightColor = color } - public enum ValuePosition { + enum class ValuePosition { INSIDE_SLICE, OUTSIDE_SLICE } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt index 65741ef1da..88fc5872b2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt @@ -1,86 +1,62 @@ -package com.github.mikephil.charting.data; +package com.github.mikephil.charting.data -import android.annotation.SuppressLint; -import android.graphics.drawable.Drawable; -import android.util.Log; +import android.annotation.SuppressLint +import android.graphics.drawable.Drawable +import android.util.Log /** * @author Philipp Jahoda */ @SuppressLint("ParcelCreator") -public class PieEntry extends Entry { +class PieEntry : Entry { + var label: String? = null - private String label; + constructor(value: Float) : super(0f, value) - public PieEntry(float value) { - super(0f, value); - } + constructor(value: Float, data: Any?) : super(0f, value, data) - public PieEntry(float value, Object data) { - super(0f, value, data); - } + constructor(value: Float, icon: Drawable?) : super(0f, value, icon) - public PieEntry(float value, Drawable icon) { - super(0f, value, icon); - } + constructor(value: Float, icon: Drawable?, data: Any?) : super(0f, value, icon, data) - public PieEntry(float value, Drawable icon, Object data) { - super(0f, value, icon, data); + constructor(value: Float, label: String?) : super(0f, value) { + this.label = label } - public PieEntry(float value, String label) { - super(0f, value); - this.label = label; + constructor(value: Float, label: String?, data: Any?) : super(0f, value, data) { + this.label = label } - public PieEntry(float value, String label, Object data) { - super(0f, value, data); - this.label = label; + constructor(value: Float, label: String?, icon: Drawable?) : super(0f, value, icon) { + this.label = label } - public PieEntry(float value, String label, Drawable icon) { - super(0f, value, icon); - this.label = label; + constructor(value: Float, label: String?, icon: Drawable?, data: Any?) : super(0f, value, icon, data) { + this.label = label } - public PieEntry(float value, String label, Drawable icon, Object data) { - super(0f, value, icon, data); - this.label = label; - } + val value: Float + /** + * This is the same as getY(). Returns the value of the PieEntry. + * + * @return + */ + get() = y - /** - * This is the same as getY(). Returns the value of the PieEntry. - * - * @return - */ - public float getValue() { - return getY(); - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - @Deprecated - @Override - public void setX(float x) { - super.setX(x); - Log.i("DEPRECATED", "Pie entries do not have x values"); - } - - @Deprecated - @Override - public float getX() { - Log.i("DEPRECATED", "Pie entries do not have x values"); - return super.getX(); - } + @get:Deprecated("") + @set:Deprecated("") + override var x: Float + get() { + Log.i("DEPRECATED", "Pie entries do not have x values") + return super.x + } + set(x) { + super.x = x + Log.i("DEPRECATED", "Pie entries do not have x values") + } - public PieEntry copy() { - PieEntry e = new PieEntry(getY(), label, getData()); - return e; + public override fun copy(): PieEntry { + val e = PieEntry(y, label, data) + return e } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt index 0c1dbe5505..b695d14123 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt @@ -1,58 +1,37 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; - -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet /** * Data container for the RadarChart. * * @author Philipp Jahoda */ -public class RadarData extends ChartData { - - private List mLabels; - - public RadarData() { - super(); - } - - public RadarData(List dataSets) { - super(dataSets); - } - - public RadarData(IRadarDataSet... dataSets) { - super(dataSets); - } - +class RadarData : ChartData { /** * Sets the labels that should be drawn around the RadarChart at the end of each web line. * * @param labels */ - public void setLabels(List labels) { - this.mLabels = labels; - } + var labels: MutableList? = null + + constructor() : super() + + constructor(dataSets: MutableList) : super(dataSets) + + constructor(vararg dataSets: IRadarDataSet) : super(*dataSets) /** * Sets the labels that should be drawn around the RadarChart at the end of each web line. * * @param labels */ - public void setLabels(String... labels) { - this.mLabels = Arrays.asList(labels); - } - - public List getLabels() { - return mLabels; + fun setLabels(vararg labels: String) { + this.labels = mutableListOf(*labels) } - @Override - public Entry getEntryForHighlight(Highlight highlight) { - return getDataSetByIndex(highlight.getDataSetIndex()).getEntryForIndex((int) highlight.getX()); + override fun getEntryForHighlight(highlight: Highlight): RadarEntry? { + return getDataSetByIndex(highlight.dataSetIndex)!!.getEntryForIndex(highlight.x.toInt()) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt index a01604b8c3..41c751fd4d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt @@ -1,122 +1,40 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; +import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet +import com.github.mikephil.charting.utils.ColorTemplate -import android.graphics.Color; +open class RadarDataSet(yVals: MutableList, label: String) : LineRadarDataSet(yVals, label), IRadarDataSet { + /** The stroke color for highlight circle. + * If Utils.COLOR_NONE, the color of the dataset is taken. */ + override var highlightCircleStrokeColor: Int = ColorTemplate.COLOR_NONE -import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; + override var highlightCircleStrokeAlpha: Int = (0.3 * 255).toInt() + override var highlightCircleInnerRadius: Float = 3.0f + override var highlightCircleOuterRadius: Float = 4.0f + override var highlightCircleStrokeWidth: Float = 2.0f -import java.util.ArrayList; -import java.util.List; + /** Returns true if highlight circle should be drawn, false if not */ + override var isDrawHighlightCircleEnabled: Boolean = false -public class RadarDataSet extends LineRadarDataSet implements IRadarDataSet { + override var highlightCircleFillColor: Int = ColorTemplate.COLOR_NONE - /// flag indicating whether highlight circle should be drawn or not - protected boolean mDrawHighlightCircleEnabled = false; - - protected int mHighlightCircleFillColor = Color.WHITE; - - /// The stroke color for highlight circle. - /// If Utils.COLOR_NONE, the color of the dataset is taken. - protected int mHighlightCircleStrokeColor = ColorTemplate.COLOR_NONE; - - protected int mHighlightCircleStrokeAlpha = (int) (0.3 * 255); - protected float mHighlightCircleInnerRadius = 3.0f; - protected float mHighlightCircleOuterRadius = 4.0f; - protected float mHighlightCircleStrokeWidth = 2.0f; - - public RadarDataSet(List yVals, String label) { - super(yVals, label); - } - - /// Returns true if highlight circle should be drawn, false if not - @Override - public boolean isDrawHighlightCircleEnabled() { - return mDrawHighlightCircleEnabled; - } - - /// Sets whether highlight circle should be drawn or not - @Override - public void setDrawHighlightCircleEnabled(boolean enabled) { - mDrawHighlightCircleEnabled = enabled; - } - - @Override - public int getHighlightCircleFillColor() { - return mHighlightCircleFillColor; - } - - public void setHighlightCircleFillColor(int color) { - mHighlightCircleFillColor = color; - } - - /// Returns the stroke color for highlight circle. - /// If Utils.COLOR_NONE, the color of the dataset is taken. - @Override - public int getHighlightCircleStrokeColor() { - return mHighlightCircleStrokeColor; - } - - /// Sets the stroke color for highlight circle. - /// Set to Utils.COLOR_NONE in order to use the color of the dataset; - public void setHighlightCircleStrokeColor(int color) { - mHighlightCircleStrokeColor = color; - } - - @Override - public int getHighlightCircleStrokeAlpha() { - return mHighlightCircleStrokeAlpha; - } - - public void setHighlightCircleStrokeAlpha(int alpha) { - mHighlightCircleStrokeAlpha = alpha; - } - - @Override - public float getHighlightCircleInnerRadius() { - return mHighlightCircleInnerRadius; - } - - public void setHighlightCircleInnerRadius(float radius) { - mHighlightCircleInnerRadius = radius; - } - - @Override - public float getHighlightCircleOuterRadius() { - return mHighlightCircleOuterRadius; - } - - public void setHighlightCircleOuterRadius(float radius) { - mHighlightCircleOuterRadius = radius; - } - - @Override - public float getHighlightCircleStrokeWidth() { - return mHighlightCircleStrokeWidth; - } - - public void setHighlightCircleStrokeWidth(float strokeWidth) { - mHighlightCircleStrokeWidth = strokeWidth; - } - - @Override - public DataSet copy() { - List entries = new ArrayList(); - for (int i = 0; i < mEntries.size(); i++) { - entries.add(mEntries.get(i).copy()); + override fun copy(): DataSet { + val entries: MutableList = ArrayList() + for (i in mEntries.indices) { + entries.add(mEntries[i].copy()) } - RadarDataSet copied = new RadarDataSet(entries, getLabel()); - copy(copied); - return copied; - } - - protected void copy(RadarDataSet radarDataSet) { - super.copy((BaseDataSet) radarDataSet); - radarDataSet.mDrawHighlightCircleEnabled = mDrawHighlightCircleEnabled; - radarDataSet.mHighlightCircleFillColor = mHighlightCircleFillColor; - radarDataSet.mHighlightCircleInnerRadius = mHighlightCircleInnerRadius; - radarDataSet.mHighlightCircleStrokeAlpha = mHighlightCircleStrokeAlpha; - radarDataSet.mHighlightCircleStrokeColor = mHighlightCircleStrokeColor; - radarDataSet.mHighlightCircleStrokeWidth = mHighlightCircleStrokeWidth; + val copied = RadarDataSet(entries, label) + copy(copied) + return copied + } + + protected fun copy(radarDataSet: RadarDataSet) { + super.copy((radarDataSet as BaseDataSet<*>?)!!) + radarDataSet.isDrawHighlightCircleEnabled = isDrawHighlightCircleEnabled + radarDataSet.highlightCircleFillColor = highlightCircleFillColor + radarDataSet.highlightCircleInnerRadius = highlightCircleInnerRadius + radarDataSet.highlightCircleStrokeAlpha = highlightCircleStrokeAlpha + radarDataSet.highlightCircleStrokeColor = highlightCircleStrokeColor + radarDataSet.highlightCircleStrokeWidth = highlightCircleStrokeWidth } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt index 02fdce7d32..dc17af9706 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt @@ -1,44 +1,33 @@ -package com.github.mikephil.charting.data; +package com.github.mikephil.charting.data -import android.annotation.SuppressLint; +import android.annotation.SuppressLint /** * Created by philipp on 13/06/16. */ @SuppressLint("ParcelCreator") -public class RadarEntry extends Entry { - - public RadarEntry(float value) { - super(0f, value); - } - - public RadarEntry(float value, Object data) { - super(0f, value, data); - } - - /** - * This is the same as getY(). Returns the value of the RadarEntry. - * - * @return - */ - public float getValue() { - return getY(); +class RadarEntry : Entry { + constructor(value: Float) : super(0f, value) + + constructor(value: Float, data: Any?) : super(0f, value, data) + + val value: Float + /** + * This is the same as getY(). Returns the value of the RadarEntry. + * + * @return + */ + get() = y + + public override fun copy(): RadarEntry { + val e = RadarEntry(y, data) + return e } - public RadarEntry copy() { - RadarEntry e = new RadarEntry(getY(), getData()); - return e; - } - - @Deprecated - @Override - public void setX(float x) { - super.setX(x); - } - - @Deprecated - @Override - public float getX() { - return super.getX(); - } + @set:Deprecated("") + override var x: Float + get() = super.x + set(x) { + super.x = x + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterData.kt index ba142360f9..030de587f5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterData.kt @@ -1,40 +1,29 @@ +package com.github.mikephil.charting.data -package com.github.mikephil.charting.data; +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; +class ScatterData : BarLineScatterCandleBubbleData { + constructor() : super() -import java.util.List; + constructor(dataSets: MutableList) : super(dataSets) -public class ScatterData extends BarLineScatterCandleBubbleData { + constructor(vararg dataSets: IScatterDataSet) : super(*dataSets) - public ScatterData() { - super(); - } + val greatestShapeSize: Float + /** + * Returns the maximum shape-size across all DataSets. + * + * @return + */ + get() { + var max = 0f - public ScatterData(List dataSets) { - super(dataSets); - } + for (set in dataSets) { + val size = set.scatterShapeSize - public ScatterData(IScatterDataSet... dataSets) { - super(dataSets); - } + if (size > max) max = size + } - /** - * Returns the maximum shape-size across all DataSets. - * - * @return - */ - public float getGreatestShapeSize() { - - float max = 0f; - - for (IScatterDataSet set : mDataSets) { - float size = set.getScatterShapeSize(); - - if (size > max) - max = size; + return max } - - return max; - } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt index 95b2e1ff05..309dcf35af 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt @@ -1,82 +1,57 @@ - -package com.github.mikephil.charting.data; - -import com.github.mikephil.charting.charts.ScatterChart; -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.renderer.scatter.ChevronDownShapeRenderer; -import com.github.mikephil.charting.renderer.scatter.ChevronUpShapeRenderer; -import com.github.mikephil.charting.renderer.scatter.CircleShapeRenderer; -import com.github.mikephil.charting.renderer.scatter.CrossShapeRenderer; -import com.github.mikephil.charting.renderer.scatter.IShapeRenderer; -import com.github.mikephil.charting.renderer.scatter.SquareShapeRenderer; -import com.github.mikephil.charting.renderer.scatter.TriangleShapeRenderer; -import com.github.mikephil.charting.renderer.scatter.XShapeRenderer; -import com.github.mikephil.charting.utils.ColorTemplate; - -import java.util.ArrayList; -import java.util.List; - -public class ScatterDataSet extends LineScatterCandleRadarDataSet implements IScatterDataSet { - +package com.github.mikephil.charting.data + +import com.github.mikephil.charting.charts.ScatterChart.ScatterShape +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.renderer.scatter.ChevronDownShapeRenderer +import com.github.mikephil.charting.renderer.scatter.ChevronUpShapeRenderer +import com.github.mikephil.charting.renderer.scatter.CircleShapeRenderer +import com.github.mikephil.charting.renderer.scatter.CrossShapeRenderer +import com.github.mikephil.charting.renderer.scatter.IShapeRenderer +import com.github.mikephil.charting.renderer.scatter.SquareShapeRenderer +import com.github.mikephil.charting.renderer.scatter.TriangleShapeRenderer +import com.github.mikephil.charting.renderer.scatter.XShapeRenderer +import com.github.mikephil.charting.utils.ColorTemplate + +open class ScatterDataSet(yVals: MutableList, label: String) : LineScatterCandleRadarDataSet(yVals, label), IScatterDataSet { /** * the size the scattershape will have, in density pixels */ - private float mShapeSize = 15f; + override var scatterShapeSize = 15f /** * Renderer responsible for rendering this DataSet, default: square */ - protected IShapeRenderer mShapeRenderer = new SquareShapeRenderer(); + override var shapeRenderer: IShapeRenderer? = SquareShapeRenderer() /** * The radius of the hole in the shape (applies to Square, Circle and Triangle) * - default: 0.0 */ - private float mScatterShapeHoleRadius = 0f; + override var scatterShapeHoleRadius = 0f /** * Color for the hole in the shape. * Setting to `ColorTemplate.COLOR_NONE` will behave as transparent. * - default: ColorTemplate.COLOR_NONE */ - private int mScatterShapeHoleColor = ColorTemplate.COLOR_NONE; - - public ScatterDataSet(List yVals, String label) { - super(yVals, label); - } + override var scatterShapeHoleColor = ColorTemplate.COLOR_NONE - @Override - public DataSet copy() { - List entries = new ArrayList(); - for (int i = 0; i < mEntries.size(); i++) { - entries.add(mEntries.get(i).copy()); + override fun copy(): DataSet { + val entries: MutableList = ArrayList() + for (i in mEntries.indices) { + entries.add(mEntries[i].copy()) } - ScatterDataSet copied = new ScatterDataSet(entries, getLabel()); - copy(copied); - return copied; - } - - protected void copy(ScatterDataSet scatterDataSet) { - super.copy((BaseDataSet) scatterDataSet); - scatterDataSet.mShapeSize = mShapeSize; - scatterDataSet.mShapeRenderer = mShapeRenderer; - scatterDataSet.mScatterShapeHoleRadius = mScatterShapeHoleRadius; - scatterDataSet.mScatterShapeHoleColor = mScatterShapeHoleColor; - } - - /** - * Sets the size in density pixels the drawn scattershape will have. This - * only applies for non custom shapes. - * - * @param size - */ - public void setScatterShapeSize(float size) { - mShapeSize = size; + val copied = ScatterDataSet(entries, label) + copy(copied) + return copied } - @Override - public float getScatterShapeSize() { - return mShapeSize; + protected fun copy(scatterDataSet: ScatterDataSet) { + super.copy((scatterDataSet as BaseDataSet<*>?)!!) + scatterDataSet.scatterShapeSize = scatterShapeSize + scatterDataSet.shapeRenderer = shapeRenderer + scatterDataSet.scatterShapeHoleRadius = scatterShapeHoleRadius + scatterDataSet.scatterShapeHoleColor = scatterShapeHoleColor } /** @@ -85,8 +60,8 @@ public class ScatterDataSet extends LineScatterCandleRadarDataSet impleme * * @param shape */ - public void setScatterShape(ScatterChart.ScatterShape shape) { - mShapeRenderer = getRendererForShape(shape); + fun setScatterShape(shape: ScatterShape) { + shapeRenderer = getRendererForShape(shape) } /** @@ -95,63 +70,21 @@ public class ScatterDataSet extends LineScatterCandleRadarDataSet impleme * * @param shapeRenderer */ - public void setShapeRenderer(IShapeRenderer shapeRenderer) { - mShapeRenderer = shapeRenderer; - } - - @Override - public IShapeRenderer getShapeRenderer() { - return mShapeRenderer; - } - - /** - * Sets the radius of the hole in the shape (applies to Square, Circle and Triangle) - * Set this to <= 0 to remove holes. - * - * @param holeRadius - */ - public void setScatterShapeHoleRadius(float holeRadius) { - mScatterShapeHoleRadius = holeRadius; - } - - @Override - public float getScatterShapeHoleRadius() { - return mScatterShapeHoleRadius; - } - - /** - * Sets the color for the hole in the shape. - * - * @param holeColor - */ - public void setScatterShapeHoleColor(int holeColor) { - mScatterShapeHoleColor = holeColor; + fun setShapeRenderer(shapeRenderer: IShapeRenderer?) { + this@ScatterDataSet.shapeRenderer = shapeRenderer } - @Override - public int getScatterShapeHoleColor() { - return mScatterShapeHoleColor; - } - - public static IShapeRenderer getRendererForShape(ScatterChart.ScatterShape shape) { - - switch (shape) { - case SQUARE: - return new SquareShapeRenderer(); - case CIRCLE: - return new CircleShapeRenderer(); - case TRIANGLE: - return new TriangleShapeRenderer(); - case CROSS: - return new CrossShapeRenderer(); - case X: - return new XShapeRenderer(); - case CHEVRON_UP: - return new ChevronUpShapeRenderer(); - case CHEVRON_DOWN: - return new ChevronDownShapeRenderer(); + companion object { + fun getRendererForShape(shape: ScatterShape): IShapeRenderer? { + return when (shape) { + ScatterShape.SQUARE -> SquareShapeRenderer() + ScatterShape.CIRCLE -> CircleShapeRenderer() + ScatterShape.TRIANGLE -> TriangleShapeRenderer() + ScatterShape.CROSS -> CrossShapeRenderer() + ScatterShape.X -> XShapeRenderer() + ScatterShape.CHEVRON_UP -> ChevronUpShapeRenderer() + ScatterShape.CHEVRON_DOWN -> ChevronDownShapeRenderer() + } } - - return null; } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt index 542188e602..2d7cb03d3d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt @@ -1,49 +1,46 @@ +package com.github.mikephil.charting.data.filter -package com.github.mikephil.charting.data.filter; - -import android.annotation.TargetApi; -import android.os.Build; - -import java.util.Arrays; +import java.util.Arrays +import kotlin.math.abs +import kotlin.math.sqrt /** - * Implemented according to Wiki-Pseudocode {@link} + * Implemented according to Wiki-Pseudocode [] * http://en.wikipedia.org/wiki/Ramer�Douglas�Peucker_algorithm * * @author Philipp Baldauf & Phliipp Jahoda */ -public class Approximator { - - @TargetApi(Build.VERSION_CODES.GINGERBREAD) - public float[] reduceWithDouglasPeucker(float[] points, float tolerance) { +class Approximator { + fun reduceWithDouglasPeucker(points: FloatArray, tolerance: Float): FloatArray { + var greatestIndex = 0 + var greatestDistance = 0f - int greatestIndex = 0; - float greatestDistance = 0f; + val line: Line = Line(points[0], points[1], points[points.size - 2], points[points.size - 1]) - Line line = new Line(points[0], points[1], points[points.length - 2], points[points.length - 1]); - - for (int i = 2; i < points.length - 2; i += 2) { - - float distance = line.distance(points[i], points[i + 1]); + var i = 2 + while (i < points.size - 2) { + val distance = line.distance(points[i], points[i + 1]) if (distance > greatestDistance) { - greatestDistance = distance; - greatestIndex = i; + greatestDistance = distance + greatestIndex = i } + i += 2 } if (greatestDistance > tolerance) { + val reduced1 = reduceWithDouglasPeucker(points.copyOfRange(0, greatestIndex + 2), tolerance) + val reduced2 = reduceWithDouglasPeucker( + points.copyOfRange(greatestIndex, points.size), + tolerance + ) - float[] reduced1 = reduceWithDouglasPeucker(Arrays.copyOfRange(points, 0, greatestIndex + 2), tolerance); - float[] reduced2 = reduceWithDouglasPeucker(Arrays.copyOfRange(points, greatestIndex, points.length), - tolerance); - - float[] result1 = reduced1; - float[] result2 = Arrays.copyOfRange(reduced2, 2, reduced2.length); + val result1 = reduced1 + val result2 = reduced2.copyOfRange(2, reduced2.size) - return concat(result1, result2); + return concat(result1, result2) } else { - return line.getPoints(); + return line.points } } @@ -53,50 +50,35 @@ public class Approximator { * @param arrays * @return */ - float[] concat(float[]... arrays) { - int length = 0; - for (float[] array : arrays) { - length += array.length; + fun concat(vararg arrays: FloatArray): FloatArray { + var length = 0 + for (array in arrays) { + length += array.size } - float[] result = new float[length]; - int pos = 0; - for (float[] array : arrays) { - for (float element : array) { - result[pos] = element; - pos++; + val result = FloatArray(length) + var pos = 0 + for (array in arrays) { + for (element in array) { + result[pos] = element + pos++ } } - return result; + return result } - private class Line { + private class Line(x1: Float, y1: Float, x2: Float, y2: Float) { + val points: FloatArray = floatArrayOf(x1, y1, x2, y2) - private float[] points; + private val sxey: Float = x1 * y2 + private val exsy: Float = x2 * y1 - private float sxey; - private float exsy; + private val dx: Float = x1 - x2 + private val dy: Float = y1 - y2 - private float dx; - private float dy; - - private float length; - - public Line(float x1, float y1, float x2, float y2) { - dx = x1 - x2; - dy = y1 - y2; - sxey = x1 * y2; - exsy = x2 * y1; - length = (float) Math.sqrt(dx * dx + dy * dy); - - points = new float[]{x1, y1, x2, y2}; - } - - public float distance(float x, float y) { - return Math.abs(dy * x - dx * y + sxey - exsy) / length; - } + private val length: Float = sqrt((dx * dx + dy * dy).toDouble()).toFloat() - public float[] getPoints() { - return points; + fun distance(x: Float, y: Float): Float { + return abs(dy * x - dx * y + sxey - exsy) / length } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt index 9351341c76..a9db514106 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt @@ -1,146 +1,145 @@ +package com.github.mikephil.charting.data.filter -package com.github.mikephil.charting.data.filter; - -import java.util.ArrayList; +import kotlin.math.abs +import kotlin.math.sqrt /** - * Implemented according to modified Douglas Peucker {@link} + * Implemented according to modified Douglas Peucker [] * http://psimpl.sourceforge.net/douglas-peucker.html */ -public class ApproximatorN -{ - public float[] reduceWithDouglasPeucker(float[] points, float resultCount) { - - int pointCount = points.length / 2; +class ApproximatorN { + fun reduceWithDouglasPeucker(points: FloatArray, resultCount: Float): FloatArray { + val pointCount = points.size / 2 // if a shape has 2 or less points it cannot be reduced - if (resultCount <= 2 || resultCount >= pointCount) - return points; + if (resultCount <= 2 || resultCount >= pointCount) return points - boolean[] keep = new boolean[pointCount]; + val keep = BooleanArray(pointCount) // first and last always stay - keep[0] = true; - keep[pointCount - 1] = true; + keep[0] = true + keep[pointCount - 1] = true - int currentStoredPoints = 2; + var currentStoredPoints = 2 - ArrayList queue = new ArrayList<>(); - Line line = new Line(0, pointCount - 1, points); - queue.add(line); + val queue = ArrayList() + var line = Line(0, pointCount - 1, points) + queue.add(line) do { - line = queue.remove(queue.size() - 1); + line = queue.removeAt(queue.size - 1) // store the key - keep[line.index] = true; + keep[line.index] = true // check point count tolerance - currentStoredPoints += 1; + currentStoredPoints += 1 - if (currentStoredPoints == resultCount) - break; + if (currentStoredPoints.toFloat() == resultCount) break // split the polyline at the key and recurse - Line left = new Line(line.start, line.index, points); + val left = Line(line.start, line.index, points) if (left.index > 0) { - int insertionIndex = insertionIndex(left, queue); - queue.add(insertionIndex, left); + val insertionIndex: Int = insertionIndex(left, queue) + queue.add(insertionIndex, left) } - Line right = new Line(line.index, line.end, points); + val right = Line(line.index, line.end, points) if (right.index > 0) { - int insertionIndex = insertionIndex(right, queue); - queue.add(insertionIndex, right); + val insertionIndex: Int = insertionIndex(right, queue) + queue.add(insertionIndex, right) } - } while (queue.isEmpty()); + } while (queue.isEmpty()) - float[] reducedEntries = new float[currentStoredPoints * 2]; + val reducedEntries = FloatArray(currentStoredPoints * 2) - for (int i = 0, i2 = 0, r2 = 0; i < currentStoredPoints; i++, r2 += 2) { + var i = 0 + var i2 = 0 + var r2 = 0 + while (i < currentStoredPoints) { if (keep[i]) { - reducedEntries[i2++] = points[r2]; - reducedEntries[i2++] = points[r2 + 1]; + reducedEntries[i2++] = points[r2] + reducedEntries[i2++] = points[r2 + 1] } + i++ + r2 += 2 } - return reducedEntries; + return reducedEntries } - private static float distanceToLine( - float ptX, float ptY, float[] - fromLinePoint1, float[] fromLinePoint2) { - float dx = fromLinePoint2[0] - fromLinePoint1[0]; - float dy = fromLinePoint2[1] - fromLinePoint1[1]; - - float dividend = Math.abs( - dy * ptX - - dx * ptY - - fromLinePoint1[0] * fromLinePoint2[1] + - fromLinePoint2[0] * fromLinePoint1[1]); - double divisor = Math.sqrt(dx * dx + dy * dy); - - return (float)(dividend / divisor); - } - - private static class Line { - int start; - int end; - - float distance = 0; - int index = 0; - - Line(int start, int end, float[] points) { - this.start = start; - this.end = end; - - float[] startPoint = new float[]{points[start * 2], points[start * 2 + 1]}; - float[] endPoint = new float[]{points[end * 2], points[end * 2 + 1]}; + private class Line(var start: Int, var end: Int, points: FloatArray) { + var distance: Float = 0f + var index: Int = 0 - if (end <= start + 1) return; + init { + val startPoint = floatArrayOf(points[start * 2], points[start * 2 + 1]) + val endPoint = floatArrayOf(points[end * 2], points[end * 2 + 1]) - for (int i = start + 1, i2 = i * 2; i < end; i++, i2 += 2) { - float distance = distanceToLine( + if (end > start + 1) { + var i = start + 1 + var i2 = i * 2 + while (i < end) { + val distance: Float = distanceToLine( points[i2], points[i2 + 1], - startPoint, endPoint); - - if (distance > this.distance) { - this.index = i; - this.distance = distance; + startPoint, endPoint + ) + + if (distance > this.distance) { + this.index = i + this.distance = distance + } + i++ + i2 += 2 } } } - boolean equals(final Line rhs) { - return (start == rhs.start) && (end == rhs.end) && index == rhs.index; + fun equals(rhs: Line): Boolean { + return (start == rhs.start) && (end == rhs.end) && index == rhs.index } - boolean lessThan(final Line rhs) { - return distance < rhs.distance; + fun lessThan(rhs: Line): Boolean { + return distance < rhs.distance } } - private static int insertionIndex(Line line, ArrayList queue) { - int min = 0; - int max = queue.size(); + companion object { + private fun distanceToLine( + ptX: Float, ptY: Float, fromLinePoint1: FloatArray, fromLinePoint2: FloatArray + ): Float { + val dx = fromLinePoint2[0] - fromLinePoint1[0] + val dy = fromLinePoint2[1] - fromLinePoint1[1] - while (!queue.isEmpty()) { - int midIndex = min + (max - min) / 2; - Line midLine = queue.get(midIndex); + val dividend = abs( + dy * ptX - dx * ptY - fromLinePoint1[0] * fromLinePoint2[1] + + fromLinePoint2[0] * fromLinePoint1[1] + ) + val divisor = sqrt((dx * dx + dy * dy).toDouble()) - if (midLine.equals(line)) { - return midIndex; - } - else if (line.lessThan(midLine)) { - // perform search in left half - max = midIndex; - } - else { - // perform search in right half - min = midIndex + 1; - } + return (dividend / divisor).toFloat() } - return min; + private fun insertionIndex(line: Line, queue: ArrayList): Int { + var min = 0 + var max = queue.size + + while (!queue.isEmpty()) { + val midIndex = min + (max - min) / 2 + val midLine = queue.get(midIndex) + + if (midLine.equals(line)) { + return midIndex + } else if (line.lessThan(midLine)) { + // perform search in left half + max = midIndex + } else { + // perform search in right half + min = midIndex + 1 + } + } + + return min + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt index d6c1237c52..2e8bad3b3d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt @@ -1,14 +1,8 @@ -package com.github.mikephil.charting.exception; +package com.github.mikephil.charting.exception -public class DrawingDataSetNotCreatedException extends RuntimeException { - - /** - * +object DrawingDataSetNotCreatedException : RuntimeException() { + /** + * */ - private static final long serialVersionUID = 1L; - - public DrawingDataSetNotCreatedException() { - super("Have to create a new drawing set first. Call ChartData's createNewDrawingDataSet() method"); - } - + private const val serialVersionUID = 1L } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.kt index 990027d497..9331c910e3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.kt @@ -16,7 +16,7 @@ open class DefaultFillFormatter : IFillFormatter { fillMin = if (dataSet!!.yMax > 0 && dataSet.yMin < 0) { 0f } else { - val max: Float = if (data.yMax > 0) 0f + val max: Float = if (data!!.yMax > 0) 0f else chartMaxY val min: Float = if (data.yMin < 0) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.kt index 023669f909..01464fcbca 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IAxisValueFormatter.kt @@ -15,5 +15,5 @@ interface IAxisValueFormatter { * @param axis the axis the value belongs to * @return */ - fun getFormattedValue(value: Float, axis: AxisBase?): String? + fun getFormattedValue(value: Float, axis: AxisBase?): String } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt index af83a4539f..6a9ed053e8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt @@ -1,45 +1,41 @@ -package com.github.mikephil.charting.highlight; +package com.github.mikephil.charting.highlight -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData; -import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.utils.MPPointD; +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData +import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.utils.MPPointD +import kotlin.math.abs +import kotlin.math.max /** * Created by Philipp Jahoda on 22/07/15. */ -public class BarHighlighter extends ChartHighlighter { +open class BarHighlighter(chart: BarDataProvider?) : ChartHighlighter(chart) { + override fun getHighlight(x: Float, y: Float): Highlight? { + val high = super.getHighlight(x, y) - public BarHighlighter(BarDataProvider chart) { - super(chart); - } - - @Override - public Highlight getHighlight(float x, float y) { - Highlight high = super.getHighlight(x, y); - - if(high == null) { - return null; + if (high == null) { + return null } - MPPointD pos = getValsForTouch(x, y); - - BarData barData = mChart.getBarData(); + val pos = getValsForTouch(x, y) - IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex()); - if (set.isStacked()) { + val barData = mChart!!.barData - return getStackedHighlight(high, - set, - (float) pos.x, - (float) pos.y); + val set = barData!!.getDataSetByIndex(high.dataSetIndex) + if (set!!.isStacked) { + return getStackedHighlight( + high, + set, + pos.x.toFloat(), + pos.y.toFloat() + ) } - MPPointD.recycleInstance(pos); + MPPointD.Companion.recycleInstance(pos) - return high; + return high } /** @@ -52,41 +48,39 @@ public class BarHighlighter extends ChartHighlighter { * @param yVal * @return */ - public Highlight getStackedHighlight(Highlight high, IBarDataSet set, float xVal, float yVal) { + fun getStackedHighlight(high: Highlight, set: IBarDataSet, xVal: Float, yVal: Float): Highlight? { + val entry = set.getEntryForXValue(xVal, yVal) - BarEntry entry = set.getEntryForXValue(xVal, yVal); - - if (entry == null) - return null; + if (entry == null) return null // not stacked - if (entry.getYVals() == null) { - return high; + if (entry.yVals == null) { + return high } else { - Range[] ranges = entry.getRanges(); + val ranges = entry.ranges - if (ranges.length > 0) { - int stackIndex = getClosestStackIndex(ranges, yVal); + if (ranges.isNotEmpty()) { + val stackIndex = getClosestStackIndex(ranges, yVal) - MPPointD pixels = mChart.getTransformer(set.getAxisDependency()).getPixelForValues(high.getX(), ranges[stackIndex].to); + val pixels = mChart!!.getTransformer(set.axisDependency)!!.getPixelForValues(high.x, ranges[stackIndex].to) - Highlight stackedHigh = new Highlight( - entry.getX(), - entry.getY(), - (float) pixels.x, - (float) pixels.y, - high.getDataSetIndex(), - stackIndex, - high.getAxis() - ); + val stackedHigh = Highlight( + entry.x, + entry.y, + pixels.x.toFloat(), + pixels.y.toFloat(), + high.dataSetIndex, + stackIndex, + high.axis + ) - MPPointD.recycleInstance(pixels); + MPPointD.Companion.recycleInstance(pixels) - return stackedHigh; + return stackedHigh } } - return null; + return null } /** @@ -98,66 +92,58 @@ public class BarHighlighter extends ChartHighlighter { * @param value * @return */ - protected int getClosestStackIndex(Range[] ranges, float value) { - - if (ranges == null || ranges.length == 0) - return 0; + protected fun getClosestStackIndex(ranges: Array?, value: Float): Int { + if (ranges == null || ranges.size == 0) return 0 - int stackIndex = 0; + var stackIndex = 0 - for (Range range : ranges) { - if (range.contains(value)) - return stackIndex; - else - stackIndex++; + for (range in ranges) { + if (range.contains(value)) return stackIndex + else stackIndex++ } - int length = Math.max(ranges.length - 1, 0); + val length = max(ranges.size - 1, 0) - return (value > ranges[length].to) ? length : 0; + return if (value > ranges[length].to) length else 0 } -// /** -// * Splits up the stack-values of the given bar-entry into Range objects. -// * -// * @param entry -// * @return -// */ -// protected Range[] getRanges(BarEntry entry) { -// -// float[] values = entry.getYVals(); -// -// if (values == null || values.length == 0) -// return new Range[0]; -// -// Range[] ranges = new Range[values.length]; -// -// float negRemain = -entry.getNegativeSum(); -// float posRemain = 0f; -// -// for (int i = 0; i < ranges.length; i++) { -// -// float value = values[i]; -// -// if (value < 0) { -// ranges[i] = new Range(negRemain, negRemain + Math.abs(value)); -// negRemain += Math.abs(value); -// } else { -// ranges[i] = new Range(posRemain, posRemain + value); -// posRemain += value; -// } -// } -// -// return ranges; -// } - - @Override - protected float getDistance(float x1, float y1, float x2, float y2) { - return Math.abs(x1 - x2); + // /** + // * Splits up the stack-values of the given bar-entry into Range objects. + // * + // * @param entry + // * @return + // */ + // protected Range[] getRanges(BarEntry entry) { + // + // float[] values = entry.getYVals(); + // + // if (values == null || values.length == 0) + // return new Range[0]; + // + // Range[] ranges = new Range[values.length]; + // + // float negRemain = -entry.getNegativeSum(); + // float posRemain = 0f; + // + // for (int i = 0; i < ranges.length; i++) { + // + // float value = values[i]; + // + // if (value < 0) { + // ranges[i] = new Range(negRemain, negRemain + Math.abs(value)); + // negRemain += Math.abs(value); + // } else { + // ranges[i] = new Range(posRemain, posRemain + value); + // posRemain += value; + // } + // } + // + // return ranges; + // } + override fun getDistance(x1: Float, y1: Float, x2: Float, y2: Float): Float { + return abs(x1 - x2) } - @Override - protected BarLineScatterCandleBubbleData getData() { - return mChart.getBarData(); - } + override val data + get() = mChart!!.barData } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/ChartHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/ChartHighlighter.kt index f889bf19d4..7af16b4f4d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/ChartHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/ChartHighlighter.kt @@ -1,45 +1,36 @@ -package com.github.mikephil.charting.highlight; - -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData; -import com.github.mikephil.charting.data.DataSet; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.utils.MPPointD; - -import java.util.ArrayList; -import java.util.List; +package com.github.mikephil.charting.highlight + +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData +import com.github.mikephil.charting.data.DataSet.Rounding +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider +import com.github.mikephil.charting.interfaces.datasets.IDataSet +import com.github.mikephil.charting.utils.MPPointD +import kotlin.math.abs +import kotlin.math.hypot /** * Created by Philipp Jahoda on 21/07/15. */ -public class ChartHighlighter implements IHighlighter -{ - +open class ChartHighlighter( /** * instance of the data-provider */ - protected T mChart; - + protected var mChart: T? +) : IHighlighter { /** * buffer for storing previously highlighted values */ - protected List mHighlightBuffer = new ArrayList(); - - public ChartHighlighter(T chart) { - this.mChart = chart; - } + protected var mHighlightBuffer: MutableList = ArrayList() - @Override - public Highlight getHighlight(float x, float y) { + override fun getHighlight(x: Float, y: Float): Highlight? { + val pos = getValsForTouch(x, y) + val xVal = pos.x.toFloat() + MPPointD.Companion.recycleInstance(pos) - MPPointD pos = getValsForTouch(x, y); - float xVal = (float) pos.x; - MPPointD.recycleInstance(pos); - - Highlight high = getHighlightForX(xVal, x, y); - return high; + val high = getHighlightForX(xVal, x, y) + return high } /** @@ -50,11 +41,11 @@ public class ChartHighlighter * @param y * @return */ - protected MPPointD getValsForTouch(float x, float y) { - + protected fun getValsForTouch(x: Float, y: Float): MPPointD { // take any transformer to determine the x-axis value - MPPointD pos = mChart.getTransformer(YAxis.AxisDependency.LEFT).getValuesByTouchPoint(x, y); - return pos; + + val pos = mChart!!.getTransformer(AxisDependency.LEFT)!!.getValuesByTouchPoint(x, y) + return pos } /** @@ -65,22 +56,21 @@ public class ChartHighlighter * @param y * @return */ - protected Highlight getHighlightForX(float xVal, float x, float y) { + protected fun getHighlightForX(xVal: Float, x: Float, y: Float): Highlight? { + val closestValues = getHighlightsAtXValue(xVal, x, y) - List closestValues = getHighlightsAtXValue(xVal, x, y); - - if(closestValues.isEmpty()) { - return null; + if (closestValues.isEmpty()) { + return null } - float leftAxisMinDist = getMinimumDistance(closestValues, y, YAxis.AxisDependency.LEFT); - float rightAxisMinDist = getMinimumDistance(closestValues, y, YAxis.AxisDependency.RIGHT); + val leftAxisMinDist = getMinimumDistance(closestValues, y, AxisDependency.LEFT) + val rightAxisMinDist = getMinimumDistance(closestValues, y, AxisDependency.RIGHT) - YAxis.AxisDependency axis = leftAxisMinDist < rightAxisMinDist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT; + val axis = if (leftAxisMinDist < rightAxisMinDist) AxisDependency.LEFT else AxisDependency.RIGHT - Highlight detail = getClosestHighlightByPixel(closestValues, x, y, axis, mChart.getMaxHighlightDistance()); + val detail = getClosestHighlightByPixel(closestValues, x, y, axis, mChart!!.maxHighlightDistance) - return detail; + return detail } /** @@ -92,28 +82,25 @@ public class ChartHighlighter * @param axis * @return */ - protected float getMinimumDistance(List closestValues, float pos, YAxis.AxisDependency axis) { - - float distance = Float.MAX_VALUE; + protected fun getMinimumDistance(closestValues: MutableList, pos: Float, axis: AxisDependency?): Float { + var distance = Float.Companion.MAX_VALUE - for (int i = 0; i < closestValues.size(); i++) { + for (i in closestValues.indices) { + val high = closestValues.get(i) - Highlight high = closestValues.get(i); - - if (high.getAxis() == axis) { - - float tempDistance = Math.abs(getHighlightPos(high) - pos); + if (high.axis == axis) { + val tempDistance = abs(getHighlightPos(high) - pos) if (tempDistance < distance) { - distance = tempDistance; + distance = tempDistance } } } - return distance; + return distance } - protected float getHighlightPos(Highlight h) { - return h.getYPx(); + protected fun getHighlightPos(h: Highlight): Float { + return h.yPx } /** @@ -125,27 +112,29 @@ public class ChartHighlighter * @param y touch position * @return */ - protected List getHighlightsAtXValue(float xVal, float x, float y) { - - mHighlightBuffer.clear(); + protected open fun getHighlightsAtXValue(xVal: Float, x: Float, y: Float): MutableList { + mHighlightBuffer.clear() - BarLineScatterCandleBubbleData data = getData(); + val data = this.data - if (data == null) - return mHighlightBuffer; + if (data == null) return mHighlightBuffer - for (int i = 0, dataSetCount = data.getDataSetCount(); i < dataSetCount; i++) { - - IDataSet dataSet = data.getDataSetByIndex(i); + var i = 0 + val dataSetCount = data.dataSetCount + while (i < dataSetCount) { + val dataSet = data.getDataSetByIndex(i) // don't include DataSets that cannot be highlighted - if (!dataSet.isHighlightEnabled()) - continue; + if (!dataSet!!.isHighlightEnabled) { + i++ + continue + } - mHighlightBuffer.addAll(buildHighlights(dataSet, i, xVal, DataSet.Rounding.CLOSEST)); + mHighlightBuffer.addAll(buildHighlights(dataSet, i, xVal, Rounding.CLOSEST)) + i++ } - return mHighlightBuffer; + return mHighlightBuffer } /** @@ -157,36 +146,35 @@ public class ChartHighlighter * @param rounding * @return */ - protected List buildHighlights(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) { + protected open fun buildHighlights(set: IDataSet<*>, dataSetIndex: Int, xVal: Float, rounding: Rounding?): MutableList { + val highlights = ArrayList() - ArrayList highlights = new ArrayList<>(); - - //noinspection unchecked - List entries = set.getEntriesForXValue(xVal); - if (entries.size() == 0) { + var entries = set.getEntriesForXValue(xVal) + if (entries.isEmpty()) { // Try to find closest x-value and take all entries for that x-value - final Entry closest = set.getEntryForXValue(xVal, Float.NaN, rounding); - if (closest != null) - { - //noinspection unchecked - entries = set.getEntriesForXValue(closest.getX()); + val closest: Entry? = set.getEntryForXValue(xVal, Float.Companion.NaN, rounding) + if (closest != null) { + entries = set.getEntriesForXValue(closest.x) } } - if (entries.size() == 0) - return highlights; + if (entries.isEmpty()) return highlights - for (Entry e : entries) { - MPPointD pixels = mChart.getTransformer( - set.getAxisDependency()).getPixelForValues(e.getX(), e.getY()); + for (e in entries) { + val pixels = mChart!!.getTransformer( + set.axisDependency + )!!.getPixelForValues(e.x, e.y) - highlights.add(new Highlight( - e.getX(), e.getY(), - (float) pixels.x, (float) pixels.y, - dataSetIndex, set.getAxisDependency())); + highlights.add( + Highlight( + e.x, e.y, + pixels.x.toFloat(), pixels.y.toFloat(), + dataSetIndex, set.axisDependency + ) + ) } - return highlights; + return highlights } /** @@ -194,35 +182,34 @@ public class ChartHighlighter * y-axis. * * @param closestValues contains two Highlight objects per DataSet closest to the selected x-position (determined by - * rounding up an down) + * rounding up an down) * @param x * @param y * @param axis the closest axis * @param minSelectionDistance * @return */ - public Highlight getClosestHighlightByPixel(List closestValues, float x, float y, - YAxis.AxisDependency axis, float minSelectionDistance) { - - Highlight closest = null; - float distance = minSelectionDistance; - - for (int i = 0; i < closestValues.size(); i++) { + fun getClosestHighlightByPixel( + closestValues: MutableList, x: Float, y: Float, + axis: AxisDependency?, minSelectionDistance: Float + ): Highlight? { + var closest: Highlight? = null + var distance = minSelectionDistance - Highlight high = closestValues.get(i); + for (i in closestValues.indices) { + val high = closestValues[i] - if (axis == null || high.getAxis() == axis) { - - float cDistance = getDistance(x, y, high.getXPx(), high.getYPx()); + if (axis == null || high.axis == axis) { + val cDistance = getDistance(x, y, high.xPx, high.yPx) if (cDistance < distance) { - closest = high; - distance = cDistance; + closest = high + distance = cDistance } } } - return closest; + return closest } /** @@ -234,13 +221,12 @@ public class ChartHighlighter * @param y2 * @return */ - protected float getDistance(float x1, float y1, float x2, float y2) { + protected open fun getDistance(x1: Float, y1: Float, x2: Float, y2: Float): Float { //return Math.abs(y1 - y2); //return Math.abs(x1 - x2); - return (float) Math.hypot(x1 - x2, y1 - y2); + return hypot((x1 - x2).toDouble(), (y1 - y2).toDouble()).toFloat() } - protected BarLineScatterCandleBubbleData getData() { - return mChart.getData(); - } + protected open val data + get() = mChart!!.data } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt index 76788af6e0..970acbfa28 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt @@ -1,93 +1,79 @@ -package com.github.mikephil.charting.highlight; +package com.github.mikephil.charting.highlight -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData; -import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.data.DataSet; -import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider; -import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; - -import java.util.List; +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.data.DataSet.Rounding +import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider +import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider +import com.github.mikephil.charting.interfaces.datasets.IDataSet /** * Created by Philipp Jahoda on 12/09/15. */ -public class CombinedHighlighter extends ChartHighlighter implements IHighlighter -{ - +open class CombinedHighlighter(chart: CombinedDataProvider?, barChart: BarDataProvider) : ChartHighlighter(chart), IHighlighter { /** * bar highlighter for supporting stacked highlighting */ - protected BarHighlighter barHighlighter; - - public CombinedHighlighter(CombinedDataProvider chart, BarDataProvider barChart) { - super(chart); - - // if there is BarData, create a BarHighlighter - barHighlighter = barChart.getBarData() == null ? null : new BarHighlighter(barChart); - } - - @Override - protected List getHighlightsAtXValue(float xVal, float x, float y) { + // if there is BarData, create a BarHighlighter + protected var barHighlighter = if (barChart.barData == null) null else BarHighlighter(barChart) - mHighlightBuffer.clear(); + override fun getHighlightsAtXValue(xVal: Float, x: Float, y: Float): MutableList { + mHighlightBuffer.clear() - List dataObjects = mChart.getCombinedData().getAllData(); + val dataObjects = mChart!!.combinedData!!.allData - for (int i = 0; i < dataObjects.size(); i++) { - - ChartData dataObject = dataObjects.get(i); + for (i in dataObjects.indices) { + val dataObject = dataObjects.get(i) // in case of BarData, let the BarHighlighter take over - if (barHighlighter != null && dataObject instanceof BarData) { - Highlight high = barHighlighter.getHighlight(x, y); + if (barHighlighter != null && dataObject is BarData) { + val high = barHighlighter!!.getHighlight(x, y) if (high != null) { - high.setDataIndex(i); - mHighlightBuffer.add(high); + high.dataIndex = i + mHighlightBuffer.add(high) } } else { - - for (int j = 0, dataSetCount = dataObject.getDataSetCount(); j < dataSetCount; j++) { - - IDataSet dataSet = dataObjects.get(i).getDataSetByIndex(j); + var j = 0 + val dataSetCount = dataObject.dataSetCount + while (j < dataSetCount) { + val dataSet = dataObjects[i].getDataSetByIndex(j) // don't include datasets that cannot be highlighted - if (!dataSet.isHighlightEnabled()) - continue; + if (!dataSet!!.isHighlightEnabled) { + j++ + continue + } - List highs = buildHighlights(dataSet, j, xVal, DataSet.Rounding.CLOSEST); - for (Highlight high : highs) - { - high.setDataIndex(i); - mHighlightBuffer.add(high); + val highs = buildHighlights(dataSet, j, xVal, Rounding.CLOSEST) + for (high in highs) { + high.dataIndex = i + mHighlightBuffer.add(high) } + j++ } } } - return mHighlightBuffer; - } - -// protected Highlight getClosest(float x, float y, Highlight... highs) { -// -// Highlight closest = null; -// float minDistance = Float.MAX_VALUE; -// -// for (Highlight high : highs) { -// -// if (high == null) -// continue; -// -// float tempDistance = getDistance(x, y, high.getXPx(), high.getYPx()); -// -// if (tempDistance < minDistance) { -// minDistance = tempDistance; -// closest = high; -// } -// } -// -// return closest; -// } + return mHighlightBuffer + } // protected Highlight getClosest(float x, float y, Highlight... highs) { + // + // Highlight closest = null; + // float minDistance = Float.MAX_VALUE; + // + // for (Highlight high : highs) { + // + // if (high == null) + // continue; + // + // float tempDistance = getDistance(x, y, high.getXPx(), high.getYPx()); + // + // if (tempDistance < minDistance) { + // minDistance = tempDistance; + // closest = high; + // } + // } + // + // return closest; + // } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt index d96298e07d..64523bfb23 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt @@ -1,85 +1,71 @@ -package com.github.mikephil.charting.highlight; +package com.github.mikephil.charting.highlight -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.DataSet; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.utils.MPPointD; - -import java.util.ArrayList; -import java.util.List; +import com.github.mikephil.charting.data.DataSet.Rounding +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider +import com.github.mikephil.charting.interfaces.datasets.IDataSet +import com.github.mikephil.charting.utils.MPPointD +import kotlin.math.abs /** * Created by Philipp Jahoda on 22/07/15. */ -public class HorizontalBarHighlighter extends BarHighlighter { - - public HorizontalBarHighlighter(BarDataProvider chart) { - super(chart); - } - - @Override - public Highlight getHighlight(float x, float y) { - - BarData barData = mChart.getBarData(); - - MPPointD pos = getValsForTouch(y, x); - - Highlight high = getHighlightForX((float) pos.y, y, x); - if (high == null) - return null; - - IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex()); - if (set.isStacked()) { - - return getStackedHighlight(high, - set, - (float) pos.y, - (float) pos.x); - } - - MPPointD.recycleInstance(pos); - - return high; - } - - @Override - protected List buildHighlights(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) { - - ArrayList highlights = new ArrayList<>(); - - //noinspection unchecked - List entries = set.getEntriesForXValue(xVal); - if (entries.size() == 0) { - // Try to find closest x-value and take all entries for that x-value - final Entry closest = set.getEntryForXValue(xVal, Float.NaN, rounding); - if (closest != null) - { - //noinspection unchecked - entries = set.getEntriesForXValue(closest.getX()); - } - } - - if (entries.size() == 0) - return highlights; - - for (Entry e : entries) { - MPPointD pixels = mChart.getTransformer( - set.getAxisDependency()).getPixelForValues(e.getY(), e.getX()); - - highlights.add(new Highlight( - e.getX(), e.getY(), - (float) pixels.x, (float) pixels.y, - dataSetIndex, set.getAxisDependency())); - } - - return highlights; - } - - @Override - protected float getDistance(float x1, float y1, float x2, float y2) { - return Math.abs(y1 - y2); - } +class HorizontalBarHighlighter(chart: BarDataProvider?) : BarHighlighter(chart) { + override fun getHighlight(x: Float, y: Float): Highlight? { + val barData = mChart!!.barData + + val pos = getValsForTouch(y, x) + + val high = getHighlightForX(pos.y.toFloat(), y, x) + if (high == null) return null + + val set = barData!!.getDataSetByIndex(high.dataSetIndex) + if (set!!.isStacked) { + return getStackedHighlight( + high, + set, + pos.y.toFloat(), + pos.x.toFloat() + ) + } + + MPPointD.Companion.recycleInstance(pos) + + return high + } + + override fun buildHighlights(set: IDataSet<*>, dataSetIndex: Int, xVal: Float, rounding: Rounding?): MutableList { + val highlights = ArrayList() + + var entries = set.getEntriesForXValue(xVal) + if (entries.isEmpty()) { + // Try to find closest x-value and take all entries for that x-value + val closest: Entry? = set.getEntryForXValue(xVal, Float.Companion.NaN, rounding) + if (closest != null) { + entries = set.getEntriesForXValue(closest.x) + } + } + + if (entries.isEmpty()) return highlights + + for (e in entries) { + val pixels = mChart!!.getTransformer( + set.axisDependency + )!!.getPixelForValues(e.y, e.x) + + highlights.add( + Highlight( + e.x, e.y, + pixels.x.toFloat(), pixels.y.toFloat(), + dataSetIndex, set.axisDependency + ) + ) + } + + return highlights + } + + override fun getDistance(x1: Float, y1: Float, x2: Float, y2: Float): Float { + return abs(y1 - y2) + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/IHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/IHighlighter.kt index d0ca0cfe57..da4cd3e5e8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/IHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/IHighlighter.kt @@ -1,11 +1,9 @@ -package com.github.mikephil.charting.highlight; +package com.github.mikephil.charting.highlight /** * Created by philipp on 10/06/16. */ -public interface IHighlighter -{ - +interface IHighlighter { /** * Returns a Highlight object corresponding to the given x- and y- touch positions in pixels. * @@ -13,5 +11,5 @@ public interface IHighlighter * @param y * @return */ - Highlight getHighlight(float x, float y); + fun getHighlight(x: Float, y: Float): Highlight? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt index 4e58b761d4..07b7dddcb5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt @@ -1,65 +1,50 @@ -package com.github.mikephil.charting.highlight; +package com.github.mikephil.charting.highlight -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.charts.PieRadarChartBase; - -import java.util.ArrayList; -import java.util.List; +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.charts.PieRadarChartBase /** * Created by philipp on 12/06/16. */ -public abstract class PieRadarHighlighter implements IHighlighter { - - protected T mChart; - - /** - * buffer for storing previously highlighted values - */ - protected List mHighlightBuffer = new ArrayList(); - - public PieRadarHighlighter(T chart) { - this.mChart = chart; - } - - @Override - public Highlight getHighlight(float x, float y) { - - float touchDistanceToCenter = mChart.distanceToCenter(x, y); - - // check if a slice was touched - if (touchDistanceToCenter > mChart.getRadius()) { - - // if no slice was touched, highlight nothing - return null; - - } else { - - float angle = mChart.getAngleForPoint(x, y); - - if (mChart instanceof PieChart) { - angle /= mChart.getAnimator().getPhaseY(); - } - - int index = mChart.getIndexForAngle(angle); - - // check if the index could be found - if (index < 0 || index >= mChart.getData().getMaxEntryCountSet().getEntryCount()) { - return null; - - } else { - return getClosestHighlight(index, x, y); - } - } - } - - /** - * Returns the closest Highlight object of the given objects based on the touch position inside the chart. - * - * @param index - * @param x - * @param y - * @return - */ - protected abstract Highlight getClosestHighlight(int index, float x, float y); +abstract class PieRadarHighlighter?>(protected var mChart: T?) : IHighlighter { + /** + * buffer for storing previously highlighted values + */ + protected var mHighlightBuffer: MutableList = ArrayList() + + override fun getHighlight(x: Float, y: Float): Highlight? { + val touchDistanceToCenter = mChart!!.distanceToCenter(x, y) + + // check if a slice was touched + if (touchDistanceToCenter > mChart!!.radius) { + // if no slice was touched, highlight nothing + + return null + } else { + var angle = mChart!!.getAngleForPoint(x, y) + + if (mChart is PieChart) { + angle /= mChart!!.animator.phaseY + } + + val index = mChart!!.getIndexForAngle(angle) + + // check if the index could be found + if (index < 0 || index >= mChart!!.data!!.maxEntryCountSet!!.entryCount) { + return null + } else { + return getClosestHighlight(index, x, y) + } + } + } + + /** + * Returns the closest Highlight object of the given objects based on the touch position inside the chart. + * + * @param index + * @param x + * @param y + * @return + */ + protected abstract fun getClosestHighlight(index: Int, x: Float, y: Float): Highlight? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt index 3c4f6d03ac..e5c6528f2a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt @@ -1,45 +1,35 @@ -package com.github.mikephil.charting.highlight; +package com.github.mikephil.charting.highlight -import com.github.mikephil.charting.charts.RadarChart; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; - -import java.util.List; +import com.github.mikephil.charting.charts.RadarChart +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Utils +import kotlin.math.abs /** * Created by philipp on 12/06/16. */ -public class RadarHighlighter extends PieRadarHighlighter { - - public RadarHighlighter(RadarChart chart) { - super(chart); - } - - @Override - protected Highlight getClosestHighlight(int index, float x, float y) { - - List highlights = getHighlightsAtIndex(index); +open class RadarHighlighter(chart: RadarChart) : PieRadarHighlighter(chart) { + override fun getClosestHighlight(index: Int, x: Float, y: Float): Highlight? { + val highlights = getHighlightsAtIndex(index) - float distanceToCenter = mChart.distanceToCenter(x, y) / mChart.getFactor(); + val distanceToCenter = mChart!!.distanceToCenter(x, y) / mChart!!.factor - Highlight closest = null; - float distance = Float.MAX_VALUE; + var closest: Highlight? = null + var distance = Float.Companion.MAX_VALUE - for (int i = 0; i < highlights.size(); i++) { + for (i in highlights.indices) { + val high = highlights[i] - Highlight high = highlights.get(i); - - float cdistance = Math.abs(high.getY() - distanceToCenter); + val cdistance = abs(high.y - distanceToCenter) if (cdistance < distance) { - closest = high; - distance = cdistance; + closest = high + distance = cdistance } } - return closest; + return closest } + /** * Returns an array of Highlight objects for the given index. The Highlight * objects give information about the value at the selected index and the @@ -49,31 +39,30 @@ public class RadarHighlighter extends PieRadarHighlighter { * @param index * @return */ - protected List getHighlightsAtIndex(int index) { - - mHighlightBuffer.clear(); - - float phaseX = mChart.getAnimator().getPhaseX(); - float phaseY = mChart.getAnimator().getPhaseY(); - float sliceangle = mChart.getSliceAngle(); - float factor = mChart.getFactor(); + protected fun getHighlightsAtIndex(index: Int): MutableList { + mHighlightBuffer.clear() - MPPointF pOut = MPPointF.getInstance(0,0); - for (int i = 0; i < mChart.getData().getDataSetCount(); i++) { + val phaseX = mChart!!.animator.phaseX + val phaseY = mChart!!.animator.phaseY + val sliceangle = mChart!!.sliceAngle + val factor = mChart!!.factor - IDataSet dataSet = mChart.getData().getDataSetByIndex(i); + val pOut: MPPointF = MPPointF.Companion.getInstance(0f, 0f) + for (i in 0.. then ranges are (-10 - 0, 0 - 5, 5 - 25). */ -public final class Range { +class Range(var from: Float, var to: Float) { + /** + * Returns true if this range contains (if the value is in between) the given value, false if not. + * + * @param value + * @return + */ + fun contains(value: Float): Boolean { + if (value > from && value <= to) return true + else return false + } - public float from; - public float to; + fun isLarger(value: Float): Boolean { + return value > to + } - public Range(float from, float to) { - this.from = from; - this.to = to; - } - - /** - * Returns true if this range contains (if the value is in between) the given value, false if not. - * - * @param value - * @return - */ - public boolean contains(float value) { - - if (value > from && value <= to) - return true; - else - return false; - } - - public boolean isLarger(float value) { - return value > to; - } - - public boolean isSmaller(float value) { - return value < from; - } + fun isSmaller(value: Float): Boolean { + return value < from + } } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt index 9dfee07f9c..3f857bb55d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt @@ -1,11 +1,10 @@ -package com.github.mikephil.charting.interfaces.dataprovider; +package com.github.mikephil.charting.interfaces.dataprovider -import com.github.mikephil.charting.data.BarData; +import com.github.mikephil.charting.data.BarData -public interface BarDataProvider extends BarLineScatterCandleBubbleDataProvider { - - BarData getBarData(); - boolean isDrawBarShadowEnabled(); - boolean isDrawValueAboveBarEnabled(); - boolean isHighlightFullBarEnabled(); +interface BarDataProvider : BarLineScatterCandleBubbleDataProvider { + val barData: BarData? + val isDrawBarShadowEnabled: Boolean + val isDrawValueAboveBarEnabled: Boolean + val isHighlightFullBarEnabled: Boolean } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarLineScatterCandleBubbleDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarLineScatterCandleBubbleDataProvider.kt index eea0b5e562..23129cbc8c 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarLineScatterCandleBubbleDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarLineScatterCandleBubbleDataProvider.kt @@ -2,6 +2,7 @@ package com.github.mikephil.charting.interfaces.dataprovider import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData +import com.github.mikephil.charting.data.ChartData import com.github.mikephil.charting.utils.Transformer interface BarLineScatterCandleBubbleDataProvider : ChartInterface { @@ -9,5 +10,6 @@ interface BarLineScatterCandleBubbleDataProvider : ChartInterface { fun isInverted(axis: AxisDependency?): Boolean val lowestVisibleX: Float val highestVisibleX: Float - override fun getData(): BarLineScatterCandleBubbleData<*> + + override val data: ChartData<*, *>? } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt index 82ee30ad7c..10cecf0a8d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt @@ -1,8 +1,7 @@ -package com.github.mikephil.charting.interfaces.dataprovider; +package com.github.mikephil.charting.interfaces.dataprovider -import com.github.mikephil.charting.data.BubbleData; +import com.github.mikephil.charting.data.BubbleData -public interface BubbleDataProvider extends BarLineScatterCandleBubbleDataProvider { - - BubbleData getBubbleData(); +interface BubbleDataProvider : BarLineScatterCandleBubbleDataProvider { + val bubbleData: BubbleData? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt index 357403f98a..e5a5347a03 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt @@ -1,8 +1,7 @@ -package com.github.mikephil.charting.interfaces.dataprovider; +package com.github.mikephil.charting.interfaces.dataprovider -import com.github.mikephil.charting.data.CandleData; +import com.github.mikephil.charting.data.CandleData -public interface CandleDataProvider extends BarLineScatterCandleBubbleDataProvider { - - CandleData getCandleData(); +interface CandleDataProvider : BarLineScatterCandleBubbleDataProvider { + val candleData: CandleData? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt index 86fc62c37f..2e1e87dc71 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt @@ -1,12 +1,9 @@ -package com.github.mikephil.charting.interfaces.dataprovider; +package com.github.mikephil.charting.interfaces.dataprovider -import android.graphics.RectF; - -import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.formatter.IValueFormatter; -import com.github.mikephil.charting.utils.MPPointF; - -import androidx.annotation.Nullable; +import android.graphics.RectF +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.formatter.IValueFormatter +import com.github.mikephil.charting.utils.MPPointF /** * Interface that provides everything there is to know about the dimensions, @@ -14,49 +11,47 @@ import androidx.annotation.Nullable; * * @author Philipp Jahoda */ -public interface ChartInterface { - +interface ChartInterface { /** * Returns the minimum x value of the chart, regardless of zoom or translation. */ - float getXChartMin(); + val xChartMin: Float /** * Returns the maximum x value of the chart, regardless of zoom or translation. */ - float getXChartMax(); + val xChartMax: Float - float getXRange(); + val xRange: Float /** * Returns the minimum y value of the chart, regardless of zoom or translation. */ - float getYChartMin(); + val yChartMin: Float /** * Returns the maximum y value of the chart, regardless of zoom or translation. */ - float getYChartMax(); + val yChartMax: Float /** * Returns the maximum distance in scren dp a touch can be away from an entry to cause it to get highlighted. */ - float getMaxHighlightDistance(); + val maxHighlightDistance: Float - int getWidth(); + val width: Int - int getHeight(); + val height: Int - MPPointF getCenterOfView(); + val centerOfView: MPPointF? - MPPointF getCenterOffsets(); + val centerOffsets: MPPointF - RectF getContentRect(); + val contentRect: RectF? - IValueFormatter getDefaultValueFormatter(); + val defaultValueFormatter: IValueFormatter? - @Nullable - ChartData getData(); + val data: ChartData<*, *>? - int getMaxVisibleCount(); + val maxVisibleCount: Int } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CombinedDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CombinedDataProvider.kt index 574d26a2a2..81472cbe63 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CombinedDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CombinedDataProvider.kt @@ -1,11 +1,10 @@ -package com.github.mikephil.charting.interfaces.dataprovider; +package com.github.mikephil.charting.interfaces.dataprovider -import com.github.mikephil.charting.data.CombinedData; +import com.github.mikephil.charting.data.CombinedData /** * Created by philipp on 11/06/16. */ -public interface CombinedDataProvider extends LineDataProvider, BarDataProvider, BubbleDataProvider, CandleDataProvider, ScatterDataProvider { - - CombinedData getCombinedData(); +interface CombinedDataProvider : LineDataProvider, BarDataProvider, BubbleDataProvider, CandleDataProvider, ScatterDataProvider { + val combinedData: CombinedData? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/LineDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/LineDataProvider.kt index 73785b90ed..48077c81df 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/LineDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/LineDataProvider.kt @@ -5,6 +5,6 @@ import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.data.LineData interface LineDataProvider : BarLineScatterCandleBubbleDataProvider { - val lineData: LineData - fun getAxis(dependency: AxisDependency): YAxis? + val lineData: LineData? + fun getAxis(axis: AxisDependency?): YAxis? } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt index b58d5af95d..43684658d0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt @@ -1,8 +1,7 @@ -package com.github.mikephil.charting.interfaces.dataprovider; +package com.github.mikephil.charting.interfaces.dataprovider -import com.github.mikephil.charting.data.ScatterData; +import com.github.mikephil.charting.data.ScatterData -public interface ScatterDataProvider extends BarLineScatterCandleBubbleDataProvider { - - ScatterData getScatterData(); +interface ScatterDataProvider : BarLineScatterCandleBubbleDataProvider { + val scatterData: ScatterData? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt index 5e82a48420..9531d15d33 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt @@ -1,25 +1,22 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.utils.Fill; - -import java.util.List; +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.utils.Fill /** * Created by philipp on 21/10/15. */ -public interface IBarDataSet extends IBarLineScatterCandleBubbleDataSet { - - List getFills(); +interface IBarDataSet : IBarLineScatterCandleBubbleDataSet { + val fills: MutableList? - Fill getFill(int index); + fun getFill(index: Int): Fill? /** * Returns true if this DataSet is stacked (stacksize > 1) or not. * * @return */ - boolean isStacked(); + val isStacked: Boolean /** * Returns the maximum number of bars that can be stacked upon another in @@ -27,7 +24,7 @@ public interface IBarDataSet extends IBarLineScatterCandleBubbleDataSet? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBubbleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBubbleDataSet.kt index e284aac209..e0f4d07363 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBubbleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBubbleDataSet.kt @@ -1,27 +1,24 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import com.github.mikephil.charting.data.BubbleEntry; +import com.github.mikephil.charting.data.BubbleEntry /** * Created by philipp on 21/10/15. */ -public interface IBubbleDataSet extends IBarLineScatterCandleBubbleDataSet { +interface IBubbleDataSet : IBarLineScatterCandleBubbleDataSet { + val maxSize: Float + + val isNormalizeSizeEnabled: Boolean + /** + * Returns the width of the highlight-circle that surrounds the bubble + * @return + */ /** * Sets the width of the circle that surrounds the bubble when highlighted, * in dp. * * @param width */ - void setHighlightCircleWidth(float width); - - float getMaxSize(); - - boolean isNormalizeSizeEnabled(); - - /** - * Returns the width of the highlight-circle that surrounds the bubble - * @return - */ - float getHighlightCircleWidth(); + var highlightCircleWidth: Float } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt index 1d004ed959..833b1c6ff8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ICandleDataSet.kt @@ -1,21 +1,19 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import android.graphics.Paint; - -import com.github.mikephil.charting.data.CandleEntry; +import android.graphics.Paint +import com.github.mikephil.charting.data.CandleEntry /** * Created by philipp on 21/10/15. */ -public interface ICandleDataSet extends ILineScatterCandleRadarDataSet { - +interface ICandleDataSet : ILineScatterCandleRadarDataSet { /** * Returns the space that is left out on the left and right side of each * candle. * * @return */ - float getBarSpace(); + val barSpace: Float /** * Returns whether the candle bars should show? @@ -25,61 +23,61 @@ public interface ICandleDataSet extends ILineScatterCandleRadarDataSet close). * * @return */ - int getDecreasingColor(); + val decreasingColor: Int /** * Returns paint style when open < close * * @return */ - Paint.Style getIncreasingPaintStyle(); + val increasingPaintStyle: Paint.Style? /** * Returns paint style when open > close * * @return */ - Paint.Style getDecreasingPaintStyle(); + val decreasingPaintStyle: Paint.Style? /** * Is the shadow color same as the candle color? * * @return */ - boolean getShadowColorSameAsCandle(); + val shadowColorSameAsCandle: Boolean } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.kt index 6f1849e7e0..dd9b869321 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.kt @@ -1,56 +1,52 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import android.graphics.DashPathEffect; -import android.graphics.Typeface; - -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.DataSet; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.formatter.IValueFormatter; -import com.github.mikephil.charting.utils.MPPointF; - -import java.util.List; - -public interface IDataSet { +import android.graphics.DashPathEffect +import android.graphics.Typeface +import com.github.mikephil.charting.components.Legend.LegendForm +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.DataSet.Rounding +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.formatter.IValueFormatter +import com.github.mikephil.charting.utils.MPPointF +interface IDataSet { /** * returns the minimum y-value this DataSet holds */ - float getYMin(); + val yMin: Float /** * returns the maximum y-value this DataSet holds */ - float getYMax(); + val yMax: Float /** * returns the minimum x-value this DataSet holds */ - float getXMin(); + val xMin: Float /** * returns the maximum x-value this DataSet holds */ - float getXMax(); + val xMax: Float /** * Returns the number of y-values this DataSet represents -> the size of the y-values array * -> yvals.size() */ - int getEntryCount(); + val entryCount: Int /** * Calculates the minimum and maximum x and y values (mXMin, mXMax, mYMin, mYMax). */ - void calcMinMax(); + fun calcMinMax() /** * Calculates the min and max y-values from the Entry closest to the given fromX to the Entry closest to the given toX value. * This is only needed for the autoScaleMinMax feature. * */ - void calcMinMaxY(float fromX, float toX); + fun calcMinMaxY(fromX: Float, toX: Float) /** * Returns the first Entry object found at the given x-value with binary @@ -63,11 +59,9 @@ public interface IDataSet { * @param xValue the x-value * @param closestToY If there are multiple y-values for the specified x-value, * @param rounding determine whether to round up/down/closest - * if there is no Entry matching the provided x-value - * - * + * if there is no Entry matching the provided x-value */ - T getEntryForXValue(float xValue, float closestToY, DataSet.Rounding rounding); + fun getEntryForXValue(xValue: Float, closestToY: Float, rounding: Rounding?): T? /** * Returns the first Entry object found at the given x-value with binary @@ -81,7 +75,7 @@ public interface IDataSet { * @param xValue the x-value * @param closestToY If there are multiple y-values for the specified x-value, */ - T getEntryForXValue(float xValue, float closestToY); + fun getEntryForXValue(xValue: Float, closestToY: Float): T? /** * Returns all Entry objects found at the given x-value with binary @@ -90,12 +84,12 @@ public interface IDataSet { * not over-use in performance critical situations. * */ - List getEntriesForXValue(float xValue); + fun getEntriesForXValue(xValue: Float): MutableList /** * Returns the Entry object found at the given index (NOT xIndex) in the values array. */ - T getEntryForIndex(int index); + fun getEntryForIndex(index: Int): T /** * Returns the first Entry index found at the given x-value with binary @@ -108,16 +102,16 @@ public interface IDataSet { * @param xValue the x-value * @param closestToY If there are multiple y-values for the specified x-value, * @param rounding determine whether to round up/down/closest - * if there is no Entry matching the provided x-value + * if there is no Entry matching the provided x-value */ - int getEntryIndex(float xValue, float closestToY, DataSet.Rounding rounding); + fun getEntryIndex(xValue: Float, closestToY: Float, rounding: Rounding?): Int /** * Returns the position of the provided entry in the DataSets Entry array. * Returns -1 if doesn't exist. * */ - int getEntryIndex(T e); + fun getEntryIndex(e: Entry): Int /** @@ -127,7 +121,7 @@ public interface IDataSet { * situations. * */ - int getIndexInEntries(int xIndex); + fun getIndexInEntries(xIndex: Int): Int /** * Adds an Entry to the DataSet dynamically. @@ -136,7 +130,7 @@ public interface IDataSet { * values of the DataSet and the value-sum. * */ - boolean addEntry(T e); + fun addEntry(e: T): Boolean /** @@ -146,21 +140,21 @@ public interface IDataSet { * values of the DataSet and the value-sum. * */ - void addEntryOrdered(T e); + fun addEntryOrdered(e: T) /** * Removes the first Entry (at index 0) of this DataSet from the entries array. * Returns true if successful, false if not. * */ - boolean removeFirst(); + fun removeFirst(): Boolean /** * Removes the last Entry (at index size-1) of this DataSet from the entries array. * Returns true if successful, false if not. * */ - boolean removeLast(); + fun removeLast(): Boolean /** * Removes an Entry from the DataSets entries array. This will also @@ -169,21 +163,21 @@ public interface IDataSet { * be removed. * */ - boolean removeEntry(T e); + fun removeEntry(e: T?): Boolean /** * Removes the Entry object closest to the given x-value from the DataSet. * Returns true if an Entry was removed, false if no Entry could be removed. * */ - boolean removeEntryByXValue(float xValue); + fun removeEntryByXValue(xValue: Float): Boolean /** * Removes the Entry object at the given index in the values array from the DataSet. * Returns true if an Entry was removed, false if no Entry could be removed. * */ - boolean removeEntry(int index); + fun removeEntry(index: Int): Boolean /** * Checks if this DataSet contains the specified Entry. Returns true if so, @@ -191,70 +185,68 @@ public interface IDataSet { * over-use in performance critical situations. * */ - boolean contains(T entry); + fun contains(entry: T?): Boolean /** * Removes all values from this DataSet and does all necessary recalculations. */ - void clear(); + fun clear() /** * Returns the label string that describes the DataSet. * */ - String getLabel(); - /** * Sets the label string that describes the DataSet. * */ - void setLabel(String label); + var label: String /** * Returns the axis this DataSet should be plotted against. */ - YAxis.AxisDependency getAxisDependency(); - /** * Set the y-axis this DataSet should be plotted against (either LEFT or * RIGHT). Default: LEFT * */ - void setAxisDependency(YAxis.AxisDependency dependency); + var axisDependency: AxisDependency? /** * returns all the colors that are set for this DataSet * */ - List getColors(); + val colors: MutableList /** * Returns the first color (index 0) of the colors-array this DataSet * contains. This is only used for performance reasons when only one color is in the colors array (size == 1) * */ - int getColor(); + val color: Int /** * Returns the color at the given index of the DataSet's color array. * Performs a IndexOutOfBounds check by modulus. * */ - int getColor(int index); + fun getColor(index: Int): Int /** * returns true if highlighting of values is enabled, false if not * */ - boolean isHighlightEnabled(); - /** * If set to true, value highlighting is enabled which means that values can * be highlighted programmatically or by touch gesture. * */ - void setHighlightEnabled(boolean enabled); + var isHighlightEnabled: Boolean + /** + * Returns the formatter used for drawing the values inside the chart. + * + */ /** * Sets the formatter to be used for drawing the values inside the chart. If * no formatter is set, the chart will automatically determine a reasonable @@ -263,146 +255,113 @@ public interface IDataSet { * calculated by the chart. * */ - void setValueFormatter(IValueFormatter f); + var valueFormatter: IValueFormatter /** - * Returns the formatter used for drawing the values inside the chart. + * Sets a list of colors to be used as the colors for the drawn values. * */ - IValueFormatter getValueFormatter(); + fun setValueTextColors(colors: MutableList) /** - * Returns true if the valueFormatter object of this DataSet is null. + * Returns only the first color of all colors that are set to be used for the values. * */ - boolean needsFormatter(); - /** * Sets the color the value-labels of this DataSet should have. * */ - void setValueTextColor(int color); + var valueTextColor: Int /** - * Sets a list of colors to be used as the colors for the drawn values. + * Returns the color at the specified index that is used for drawing the values inside the chart. + * Uses modulus internally. * */ - void setValueTextColors(List colors); + fun getValueTextColor(index: Int): Int /** - * Sets a Typeface for the value-labels of this DataSet. + * Returns the typeface that is used for drawing the values inside the chart * */ - void setValueTypeface(Typeface tf); - /** - * Sets the text-size of the value-labels of this DataSet in dp. + * Sets a Typeface for the value-labels of this DataSet. * */ - void setValueTextSize(float size); + var valueTypeface: Typeface? /** - * Returns only the first color of all colors that are set to be used for the values. + * Returns the text size that is used for drawing the values inside the chart * */ - int getValueTextColor(); - /** - * Returns the color at the specified index that is used for drawing the values inside the chart. - * Uses modulus internally. + * Sets the text-size of the value-labels of this DataSet in dp. * */ - int getValueTextColor(int index); + var valueTextSize: Float /** - * Returns the typeface that is used for drawing the values inside the chart + * The form to draw for this dataset in the legend. * - */ - Typeface getValueTypeface(); - - /** - * Returns the text size that is used for drawing the values inside the chart * - */ - float getValueTextSize(); - - /** - * The form to draw for this dataset in the legend. - *

* Return `DEFAULT` to use the default legend form. */ - Legend.LegendForm getForm(); + val form: LegendForm? /** * The form size to draw for this dataset in the legend. - *

+ * + * * Return `Float.NaN` to use the default legend form size. */ - float getFormSize(); + val formSize: Float /** * The line width for drawing the form of this dataset in the legend - *

+ * + * * Return `Float.NaN` to use the default legend form line width. */ - float getFormLineWidth(); + val formLineWidth: Float /** * The line dash path effect used for shapes that consist of lines. - *

+ * + * * Return `null` to use the default legend form line dash effect. */ - DashPathEffect getFormLineDashEffect(); - - /** - * set this to true to draw y-values on the chart. - * NOTE (for bar and line charts): if `maxVisibleCount` is reached, no values will be drawn even - * if this is enabled - */ - void setDrawValues(boolean enabled); + val formLineDashEffect: DashPathEffect? /** * Returns true if y-value drawing is enabled, false if not * */ - boolean isDrawValuesEnabled(); + var isDrawValuesEnabled: Boolean /** - * Set this to true to draw y-icons on the chart. - * NOTE (for bar and line charts): if `maxVisibleCount` is reached, no icons will be drawn even - * if this is enabled + * Returns true if y-icon drawing is enabled, false if not * */ - void setDrawIcons(boolean enabled); + var isDrawIconsEnabled: Boolean /** - * Returns true if y-icon drawing is enabled, false if not - * + * Get the offset for drawing icons. */ - boolean isDrawIconsEnabled(); - /** * Offset of icons drawn on the chart. * For all charts except Pie and Radar it will be ordinary (x offset,y offset). * For Pie and Radar chart it will be (y offset, distance from center offset); so if you want icon to be rendered under value, you should increase X component of CGPoint, and if you want icon to be rendered closet to center, you should decrease height component of CGPoint. */ - void setIconsOffset(MPPointF offset); + var iconsOffset: MPPointF /** - * Get the offset for drawing icons. + * Returns true if this DataSet is visible inside the chart, or false if it + * is currently hidden. */ - MPPointF getIconsOffset(); - /** * Set the visibility of this DataSet. If not visible, the DataSet will not * be drawn to the chart upon refreshing it. * */ - void setVisible(boolean visible); - - /** - * Returns true if this DataSet is visible inside the chart, or false if it - * is currently hidden. - */ - boolean isVisible(); + var isVisible: Boolean } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt index 3f534fe848..3c57afcf22 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt @@ -1,22 +1,20 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import android.graphics.DashPathEffect; - -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.formatter.IFillFormatter; +import android.graphics.DashPathEffect +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.formatter.IFillFormatter /** * Created by Philpp Jahoda on 21/10/15. */ -public interface ILineDataSet extends ILineRadarDataSet { - +interface ILineDataSet : ILineRadarDataSet { /** * Returns the drawing mode for this line dataset * * @return */ - LineDataSet.Mode getMode(); + val mode: LineDataSet.Mode? /** * Returns the intensity of the cubic lines (the effect intensity). @@ -24,23 +22,23 @@ public interface ILineDataSet extends ILineRadarDataSet { * * @return */ - float getCubicIntensity(); + val cubicIntensity: Float - @Deprecated - boolean isDrawCubicEnabled(); + @get:Deprecated("") + val isDrawCubicEnabled: Boolean - @Deprecated - boolean isDrawSteppedEnabled(); + @get:Deprecated("") + val isDrawSteppedEnabled: Boolean /** * Returns the size of the drawn circles. */ - float getCircleRadius(); + val circleRadius: Float /** * Returns the hole radius of the drawn circles. */ - float getCircleHoleRadius(); + val circleHoleRadius: Float /** * Returns the color at the given index of the DataSet's circle-color array. @@ -49,42 +47,42 @@ public interface ILineDataSet extends ILineRadarDataSet { * @param index * @return */ - int getCircleColor(int index); + fun getCircleColor(index: Int): Int /** * Returns the number of colors in this DataSet's circle-color array. * * @return */ - int getCircleColorCount(); + val circleColorCount: Int /** * Returns true if drawing circles for this DataSet is enabled, false if not * * @return */ - boolean isDrawCirclesEnabled(); + val isDrawCirclesEnabled: Boolean /** * Returns the color of the inner circle (the circle-hole). * * @return */ - int getCircleHoleColor(); + val circleHoleColor: Int /** * Returns true if drawing the circle-holes is enabled, false if not. * * @return */ - boolean isDrawCircleHoleEnabled(); + val isDrawCircleHoleEnabled: Boolean /** * Returns the DashPathEffect that is used for drawing the lines. * * @return */ - DashPathEffect getDashPathEffect(); + val dashPathEffect: DashPathEffect? /** * Returns true if the dashed-line effect is enabled, false if not. @@ -92,12 +90,12 @@ public interface ILineDataSet extends ILineRadarDataSet { * * @return */ - boolean isDashedLineEnabled(); + val isDashedLineEnabled: Boolean /** * Returns the IFillFormatter that is set for this DataSet. * * @return */ - IFillFormatter getFillFormatter(); + val fillFormatter: IFillFormatter } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt index ce89822716..9876a279bc 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt @@ -1,27 +1,25 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import android.graphics.drawable.Drawable; - -import com.github.mikephil.charting.data.Entry; +import android.graphics.drawable.Drawable +import com.github.mikephil.charting.data.Entry /** * Created by Philipp Jahoda on 21/10/15. */ -public interface ILineRadarDataSet extends ILineScatterCandleRadarDataSet { - +interface ILineRadarDataSet : ILineScatterCandleRadarDataSet { /** * Returns the color that is used for filling the line surface area. * * @return */ - int getFillColor(); + val fillColor: Int /** * Returns the drawable used for filling the area below the line. * * @return */ - Drawable getFillDrawable(); + val fillDrawable: Drawable? /** * Returns the alpha value that is used for filling the line surface, @@ -29,21 +27,21 @@ public interface ILineRadarDataSet extends ILineScatterCandleRa * * @return */ - int getFillAlpha(); + val fillAlpha: Int /** * Returns the stroke-width of the drawn line * * @return */ - float getLineWidth(); + val lineWidth: Float /** * Returns true if filled drawing is enabled, false if not * * @return */ - boolean isDrawFilledEnabled(); + val isDrawFilledEnabled: Boolean /** * Set to true if the DataSet should be drawn filled (surface), and not just @@ -54,5 +52,5 @@ public interface ILineRadarDataSet extends ILineScatterCandleRa * * @param enabled */ - void setDrawFilled(boolean enabled); + fun setDrawFilled(enabled: Boolean) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IPieDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IPieDataSet.kt index a11246b56f..c86e654092 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IPieDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IPieDataSet.kt @@ -1,74 +1,69 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import androidx.annotation.Nullable; - -import com.github.mikephil.charting.data.PieDataSet; -import com.github.mikephil.charting.data.PieEntry; - -public interface IPieDataSet extends IDataSet { +import com.github.mikephil.charting.data.PieDataSet.ValuePosition +import com.github.mikephil.charting.data.PieEntry +interface IPieDataSet : IDataSet { /** * Returns the space that is set to be between the piechart-slices of this * DataSet, in pixels. */ - float getSliceSpace(); + val sliceSpace: Float /** * When enabled, slice spacing will be 0.0 when the smallest value is going to be * smaller than the slice spacing itself. */ - boolean isAutomaticallyDisableSliceSpacingEnabled(); + val isAutomaticallyDisableSliceSpacingEnabled: Boolean /** * Returns the distance a highlighted piechart slice is "shifted" away from * the chart-center in dp. */ - float getSelectionShift(); + val selectionShift: Float - PieDataSet.ValuePosition getXValuePosition(); + val xValuePosition: ValuePosition? - PieDataSet.ValuePosition getYValuePosition(); + val yValuePosition: ValuePosition? /** * When valuePosition is OutsideSlice, indicates line color */ - int getValueLineColor(); + val valueLineColor: Int /** * When valuePosition is OutsideSlice and enabled, line will have the same color as the slice */ - boolean isUseValueColorForLineEnabled(); + val isUseValueColorForLineEnabled: Boolean /** * When valuePosition is OutsideSlice, indicates line width */ - float getValueLineWidth(); + val valueLineWidth: Float /** * When valuePosition is OutsideSlice, indicates offset as percentage out of the slice size */ - float getValueLinePart1OffsetPercentage(); + val valueLinePart1OffsetPercentage: Float /** * When valuePosition is OutsideSlice, indicates length of first half of the line */ - float getValueLinePart1Length(); + val valueLinePart1Length: Float /** * When valuePosition is OutsideSlice, indicates length of second half of the line */ - float getValueLinePart2Length(); + val valueLinePart2Length: Float /** * When valuePosition is OutsideSlice, this allows variable line length */ - boolean isValueLineVariableLength(); + val isValueLineVariableLength: Boolean /** * Gets the color for the highlighted sector */ - @Nullable - Integer getHighlightColor(); - + val highlightColor: Int? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IRadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IRadarDataSet.kt index 8af00d5376..12fa903f8b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IRadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IRadarDataSet.kt @@ -1,30 +1,26 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import com.github.mikephil.charting.data.RadarEntry; +import com.github.mikephil.charting.data.RadarEntry /** * Created by Philipp Jahoda on 03/11/15. */ -public interface IRadarDataSet extends ILineRadarDataSet { +interface IRadarDataSet : ILineRadarDataSet { + /** flag indicating whether highlight circle should be drawn or not */ + /** Sets whether highlight circle should be drawn or not */ + var isDrawHighlightCircleEnabled: Boolean - /// flag indicating whether highlight circle should be drawn or not - boolean isDrawHighlightCircleEnabled(); + val highlightCircleFillColor: Int - /// Sets whether highlight circle should be drawn or not - void setDrawHighlightCircleEnabled(boolean enabled); + /** The stroke color for highlight circle. + * If Utils.COLOR_NONE, the color of the dataset is taken. */ + val highlightCircleStrokeColor: Int - int getHighlightCircleFillColor(); + val highlightCircleStrokeAlpha: Int - /// The stroke color for highlight circle. - /// If Utils.COLOR_NONE, the color of the dataset is taken. - int getHighlightCircleStrokeColor(); + val highlightCircleInnerRadius: Float - int getHighlightCircleStrokeAlpha(); - - float getHighlightCircleInnerRadius(); - - float getHighlightCircleOuterRadius(); - - float getHighlightCircleStrokeWidth(); + val highlightCircleOuterRadius: Float + val highlightCircleStrokeWidth: Float } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IScatterDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IScatterDataSet.kt index ac6122742a..842ba6ea09 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IScatterDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IScatterDataSet.kt @@ -1,38 +1,37 @@ -package com.github.mikephil.charting.interfaces.datasets; +package com.github.mikephil.charting.interfaces.datasets -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.renderer.scatter.IShapeRenderer; +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.renderer.scatter.IShapeRenderer /** * Created by philipp on 21/10/15. */ -public interface IScatterDataSet extends ILineScatterCandleRadarDataSet { - +interface IScatterDataSet : ILineScatterCandleRadarDataSet { /** * Returns the currently set scatter shape size * * @return */ - float getScatterShapeSize(); + val scatterShapeSize: Float /** * Returns radius of the hole in the shape * * @return */ - float getScatterShapeHoleRadius(); + val scatterShapeHoleRadius: Float /** * Returns the color for the hole in the shape * * @return */ - int getScatterShapeHoleColor(); + val scatterShapeHoleColor: Int /** * Returns the IShapeRenderer responsible for rendering this DataSet. * * @return */ - IShapeRenderer getShapeRenderer(); + val shapeRenderer: IShapeRenderer? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt index e6903cf854..fde6a44c80 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt @@ -1,72 +1,81 @@ -package com.github.mikephil.charting.jobs; +package com.github.mikephil.charting.jobs -import android.animation.ValueAnimator; -import android.annotation.SuppressLint; -import android.view.View; - -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.animation.ValueAnimator +import android.annotation.SuppressLint +import android.view.View +import com.github.mikephil.charting.utils.ObjectPool +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by Philipp Jahoda on 19/02/16. */ @SuppressLint("NewApi") -public class AnimatedMoveViewJob extends AnimatedViewPortJob { - - private static ObjectPool pool; - - static { - pool = ObjectPool.create(4, new AnimatedMoveViewJob(null,0,0,null,null,0,0,0)); - pool.setReplenishPercentage(0.5f); - } +class AnimatedMoveViewJob( + viewPortHandler: ViewPortHandler, + xValue: Float, + yValue: Float, + trans: Transformer?, + v: View?, + xOrigin: Float, + yOrigin: Float, + duration: Long +) : AnimatedViewPortJob(viewPortHandler, xValue, yValue, trans, v, xOrigin, yOrigin, duration) { + override fun onAnimationUpdate(animation: ValueAnimator) { + pts[0] = xOrigin + (xValue - xOrigin) * phase + pts[1] = yOrigin + (yValue - yOrigin) * phase - public static AnimatedMoveViewJob getInstance(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v, float xOrigin, float yOrigin, long duration){ - AnimatedMoveViewJob result = pool.get(); - result.mViewPortHandler = viewPortHandler; - result.xValue = xValue; - result.yValue = yValue; - result.mTrans = trans; - result.view = v; - result.xOrigin = xOrigin; - result.yOrigin = yOrigin; - //result.resetAnimator(); - result.animator.setDuration(duration); - return result; + mTrans!!.pointValuesToPixel(pts) + mViewPortHandler.centerViewPort(pts, view) } - public static void recycleInstance(AnimatedMoveViewJob instance){ - // Clear reference avoid memory leak - instance.xValue = 0f; - instance.yValue = 0f; - instance.xOrigin = 0f; - instance.yOrigin = 0f; - instance.animator.setDuration(0); - instance.recycle(); - pool.recycle(instance); + override fun recycleSelf() { + recycleInstance(this) } - - public AnimatedMoveViewJob(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v, float xOrigin, float yOrigin, long duration) { - super(viewPortHandler, xValue, yValue, trans, v, xOrigin, yOrigin, duration); + override fun instantiate(): AnimatedMoveViewJob { + return AnimatedMoveViewJob(ViewPortHandler(), 0f, 0f, null, null, 0f, 0f, 0) } - @Override - public void onAnimationUpdate(ValueAnimator animation) { - - pts[0] = xOrigin + (xValue - xOrigin) * phase; - pts[1] = yOrigin + (yValue - yOrigin) * phase; + companion object { + private val pool = ObjectPool.Companion.create(4, AnimatedMoveViewJob(ViewPortHandler(), 0f, 0f, null, null, 0f, 0f, 0)) - mTrans.pointValuesToPixel(pts); - mViewPortHandler.centerViewPort(pts, view); - } + init { + pool.setReplenishPercentage(0.5f) + } - public void recycleSelf(){ - recycleInstance(this); - } + fun getInstance( + viewPortHandler: ViewPortHandler, + xValue: Float, + yValue: Float, + trans: Transformer?, + v: View?, + xOrigin: Float, + yOrigin: Float, + duration: Long + ): AnimatedMoveViewJob { + val result: AnimatedMoveViewJob = pool.get() + result.mViewPortHandler = viewPortHandler + result.xValue = xValue + result.yValue = yValue + result.mTrans = trans + result.view = v + result.xOrigin = xOrigin + result.yOrigin = yOrigin + //result.resetAnimator(); + result.animator.setDuration(duration) + return result + } - @Override - protected ObjectPool.Poolable instantiate() { - return new AnimatedMoveViewJob(null,0,0,null,null,0,0,0); + fun recycleInstance(instance: AnimatedMoveViewJob) { + // Clear reference avoid memory leak + instance.xValue = 0f + instance.yValue = 0f + instance.xOrigin = 0f + instance.yOrigin = 0f + instance.animator.setDuration(0) + instance.recycle() + pool.recycle(instance) + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt index f8b520a419..627cf1efb8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt @@ -1,99 +1,75 @@ -package com.github.mikephil.charting.jobs; +package com.github.mikephil.charting.jobs -import android.animation.Animator; -import android.animation.ObjectAnimator; -import android.animation.ValueAnimator; -import android.annotation.SuppressLint; -import android.view.View; - -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.animation.Animator +import android.animation.ObjectAnimator +import android.animation.ValueAnimator +import android.animation.ValueAnimator.AnimatorUpdateListener +import android.annotation.SuppressLint +import android.view.View +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by Philipp Jahoda on 19/02/16. */ @SuppressLint("NewApi") -public abstract class AnimatedViewPortJob extends ViewPortJob implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener { - - protected ObjectAnimator animator; - - protected float phase; - - protected float xOrigin; - protected float yOrigin; - - public AnimatedViewPortJob(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v, float xOrigin, float yOrigin, long duration) { - super(viewPortHandler, xValue, yValue, trans, v); - this.xOrigin = xOrigin; - this.yOrigin = yOrigin; - animator = ObjectAnimator.ofFloat(this, "phase", 0f, 1f); - animator.setDuration(duration); - animator.addUpdateListener(this); - animator.addListener(this); +abstract class AnimatedViewPortJob>( + viewPortHandler: ViewPortHandler, + xValue: Float, + yValue: Float, + trans: Transformer?, + v: View?, + var xOrigin: Float, + var yOrigin: Float, + duration: Long +) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v), AnimatorUpdateListener, Animator.AnimatorListener { + protected val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "phase", 0f, 1f) + + var phase: Float = 0f + + init { + animator.setDuration(duration) + animator.addUpdateListener(this) + animator.addListener(this) } @SuppressLint("NewApi") - @Override - public void run() { - animator.start(); - } - - public float getPhase() { - return phase; - } - - public void setPhase(float phase) { - this.phase = phase; - } - - public float getXOrigin() { - return xOrigin; - } - - public float getYOrigin() { - return yOrigin; + override fun run() { + animator.start() } - public abstract void recycleSelf(); + abstract fun recycleSelf() - protected void resetAnimator(){ - animator.removeAllListeners(); - animator.removeAllUpdateListeners(); - animator.reverse(); - animator.addUpdateListener(this); - animator.addListener(this); + protected fun resetAnimator() { + animator.removeAllListeners() + animator.removeAllUpdateListeners() + animator.reverse() + animator.addUpdateListener(this) + animator.addListener(this) } - @Override - public void onAnimationStart(Animator animation) { - + override fun onAnimationStart(animation: Animator) { } - @Override - public void onAnimationEnd(Animator animation) { - try{ - recycleSelf(); - }catch (IllegalArgumentException e){ + override fun onAnimationEnd(animation: Animator) { + try { + recycleSelf() + } catch (e: IllegalArgumentException) { // don't worry about it. } } - @Override - public void onAnimationCancel(Animator animation) { - try{ - recycleSelf(); - }catch (IllegalArgumentException e){ + override fun onAnimationCancel(animation: Animator) { + try { + recycleSelf() + } catch (e: IllegalArgumentException) { // don't worry about it. } } - @Override - public void onAnimationRepeat(Animator animation) { - + override fun onAnimationRepeat(animation: Animator) { } - @Override - public void onAnimationUpdate(ValueAnimator animation) { - + override fun onAnimationUpdate(animation: ValueAnimator) { } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt index d442a2ceeb..d19d10513f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt @@ -1,123 +1,125 @@ -package com.github.mikephil.charting.jobs; - -import android.animation.Animator; -import android.animation.ValueAnimator; -import android.annotation.SuppressLint; -import android.graphics.Matrix; -import android.view.View; - -import com.github.mikephil.charting.charts.BarLineChartBase; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; +package com.github.mikephil.charting.jobs + +import android.animation.Animator +import android.animation.ValueAnimator +import android.annotation.SuppressLint +import android.graphics.Matrix +import android.view.View +import com.github.mikephil.charting.charts.BarLineChartBase +import com.github.mikephil.charting.components.YAxis +import com.github.mikephil.charting.utils.ObjectPool +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by Philipp Jahoda on 19/02/16. */ @SuppressLint("NewApi") -public class AnimatedZoomJob extends AnimatedViewPortJob implements Animator.AnimatorListener { - - private static ObjectPool pool; - - static { - pool = ObjectPool.create(8, new AnimatedZoomJob(null,null,null,null,0,0,0,0,0,0,0,0,0,0)); +open class AnimatedZoomJob @SuppressLint("NewApi") constructor( + viewPortHandler: ViewPortHandler, + v: View?, + trans: Transformer?, + axis: YAxis, + xAxisRange: Float, + scaleX: Float, + scaleY: Float, + xOrigin: Float, + yOrigin: Float, + protected var zoomCenterX: Float, + protected var zoomCenterY: Float, + protected var zoomOriginX: Float, + protected var zoomOriginY: Float, + duration: Long +) : AnimatedViewPortJob(viewPortHandler, scaleX, scaleY, trans, v, xOrigin, yOrigin, duration), Animator.AnimatorListener { + protected var yAxis: YAxis + + protected var xAxisRange: Float + + protected var mOnAnimationUpdateMatrixBuffer: Matrix = Matrix() + + init { + this.animator.addListener(this) + this.yAxis = axis + this.xAxisRange = xAxisRange } - public static AnimatedZoomJob getInstance(ViewPortHandler viewPortHandler, View v, Transformer trans, YAxis axis, float xAxisRange, float scaleX, float scaleY, float xOrigin, float yOrigin, float zoomCenterX, float zoomCenterY, float zoomOriginX, float zoomOriginY, long duration) { - AnimatedZoomJob result = pool.get(); - result.mViewPortHandler = viewPortHandler; - result.xValue = scaleX; - result.yValue = scaleY; - result.mTrans = trans; - result.view = v; - result.xOrigin = xOrigin; - result.yOrigin = yOrigin; - result.yAxis = axis; - result.xAxisRange = xAxisRange; - result.zoomCenterX = zoomCenterX; - result.zoomCenterY = zoomCenterY; - result.zoomOriginX = zoomOriginX; - result.zoomOriginY = zoomOriginY; - result.resetAnimator(); - result.animator.setDuration(duration); - return result; - } - - protected float zoomOriginX; - protected float zoomOriginY; + override fun onAnimationUpdate(animation: ValueAnimator) { + val scaleX = xOrigin + (xValue - xOrigin) * phase + val scaleY = yOrigin + (yValue - yOrigin) * phase - protected float zoomCenterX; - protected float zoomCenterY; + val save = mOnAnimationUpdateMatrixBuffer + mViewPortHandler.setZoom(scaleX, scaleY, save) + mViewPortHandler.refresh(save, view, false) - protected YAxis yAxis; + val valsInView = yAxis.mAxisRange / mViewPortHandler.scaleY + val xsInView = xAxisRange / mViewPortHandler.scaleX - protected float xAxisRange; + pts[0] = zoomOriginX + ((zoomCenterX - xsInView / 2f) - zoomOriginX) * phase + pts[1] = zoomOriginY + ((zoomCenterY + valsInView / 2f) - zoomOriginY) * phase - @SuppressLint("NewApi") - public AnimatedZoomJob(ViewPortHandler viewPortHandler, View v, Transformer trans, YAxis axis, float xAxisRange, float scaleX, float scaleY, float xOrigin, float yOrigin, float zoomCenterX, float zoomCenterY, float zoomOriginX, float zoomOriginY, long duration) { - super(viewPortHandler, scaleX, scaleY, trans, v, xOrigin, yOrigin, duration); + mTrans?.pointValuesToPixel(pts) - this.zoomCenterX = zoomCenterX; - this.zoomCenterY = zoomCenterY; - this.zoomOriginX = zoomOriginX; - this.zoomOriginY = zoomOriginY; - this.animator.addListener(this); - this.yAxis = axis; - this.xAxisRange = xAxisRange; + mViewPortHandler.translate(pts, save) + mViewPortHandler.refresh(save, view, true) } - protected Matrix mOnAnimationUpdateMatrixBuffer = new Matrix(); - @Override - public void onAnimationUpdate(ValueAnimator animation) { - - float scaleX = xOrigin + (xValue - xOrigin) * phase; - float scaleY = yOrigin + (yValue - yOrigin) * phase; - - Matrix save = mOnAnimationUpdateMatrixBuffer; - mViewPortHandler.setZoom(scaleX, scaleY, save); - mViewPortHandler.refresh(save, view, false); - - float valsInView = yAxis.mAxisRange / mViewPortHandler.getScaleY(); - float xsInView = xAxisRange / mViewPortHandler.getScaleX(); - - pts[0] = zoomOriginX + ((zoomCenterX - xsInView / 2f) - zoomOriginX) * phase; - pts[1] = zoomOriginY + ((zoomCenterY + valsInView / 2f) - zoomOriginY) * phase; - - mTrans.pointValuesToPixel(pts); - - mViewPortHandler.translate(pts, save); - mViewPortHandler.refresh(save, view, true); + override fun onAnimationEnd(animation: Animator) { + (view as? BarLineChartBase<*, *, *>)?.calculateOffsets() + view?.postInvalidate() } - @Override - public void onAnimationEnd(Animator animation) { - ((BarLineChartBase) view).calculateOffsets(); - view.postInvalidate(); + override fun onAnimationCancel(animation: Animator) { } - @Override - public void onAnimationCancel(Animator animation) { - + override fun onAnimationRepeat(animation: Animator) { } - @Override - public void onAnimationRepeat(Animator animation) { - + override fun recycleSelf() { } - @Override - public void recycleSelf() { - + override fun onAnimationStart(animation: Animator) { } - @Override - public void onAnimationStart(Animator animation) { - + override fun instantiate(): AnimatedZoomJob? { + return AnimatedZoomJob(ViewPortHandler(), null, null, YAxis(), 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0) } - @Override - protected ObjectPool.Poolable instantiate() { - return new AnimatedZoomJob(null,null,null,null,0,0,0,0,0,0,0,0,0,0); + companion object { + private val pool = ObjectPool.Companion.create(8, AnimatedZoomJob(ViewPortHandler(), null, null, YAxis(), 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0)) + + fun getInstance( + viewPortHandler: ViewPortHandler, + v: View?, + trans: Transformer, + axis: YAxis, + xAxisRange: Float, + scaleX: Float, + scaleY: Float, + xOrigin: Float, + yOrigin: Float, + zoomCenterX: Float, + zoomCenterY: Float, + zoomOriginX: Float, + zoomOriginY: Float, + duration: Long + ): AnimatedZoomJob { + val result: AnimatedZoomJob = pool.get() + result.mViewPortHandler = viewPortHandler + result.xValue = scaleX + result.yValue = scaleY + result.mTrans = trans + result.view = v + result.xOrigin = xOrigin + result.yOrigin = yOrigin + result.yAxis = axis + result.xAxisRange = xAxisRange + result.zoomCenterX = zoomCenterX + result.zoomCenterY = zoomCenterY + result.zoomOriginX = zoomOriginX + result.zoomOriginY = zoomOriginY + result.resetAnimator() + result.animator.setDuration(duration) + return result + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt index e24d746f4a..8e5fe58cb7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt @@ -1,60 +1,52 @@ +package com.github.mikephil.charting.jobs -package com.github.mikephil.charting.jobs; - -import android.view.View; - -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.view.View +import com.github.mikephil.charting.utils.ObjectPool +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by Philipp Jahoda on 19/02/16. */ -public class MoveViewJob extends ViewPortJob { - - private static ObjectPool pool; +open class MoveViewJob(viewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, v: View?) : + ViewPortJob(viewPortHandler, xValue, yValue, trans, v) { + override fun run() { + pts[0] = xValue + pts[1] = yValue - static { - pool = ObjectPool.create(2, new MoveViewJob(null,0,0,null,null)); - pool.setReplenishPercentage(0.5f); - } - - public static MoveViewJob getInstance(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v){ - MoveViewJob result = pool.get(); - result.mViewPortHandler = viewPortHandler; - result.xValue = xValue; - result.yValue = yValue; - result.mTrans = trans; - result.view = v; - return result; - } + mTrans?.pointValuesToPixel(pts) + mViewPortHandler.centerViewPort(pts, view) - public static void recycleInstance(MoveViewJob instance){ - instance.recycle(); - // Clear reference avoid memory leak - instance.xValue = 0f; - instance.yValue = 0f; - pool.recycle(instance); + recycleInstance(this) } - public MoveViewJob(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v) { - super(viewPortHandler, xValue, yValue, trans, v); - } - - @Override - public void run() { - - pts[0] = xValue; - pts[1] = yValue; - - mTrans.pointValuesToPixel(pts); - mViewPortHandler.centerViewPort(pts, view); - - this.recycleInstance(this); + override fun instantiate(): MoveViewJob { + return MoveViewJob(mViewPortHandler, xValue, yValue, mTrans, view) } - @Override - protected ObjectPool.Poolable instantiate() { - return new MoveViewJob(mViewPortHandler, xValue, yValue, mTrans, view); + companion object { + private val pool = ObjectPool.Companion.create(2, MoveViewJob(ViewPortHandler(), 0f, 0f, null, null)) + + init { + pool.setReplenishPercentage(0.5f) + } + + fun getInstance(viewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, v: View?): MoveViewJob { + val result: MoveViewJob = pool.get() + result.mViewPortHandler = viewPortHandler + result.xValue = xValue + result.yValue = yValue + result.mTrans = trans + result.view = v + return result + } + + fun recycleInstance(instance: MoveViewJob) { + instance.recycle() + // Clear reference avoid memory leak + instance.xValue = 0f + instance.yValue = 0f + pool.recycle(instance) + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt index a1df37425b..8c75822a25 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt @@ -1,11 +1,9 @@ +package com.github.mikephil.charting.jobs -package com.github.mikephil.charting.jobs; - -import android.view.View; - -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.view.View +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler /** * Runnable that is used for viewport modifications since they cannot be @@ -13,41 +11,30 @@ import com.github.mikephil.charting.utils.ViewPortHandler; * modifications until the onSizeChanged(...) method of the chart-view is called. * This is especially important if viewport modifying methods are called on the chart * directly after initialization. - * + * * @author Philipp Jahoda */ -public abstract class ViewPortJob extends ObjectPool.Poolable implements Runnable { - - protected float[] pts = new float[2]; - - protected ViewPortHandler mViewPortHandler; - protected float xValue = 0f; - protected float yValue = 0f; - protected Transformer mTrans; - protected View view; - - public ViewPortJob(ViewPortHandler viewPortHandler, float xValue, float yValue, - Transformer trans, View v) { - - this.mViewPortHandler = viewPortHandler; - this.xValue = xValue; - this.yValue = yValue; - this.mTrans = trans; - this.view = v; - - } - - public float getXValue() { - return xValue; - } - - public float getYValue() { - return yValue; +abstract class ViewPortJob>( + protected var mViewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, + trans: Transformer?, v: View? +) : Poolable(), Runnable { + protected var pts: FloatArray = FloatArray(2) + + var xValue: Float = 0f + protected set + var yValue: Float = 0f + protected set + protected var mTrans: Transformer? + protected var view: View? + + init { + this.xValue = xValue + this.yValue = yValue + this.mTrans = trans + this.view = v } - protected void recycle() { - mViewPortHandler = null; - mTrans = null; - view = null; + protected fun recycle() { + view = null } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt index 8c4c74df96..544e31d8b0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt @@ -1,94 +1,83 @@ +package com.github.mikephil.charting.jobs -package com.github.mikephil.charting.jobs; - -import android.graphics.Matrix; -import android.view.View; - -import com.github.mikephil.charting.charts.BarLineChartBase; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.graphics.Matrix +import android.view.View +import com.github.mikephil.charting.charts.BarLineChartBase +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.utils.ObjectPool +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by Philipp Jahoda on 19/02/16. */ -public class ZoomJob extends ViewPortJob { +open class ZoomJob( + viewPortHandler: ViewPortHandler, protected var scaleX: Float, protected var scaleY: Float, xValue: Float, yValue: Float, trans: Transformer?, + protected var axisDependency: AxisDependency?, v: View? +) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v) { + protected var mRunMatrixBuffer: Matrix = Matrix() - private static ObjectPool pool; + override fun run() { + val save = mRunMatrixBuffer + mViewPortHandler.zoom(scaleX, scaleY, save) + mViewPortHandler.refresh(save, view, false) - static { - pool = ObjectPool.create(1, new ZoomJob(null, 0, 0, 0, 0, null, null, null)); - pool.setReplenishPercentage(0.5f); - } + (view as? BarLineChartBase<*, *, *>)?.let { view -> + val yValsInView = view.getAxis(axisDependency).mAxisRange / mViewPortHandler.scaleY + val xValsInView = view.xAxis.mAxisRange / mViewPortHandler.scaleX - public static ZoomJob getInstance(ViewPortHandler viewPortHandler, float scaleX, float scaleY, float xValue, float yValue, - Transformer trans, YAxis.AxisDependency axis, View v) { - ZoomJob result = pool.get(); - result.xValue = xValue; - result.yValue = yValue; - result.scaleX = scaleX; - result.scaleY = scaleY; - result.mViewPortHandler = viewPortHandler; - result.mTrans = trans; - result.axisDependency = axis; - result.view = v; - return result; - } + pts[0] = xValue - xValsInView / 2f + pts[1] = yValue + yValsInView / 2f - public static void recycleInstance(ZoomJob instance) { - // Clear reference avoid memory leak - instance.xValue = 0f; - instance.yValue = 0f; - instance.scaleX = 0f; - instance.scaleY = 0f; - instance.axisDependency = null; - instance.recycle(); - pool.recycle(instance); - } - - protected float scaleX; - protected float scaleY; + mTrans?.pointValuesToPixel(pts) - protected YAxis.AxisDependency axisDependency; + mViewPortHandler.translate(pts, save) + mViewPortHandler.refresh(save, view, false) - public ZoomJob(ViewPortHandler viewPortHandler, float scaleX, float scaleY, float xValue, float yValue, Transformer trans, - YAxis.AxisDependency axis, View v) { - super(viewPortHandler, xValue, yValue, trans, v); + view.calculateOffsets() + view.postInvalidate() + } - this.scaleX = scaleX; - this.scaleY = scaleY; - this.axisDependency = axis; + recycleInstance(this) } - protected Matrix mRunMatrixBuffer = new Matrix(); - - @Override - public void run() { - - Matrix save = mRunMatrixBuffer; - mViewPortHandler.zoom(scaleX, scaleY, save); - mViewPortHandler.refresh(save, view, false); - - float yValsInView = ((BarLineChartBase) view).getAxis(axisDependency).mAxisRange / mViewPortHandler.getScaleY(); - float xValsInView = ((BarLineChartBase) view).getXAxis().mAxisRange / mViewPortHandler.getScaleX(); - - pts[0] = xValue - xValsInView / 2f; - pts[1] = yValue + yValsInView / 2f; - - mTrans.pointValuesToPixel(pts); - - mViewPortHandler.translate(pts, save); - mViewPortHandler.refresh(save, view, false); - - ((BarLineChartBase) view).calculateOffsets(); - view.postInvalidate(); - - recycleInstance(this); + override fun instantiate(): ZoomJob { + return ZoomJob(ViewPortHandler(), 0f, 0f, 0f, 0f, null, null, null) } - @Override - protected ObjectPool.Poolable instantiate() { - return new ZoomJob(null, 0, 0, 0, 0, null, null, null); + companion object { + private val pool = ObjectPool.Companion.create(1, ZoomJob(ViewPortHandler(), 0f, 0f, 0f, 0f, null, null, null)) + + init { + pool.setReplenishPercentage(0.5f) + } + + fun getInstance( + viewPortHandler: ViewPortHandler, scaleX: Float, scaleY: Float, xValue: Float, yValue: Float, + trans: Transformer?, axis: AxisDependency?, v: View? + ): ZoomJob { + val result: ZoomJob = pool.get() + result.xValue = xValue + result.yValue = yValue + result.scaleX = scaleX + result.scaleY = scaleY + result.mViewPortHandler = viewPortHandler + result.mTrans = trans + result.axisDependency = axis + result.view = v + return result + } + + fun recycleInstance(instance: ZoomJob) { + // Clear reference avoid memory leak + instance.xValue = 0f + instance.yValue = 0f + instance.scaleX = 0f + instance.scaleY = 0f + instance.axisDependency = null + instance.recycle() + pool.recycle(instance) + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt index c18906be97..09744b6fc9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt @@ -24,11 +24,10 @@ import kotlin.math.sqrt */ @Suppress("MemberVisibilityCanBePrivate") class BarLineChartTouchListener( - chart: BarLineChartBase?>?>, + chart: BarLineChartBase<*, *, *>, touchMatrix: Matrix, dragTriggerDistance: Float -) : - ChartTouchListener?>?>?>(chart) { +) : ChartTouchListener>(chart) { /** * returns the matrix object the listener holds * @@ -114,11 +113,11 @@ class BarLineChartTouchListener( } } - if (mTouchMode == NONE || mChart!!.isFlingEnabled) { - mGestureDetector.onTouchEvent(event) + if (touchMode == NONE || mChart.isFlingEnabled) { + mGestureDetector?.onTouchEvent(event) } - if (!mChart!!.isDragEnabled && (!mChart!!.isScaleXEnabled && !mChart!!.isScaleYEnabled)) return true + if (!mChart.isDragEnabled && (!mChart.isScaleXEnabled && !mChart.isScaleYEnabled)) return true // Handle touch events here... when (event.action and MotionEvent.ACTION_MASK) { @@ -131,7 +130,7 @@ class BarLineChartTouchListener( } MotionEvent.ACTION_POINTER_DOWN -> if (event.pointerCount >= 2) { - mChart!!.disableScroll() + mChart.disableScroll() saveTouchStart(event) @@ -145,11 +144,11 @@ class BarLineChartTouchListener( savedDist = spacing(event) if (savedDist > 10f) { - mTouchMode = if (mChart!!.isPinchZoomEnabled) { + touchMode = if (mChart.isPinchZoomEnabled) { PINCH_ZOOM } else { - if (mChart!!.isScaleXEnabled != mChart!!.isScaleYEnabled) { - if (mChart!!.isScaleXEnabled) X_ZOOM else Y_ZOOM + if (mChart.isScaleXEnabled != mChart.isScaleYEnabled) { + if (mChart.isScaleXEnabled) X_ZOOM else Y_ZOOM } else { if (savedXDist > savedYDist) X_ZOOM else Y_ZOOM } @@ -160,18 +159,18 @@ class BarLineChartTouchListener( midPoint(touchPointCenter, event) } - MotionEvent.ACTION_MOVE -> if (mTouchMode == DRAG) { - mChart!!.disableScroll() + MotionEvent.ACTION_MOVE -> if (touchMode == DRAG) { + mChart.disableScroll() - val x = if (mChart!!.isDragXEnabled) event.x - touchStartPoint.x else 0f - val y = if (mChart!!.isDragYEnabled) event.y - touchStartPoint.y else 0f + val x = if (mChart.isDragXEnabled) event.x - touchStartPoint.x else 0f + val y = if (mChart.isDragYEnabled) event.y - touchStartPoint.y else 0f performDrag(event, x, y) - } else if (mTouchMode == X_ZOOM || mTouchMode == Y_ZOOM || mTouchMode == PINCH_ZOOM) { - mChart!!.disableScroll() + } else if (touchMode == X_ZOOM || touchMode == Y_ZOOM || touchMode == PINCH_ZOOM) { + mChart.disableScroll() - if (mChart!!.isScaleXEnabled || mChart!!.isScaleYEnabled) performZoom(event) - } else if (mTouchMode == NONE + if (mChart.isScaleXEnabled || mChart.isScaleYEnabled) performZoom(event) + } else if (touchMode == NONE && abs( distance( event.x, touchStartPoint.x, event.y, @@ -179,26 +178,26 @@ class BarLineChartTouchListener( ).toDouble() ) > dragTriggerDist ) { - if (mChart!!.isDragEnabled) { - val shouldPan = !mChart!!.isFullyZoomedOut || - !mChart!!.hasNoDragOffset() + if (mChart.isDragEnabled) { + val shouldPan = !mChart.isFullyZoomedOut || + !mChart.hasNoDragOffset() if (shouldPan) { val distanceX = abs((event.x - touchStartPoint.x).toDouble()).toFloat() val distanceY = abs((event.y - touchStartPoint.y).toDouble()).toFloat() // Disable dragging in a direction that's disallowed - if ((mChart!!.isDragXEnabled || distanceY >= distanceX) && - (mChart!!.isDragYEnabled || distanceY <= distanceX) + if ((mChart.isDragXEnabled || distanceY >= distanceX) && + (mChart.isDragYEnabled || distanceY <= distanceX) ) { - mLastGesture = ChartGesture.DRAG - mTouchMode = DRAG + lastGesture = ChartGesture.DRAG + touchMode = DRAG } } else { - if (mChart!!.isHighlightPerDragEnabled) { - mLastGesture = ChartGesture.DRAG + if (mChart.isHighlightPerDragEnabled) { + lastGesture = ChartGesture.DRAG - if (mChart!!.isHighlightPerDragEnabled) performHighlightDrag(event) + if (mChart.isHighlightPerDragEnabled) performHighlightDrag(event) } } } @@ -207,14 +206,14 @@ class BarLineChartTouchListener( MotionEvent.ACTION_UP -> { val velocityTracker = mVelocityTracker val pointerId = event.getPointerId(0) - velocityTracker!!.computeCurrentVelocity(1000, Utils.getMaximumFlingVelocity().toFloat()) + velocityTracker!!.computeCurrentVelocity(1000, Utils.maximumFlingVelocity.toFloat()) val velocityY = velocityTracker.getYVelocity(pointerId) val velocityX = velocityTracker.getXVelocity(pointerId) - if (abs(velocityX.toDouble()) > Utils.getMinimumFlingVelocity() || - abs(velocityY.toDouble()) > Utils.getMinimumFlingVelocity() + if (abs(velocityX.toDouble()) > Utils.minimumFlingVelocity || + abs(velocityY.toDouble()) > Utils.minimumFlingVelocity ) { - if (mTouchMode == DRAG && mChart!!.isDragDecelerationEnabled) { + if (touchMode == DRAG && mChart.isDragDecelerationEnabled) { stopDeceleration() mDecelerationLastTime = AnimationUtils.currentAnimationTimeMillis() @@ -230,17 +229,17 @@ class BarLineChartTouchListener( } } - if (mTouchMode == X_ZOOM || mTouchMode == Y_ZOOM || mTouchMode == PINCH_ZOOM || mTouchMode == POST_ZOOM) { + if (touchMode == X_ZOOM || touchMode == Y_ZOOM || touchMode == PINCH_ZOOM || touchMode == POST_ZOOM) { // Range might have changed, which means that Y-axis labels // could have changed in size, affecting Y-axis size. // So we need to recalculate offsets. - mChart!!.calculateOffsets() - mChart!!.postInvalidate() + mChart.calculateOffsets() + mChart.postInvalidate() } - mTouchMode = NONE - mChart!!.enableScroll() + touchMode = NONE + mChart.enableScroll() mVelocityTracker?.recycle() mVelocityTracker = null @@ -249,19 +248,19 @@ class BarLineChartTouchListener( } MotionEvent.ACTION_POINTER_UP -> { - Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, mVelocityTracker) + Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, mVelocityTracker!!) - mTouchMode = POST_ZOOM + touchMode = POST_ZOOM } MotionEvent.ACTION_CANCEL -> { - mTouchMode = NONE + touchMode = NONE endAction(event) } } // perform the transformation, update the chart - matrix = mChart!!.viewPortHandler.refresh(matrix, mChart!!, true) + matrix = mChart.viewPortHandler.refresh(matrix, mChart, true) return true // indicate event was handled } @@ -277,7 +276,7 @@ class BarLineChartTouchListener( touchStartPoint.x = event.x touchStartPoint.y = event.y - mClosestDataSetToTouch = mChart!!.getDataSetByTouchPoint(event.x, event.y) + mClosestDataSetToTouch = mChart.getDataSetByTouchPoint(event.x, event.y) } /** @@ -288,11 +287,11 @@ class BarLineChartTouchListener( private fun performDrag(event: MotionEvent, distanceX: Float, distanceY: Float) { var distanceXLocal = distanceX var distanceYLocal = distanceY - mLastGesture = ChartGesture.DRAG + lastGesture = ChartGesture.DRAG matrix.set(savedMatrix) - val l = mChart!!.onChartGestureListener + val l = mChart.onChartGestureListener // check if axis is inverted if (inverted()) { @@ -318,7 +317,7 @@ class BarLineChartTouchListener( private fun performZoom(event: MotionEvent) { if (event.pointerCount >= 2) { // two finger zoom - val l = mChart!!.onChartGestureListener + val l = mChart.onChartGestureListener // get the distance between the pointers of the touch event val totalDist = spacing(event) @@ -327,11 +326,11 @@ class BarLineChartTouchListener( // get the translation val t = getTrans(touchPointCenter.x, touchPointCenter.y) - val h = mChart!!.viewPortHandler + val h = mChart.viewPortHandler // take actions depending on the activated touch mode - if (mTouchMode == PINCH_ZOOM) { - mLastGesture = ChartGesture.PINCH_ZOOM + if (touchMode == PINCH_ZOOM) { + lastGesture = ChartGesture.PINCH_ZOOM val scale = totalDist / savedDist // total scale @@ -341,8 +340,8 @@ class BarLineChartTouchListener( val canZoomMoreY = if (isZoomingOut) h.canZoomOutMoreY() else h.canZoomInMoreY() - val scaleX = if (mChart!!.isScaleXEnabled) scale else 1f - val scaleY = if (mChart!!.isScaleYEnabled) scale else 1f + val scaleX = if (mChart.isScaleXEnabled) scale else 1f + val scaleY = if (mChart.isScaleYEnabled) scale else 1f if (canZoomMoreY || canZoomMoreX) { matrix.set(savedMatrix) @@ -350,8 +349,8 @@ class BarLineChartTouchListener( l?.onChartScale(event, scaleX, scaleY) } - } else if (mTouchMode == X_ZOOM && mChart!!.isScaleXEnabled) { - mLastGesture = ChartGesture.X_ZOOM + } else if (touchMode == X_ZOOM && mChart.isScaleXEnabled) { + lastGesture = ChartGesture.X_ZOOM val xDist = getXDist(event) val scaleX = xDist / savedXDist // x-axis scale @@ -365,8 +364,8 @@ class BarLineChartTouchListener( l?.onChartScale(event, scaleX, 1f) } - } else if (mTouchMode == Y_ZOOM && mChart!!.isScaleYEnabled) { - mLastGesture = ChartGesture.Y_ZOOM + } else if (touchMode == Y_ZOOM && mChart.isScaleYEnabled) { + lastGesture = ChartGesture.Y_ZOOM val yDist = getYDist(event) val scaleY = yDist / savedYDist // y-axis scale @@ -394,7 +393,7 @@ class BarLineChartTouchListener( * @return */ private fun getLimitedScaleX(scaleX: Float, t: MPPointF): Float { - val h = mChart!!.viewPortHandler + val h = mChart.viewPortHandler tempMatrix.set(savedMatrix) tempMatrix.postScale(scaleX, 1f, t.x, t.y) @@ -421,7 +420,7 @@ class BarLineChartTouchListener( * @return */ private fun getLimitedScaleY(scaleY: Float, t: MPPointF): Float { - val h = mChart!!.viewPortHandler + val h = mChart.viewPortHandler tempMatrix.set(savedMatrix) tempMatrix.postScale(1f, scaleY, t.x, t.y) @@ -447,11 +446,11 @@ class BarLineChartTouchListener( * @param e */ private fun performHighlightDrag(e: MotionEvent) { - val h = mChart!!.getHighlightByTouchPoint(e.x, e.y) + val h = mChart.getHighlightByTouchPoint(e.x, e.y) if (h != null && !h.equalTo(mLastHighlighted)) { mLastHighlighted = h - mChart!!.highlightValue(h, true) + mChart.highlightValue(h, true) } } @@ -466,7 +465,7 @@ class BarLineChartTouchListener( * @return */ fun getTrans(x: Float, y: Float): MPPointF { - val vph = mChart!!.viewPortHandler + val vph = mChart.viewPortHandler val xTrans = x - vph.offsetLeft() var yTrans = 0f @@ -475,7 +474,7 @@ class BarLineChartTouchListener( yTrans = if (inverted()) { -(y - vph.offsetTop()) } else { - -(mChart!!.measuredHeight - y - vph.offsetBottom()) + -(mChart.measuredHeight - y - vph.offsetBottom()) } return MPPointF.getInstance(xTrans, yTrans) @@ -487,8 +486,8 @@ class BarLineChartTouchListener( * @return */ private fun inverted(): Boolean { - return (mClosestDataSetToTouch == null && mChart!!.isAnyAxisInverted) || (mClosestDataSetToTouch != null - && mChart!!.isInverted(mClosestDataSetToTouch!!.axisDependency)) + return (mClosestDataSetToTouch == null && mChart.isAnyAxisInverted) || (mClosestDataSetToTouch != null + && mChart.isInverted(mClosestDataSetToTouch!!.axisDependency)) } /** @@ -507,22 +506,22 @@ class BarLineChartTouchListener( } override fun onDoubleTap(e: MotionEvent): Boolean { - mLastGesture = ChartGesture.DOUBLE_TAP + lastGesture = ChartGesture.DOUBLE_TAP - val l = mChart!!.onChartGestureListener + val l = mChart.onChartGestureListener l?.onChartDoubleTapped(e) // check if double-tap zooming is enabled - if (mChart!!.isDoubleTapToZoomEnabled && mChart!!.data!!.entryCount > 0) { + if (mChart.isDoubleTapToZoomEnabled && mChart.data!!.entryCount > 0) { val trans = getTrans(e.x, e.y) - val scaleX = if (mChart!!.isScaleXEnabled) 1.4f else 1f - val scaleY = if (mChart!!.isScaleYEnabled) 1.4f else 1f + val scaleX = if (mChart.isScaleXEnabled) 1.4f else 1f + val scaleY = if (mChart.isScaleYEnabled) 1.4f else 1f - mChart!!.zoom(scaleX, scaleY, trans.x, trans.y) + mChart.zoom(scaleX, scaleY, trans.x, trans.y) - if (mChart!!.isLogEnabled) Log.i( + if (mChart.isLogEnabled) Log.i( "BarlineChartTouch", ("Double-Tap, Zooming In, x: " + trans.x + ", y: " + trans.y) ) @@ -536,34 +535,34 @@ class BarLineChartTouchListener( } override fun onLongPress(e: MotionEvent) { - mLastGesture = ChartGesture.LONG_PRESS + lastGesture = ChartGesture.LONG_PRESS - val l = mChart!!.onChartGestureListener + val l = mChart.onChartGestureListener l?.onChartLongPressed(e) } override fun onSingleTapUp(e: MotionEvent): Boolean { - mLastGesture = ChartGesture.SINGLE_TAP + lastGesture = ChartGesture.SINGLE_TAP - val l = mChart!!.onChartGestureListener + val l = mChart.onChartGestureListener l?.onChartSingleTapped(e) - if (!mChart!!.isHighlightPerTapEnabled) { + if (!mChart.isHighlightPerTapEnabled) { return false } - val h = mChart!!.getHighlightByTouchPoint(e.x, e.y) + val h = mChart.getHighlightByTouchPoint(e.x, e.y) performHighlight(h, e) return super.onSingleTapUp(e) } override fun onFling(e1: MotionEvent?, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean { - mLastGesture = ChartGesture.FLING + lastGesture = ChartGesture.FLING - val l = mChart!!.onChartGestureListener + val l = mChart.onChartGestureListener l?.onChartFling(e1, e2, velocityX, velocityY) @@ -581,8 +580,8 @@ class BarLineChartTouchListener( val currentTime = AnimationUtils.currentAnimationTimeMillis() - mDecelerationVelocity.x *= mChart!!.dragDecelerationFrictionCoef - mDecelerationVelocity.y *= mChart!!.dragDecelerationFrictionCoef + mDecelerationVelocity.x *= mChart.dragDecelerationFrictionCoef + mDecelerationVelocity.y *= mChart.dragDecelerationFrictionCoef val timeInterval = (currentTime - mDecelerationLastTime).toFloat() / 1000f @@ -597,13 +596,13 @@ class BarLineChartTouchListener( mDecelerationCurrentPoint.y, 0 ) - val dragDistanceX = if (mChart!!.isDragXEnabled) mDecelerationCurrentPoint.x - touchStartPoint.x else 0f - val dragDistanceY = if (mChart!!.isDragYEnabled) mDecelerationCurrentPoint.y - touchStartPoint.y else 0f + val dragDistanceX = if (mChart.isDragXEnabled) mDecelerationCurrentPoint.x - touchStartPoint.x else 0f + val dragDistanceY = if (mChart.isDragYEnabled) mDecelerationCurrentPoint.y - touchStartPoint.y else 0f performDrag(event, dragDistanceX, dragDistanceY) event.recycle() - matrix = mChart!!.viewPortHandler.refresh(matrix, mChart!!, false) + matrix = mChart.viewPortHandler.refresh(matrix, mChart, false) mDecelerationLastTime = currentTime @@ -612,8 +611,8 @@ class BarLineChartTouchListener( // Range might have changed, which means that Y-axis labels // could have changed in size, affecting Y-axis size. // So we need to recalculate offsets. - mChart!!.calculateOffsets() - mChart!!.postInvalidate() + mChart.calculateOffsets() + mChart.postInvalidate() stopDeceleration() } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt index 75c8e864b4..34ab681e46 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt @@ -1,72 +1,67 @@ -package com.github.mikephil.charting.listener; +package com.github.mikephil.charting.listener -import android.view.GestureDetector; -import android.view.MotionEvent; -import android.view.View; - -import com.github.mikephil.charting.charts.Chart; -import com.github.mikephil.charting.highlight.Highlight; +import android.view.GestureDetector +import android.view.GestureDetector.SimpleOnGestureListener +import android.view.MotionEvent +import android.view.View.OnTouchListener +import com.github.mikephil.charting.charts.Chart +import com.github.mikephil.charting.highlight.Highlight +import kotlin.math.sqrt /** * Created by philipp on 12/06/15. */ -public abstract class ChartTouchListener> extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener { - - public enum ChartGesture { +abstract class ChartTouchListener>( + /** + * the chart the listener represents + */ + protected var mChart: T, +) : SimpleOnGestureListener(), OnTouchListener { + enum class ChartGesture { NONE, DRAG, X_ZOOM, Y_ZOOM, PINCH_ZOOM, ROTATE, SINGLE_TAP, DOUBLE_TAP, LONG_PRESS, FLING } + /** + * Returns the last gesture that has been performed on the chart. + * + * @return + */ /** * the last touch gesture that has been performed - **/ - protected ChartGesture mLastGesture = ChartGesture.NONE; - - // states - protected static final int NONE = 0; - protected static final int DRAG = 1; - protected static final int X_ZOOM = 2; - protected static final int Y_ZOOM = 3; - protected static final int PINCH_ZOOM = 4; - protected static final int POST_ZOOM = 5; - protected static final int ROTATE = 6; + */ + var lastGesture: ChartGesture = ChartGesture.NONE + protected set + /** + * returns the touch mode the listener is currently in + * + * @return + */ /** * integer field that holds the current touch-state */ - protected int mTouchMode = NONE; + var touchMode: Int = NONE + protected set /** * the last highlighted object (via touch) */ - protected Highlight mLastHighlighted; + protected var mLastHighlighted: Highlight? = null /** * the gesturedetector used for detecting taps and longpresses, ... */ - protected GestureDetector mGestureDetector; - - /** - * the chart the listener represents - */ - protected T mChart; - - public ChartTouchListener(T chart) { - this.mChart = chart; - - mGestureDetector = new GestureDetector(chart.getContext(), this); - } + protected val mGestureDetector: GestureDetector? = GestureDetector(mChart.context, this) /** * Calls the OnChartGestureListener to do the start callback * * @param me */ - public void startAction(MotionEvent me) { - - OnChartGestureListener l = mChart.getOnChartGestureListener(); + fun startAction(me: MotionEvent?) { + val l = mChart.onChartGestureListener - if (l != null) - l.onChartGestureStart(me, mLastGesture); + l?.onChartGestureStart(me, this.lastGesture) } /** @@ -74,12 +69,10 @@ public abstract class ChartTouchListener> extends GestureDete * * @param me */ - public void endAction(MotionEvent me) { - - OnChartGestureListener l = mChart.getOnChartGestureListener(); + fun endAction(me: MotionEvent?) { + val l = mChart.onChartGestureListener - if (l != null) - l.onChartGestureEnd(me, mLastGesture); + l?.onChartGestureEnd(me, this.lastGesture) } /** @@ -87,26 +80,8 @@ public abstract class ChartTouchListener> extends GestureDete * * @param high */ - public void setLastHighlighted(Highlight high) { - mLastHighlighted = high; - } - - /** - * returns the touch mode the listener is currently in - * - * @return - */ - public int getTouchMode() { - return mTouchMode; - } - - /** - * Returns the last gesture that has been performed on the chart. - * - * @return - */ - public ChartGesture getLastGesture() { - return mLastGesture; + fun setLastHighlighted(high: Highlight?) { + mLastHighlighted = high } @@ -115,29 +90,39 @@ public abstract class ChartTouchListener> extends GestureDete * * @param e */ - protected void performHighlight(Highlight h, MotionEvent e) { - + protected fun performHighlight(h: Highlight?, e: MotionEvent?) { if (h == null || h.equalTo(mLastHighlighted)) { - mChart.highlightValue(null, true); - mLastHighlighted = null; + mChart.highlightValue(null, true) + mLastHighlighted = null } else { - mChart.highlightValue(h, true); - mLastHighlighted = h; + mChart.highlightValue(h, true) + mLastHighlighted = h } } - /** - * returns the distance between two points - * - * @param eventX - * @param startX - * @param eventY - * @param startY - * @return - */ - protected static float distance(float eventX, float startX, float eventY, float startY) { - float dx = eventX - startX; - float dy = eventY - startY; - return (float) Math.sqrt(dx * dx + dy * dy); + companion object { + // states + protected const val NONE: Int = 0 + protected const val DRAG: Int = 1 + protected const val X_ZOOM: Int = 2 + protected const val Y_ZOOM: Int = 3 + protected const val PINCH_ZOOM: Int = 4 + protected const val POST_ZOOM: Int = 5 + protected const val ROTATE: Int = 6 + + /** + * returns the distance between two points + * + * @param eventX + * @param startX + * @param eventY + * @param startY + * @return + */ + protected fun distance(eventX: Float, startX: Float, eventY: Float, startY: Float): Float { + val dx = eventX - startX + val dy = eventY - startY + return sqrt((dx * dx + dy * dy).toDouble()).toFloat() + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt index da0c5ed180..85e9e548f1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt @@ -1,21 +1,21 @@ -package com.github.mikephil.charting.listener; +package com.github.mikephil.charting.listener -import android.view.MotionEvent; +import android.view.MotionEvent +import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture /** * Listener for callbacks when doing gestures on the chart. * * @author Philipp Jahoda */ -public interface OnChartGestureListener { - +interface OnChartGestureListener { /** * Callbacks when a touch-gesture has started on the chart (ACTION_DOWN) * * @param me * @param lastPerformedGesture */ - void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture); + fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartGesture?) /** * Callbacks when a touch-gesture has ended on the chart (ACTION_UP, ACTION_CANCEL) @@ -23,28 +23,28 @@ public interface OnChartGestureListener { * @param me * @param lastPerformedGesture */ - void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture); + fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartGesture?) /** * Callbacks when the chart is longpressed. * * @param me */ - void onChartLongPressed(MotionEvent me); + fun onChartLongPressed(me: MotionEvent?) /** * Callbacks when the chart is double-tapped. * * @param me */ - void onChartDoubleTapped(MotionEvent me); + fun onChartDoubleTapped(me: MotionEvent?) /** * Callbacks when the chart is single-tapped. * * @param me */ - void onChartSingleTapped(MotionEvent me); + fun onChartSingleTapped(me: MotionEvent?) /** * Callbacks then a fling gesture is made on the chart. @@ -54,7 +54,7 @@ public interface OnChartGestureListener { * @param velocityX * @param velocityY */ - void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY); + fun onChartFling(me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float) /** * Callbacks when the chart is scaled / zoomed via pinch zoom / double-tap gesture. @@ -63,7 +63,7 @@ public interface OnChartGestureListener { * @param scaleX scalefactor on the x-axis * @param scaleY scalefactor on the y-axis */ - void onChartScale(MotionEvent me, float scaleX, float scaleY); + fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) /** * Callbacks when the chart is moved / translated via drag gesture. @@ -72,5 +72,5 @@ public interface OnChartGestureListener { * @param dX translation distance on the x-axis * @param dY translation distance on the y-axis */ - void onChartTranslate(MotionEvent me, float dX, float dY); + fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt index 7f50232b7e..d3aaae7cf7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt @@ -1,7 +1,7 @@ -package com.github.mikephil.charting.listener; +package com.github.mikephil.charting.listener -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight /** * Listener for callbacks when selecting values inside the chart by @@ -9,19 +9,18 @@ import com.github.mikephil.charting.highlight.Highlight; * * @author Philipp Jahoda */ -public interface OnChartValueSelectedListener { - +interface OnChartValueSelectedListener { /** * Called when a value has been selected inside the chart. * * @param e The selected Entry * @param h The corresponding highlight object that contains information - * about the highlighted position such as dataSetIndex, ... + * about the highlighted position such as dataSetIndex, ... */ - void onValueSelected(Entry e, Highlight h); + fun onValueSelected(e: Entry?, h: Highlight?) /** * Called when nothing has been selected or an "un-select" has been made. */ - void onNothingSelected(); + fun onNothingSelected() } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt index ca3a67f677..1b1b99e1c6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt @@ -1,15 +1,12 @@ -package com.github.mikephil.charting.listener; +package com.github.mikephil.charting.listener -import android.view.GestureDetector.SimpleOnGestureListener; -import android.view.MotionEvent; -import android.view.View; -import android.view.View.OnTouchListener; - -public class OnDrawLineChartTouchListener extends SimpleOnGestureListener implements OnTouchListener { - - @Override - public boolean onTouch(View v, MotionEvent event) { - return false; - } +import android.view.GestureDetector.SimpleOnGestureListener +import android.view.MotionEvent +import android.view.View +import android.view.View.OnTouchListener +class OnDrawLineChartTouchListener : SimpleOnGestureListener(), OnTouchListener { + override fun onTouch(v: View?, event: MotionEvent?): Boolean { + return false + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt index 5890350bcd..3eba66f1c0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt @@ -1,38 +1,35 @@ -package com.github.mikephil.charting.listener; +package com.github.mikephil.charting.listener -import com.github.mikephil.charting.data.DataSet; -import com.github.mikephil.charting.data.Entry; +import com.github.mikephil.charting.data.DataSet +import com.github.mikephil.charting.data.Entry /** * Listener for callbacks when drawing on the chart. - * + * * @author Philipp - * */ -public interface OnDrawListener { +interface OnDrawListener { + /** + * Called whenever an entry is added with the finger. Note this is also called for entries that are generated by the + * library, when the touch gesture is too fast and skips points. + * + * @param entry + * the last drawn entry + */ + fun onEntryAdded(entry: Entry?) - /** - * Called whenever an entry is added with the finger. Note this is also called for entries that are generated by the - * library, when the touch gesture is too fast and skips points. - * - * @param entry - * the last drawn entry - */ - void onEntryAdded(Entry entry); - - /** - * Called whenever an entry is moved by the user after beeing highlighted - * - * @param entry - */ - void onEntryMoved(Entry entry); - - /** - * Called when drawing finger is lifted and the draw is finished. - * - * @param dataSet - * the last drawn DataSet - */ - void onDrawFinished(DataSet dataSet); + /** + * Called whenever an entry is moved by the user after beeing highlighted + * + * @param entry + */ + fun onEntryMoved(entry: Entry?) + /** + * Called when drawing finger is lifted and the draw is finished. + * + * @param dataSet + * the last drawn DataSet + */ + fun onDrawFinished(dataSet: DataSet<*>?) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt index d3527f924a..b8d36584b4 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt @@ -1,228 +1,198 @@ +package com.github.mikephil.charting.listener -package com.github.mikephil.charting.listener; - -import android.annotation.SuppressLint; -import android.graphics.PointF; -import android.view.MotionEvent; -import android.view.View; -import android.view.animation.AnimationUtils; - -import com.github.mikephil.charting.charts.PieRadarChartBase; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; - -import java.util.ArrayList; +import android.annotation.SuppressLint +import android.view.MotionEvent +import android.view.View +import android.view.animation.AnimationUtils +import com.github.mikephil.charting.charts.PieRadarChartBase +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Utils +import kotlin.math.abs /** * Touchlistener for the PieChart. * * @author Philipp Jahoda */ -public class PieRadarChartTouchListener extends ChartTouchListener> { - - private MPPointF mTouchStartPoint = MPPointF.getInstance(0,0); +class PieRadarChartTouchListener(chart: PieRadarChartBase<*, *, *>) : ChartTouchListener>(chart) { + private val mTouchStartPoint: MPPointF = MPPointF.Companion.getInstance(0f, 0f) /** * the angle where the dragging started */ - private float mStartAngle = 0f; + private var mStartAngle = 0f - private ArrayList _velocitySamples = new ArrayList(); + private val _velocitySamples: ArrayList = ArrayList() - private long mDecelerationLastTime = 0; - private float mDecelerationAngularVelocity = 0.f; - - public PieRadarChartTouchListener(PieRadarChartBase chart) { - super(chart); - } + private var mDecelerationLastTime: Long = 0 + private var mDecelerationAngularVelocity = 0f @SuppressLint("ClickableViewAccessibility") - @Override - public boolean onTouch(View v, MotionEvent event) { - - if (mGestureDetector.onTouchEvent(event)) - return true; + override fun onTouch(v: View?, event: MotionEvent): Boolean { + if (mGestureDetector?.onTouchEvent(event) == true) return true // if rotation by touch is enabled // TODO: Also check if the pie itself is being touched, rather than the entire chart area - if (mChart.isRotationEnabled()) { - - float x = event.getX(); - float y = event.getY(); - - switch (event.getAction()) { - - case MotionEvent.ACTION_DOWN: - - startAction(event); - - stopDeceleration(); - - resetVelocity(); - - if (mChart.isDragDecelerationEnabled()) - sampleVelocity(x, y); - - setGestureStartAngle(x, y); - mTouchStartPoint.x = x; - mTouchStartPoint.y = y; - - break; - case MotionEvent.ACTION_MOVE: - - if (mChart.isDragDecelerationEnabled()) - sampleVelocity(x, y); - - if (mTouchMode == NONE - && distance(x, mTouchStartPoint.x, y, mTouchStartPoint.y) - > Utils.convertDpToPixel(8f)) { - mLastGesture = ChartGesture.ROTATE; - mTouchMode = ROTATE; - mChart.disableScroll(); - } else if (mTouchMode == ROTATE) { - updateGestureRotation(x, y); - mChart.invalidate(); + if (mChart.isRotationEnabled) { + val x = event.x + val y = event.y + + when (event.action) { + MotionEvent.ACTION_DOWN -> { + startAction(event) + + stopDeceleration() + + resetVelocity() + + if (mChart.isDragDecelerationEnabled) sampleVelocity(x, y) + + setGestureStartAngle(x, y) + mTouchStartPoint.x = x + mTouchStartPoint.y = y + } + + MotionEvent.ACTION_MOVE -> { + if (mChart.isDragDecelerationEnabled) sampleVelocity(x, y) + + if (touchMode == NONE + && (distance(x, mTouchStartPoint.x, y, mTouchStartPoint.y) + > Utils.convertDpToPixel(8f)) + ) { + lastGesture = ChartGesture.ROTATE + touchMode = ROTATE + mChart.disableScroll() + } else if (touchMode == ROTATE) { + updateGestureRotation(x, y) + mChart.invalidate() } - endAction(event); - - break; - case MotionEvent.ACTION_UP: - - if (mChart.isDragDecelerationEnabled()) { + endAction(event) + } - stopDeceleration(); + MotionEvent.ACTION_UP -> { + if (mChart.isDragDecelerationEnabled) { + stopDeceleration() - sampleVelocity(x, y); + sampleVelocity(x, y) - mDecelerationAngularVelocity = calculateVelocity(); + mDecelerationAngularVelocity = calculateVelocity() - if (mDecelerationAngularVelocity != 0.f) { - mDecelerationLastTime = AnimationUtils.currentAnimationTimeMillis(); + if (mDecelerationAngularVelocity != 0f) { + mDecelerationLastTime = AnimationUtils.currentAnimationTimeMillis() - Utils.postInvalidateOnAnimation(mChart); // This causes computeScroll to fire, recommended for this by Google + Utils.postInvalidateOnAnimation(mChart) // This causes computeScroll to fire, recommended for this by Google } } - mChart.enableScroll(); - mTouchMode = NONE; + mChart.enableScroll() + touchMode = NONE - endAction(event); - - break; + endAction(event) + } } } - return true; + return true } - @Override - public void onLongPress(MotionEvent me) { - - mLastGesture = ChartGesture.LONG_PRESS; + override fun onLongPress(me: MotionEvent) { + lastGesture = ChartGesture.LONG_PRESS - OnChartGestureListener l = mChart.getOnChartGestureListener(); + val l = mChart.onChartGestureListener - if (l != null) { - l.onChartLongPressed(me); - } + l?.onChartLongPressed(me) } - @Override - public boolean onSingleTapConfirmed(MotionEvent e) { - return true; + override fun onSingleTapConfirmed(e: MotionEvent): Boolean { + return true } - @Override - public boolean onSingleTapUp(MotionEvent e) { + override fun onSingleTapUp(e: MotionEvent): Boolean { + lastGesture = ChartGesture.SINGLE_TAP - mLastGesture = ChartGesture.SINGLE_TAP; + val l = mChart.onChartGestureListener - OnChartGestureListener l = mChart.getOnChartGestureListener(); + l?.onChartSingleTapped(e) - if (l != null) { - l.onChartSingleTapped(e); + if (!mChart.isHighlightPerTapEnabled) { + return false } - if(!mChart.isHighlightPerTapEnabled()) { - return false; - } + val high = mChart.getHighlightByTouchPoint(e.x, e.y) + performHighlight(high, e) - Highlight high = mChart.getHighlightByTouchPoint(e.getX(), e.getY()); - performHighlight(high, e); - - return true; + return true } - private void resetVelocity() { - _velocitySamples.clear(); + private fun resetVelocity() { + _velocitySamples.clear() } - private void sampleVelocity(float touchLocationX, float touchLocationY) { - - long currentTime = AnimationUtils.currentAnimationTimeMillis(); + private fun sampleVelocity(touchLocationX: Float, touchLocationY: Float) { + val currentTime = AnimationUtils.currentAnimationTimeMillis() - _velocitySamples.add(new AngularVelocitySample(currentTime, mChart.getAngleForPoint(touchLocationX, touchLocationY))); + _velocitySamples.add(AngularVelocitySample(currentTime, mChart.getAngleForPoint(touchLocationX, touchLocationY))) // Remove samples older than our sample time - 1 seconds - for (int i = 0, count = _velocitySamples.size(); i < count - 2; i++) { + var i = 0 + var count = _velocitySamples.size + while (i < count - 2) { if (currentTime - _velocitySamples.get(i).time > 1000) { - _velocitySamples.remove(0); - i--; - count--; + _velocitySamples.removeAt(0) + i-- + count-- } else { - break; + break } + i++ } } - private float calculateVelocity() { + private fun calculateVelocity(): Float { + if (_velocitySamples.isEmpty()) return 0f - if (_velocitySamples.isEmpty()) - return 0.f; - - AngularVelocitySample firstSample = _velocitySamples.get(0); - AngularVelocitySample lastSample = _velocitySamples.get(_velocitySamples.size() - 1); + val firstSample = _velocitySamples[0] + val lastSample = _velocitySamples[_velocitySamples.size - 1] // Look for a sample that's closest to the latest sample, but not the same, so we can deduce the direction - AngularVelocitySample beforeLastSample = firstSample; - for (int i = _velocitySamples.size() - 1; i >= 0; i--) { - beforeLastSample = _velocitySamples.get(i); + var beforeLastSample = firstSample + for (i in _velocitySamples.indices.reversed()) { + beforeLastSample = _velocitySamples[i] if (beforeLastSample.angle != lastSample.angle) { - break; + break } } // Calculate the sampling time - float timeDelta = (lastSample.time - firstSample.time) / 1000.f; - if (timeDelta == 0.f) { - timeDelta = 0.1f; + var timeDelta = (lastSample.time - firstSample.time) / 1000f + if (timeDelta == 0f) { + timeDelta = 0.1f } // Calculate clockwise/ccw by choosing two values that should be closest to each other, // so if the angles are two far from each other we know they are inverted "for sure" - boolean clockwise = lastSample.angle >= beforeLastSample.angle; - if (Math.abs(lastSample.angle - beforeLastSample.angle) > 270.0) { - clockwise = !clockwise; + var clockwise = lastSample.angle >= beforeLastSample.angle + if (abs(lastSample.angle - beforeLastSample.angle) > 270.0) { + clockwise = !clockwise } // Now if the "gesture" is over a too big of an angle - then we know the angles are inverted, and we need to move them closer to each other from both sides of the 360.0 wrapping point if (lastSample.angle - firstSample.angle > 180.0) { - firstSample.angle += 360.0; + firstSample.angle += 360.0.toFloat() } else if (firstSample.angle - lastSample.angle > 180.0) { - lastSample.angle += 360.0; + lastSample.angle += 360.0.toFloat() } // The velocity - float velocity = Math.abs((lastSample.angle - firstSample.angle) / timeDelta); + var velocity = abs((lastSample.angle - firstSample.angle) / timeDelta) // Direction? if (!clockwise) { - velocity = -velocity; + velocity = -velocity } - return velocity; + return velocity } /** @@ -232,8 +202,8 @@ public class PieRadarChartTouchListener extends ChartTouchListener= 0.001) - Utils.postInvalidateOnAnimation(mChart); // This causes computeScroll to fire, recommended for this by Google - else - stopDeceleration(); + if (abs(mDecelerationAngularVelocity) >= 0.001) Utils.postInvalidateOnAnimation(mChart) // This causes computeScroll to fire, recommended for this by Google + else stopDeceleration() } - private class AngularVelocitySample { - - public long time; - public float angle; - - public AngularVelocitySample(long time, float angle) { - this.time = time; - this.angle = angle; - } - } + private inner class AngularVelocitySample(var time: Long, var angle: Float) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.kt index c4457248d9..88f069ab32 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.kt @@ -1,134 +1,137 @@ +package com.github.mikephil.charting.matrix -package com.github.mikephil.charting.matrix; +import kotlin.math.sqrt /** * Simple 3D vector class. Handles basic vector math for 3D vectors. */ -public final class Vector3 { - public float x; - public float y; - public float z; - - public static final Vector3 ZERO = new Vector3(0, 0, 0); - public static final Vector3 UNIT_X = new Vector3(1, 0, 0); - public static final Vector3 UNIT_Y = new Vector3(0, 1, 0); - public static final Vector3 UNIT_Z = new Vector3(0, 0, 1); - - public Vector3() { - } - - public Vector3(float[] array) { - set(array[0], array[1], array[2]); - } - - public Vector3(float xValue, float yValue, float zValue) { - set(xValue, yValue, zValue); - } - - public Vector3(Vector3 other) { - set(other); - } - - public final void add(Vector3 other) { - x += other.x; - y += other.y; - z += other.z; - } - - public final void add(float otherX, float otherY, float otherZ) { - x += otherX; - y += otherY; - z += otherZ; - } - - public final void subtract(Vector3 other) { - x -= other.x; - y -= other.y; - z -= other.z; - } - - public final void subtractMultiple(Vector3 other, float multiplicator) { - x -= other.x * multiplicator; - y -= other.y * multiplicator; - z -= other.z * multiplicator; - } - - public final void multiply(float magnitude) { - x *= magnitude; - y *= magnitude; - z *= magnitude; - } - - public final void multiply(Vector3 other) { - x *= other.x; - y *= other.y; - z *= other.z; - } - - public final void divide(float magnitude) { - if (magnitude != 0.0f) { - x /= magnitude; - y /= magnitude; - z /= magnitude; - } - } - - public final void set(Vector3 other) { - x = other.x; - y = other.y; - z = other.z; - } - - public final void set(float xValue, float yValue, float zValue) { - x = xValue; - y = yValue; - z = zValue; - } - - public final float dot(Vector3 other) { - return (x * other.x) + (y * other.y) + (z * other.z); - } - - public final Vector3 cross(Vector3 other) { - return new Vector3(y * other.z - z * other.y, - z * other.x - x * other.z, - x * other.y - y * other.x); - } - - public final float length() { - return (float) Math.sqrt(length2()); - } - - public final float length2() { - return (x * x) + (y * y) + (z * z); - } - - public final float distance2(Vector3 other) { - float dx = x - other.x; - float dy = y - other.y; - float dz = z - other.z; - return (dx * dx) + (dy * dy) + (dz * dz); - } - - public final float normalize() { - final float magnitude = length(); - - // TODO: I'm choosing safety over speed here. - if (magnitude != 0.0f) { - x /= magnitude; - y /= magnitude; - z /= magnitude; - } - - return magnitude; - } - - public final void zero() { - set(0.0f, 0.0f, 0.0f); - } - - public final boolean pointsInSameDirection(Vector3 other) { - return this.dot(other) > 0; - } - +class Vector3 { + var x: Float = 0f + var y: Float = 0f + var z: Float = 0f + + constructor() + + constructor(array: FloatArray) { + set(array[0], array[1], array[2]) + } + + constructor(xValue: Float, yValue: Float, zValue: Float) { + set(xValue, yValue, zValue) + } + + constructor(other: Vector3) { + set(other) + } + + fun add(other: Vector3) { + x += other.x + y += other.y + z += other.z + } + + fun add(otherX: Float, otherY: Float, otherZ: Float) { + x += otherX + y += otherY + z += otherZ + } + + fun subtract(other: Vector3) { + x -= other.x + y -= other.y + z -= other.z + } + + fun subtractMultiple(other: Vector3, multiplicator: Float) { + x -= other.x * multiplicator + y -= other.y * multiplicator + z -= other.z * multiplicator + } + + fun multiply(magnitude: Float) { + x *= magnitude + y *= magnitude + z *= magnitude + } + + fun multiply(other: Vector3) { + x *= other.x + y *= other.y + z *= other.z + } + + fun divide(magnitude: Float) { + if (magnitude != 0.0f) { + x /= magnitude + y /= magnitude + z /= magnitude + } + } + + fun set(other: Vector3) { + x = other.x + y = other.y + z = other.z + } + + fun set(xValue: Float, yValue: Float, zValue: Float) { + x = xValue + y = yValue + z = zValue + } + + fun dot(other: Vector3): Float { + return (x * other.x) + (y * other.y) + (z * other.z) + } + + fun cross(other: Vector3): Vector3 { + return Vector3( + y * other.z - z * other.y, + z * other.x - x * other.z, + x * other.y - y * other.x + ) + } + + fun length(): Float { + return sqrt(length2().toDouble()).toFloat() + } + + fun length2(): Float { + return (x * x) + (y * y) + (z * z) + } + + fun distance2(other: Vector3): Float { + val dx = x - other.x + val dy = y - other.y + val dz = z - other.z + return (dx * dx) + (dy * dy) + (dz * dz) + } + + fun normalize(): Float { + val magnitude = length() + + // TODO: I'm choosing safety over speed here. + if (magnitude != 0.0f) { + x /= magnitude + y /= magnitude + z /= magnitude + } + + return magnitude + } + + fun zero() { + set(0.0f, 0.0f, 0.0f) + } + + fun pointsInSameDirection(other: Vector3): Boolean { + return this.dot(other) > 0 + } + + companion object { + val ZERO: Vector3 = Vector3(0f, 0f, 0f) + val UNIT_X: Vector3 = Vector3(1f, 0f, 0f) + val UNIT_Y: Vector3 = Vector3(0f, 1f, 0f) + val UNIT_Z: Vector3 = Vector3(0f, 0f, 1f) + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/model/GradientColor.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/model/GradientColor.kt index 55f6bf6b6e..fc5c166fc8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/model/GradientColor.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/model/GradientColor.kt @@ -1,60 +1,57 @@ -package com.github.mikephil.charting.model; +package com.github.mikephil.charting.model -import com.github.mikephil.charting.utils.Fill; +import com.github.mikephil.charting.utils.Fill /** * Deprecated. Use `Fill` */ -@Deprecated -public class GradientColor extends Fill { - /** - * Deprecated. Use `Fill.getGradientColors()` - */ - @Deprecated - public int getStartColor() { - return getGradientColors()[0]; - } - - /** - * Deprecated. Use `Fill.setGradientColors(...)` - */ - @Deprecated - public void setStartColor(int startColor) { - if (getGradientColors() == null || getGradientColors().length != 2) { - setGradientColors(new int[]{ - startColor, - getGradientColors() != null && getGradientColors().length > 1 - ? getGradientColors()[1] - : 0 - }); - } else { - getGradientColors()[0] = startColor; - } - } - - /** - * Deprecated. Use `Fill.getGradientColors()` - */ - @Deprecated - public int getEndColor() { - return getGradientColors()[1]; - } - - /** - * Deprecated. Use `Fill.setGradientColors(...)` - */ - @Deprecated - public void setEndColor(int endColor) { - if (getGradientColors() == null || getGradientColors().length != 2) { - setGradientColors(new int[]{ - getGradientColors() != null && getGradientColors().length > 0 - ? getGradientColors()[0] - : 0, - endColor - }); - } else { - getGradientColors()[1] = endColor; - } - } +@Deprecated("") +class GradientColor : Fill() { + @get:Deprecated("") + @set:Deprecated("") + var startColor: Int + /** + * Deprecated. Use `Fill.getGradientColors()` + */ + get() = gradientColors!![0] + /** + * Deprecated. Use `Fill.setGradientColors(...)` + */ + set(startColor) { + if (gradientColors == null || gradientColors!!.size != 2) { + this.gradientColors = intArrayOf( + startColor, + if (gradientColors != null && gradientColors!!.size > 1) + gradientColors!![1] + else + 0 + ) + } else { + gradientColors!![0] = startColor + } + } + @get:Deprecated("") + @set:Deprecated("") + var endColor: Int + /** + * Deprecated. Use `Fill.getGradientColors()` + */ + get() = gradientColors!![1] + /** + * Deprecated. Use `Fill.setGradientColors(...)` + */ + set(endColor) { + if (gradientColors == null || gradientColors!!.size != 2) { + this.gradientColors = intArrayOf( + if (gradientColors != null && gradientColors!!.isNotEmpty()) + gradientColors!![0] + else + 0, + endColor + ) + } else { + gradientColors!![1] = endColor + } + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt index 5279dd1b90..dd20a79834 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt @@ -19,8 +19,8 @@ import kotlin.math.min open class BarChartRenderer( @JvmField var chart: BarDataProvider, - animator: ChartAnimator?, - viewPortHandler: ViewPortHandler? + animator: ChartAnimator, + viewPortHandler: ViewPortHandler, ) : BarLineScatterCandleBubbleRenderer(animator, viewPortHandler) { /** * the rect object that is used for drawing the bars @@ -48,8 +48,8 @@ open class BarChartRenderer( private var roundedBarRadius = 0f constructor( - chart: BarDataProvider, animator: ChartAnimator?, - viewPortHandler: ViewPortHandler?, mDrawRoundedBars: Boolean, mRoundedBarRadius: Float + chart: BarDataProvider, animator: ChartAnimator, + viewPortHandler: ViewPortHandler, mDrawRoundedBars: Boolean, mRoundedBarRadius: Float ) : this(chart, animator, viewPortHandler) { this.drawRoundedBars = mDrawRoundedBars this.roundedBarRadius = mRoundedBarRadius @@ -59,7 +59,7 @@ open class BarChartRenderer( val barData = chart.barData barBuffers = mutableListOf() - barData.dataSets.forEach { + barData?.dataSets?.forEach { barBuffers.add( BarBuffer( it.entryCount * 4 * (if (it.isStacked) it.stackSize else 1), @@ -70,14 +70,14 @@ open class BarChartRenderer( } override fun drawData(c: Canvas) { - if (barBuffers.size == 0) { + if (barBuffers.isEmpty()) { initBuffers() } - val barData = chart.barData + val barData = chart.barData ?: return for (i in 0..) { - val barData = chart.barData + val barData = chart.barData ?: return for (high in indices) { val set = barData.getDataSetByIndex(high.dataSetIndex) @@ -506,7 +505,7 @@ open class BarChartRenderer( val e = set.getEntryForXValue(high.x, high.y) - if (!isInBoundsX(e, set)) { + if (!isInBoundsX(e, set) || e == null) { continue } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt index 6610748375..3820f8f128 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt @@ -1,23 +1,20 @@ -package com.github.mikephil.charting.renderer; - -import com.github.mikephil.charting.animation.ChartAnimator; -import com.github.mikephil.charting.data.DataSet; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider; -import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.utils.ViewPortHandler; - -public abstract class BarLineScatterCandleBubbleRenderer extends DataRenderer { - +package com.github.mikephil.charting.renderer + +import com.github.mikephil.charting.animation.ChartAnimator +import com.github.mikephil.charting.data.DataSet.Rounding +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider +import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet +import com.github.mikephil.charting.interfaces.datasets.IDataSet +import com.github.mikephil.charting.utils.ViewPortHandler +import kotlin.math.max +import kotlin.math.min + +abstract class BarLineScatterCandleBubbleRenderer(animator: ChartAnimator, viewPortHandler: ViewPortHandler) : DataRenderer(animator, viewPortHandler) { /** * buffer for storing the current minimum and maximum visible x */ - protected XBounds xBounds = new XBounds(); - - public BarLineScatterCandleBubbleRenderer(ChartAnimator animator, ViewPortHandler viewPortHandler) { - super(animator, viewPortHandler); - } + protected var xBounds: XBounds = XBounds() /** * Returns true if the DataSet values should be drawn, false if not. @@ -25,8 +22,8 @@ public abstract class BarLineScatterCandleBubbleRenderer extends DataRenderer { * @param set * @return */ - protected boolean shouldDrawValues(IDataSet set) { - return set.isVisible() && (set.isDrawValuesEnabled() || set.isDrawIconsEnabled()); + protected fun shouldDrawValues(set: IDataSet<*>): Boolean { + return set.isVisible && (set.isDrawValuesEnabled || set.isDrawIconsEnabled) } /** @@ -36,39 +33,36 @@ public abstract class BarLineScatterCandleBubbleRenderer extends DataRenderer { * @param set * @return */ - protected boolean isInBoundsX(Entry e, IBarLineScatterCandleBubbleDataSet set) { - - if (e == null) - return false; + protected fun isInBoundsX(e: T?, set: IBarLineScatterCandleBubbleDataSet): Boolean { + if (e == null) return false - float entryIndex = set.getEntryIndex(e); + val entryIndex = set.getEntryIndex(e).toFloat() - if (e == null || entryIndex >= set.getEntryCount() * animator.getPhaseX()) { - return false; + if (entryIndex >= set.entryCount * animator.phaseX) { + return false } else { - return true; + return true } } /** * Class representing the bounds of the current viewport in terms of indices in the values array of a DataSet. */ - protected class XBounds { - + protected inner class XBounds { /** * minimum visible entry index */ - public int min; + var min: Int = 0 /** * maximum visible entry index */ - public int max; + var max: Int = 0 /** * range of visible entry indices */ - public int range; + var range: Int = 0 /** * Calculates the minimum and maximum x values as well as the range between them. @@ -76,18 +70,18 @@ public abstract class BarLineScatterCandleBubbleRenderer extends DataRenderer { * @param chart * @param dataSet */ - public void set(BarLineScatterCandleBubbleDataProvider chart, IBarLineScatterCandleBubbleDataSet dataSet) { - float phaseX = Math.max(0.f, Math.min(1.f, animator.getPhaseX())); + operator fun set(chart: BarLineScatterCandleBubbleDataProvider, dataSet: IBarLineScatterCandleBubbleDataSet) { + val phaseX = max(0f, min(1f, animator.phaseX)) - float low = chart.getLowestVisibleX(); - float high = chart.getHighestVisibleX(); + val low = chart.lowestVisibleX + val high = chart.highestVisibleX - Entry entryFrom = dataSet.getEntryForXValue(low, Float.NaN, DataSet.Rounding.DOWN); - Entry entryTo = dataSet.getEntryForXValue(high, Float.NaN, DataSet.Rounding.UP); + val entryFrom = dataSet.getEntryForXValue(low, Float.Companion.NaN, Rounding.DOWN) + val entryTo = dataSet.getEntryForXValue(high, Float.Companion.NaN, Rounding.UP) - min = entryFrom == null ? 0 : dataSet.getEntryIndex(entryFrom); - max = entryTo == null ? 0 : dataSet.getEntryIndex(entryTo); - range = (int) ((max - min) * phaseX); + min = if (entryFrom == null) 0 else dataSet.getEntryIndex(entryFrom) + max = if (entryTo == null) 0 else dataSet.getEntryIndex(entryTo) + range = ((max - min) * phaseX).toInt() } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt index 7f2dd4b7af..20ea5d6ed2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt @@ -18,14 +18,14 @@ import kotlin.math.sqrt @Suppress("MemberVisibilityCanBePrivate") open class BubbleChartRenderer( @JvmField - var chart: BubbleDataProvider, animator: ChartAnimator?, - viewPortHandler: ViewPortHandler? + var chart: BubbleDataProvider, animator: ChartAnimator, + viewPortHandler: ViewPortHandler ) : BarLineScatterCandleBubbleRenderer(animator, viewPortHandler) { override fun initBuffers() { } override fun drawData(c: Canvas) { - val bubbleData = chart.bubbleData + val bubbleData = chart.bubbleData ?: return for (set in bubbleData.dataSets) { if (set.isVisible) drawDataSet(c, set) @@ -63,7 +63,7 @@ open class BubbleChartRenderer( val referenceSize = min(maxBubbleHeight.toDouble(), maxBubbleWidth.toDouble()).toFloat() for (j in xBounds.min..xBounds.range + xBounds.min) { - val entry = dataSet.getEntryForIndex(j) + val entry = dataSet.getEntryForIndex(j) ?: continue pointBuffer[0] = entry.x pointBuffer[1] = (entry.y) * phaseY @@ -142,7 +142,7 @@ open class BubbleChartRenderer( continue } - val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) + val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) ?: continue if (dataSet.isDrawValuesEnabled) { drawValue( @@ -185,7 +185,7 @@ open class BubbleChartRenderer( } override fun drawHighlighted(c: Canvas, indices: Array) { - val bubbleData = chart.bubbleData + val bubbleData = chart.bubbleData ?: return val phaseY = animator.phaseY @@ -194,7 +194,7 @@ open class BubbleChartRenderer( if (set == null || !set.isHighlightEnabled) continue - val entry = set.getEntryForXValue(high.x, high.y) + val entry = set.getEntryForXValue(high.x, high.y) ?: continue if (entry.y != high.y) continue diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt index 15618802ed..167447fa46 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt @@ -13,8 +13,8 @@ import com.github.mikephil.charting.utils.ViewPortHandler open class CandleStickChartRenderer( @JvmField - var chart: CandleDataProvider, animator: ChartAnimator?, - viewPortHandler: ViewPortHandler? + var chart: CandleDataProvider, animator: ChartAnimator, + viewPortHandler: ViewPortHandler ) : LineScatterCandleRadarRenderer(animator, viewPortHandler) { private val shadowBuffers = FloatArray(8) private val bodyBuffers = FloatArray(4) @@ -25,7 +25,7 @@ open class CandleStickChartRenderer( override fun initBuffers() = Unit override fun drawData(c: Canvas) { - val candleData = chart.candleData + val candleData = chart.candleData ?: return for (set in candleData.dataSets) { if (set.isVisible) drawDataSet(c, set) @@ -206,7 +206,7 @@ open class CandleStickChartRenderer( override fun drawValues(c: Canvas) { // if values are drawn if (isDrawingValuesAllowed(chart)) { - val dataSets = chart.candleData.dataSets + val dataSets = chart.candleData?.dataSets ?: return for (i in dataSets.indices) { val dataSet = dataSets[i] @@ -246,7 +246,7 @@ open class CandleStickChartRenderer( continue } - val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) + val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) ?: continue if (dataSet.isDrawValuesEnabled) { drawValue( @@ -285,14 +285,14 @@ open class CandleStickChartRenderer( override fun drawExtras(c: Canvas) = Unit override fun drawHighlighted(c: Canvas, indices: Array) { - val candleData = chart.candleData + val candleData = chart.candleData ?: return for (high in indices) { val set = candleData.getDataSetByIndex(high.dataSetIndex) if (set == null || !set.isHighlightEnabled) continue - val e = set.getEntryForXValue(high.x, high.y) + val e = set.getEntryForXValue(high.x, high.y) ?: continue if (!isInBoundsX(e, set)) continue diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.kt index 18b80e47fa..424a6c6a42 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.kt @@ -21,7 +21,7 @@ open class CombinedChartRenderer(chart: CombinedChart, animator: ChartAnimator, */ protected var dataRenderers: MutableList = ArrayList(5) - protected var weakChart: WeakReference> = WeakReference(chart) + protected var weakChart: WeakReference> = WeakReference(chart) /** * Creates the renderers needed for this combined-renderer in the required order. Also takes the DrawOrder into @@ -71,7 +71,7 @@ open class CombinedChartRenderer(chart: CombinedChart, animator: ChartAnimator, val chart = weakChart.get() ?: return for (renderer in dataRenderers) { - var data: ChartData<*>? = null + var data: ChartData<*, *>? = null if (renderer is BarChartRenderer) data = renderer.chart.barData else if (renderer is LineChartRenderer) data = renderer.chart.lineData diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt index 0b2ec45a2e..3a1f0b8664 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt @@ -22,15 +22,15 @@ import kotlin.math.min */ @Suppress("MemberVisibilityCanBePrivate") open class HorizontalBarChartRenderer( - chart: BarDataProvider, animator: ChartAnimator?, - viewPortHandler: ViewPortHandler? + chart: BarDataProvider, animator: ChartAnimator, + viewPortHandler: ViewPortHandler ) : BarChartRenderer(chart, animator, viewPortHandler) { override fun initBuffers() { - val barData = chart.barData + val barData = chart.barData ?: return barBuffers = arrayOfNulls(barData.dataSetCount).toMutableList() for (i in barBuffers.indices) { - val set = barData.getDataSetByIndex(i) + val set = barData.getDataSetByIndex(i) ?: continue barBuffers[i] = HorizontalBarBuffer( set.entryCount * 4 * (if (set.isStacked) set.stackSize else 1), barData.dataSetCount, set.isStacked @@ -55,21 +55,20 @@ open class HorizontalBarChartRenderer( val phaseX = animator.phaseX val phaseY = animator.phaseY + val barData = chart.barData ?: return + // draw the bar shadow before the values if (chart.isDrawBarShadowEnabled) { shadowPaint.color = dataSet.barShadowColor - val barData = chart.barData - val barWidth = barData.barWidth val barWidthHalf = barWidth / 2.0f var x: Float var i = 0 val count = min((ceil(((dataSet.entryCount).toFloat() * phaseX).toDouble())).toInt().toDouble(), dataSet.entryCount.toDouble()).toInt() - while (i < count - ) { - val e = dataSet.getEntryForIndex(i) + while (i < count) { + val e = dataSet.getEntryForIndex(i) ?: continue x = e.x @@ -100,13 +99,13 @@ open class HorizontalBarChartRenderer( buffer.setPhases(phaseX, phaseY) buffer.setDataSet(index) buffer.setInverted(chart.isInverted(dataSet.axisDependency)) - buffer.setBarWidth(chart.barData.barWidth) + buffer.setBarWidth(barData.barWidth) buffer.feed(dataSet) trans!!.pointValuesToPixel(buffer.buffer) - val isCustomFill = dataSet.fills != null && dataSet.fills.isNotEmpty() + val isCustomFill = dataSet.fills != null && !dataSet.fills.isNullOrEmpty() val isSingleColor = dataSet.colors.size == 1 val isInverted = chart.isInverted(dataSet.axisDependency) @@ -134,8 +133,7 @@ open class HorizontalBarChartRenderer( } if (isCustomFill) { - dataSet.getFill(pos) - .fillRect( + dataSet.getFill(pos)?.fillRect( c, paintRender, buffer.buffer[j], buffer.buffer[j + 1], @@ -165,14 +163,15 @@ open class HorizontalBarChartRenderer( override fun drawValues(c: Canvas) { // if values are drawn if (isDrawingValuesAllowed(chart)) { - val dataSets = chart.barData.dataSets + val barData = chart.barData ?: return + val dataSets = barData.dataSets val valueOffsetPlus = Utils.convertDpToPixel(5f) var posOffset: Float var negOffset: Float val drawValueAboveBar = chart.isDrawValueAboveBarEnabled - for (i in 0..) { + fun computeLegend(data: ChartData<*, *>) { if (!legend.isLegendCustom) { computedEntries.clear() @@ -59,40 +59,39 @@ open class LegendRenderer( val minEntries = min(clrs.size.toDouble(), bds.stackSize.toDouble()).toInt() - for (j in 0.. { @@ -316,11 +310,15 @@ open class LegendRenderer( if (!isStacked) { if (drawingForm) posX += if (direction == LegendDirection.RIGHT_TO_LEFT) -formToTextSpace else formToTextSpace - if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= calculatedLabelSizes[i].width + calculatedLabelSizes[i].let { + if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= it.width + } - drawLabel(c, posX, posY + labelLineHeight, e.label) + drawLabel(c, posX, posY + labelLineHeight, e.label!!) - if (direction == LegendDirection.LEFT_TO_RIGHT) posX += calculatedLabelSizes[i].width + calculatedLabelSizes[i].let { + if (direction == LegendDirection.LEFT_TO_RIGHT) posX += it.width + } posX += if (direction == LegendDirection.RIGHT_TO_LEFT) -xEntrySpace else xEntrySpace } else posX += if (direction == LegendDirection.RIGHT_TO_LEFT) -stackSpace else stackSpace @@ -332,7 +330,7 @@ open class LegendRenderer( // contains the stacked legend size in pixels var stack = 0f var wasStacked = false - var posY = 0f + var posY: Float when (verticalAlignment) { LegendVerticalAlignment.TOP -> { @@ -383,10 +381,10 @@ open class LegendRenderer( if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= Utils.calcTextWidth(labelPaint, e.label).toFloat() if (!wasStacked) { - drawLabel(c, posX, posY + labelLineHeight, e.label) + drawLabel(c, posX, posY + labelLineHeight, e.label!!) } else { posY += labelLineHeight + labelLineSpacing - drawLabel(c, posX, posY + labelLineHeight, e.label) + drawLabel(c, posX, posY + labelLineHeight, e.label!!) } // make a step down @@ -479,6 +477,8 @@ open class LegendRenderer( mLineFormPath.lineTo(x + formSize, y) c.drawPath(mLineFormPath, formPaint) } + + else -> {} } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt index 195cb7ea4b..47b75c0542 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt @@ -22,7 +22,7 @@ import java.lang.ref.WeakReference import kotlin.math.max import kotlin.math.min -class LineChartRenderer( +open class LineChartRenderer( @JvmField var chart: LineDataProvider, animator: ChartAnimator?, viewPortHandler: ViewPortHandler? ) : LineRadarRenderer(animator, viewPortHandler) { @@ -74,7 +74,7 @@ class LineChartRenderer( drawBitmapLocal.eraseColor(Color.TRANSPARENT) - val lineData = chart.lineData + val lineData = chart.lineData ?: return for (set in lineData.dataSets) { if (set.isVisible) drawDataSet(c, set) @@ -179,8 +179,6 @@ class LineChartRenderer( var next = cur var nextIndex = -1 - if (cur == null) return - // let the spline start cubicPath.moveTo(cur.x, cur.y * phaseY) @@ -488,7 +486,7 @@ class LineChartRenderer( override fun drawValues(c: Canvas) { if (isDrawingValuesAllowed(chart)) { - val dataSets = chart.lineData.dataSets + val dataSets = chart.lineData?.dataSets ?: return for (i in dataSets.indices) { val dataSet = dataSets[i] @@ -590,7 +588,7 @@ class LineChartRenderer( mCirclesBuffer[0] = 0f mCirclesBuffer[1] = 0f - val dataSets = chart.lineData.dataSets + val dataSets = chart.lineData?.dataSets ?: return for (i in dataSets.indices) { val dataSet = dataSets[i] @@ -654,13 +652,13 @@ class LineChartRenderer( val lineData = chart.lineData for (high in indices) { - val set = lineData.getDataSetByIndex(high.dataSetIndex) + val set = lineData?.getDataSetByIndex(high.dataSetIndex) if (set == null || !set.isHighlightEnabled) continue val e = set.getEntryForXValue(high.x, high.y) - if (!isInBoundsX(e, set)) continue + if (!isInBoundsX(e, set) || e == null) continue val pix = chart.getTransformer(set.axisDependency)!!.getPixelForValues( e.x, e.y * animator diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineRadarRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineRadarRenderer.kt index 7a499f073d..040f6c68c5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineRadarRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineRadarRenderer.kt @@ -9,7 +9,7 @@ import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler import androidx.core.graphics.withClip -abstract class LineRadarRenderer(animator: ChartAnimator?, viewPortHandler: ViewPortHandler?) : +abstract class LineRadarRenderer(animator: ChartAnimator, viewPortHandler: ViewPortHandler) : LineScatterCandleRadarRenderer(animator, viewPortHandler) { /** * Draws the provided path in filled mode with the provided drawable. @@ -32,7 +32,7 @@ abstract class LineRadarRenderer(animator: ChartAnimator?, viewPortHandler: View } else { throw RuntimeException( "Fill-drawables not (yet) supported below API level 18, " + - "this code was run on API level " + Utils.getSDKInt() + "." + "this code was run on API level " + Utils.sDKInt + "." ) } } @@ -77,6 +77,6 @@ abstract class LineRadarRenderer(animator: ChartAnimator?, viewPortHandler: View * @return */ private fun clipPathSupported(): Boolean { - return Utils.getSDKInt() >= 18 + return Utils.sDKInt >= 18 } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineScatterCandleRadarRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineScatterCandleRadarRenderer.kt index 5616e3a8b2..5ea5cf8f04 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineScatterCandleRadarRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineScatterCandleRadarRenderer.kt @@ -6,7 +6,7 @@ import com.github.mikephil.charting.animation.ChartAnimator import com.github.mikephil.charting.interfaces.datasets.ILineScatterCandleRadarDataSet import com.github.mikephil.charting.utils.ViewPortHandler -abstract class LineScatterCandleRadarRenderer(animator: ChartAnimator?, viewPortHandler: ViewPortHandler?) : +abstract class LineScatterCandleRadarRenderer(animator: ChartAnimator, viewPortHandler: ViewPortHandler) : BarLineScatterCandleBubbleRenderer(animator, viewPortHandler) { /** * path that is used for drawing highlight-lines (drawLines(...) cannot be used because of dashes) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt index 5043bef5ff..72e9875640 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt @@ -31,6 +31,7 @@ import kotlin.math.sin import kotlin.math.sqrt import kotlin.math.tan import androidx.core.graphics.withSave +import com.github.mikephil.charting.data.PieData open class PieChartRenderer( protected var chart: PieChart, animator: ChartAnimator, @@ -164,7 +165,7 @@ open class PieChartRenderer( if (!dataSet.isAutomaticallyDisableSliceSpacingEnabled) return dataSet.sliceSpace val spaceSizeRatio = dataSet.sliceSpace / viewPortHandler.smallestContentExtension - val minValueRatio = dataSet.yMin / chart.data!!.yValueSum * 2 + val minValueRatio = dataSet.yMin / (chart.data as PieData).yValueSum * 2 val sliceSpace = if (spaceSizeRatio > minValueRatio) 0f else dataSet.sliceSpace @@ -392,7 +393,7 @@ open class PieChartRenderer( val data = chart.data val dataSets = data!!.dataSets - val yValueSum = data.yValueSum + val yValueSum = (data as PieData).yValueSum val drawEntryLabels = chart.isDrawEntryLabelsEnabled @@ -964,7 +965,7 @@ open class PieChartRenderer( protected fun drawRoundedSlices(c: Canvas?) { if (!chart.isDrawRoundedSlicesEnabled) return - val dataSet = chart.data!!.dataSet + val dataSet = (chart.data as PieData).dataSet if (!dataSet.isVisible) return diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt index 99bf27ac59..abb27e5fc2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt @@ -15,8 +15,8 @@ import com.github.mikephil.charting.utils.ViewPortHandler import androidx.core.graphics.withSave open class RadarChartRenderer( - protected var chart: RadarChart, animator: ChartAnimator?, - viewPortHandler: ViewPortHandler? + protected var chart: RadarChart, animator: ChartAnimator, + viewPortHandler: ViewPortHandler ) : LineRadarRenderer(animator, viewPortHandler) { var webPaint: Paint protected set @@ -31,9 +31,9 @@ open class RadarChartRenderer( override fun initBuffers() = Unit override fun drawData(c: Canvas) { - val radarData = chart.data + val radarData = chart.data ?: return - val mostEntries = radarData!!.maxEntryCountSet.entryCount + val mostEntries = radarData.maxEntryCountSet?.entryCount ?: return for (set in radarData.dataSets) { if (set.isVisible) { @@ -219,7 +219,7 @@ open class RadarChartRenderer( webPaint.alpha = chart.webAlpha val xIncrements = 1 + chart.skipWebLineCount - val maxEntryCount = chart.data!!.maxEntryCountSet.entryCount + val maxEntryCount = chart.data!!.maxEntryCountSet?.entryCount ?: return val p = MPPointF.getInstance(0f, 0f) var i = 0 @@ -298,7 +298,7 @@ open class RadarChartRenderer( for (high in indices) { val set = radarData!!.getDataSetByIndex(high.dataSetIndex) - if (set == null || !set.isHighlightEnabled) continue + if (!set.isHighlightEnabled) continue val e = set.getEntryForIndex(high.x.toInt()) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedBarChartRenderer.kt index 62de1edeff..a7bcb7dd16 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedBarChartRenderer.kt @@ -15,7 +15,7 @@ import kotlin.math.min /** @noinspection unused */ -class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator?, viewPortHandler: ViewPortHandler?) : +class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator, viewPortHandler: ViewPortHandler) : BarChartRenderer(chart, animator, viewPortHandler) { private val mBarShadowRectBuffer = RectF() private val mRadius = 20f @@ -47,7 +47,7 @@ class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator?, if (chart.isDrawBarShadowEnabled) { shadowPaint.color = dataSet.barShadowColor - val barData = chart.barData + val barData = chart.barData ?: return val barWidth = barData.barWidth val barWidthHalf = barWidth / 2.0f var x: Float @@ -79,11 +79,11 @@ class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator?, } } - val buffer = barBuffers!![index]!! + val buffer = barBuffers[index]!! buffer.setPhases(phaseX, phaseY) buffer.setDataSet(index) buffer.setInverted(chart.isInverted(dataSet.axisDependency)) - buffer.setBarWidth(chart.barData.barWidth) + buffer.setBarWidth(chart.barData!!.barWidth) buffer.feed(dataSet) trans!!.pointValuesToPixel(buffer.buffer) @@ -261,15 +261,15 @@ class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator?, val barData = chart.barData for (high in indices) { - val set = barData.getDataSetByIndex(high.dataSetIndex) + val set = barData?.getDataSetByIndex(high.dataSetIndex) ?: continue - if (set == null || !set.isHighlightEnabled) { + if (!set.isHighlightEnabled) { continue } val e = set.getEntryForXValue(high.x, high.y) - if (!isInBoundsX(e, set)) { + if (!isInBoundsX(e, set) || e == null) { continue } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedHorizontalBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedHorizontalBarChartRenderer.kt index b169a2202f..b1da10b2a1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedHorizontalBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedHorizontalBarChartRenderer.kt @@ -14,7 +14,7 @@ import kotlin.math.min /** * @noinspection unused */ -class RoundedHorizontalBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator?, viewPortHandler: ViewPortHandler?) : +class RoundedHorizontalBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator, viewPortHandler: ViewPortHandler) : HorizontalBarChartRenderer(chart, animator, viewPortHandler) { private val mBarShadowRectBuffer = RectF() private var roundedShadowRadius = 0f @@ -49,7 +49,7 @@ class RoundedHorizontalBarChartRenderer(chart: BarDataProvider, animator: ChartA if (chart.isDrawBarShadowEnabled) { shadowPaint.color = dataSet.barShadowColor - val barData = chart.barData + val barData = chart.barData ?: return val barWidth = barData.barWidth val barWidthHalf = barWidth / 2.0f var x: Float @@ -84,7 +84,7 @@ class RoundedHorizontalBarChartRenderer(chart: BarDataProvider, animator: ChartA buffer.setPhases(phaseX, phaseY) buffer.setDataSet(index) buffer.setInverted(chart.isInverted(dataSet.axisDependency)) - buffer.setBarWidth(chart.barData.barWidth) + buffer.setBarWidth(chart.barData!!.barWidth) buffer.feed(dataSet) trans!!.pointValuesToPixel(buffer.buffer) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt index 931dca35d0..91e3e73675 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt @@ -12,13 +12,13 @@ import com.github.mikephil.charting.utils.ViewPortHandler import kotlin.math.ceil import kotlin.math.min -open class ScatterChartRenderer(@JvmField var chart: ScatterDataProvider, animator: ChartAnimator?, viewPortHandler: ViewPortHandler?) : +open class ScatterChartRenderer(@JvmField var chart: ScatterDataProvider, animator: ChartAnimator, viewPortHandler: ViewPortHandler) : LineScatterCandleRadarRenderer(animator, viewPortHandler) { override fun initBuffers() { } override fun drawData(c: Canvas) { - val scatterData = chart.scatterData + val scatterData = chart.scatterData ?: return for (set in scatterData.dataSets) { if (set.isVisible) drawDataSet(c, set) @@ -74,9 +74,9 @@ open class ScatterChartRenderer(@JvmField var chart: ScatterDataProvider, animat // if values are drawn if (isDrawingValuesAllowed(chart)) { - val dataSets = chart.scatterData.dataSets + val dataSets = chart.scatterData?.dataSets ?: return - for (i in 0.. { - limitLinePaint!!.textAlign = Align.RIGHT + limitLinePaint.textAlign = Align.RIGHT c.drawText( label, viewPortHandler.contentRight() - xOffset, - pts[1] - yOffset + labelLineHeight, limitLinePaint!! + pts[1] - yOffset + labelLineHeight, limitLinePaint ) } LimitLabelPosition.RIGHT_BOTTOM -> { - limitLinePaint!!.textAlign = Align.RIGHT + limitLinePaint.textAlign = Align.RIGHT c.drawText( label, viewPortHandler.contentRight() - xOffset, - pts[1] + yOffset, limitLinePaint!! + pts[1] + yOffset, limitLinePaint ) } LimitLabelPosition.LEFT_TOP -> { - limitLinePaint!!.textAlign = Align.LEFT + limitLinePaint.textAlign = Align.LEFT c.drawText( label, viewPortHandler.contentLeft() + xOffset, - pts[1] - yOffset + labelLineHeight, limitLinePaint!! + pts[1] - yOffset + labelLineHeight, limitLinePaint ) } else -> { - limitLinePaint!!.textAlign = Align.LEFT + limitLinePaint.textAlign = Align.LEFT c.drawText( label, viewPortHandler.offsetLeft() + xOffset, - pts[1] + yOffset, limitLinePaint!! + pts[1] + yOffset, limitLinePaint ) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt index 98c5829aeb..499818b31e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt @@ -27,7 +27,7 @@ class XAxisRendererRadarChart(viewPortHandler: ViewPortHandler, xAxis: XAxis, pr val center = chart.centerOffsets val pOut = MPPointF.getInstance(0f, 0f) - for (i in 0.. 0) { + if (limitRanges.isNotEmpty()) { val ptsr = FloatArray(2) ptsr[0] = 0f ptsr[1] = 0f @@ -374,105 +374,105 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v if (!limitRange.isEnabled) continue - val clipRestoreCount = c.save() - limitLineClippingRect.set(viewPortHandler.contentRect) - limitLineClippingRect.inset(0f, -limitRange.lineWidth) - c.clipRect(limitLineClippingRect) - - limitRangePaint.style = Paint.Style.STROKE - limitRangePaint.color = limitRange.lineColor - limitRangePaint.strokeWidth = limitRange.lineWidth - limitRangePaint.setPathEffect(limitRange.dashPathEffect) - - limitRangePaintFill.style = Paint.Style.FILL - limitRangePaintFill.color = limitRange.rangeColor - - ptsr[1] = limitRange.limit.high - ptsr2[1] = limitRange.limit.low - - transformer?.pointValuesToPixel(ptsr) - transformer?.pointValuesToPixel(ptsr2) - - limitRangePathFill.moveTo(viewPortHandler.contentLeft(), ptsr[1]) - limitRangePathFill.addRect( - viewPortHandler.contentLeft(), - ptsr[1], - viewPortHandler.contentRight(), - ptsr2[1], - Path.Direction.CW - ) - c.drawPath(limitRangePathFill, limitRangePaintFill) - limitRangePathFill.reset() - - if (limitRange.lineWidth > 0) { - limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr[1]) - limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr[1]) - c.drawPath(limitRangePath, limitRangePaint) - - limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr2[1]) - limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr2[1]) - c.drawPath(limitRangePath, limitRangePaint) - } - - limitRangePath.reset() - - val label = limitRange.label + c.withSave { + limitLineClippingRect.set(viewPortHandler.contentRect) + limitLineClippingRect.inset(0f, -limitRange.lineWidth) + c.clipRect(limitLineClippingRect) - // if drawing the limit-value label is enabled - if (label != null && label != "") { - limitRangePaint.style = limitRange.textStyle - limitRangePaint.setPathEffect(null) - limitRangePaint.color = limitRange.textColor - limitRangePaint.setTypeface(limitRange.typeface) - limitRangePaint.strokeWidth = 0.5f - limitRangePaint.textSize = limitRange.textSize + limitRangePaint.style = Paint.Style.STROKE + limitRangePaint.color = limitRange.lineColor + limitRangePaint.strokeWidth = limitRange.lineWidth + limitRangePaint.setPathEffect(limitRange.dashPathEffect) + + limitRangePaintFill.style = Paint.Style.FILL + limitRangePaintFill.color = limitRange.rangeColor + + ptsr[1] = limitRange.limit.high + ptsr2[1] = limitRange.limit.low + + transformer?.pointValuesToPixel(ptsr) + transformer?.pointValuesToPixel(ptsr2) + + limitRangePathFill.moveTo(viewPortHandler.contentLeft(), ptsr[1]) + limitRangePathFill.addRect( + viewPortHandler.contentLeft(), + ptsr[1], + viewPortHandler.contentRight(), + ptsr2[1], + Path.Direction.CW + ) + c.drawPath(limitRangePathFill, limitRangePaintFill) + limitRangePathFill.reset() + + if (limitRange.lineWidth > 0) { + limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr[1]) + limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr[1]) + c.drawPath(limitRangePath, limitRangePaint) + + limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr2[1]) + limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr2[1]) + c.drawPath(limitRangePath, limitRangePaint) + } - val labelLineHeight = Utils.calcTextHeight(limitRangePaint, label).toFloat() - val xOffset = Utils.convertDpToPixel(4f) + limitRange.xOffset - val yOffset = limitRange.lineWidth + labelLineHeight + limitRange.yOffset + limitRangePath.reset() - val position = limitRange.labelPosition + val label = limitRange.label - when (position) { - LimitLabelPosition.RIGHT_TOP -> { - limitRangePaint.textAlign = Align.RIGHT - c.drawText( - label, - viewPortHandler.contentRight() - xOffset, - ptsr[1] - yOffset + labelLineHeight, limitRangePaint - ) - } - - LimitLabelPosition.RIGHT_BOTTOM -> { - limitRangePaint.textAlign = Align.RIGHT - c.drawText( - label, - viewPortHandler.contentRight() - xOffset, - ptsr[1] + yOffset, limitRangePaint - ) - } - - LimitLabelPosition.LEFT_TOP -> { - limitRangePaint.textAlign = Align.LEFT - c.drawText( - label, - viewPortHandler.contentLeft() + xOffset, - ptsr[1] - yOffset + labelLineHeight, limitRangePaint - ) - } - - else -> { - limitRangePaint.textAlign = Align.LEFT - c.drawText( - label, - viewPortHandler.offsetLeft() + xOffset, - ptsr[1] + yOffset, limitRangePaint - ) + // if drawing the limit-value label is enabled + if (label != null && label != "") { + limitRangePaint.style = limitRange.textStyle + limitRangePaint.setPathEffect(null) + limitRangePaint.color = limitRange.textColor + limitRangePaint.setTypeface(limitRange.typeface) + limitRangePaint.strokeWidth = 0.5f + limitRangePaint.textSize = limitRange.textSize + + val labelLineHeight = Utils.calcTextHeight(limitRangePaint, label).toFloat() + val xOffset = Utils.convertDpToPixel(4f) + limitRange.xOffset + val yOffset = limitRange.lineWidth + labelLineHeight + limitRange.yOffset + + val position = limitRange.labelPosition + + when (position) { + LimitLabelPosition.RIGHT_TOP -> { + limitRangePaint.textAlign = Align.RIGHT + c.drawText( + label, + viewPortHandler.contentRight() - xOffset, + ptsr[1] - yOffset + labelLineHeight, limitRangePaint + ) + } + + LimitLabelPosition.RIGHT_BOTTOM -> { + limitRangePaint.textAlign = Align.RIGHT + c.drawText( + label, + viewPortHandler.contentRight() - xOffset, + ptsr[1] + yOffset, limitRangePaint + ) + } + + LimitLabelPosition.LEFT_TOP -> { + limitRangePaint.textAlign = Align.LEFT + c.drawText( + label, + viewPortHandler.contentLeft() + xOffset, + ptsr[1] - yOffset + labelLineHeight, limitRangePaint + ) + } + + else -> { + limitRangePaint.textAlign = Align.LEFT + c.drawText( + label, + viewPortHandler.offsetLeft() + xOffset, + ptsr[1] + yOffset, limitRangePaint + ) + } } } - } - c.restoreToCount(clipRestoreCount) + } } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt index e2a0afcbfd..6aacd6c1e6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt @@ -167,7 +167,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr } override fun renderLimitLines(c: Canvas) { - val limitLines = yAxis.limitLines ?: return + val limitLines = yAxis.limitLines val sliceAngle = chart.sliceAngle @@ -192,7 +192,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr limitPath.reset() - for (j in 0.. 0.0) { - renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shapeStrokeSize); + renderPaint.style = Paint.Style.STROKE + renderPaint.strokeWidth = shapeStrokeSize - c.drawCircle( - posX, - posY, - shapeHoleSizeHalf + shapeStrokeSizeHalf, - renderPaint); + c?.drawCircle( + posX, + posY, + shapeHoleSizeHalf + shapeStrokeSizeHalf, + renderPaint + ) if (shapeHoleColor != ColorTemplate.COLOR_NONE) { - renderPaint.setStyle(Paint.Style.FILL); - - renderPaint.setColor(shapeHoleColor); - c.drawCircle( - posX, - posY, - shapeHoleSizeHalf, - renderPaint); - } - } else { - renderPaint.setStyle(Paint.Style.FILL); + renderPaint.style = Paint.Style.FILL - c.drawCircle( + renderPaint.setColor(shapeHoleColor) + c?.drawCircle( posX, posY, - shapeHalf, - renderPaint); + shapeHoleSizeHalf, + renderPaint + ) + } + } else { + renderPaint.style = Paint.Style.FILL + + c?.drawCircle( + posX, + posY, + shapeHalf, + renderPaint + ) } - } - } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CrossShapeRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CrossShapeRenderer.kt index 2a62bffd82..676b0d9dcf 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CrossShapeRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CrossShapeRenderer.kt @@ -1,41 +1,38 @@ -package com.github.mikephil.charting.renderer.scatter; +package com.github.mikephil.charting.renderer.scatter -import android.graphics.Canvas; -import android.graphics.Paint; - -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.graphics.Canvas +import android.graphics.Paint +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by wajdic on 15/06/2016. * Created at Time 09:08 */ -public class CrossShapeRenderer implements IShapeRenderer -{ - - - @Override - public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint) { - - final float shapeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeSize()) / 2f; - - renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(Utils.convertDpToPixel(1f)); - - c.drawLine( - posX - shapeHalf, - posY, - posX + shapeHalf, - posY, - renderPaint); - c.drawLine( - posX, - posY - shapeHalf, - posX, - posY + shapeHalf, - renderPaint); - +class CrossShapeRenderer : IShapeRenderer { + override fun renderShape( + c: Canvas?, dataSet: IScatterDataSet, viewPortHandler: ViewPortHandler, + posX: Float, posY: Float, renderPaint: Paint + ) { + val shapeHalf = Utils.convertDpToPixel(dataSet.scatterShapeSize) / 2f + + renderPaint.style = Paint.Style.STROKE + renderPaint.strokeWidth = Utils.convertDpToPixel(1f) + + c?.drawLine( + posX - shapeHalf, + posY, + posX + shapeHalf, + posY, + renderPaint + ) + c?.drawLine( + posX, + posY - shapeHalf, + posX, + posY + shapeHalf, + renderPaint + ) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/IShapeRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/IShapeRenderer.kt index 20b57a900d..543d48d454 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/IShapeRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/IShapeRenderer.kt @@ -1,18 +1,15 @@ -package com.github.mikephil.charting.renderer.scatter; +package com.github.mikephil.charting.renderer.scatter -import android.graphics.Canvas; -import android.graphics.Paint; - -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.graphics.Canvas +import android.graphics.Paint +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by wajdic on 15/06/2016. * Created at Time 09:07 */ -public interface IShapeRenderer -{ - +interface IShapeRenderer { /** * Renders the provided ScatterDataSet with a shape. * @@ -23,6 +20,8 @@ public interface IShapeRenderer * @param posY Position to draw the shape at * @param renderPaint Paint object used for styling and drawing */ - void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint); + fun renderShape( + c: Canvas?, dataSet: IScatterDataSet, viewPortHandler: ViewPortHandler, + posX: Float, posY: Float, renderPaint: Paint, + ) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.kt index c6a4bf1350..2e5734dcfb 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.kt @@ -1,63 +1,64 @@ -package com.github.mikephil.charting.renderer.scatter; +package com.github.mikephil.charting.renderer.scatter -import android.graphics.Canvas; -import android.graphics.Paint; - -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.graphics.Canvas +import android.graphics.Paint +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by wajdic on 15/06/2016. * Created at Time 09:08 */ -public class SquareShapeRenderer implements IShapeRenderer -{ - - - @Override - public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint) { +class SquareShapeRenderer : IShapeRenderer { + override fun renderShape( + c: Canvas?, dataSet: IScatterDataSet, viewPortHandler: ViewPortHandler, + posX: Float, posY: Float, renderPaint: Paint + ) { + val shapeSize = Utils.convertDpToPixel(dataSet.scatterShapeSize) + val shapeHalf = shapeSize / 2f + val shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.scatterShapeHoleRadius) + val shapeHoleSize = shapeHoleSizeHalf * 2f + val shapeStrokeSize = (shapeSize - shapeHoleSize) / 2f + val shapeStrokeSizeHalf = shapeStrokeSize / 2f - final float shapeSize = Utils.convertDpToPixel(dataSet.getScatterShapeSize()); - final float shapeHalf = shapeSize / 2f; - final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); - final float shapeHoleSize = shapeHoleSizeHalf * 2.f; - final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; - final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; - - final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); + val shapeHoleColor = dataSet.scatterShapeHoleColor if (shapeSize > 0.0) { - renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shapeStrokeSize); + renderPaint.style = Paint.Style.STROKE + renderPaint.strokeWidth = shapeStrokeSize - c.drawRect(posX - shapeHoleSizeHalf - shapeStrokeSizeHalf, - posY - shapeHoleSizeHalf - shapeStrokeSizeHalf, - posX + shapeHoleSizeHalf + shapeStrokeSizeHalf, - posY + shapeHoleSizeHalf + shapeStrokeSizeHalf, - renderPaint); + c?.drawRect( + posX - shapeHoleSizeHalf - shapeStrokeSizeHalf, + posY - shapeHoleSizeHalf - shapeStrokeSizeHalf, + posX + shapeHoleSizeHalf + shapeStrokeSizeHalf, + posY + shapeHoleSizeHalf + shapeStrokeSizeHalf, + renderPaint + ) if (shapeHoleColor != ColorTemplate.COLOR_NONE) { - renderPaint.setStyle(Paint.Style.FILL); - - renderPaint.setColor(shapeHoleColor); - c.drawRect(posX - shapeHoleSizeHalf, - posY - shapeHoleSizeHalf, - posX + shapeHoleSizeHalf, - posY + shapeHoleSizeHalf, - renderPaint); - } + renderPaint.style = Paint.Style.FILL + renderPaint.setColor(shapeHoleColor) + c?.drawRect( + posX - shapeHoleSizeHalf, + posY - shapeHoleSizeHalf, + posX + shapeHoleSizeHalf, + posY + shapeHoleSizeHalf, + renderPaint + ) + } } else { - renderPaint.setStyle(Paint.Style.FILL); + renderPaint.style = Paint.Style.FILL - c.drawRect(posX - shapeHalf, - posY - shapeHalf, - posX + shapeHalf, - posY + shapeHalf, - renderPaint); + c?.drawRect( + posX - shapeHalf, + posY - shapeHalf, + posX + shapeHalf, + posY + shapeHalf, + renderPaint + ) } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt index c355c0da17..fd55b6f5f4 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt @@ -1,80 +1,89 @@ -package com.github.mikephil.charting.renderer.scatter; +package com.github.mikephil.charting.renderer.scatter -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Path; - -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Path +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by wajdic on 15/06/2016. * Created at Time 09:08 */ -public class TriangleShapeRenderer implements IShapeRenderer -{ - - protected Path mTrianglePathBuffer = new Path(); - - @Override - public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint) { +class TriangleShapeRenderer : IShapeRenderer { + protected var mTrianglePathBuffer: Path = Path() - final float shapeSize = Utils.convertDpToPixel(dataSet.getScatterShapeSize()); - final float shapeHalf = shapeSize / 2f; - final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); - final float shapeHoleSize = shapeHoleSizeHalf * 2.f; - final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; + override fun renderShape( + c: Canvas?, dataSet: IScatterDataSet, viewPortHandler: ViewPortHandler, + posX: Float, posY: Float, renderPaint: Paint + ) { + val shapeSize = Utils.convertDpToPixel(dataSet.scatterShapeSize) + val shapeHalf = shapeSize / 2f + val shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.scatterShapeHoleRadius) + val shapeHoleSize = shapeHoleSizeHalf * 2f + val shapeStrokeSize = (shapeSize - shapeHoleSize) / 2f - final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); + val shapeHoleColor = dataSet.scatterShapeHoleColor - renderPaint.setStyle(Paint.Style.FILL); + renderPaint.style = Paint.Style.FILL // create a triangle path - Path tri = mTrianglePathBuffer; - tri.reset(); + val tri = mTrianglePathBuffer + tri.reset() - tri.moveTo(posX, posY - shapeHalf); - tri.lineTo(posX + shapeHalf, posY + shapeHalf); - tri.lineTo(posX - shapeHalf, posY + shapeHalf); + tri.moveTo(posX, posY - shapeHalf) + tri.lineTo(posX + shapeHalf, posY + shapeHalf) + tri.lineTo(posX - shapeHalf, posY + shapeHalf) if (shapeSize > 0.0) { - tri.lineTo(posX, posY - shapeHalf); - - tri.moveTo(posX - shapeHalf + shapeStrokeSize, - posY + shapeHalf - shapeStrokeSize); - tri.lineTo(posX + shapeHalf - shapeStrokeSize, - posY + shapeHalf - shapeStrokeSize); - tri.lineTo(posX, - posY - shapeHalf + shapeStrokeSize); - tri.lineTo(posX - shapeHalf + shapeStrokeSize, - posY + shapeHalf - shapeStrokeSize); + tri.lineTo(posX, posY - shapeHalf) + + tri.moveTo( + posX - shapeHalf + shapeStrokeSize, + posY + shapeHalf - shapeStrokeSize + ) + tri.lineTo( + posX + shapeHalf - shapeStrokeSize, + posY + shapeHalf - shapeStrokeSize + ) + tri.lineTo( + posX, + posY - shapeHalf + shapeStrokeSize + ) + tri.lineTo( + posX - shapeHalf + shapeStrokeSize, + posY + shapeHalf - shapeStrokeSize + ) } - tri.close(); + tri.close() - c.drawPath(tri, renderPaint); - tri.reset(); + c?.drawPath(tri, renderPaint) + tri.reset() if (shapeSize > 0.0 && - shapeHoleColor != ColorTemplate.COLOR_NONE) { - - renderPaint.setColor(shapeHoleColor); - - tri.moveTo(posX, - posY - shapeHalf + shapeStrokeSize); - tri.lineTo(posX + shapeHalf - shapeStrokeSize, - posY + shapeHalf - shapeStrokeSize); - tri.lineTo(posX - shapeHalf + shapeStrokeSize, - posY + shapeHalf - shapeStrokeSize); - tri.close(); - - c.drawPath(tri, renderPaint); - tri.reset(); + shapeHoleColor != ColorTemplate.COLOR_NONE + ) { + renderPaint.setColor(shapeHoleColor) + + tri.moveTo( + posX, + posY - shapeHalf + shapeStrokeSize + ) + tri.lineTo( + posX + shapeHalf - shapeStrokeSize, + posY + shapeHalf - shapeStrokeSize + ) + tri.lineTo( + posX - shapeHalf + shapeStrokeSize, + posY + shapeHalf - shapeStrokeSize + ) + tri.close() + + c?.drawPath(tri, renderPaint) + tri.reset() } - } - } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/XShapeRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/XShapeRenderer.kt index f4d1a40736..782fda1326 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/XShapeRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/XShapeRenderer.kt @@ -1,42 +1,38 @@ -package com.github.mikephil.charting.renderer.scatter; +package com.github.mikephil.charting.renderer.scatter -import android.graphics.Canvas; -import android.graphics.Paint; - -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.graphics.Canvas +import android.graphics.Paint +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.ViewPortHandler /** * Created by wajdic on 15/06/2016. * Created at Time 09:08 */ -public class XShapeRenderer implements IShapeRenderer -{ - - - @Override - public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint) { - - final float shapeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeSize()) / 2f; - - renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(Utils.convertDpToPixel(1f)); - - c.drawLine( - posX - shapeHalf, - posY - shapeHalf, - posX + shapeHalf, - posY + shapeHalf, - renderPaint); - c.drawLine( - posX + shapeHalf, - posY - shapeHalf, - posX - shapeHalf, - posY + shapeHalf, - renderPaint); - +class XShapeRenderer : IShapeRenderer { + override fun renderShape( + c: Canvas?, dataSet: IScatterDataSet, viewPortHandler: ViewPortHandler, + posX: Float, posY: Float, renderPaint: Paint + ) { + val shapeHalf = Utils.convertDpToPixel(dataSet.scatterShapeSize) / 2f + + renderPaint.style = Paint.Style.STROKE + renderPaint.strokeWidth = Utils.convertDpToPixel(1f) + + c?.drawLine( + posX - shapeHalf, + posY - shapeHalf, + posX + shapeHalf, + posY + shapeHalf, + renderPaint + ) + c?.drawLine( + posX + shapeHalf, + posY - shapeHalf, + posX - shapeHalf, + posY + shapeHalf, + renderPaint + ) } - } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt index 4d9c1de790..336b9d30f3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt @@ -1,11 +1,7 @@ +package com.github.mikephil.charting.utils -package com.github.mikephil.charting.utils; - -import android.content.res.Resources; -import android.graphics.Color; - -import java.util.ArrayList; -import java.util.List; +import android.content.res.Resources +import android.graphics.Color /** * Class that holds predefined color integer arrays (e.g. @@ -14,46 +10,51 @@ import java.util.List; * * @author Philipp Jahoda */ -public class ColorTemplate { - +object ColorTemplate { /** * an "invalid" color that indicates that no color is set */ - public static final int COLOR_NONE = 0x00112233; + const val COLOR_NONE: Int = 0x00112233 /** * this "color" is used for the Legend creation and indicates that the next * form should be skipped */ - public static final int COLOR_SKIP = 0x00112234; + const val COLOR_SKIP: Int = 0x00112234 /** * THE COLOR THEMES ARE PREDEFINED (predefined color integer arrays), FEEL * FREE TO CREATE YOUR OWN WITH AS MANY DIFFERENT COLORS AS YOU WANT */ - public static final int[] LIBERTY_COLORS = { - Color.rgb(207, 248, 246), Color.rgb(148, 212, 212), Color.rgb(136, 180, 187), - Color.rgb(118, 174, 175), Color.rgb(42, 109, 130) - }; - public static final int[] JOYFUL_COLORS = { - Color.rgb(217, 80, 138), Color.rgb(254, 149, 7), Color.rgb(254, 247, 120), - Color.rgb(106, 167, 134), Color.rgb(53, 194, 209) - }; - public static final int[] PASTEL_COLORS = { - Color.rgb(64, 89, 128), Color.rgb(149, 165, 124), Color.rgb(217, 184, 162), - Color.rgb(191, 134, 134), Color.rgb(179, 48, 80) - }; - public static final int[] COLORFUL_COLORS = { - Color.rgb(193, 37, 82), Color.rgb(255, 102, 0), Color.rgb(245, 199, 0), - Color.rgb(106, 150, 31), Color.rgb(179, 100, 53) - }; - public static final int[] VORDIPLOM_COLORS = { - Color.rgb(192, 255, 140), Color.rgb(255, 247, 140), Color.rgb(255, 208, 140), - Color.rgb(140, 234, 255), Color.rgb(255, 140, 157) - }; - public static final int[] MATERIAL_COLORS = { - rgb("#2ecc71"), rgb("#f1c40f"), rgb("#e74c3c"), rgb("#3498db") - }; + @JvmField + val LIBERTY_COLORS: IntArray = intArrayOf( + Color.rgb(207, 248, 246), Color.rgb(148, 212, 212), Color.rgb(136, 180, 187), + Color.rgb(118, 174, 175), Color.rgb(42, 109, 130) + ) + @JvmField + val JOYFUL_COLORS: IntArray = intArrayOf( + Color.rgb(217, 80, 138), Color.rgb(254, 149, 7), Color.rgb(254, 247, 120), + Color.rgb(106, 167, 134), Color.rgb(53, 194, 209) + ) + @JvmField + val PASTEL_COLORS: IntArray = intArrayOf( + Color.rgb(64, 89, 128), Color.rgb(149, 165, 124), Color.rgb(217, 184, 162), + Color.rgb(191, 134, 134), Color.rgb(179, 48, 80) + ) + @JvmField + val COLORFUL_COLORS: IntArray = intArrayOf( + Color.rgb(193, 37, 82), Color.rgb(255, 102, 0), Color.rgb(245, 199, 0), + Color.rgb(106, 150, 31), Color.rgb(179, 100, 53) + ) + @JvmField + val VORDIPLOM_COLORS: IntArray = intArrayOf( + Color.rgb(192, 255, 140), Color.rgb(255, 247, 140), Color.rgb(255, 208, 140), + Color.rgb(140, 234, 255), Color.rgb(255, 140, 157) + ) + @JvmField + val MATERIAL_COLORS: IntArray = intArrayOf( + rgb("#2ecc71"), rgb("#f1c40f"), rgb("#e74c3c"), rgb("#3498db") + ) /** * Converts the given hex-color-string to rgb. @@ -61,22 +62,22 @@ public class ColorTemplate { * @param hex * @return */ - public static int rgb(String hex) { - int color = (int) Long.parseLong(hex.replace("#", ""), 16); - int r = (color >> 16) & 0xFF; - int g = (color >> 8) & 0xFF; - int b = (color >> 0) & 0xFF; - return Color.rgb(r, g, b); + fun rgb(hex: String): Int { + val color = hex.replace("#", "").toLong(16).toInt() + val r = (color shr 16) and 0xFF + val g = (color shr 8) and 0xFF + val b = (color shr 0) and 0xFF + return Color.rgb(r, g, b) } - /** - * Returns the Android ICS holo blue light color. - * - * @return - */ - public static int getHoloBlue() { - return Color.rgb(51, 181, 229); - } + @JvmStatic + val holoBlue: Int + /** + * Returns the Android ICS holo blue light color. + * + * @return + */ + get() = Color.rgb(51, 181, 229) /** * Sets the alpha component of the given color. @@ -85,8 +86,9 @@ public class ColorTemplate { * @param alpha 0 - 255 * @return */ - public static int colorWithAlpha(int color, int alpha) { - return (color & 0xffffff) | ((alpha & 0xff) << 24); + @JvmStatic + fun colorWithAlpha(color: Int, alpha: Int): Int { + return (color and 0xffffff) or ((alpha and 0xff) shl 24) } /** @@ -97,15 +99,14 @@ public class ColorTemplate { * @param colors an integer array of resource id's of colors * @return */ - public static List createColors(Resources r, int[] colors) { + fun createColors(r: Resources, colors: IntArray): MutableList { + val result: MutableList = ArrayList() - List result = new ArrayList(); - - for (int i : colors) { - result.add(r.getColor(i)); + for (i in colors) { + result.add(r.getColor(i)) } - return result; + return result } /** @@ -115,14 +116,13 @@ public class ColorTemplate { * @param colors * @return */ - public static List createColors(int[] colors) { - - List result = new ArrayList(); + fun createColors(colors: IntArray): MutableList { + val result: MutableList = ArrayList() - for (int i : colors) { - result.add(i); + for (i in colors) { + result.add(i) } - return result; + return result } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt index 8f59c12d07..b70ef8ee7d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt @@ -1,22 +1,19 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.utils -import com.github.mikephil.charting.data.Entry; - -import java.util.Comparator; +import com.github.mikephil.charting.data.Entry /** * Comparator for comparing Entry-objects by their x-value. * Created by philipp on 17/06/15. */ -public class EntryXComparator implements Comparator { - @Override - public int compare(Entry entry1, Entry entry2) { - float diff = entry1.getX() - entry2.getX(); +class EntryXComparator : Comparator { + override fun compare(entry1: Entry, entry2: Entry): Int { + val diff = entry1.x - entry2.x - if (diff == 0f) return 0; + if (diff == 0f) return 0 else { - if (diff > 0f) return 1; - else return -1; + if (diff > 0f) return 1 + else return -1 } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt index a12bc45918..70c68f0333 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt @@ -1,79 +1,77 @@ +package com.github.mikephil.charting.utils -package com.github.mikephil.charting.utils; - -import java.util.List; +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import kotlin.Any +import kotlin.Boolean +import kotlin.Int +import kotlin.String /** * Class for describing width and height dimensions in some arbitrary * unit. Replacement for the android.Util.SizeF which is available only on API >= 21. */ -public final class FSize extends ObjectPool.Poolable{ - +open class FSize : Poolable { // TODO : Encapsulate width & height + var width: Float = 0f + var height: Float = 0f - public float width; - public float height; - - private static ObjectPool pool; - - static { - pool = ObjectPool.create(256, new FSize(0,0)); - pool.setReplenishPercentage(0.5f); + override fun instantiate(): FSize { + return FSize(0f, 0f) } + constructor() - protected ObjectPool.Poolable instantiate(){ - return new FSize(0,0); + constructor(width: Float, height: Float) { + this.width = width + this.height = height } - public static FSize getInstance(final float width, final float height){ - FSize result = pool.get(); - result.width = width; - result.height = height; - return result; - } - - public static void recycleInstance(FSize instance){ - pool.recycle(instance); - } - - public static void recycleInstances(List instances){ - pool.recycle(instances); - } - - public FSize() { - } - - public FSize(final float width, final float height) { - this.width = width; - this.height = height; - } - - @Override - public boolean equals(final Object obj) { + override fun equals(obj: Any?): Boolean { if (obj == null) { - return false; + return false } - if (this == obj) { - return true; + if (this === obj) { + return true } - if (obj instanceof FSize) { - final FSize other = (FSize) obj; - return width == other.width && height == other.height; + if (obj is FSize) { + val other = obj + return width == other.width && height == other.height } - return false; + return false } - @Override - public String toString() { - return width + "x" + height; + override fun toString(): String { + return width.toString() + "x" + height } /** * {@inheritDoc} */ - @Override - public int hashCode() { - return Float.floatToIntBits(width) ^ Float.floatToIntBits(height); + override fun hashCode(): Int { + return width.toBits() xor height.toBits() + } + + companion object { + private val pool: ObjectPool = ObjectPool.create(256, FSize(0f, 0f)) + + init { + pool.setReplenishPercentage(0.5f) + } + + + fun getInstance(width: Float, height: Float): FSize { + val result: FSize = pool.get() + result.width = width + result.height = height + return result + } + + fun recycleInstance(instance: FSize) { + pool.recycle(instance) + } + + fun recycleInstances(instances: MutableList) { + pool.recycle(instances) + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt index 5aff51ff84..c577aab3a2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt @@ -1,74 +1,65 @@ - -package com.github.mikephil.charting.utils; - -import android.content.res.AssetManager; -import android.os.Environment; -import android.util.Log; - -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; +package com.github.mikephil.charting.utils + +import android.content.res.AssetManager +import android.os.Environment +import android.util.Log +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.File +import java.io.FileReader +import java.io.FileWriter +import java.io.IOException +import java.io.InputStreamReader /** * Utilities class for interacting with the assets and the devices storage to * load and save DataSet objects from and to .txt files. - * + * * @author Philipp Jahoda */ -public class FileUtils { - - private static final String LOG = "MPChart-FileUtils"; +object FileUtils { + private const val LOG = "MPChart-FileUtils" /** * Loads a an Array of Entries from a textfile from the sd-card. - * + * * @param path the name of the file on the sd-card (+ path if needed) * @return */ - public static List loadEntriesFromFile(String path) { - - File sdcard = Environment.getExternalStorageDirectory(); + fun loadEntriesFromFile(path: String): MutableList { + val sdcard = Environment.getExternalStorageDirectory() // Get the text file - File file = new File(sdcard, path); + val file = File(sdcard, path) - List entries = new ArrayList(); + val entries: MutableList = ArrayList() try { - @SuppressWarnings("resource") - BufferedReader br = new BufferedReader(new FileReader(file)); - String line; + val br = BufferedReader(FileReader(file)) + var line: String? - while ((line = br.readLine()) != null) { - String[] split = line.split("#"); + while ((br.readLine().also { line = it }) != null) { + val split: Array = line!!.split("#".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - if (split.length <= 2) { - entries.add(new Entry(Float.parseFloat(split[0]), Integer.parseInt(split[1]))); + if (split.size <= 2) { + entries.add(Entry(split[0]!!.toFloat(), split[1]!!.toInt().toFloat())) } else { + val vals = FloatArray(split.size - 1) - float[] vals = new float[split.length - 1]; - - for (int i = 0; i < vals.length; i++) { - vals[i] = Float.parseFloat(split[i]); + for (i in vals.indices) { + vals[i] = split[i]!!.toFloat() } - entries.add(new BarEntry(Integer.parseInt(split[split.length - 1]), vals)); + entries.add(BarEntry(split[split.size - 1]!!.toInt().toFloat(), vals)) } } - } catch (IOException e) { - Log.e(LOG, e.toString()); + } catch (e: IOException) { + Log.e(LOG, e.toString()) } - return entries; + return entries // File sdcard = Environment.getExternalStorageDirectory(); // @@ -101,55 +92,53 @@ public class FileUtils { /** * Loads an array of Entries from a textfile from the assets folder. - * + * * @param am * @param path the name of the file in the assets folder (+ path if needed) * @return */ - public static List loadEntriesFromAssets(AssetManager am, String path) { - - List entries = new ArrayList(); + @JvmStatic + fun loadEntriesFromAssets(am: AssetManager, path: String): MutableList { + val entries: MutableList = ArrayList() - BufferedReader reader = null; + var reader: BufferedReader? = null try { - reader = new BufferedReader( - new InputStreamReader(am.open(path), "UTF-8")); + reader = BufferedReader( + InputStreamReader(am.open(path), "UTF-8") + ) - String line = reader.readLine(); + var line = reader.readLine() while (line != null) { // process line - String[] split = line.split("#"); + val split: Array = line.split("#".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - if (split.length <= 2) { - entries.add(new Entry(Float.parseFloat(split[1]), Float.parseFloat(split[0]))); + if (split.size <= 2) { + entries.add(Entry(split[1]!!.toFloat(), split[0]!!.toFloat())) } else { + val vals = FloatArray(split.size - 1) - float[] vals = new float[split.length - 1]; - - for (int i = 0; i < vals.length; i++) { - vals[i] = Float.parseFloat(split[i]); + for (i in vals.indices) { + vals[i] = split[i]!!.toFloat() } - entries.add(new BarEntry(Integer.parseInt(split[split.length - 1]), vals)); + entries.add(BarEntry(split[split.size - 1]!!.toInt().toFloat(), vals)) } - line = reader.readLine(); + line = reader.readLine() } - } catch (IOException e) { - Log.e(LOG, e.toString()); - + } catch (e: IOException) { + Log.e(LOG, e.toString()) } finally { - if (reader != null) { try { - reader.close(); - } catch (IOException e) { - Log.e(LOG, e.toString()); + reader.close() + } catch (e: IOException) { + Log.e(LOG, e.toString()) } } } - return entries; + return entries // String label = null; // List entries = new ArrayList(); @@ -190,77 +179,69 @@ public class FileUtils { /** * Saves an Array of Entries to the specified location on the sdcard - * + * * @param entries * @param path */ - public static void saveToSdCard(List entries, String path) { - - File sdcard = Environment.getExternalStorageDirectory(); - - File saved = new File(sdcard, path); - if (!saved.exists()) - { - try - { - saved.createNewFile(); - } catch (IOException e) - { - Log.e(LOG, e.toString()); + fun saveToSdCard(entries: MutableList, path: String) { + val sdcard = Environment.getExternalStorageDirectory() + + val saved = File(sdcard, path) + if (!saved.exists()) { + try { + saved.createNewFile() + } catch (e: IOException) { + Log.e(LOG, e.toString()) } } - try - { + try { // BufferedWriter for performance, true to set append to file flag - BufferedWriter buf = new BufferedWriter(new FileWriter(saved, true)); + val buf = BufferedWriter(FileWriter(saved, true)) - for (Entry e : entries) { - - buf.append(e.getY() + "#" + e.getX()); - buf.newLine(); + for (e in entries) { + buf.append(e.y.toString() + "#" + e.x) + buf.newLine() } - buf.close(); - } catch (IOException e) - { - Log.e(LOG, e.toString()); + buf.close() + } catch (e: IOException) { + Log.e(LOG, e.toString()) } } - public static List loadBarEntriesFromAssets(AssetManager am, String path) { - - List entries = new ArrayList(); + @JvmStatic + fun loadBarEntriesFromAssets(am: AssetManager, path: String): MutableList { + val entries: MutableList = ArrayList() - BufferedReader reader = null; + var reader: BufferedReader? = null try { - reader = new BufferedReader( - new InputStreamReader(am.open(path), "UTF-8")); + reader = BufferedReader( + InputStreamReader(am.open(path), "UTF-8") + ) - String line = reader.readLine(); + var line = reader.readLine() while (line != null) { // process line - String[] split = line.split("#"); + val split: Array = line.split("#".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - entries.add(new BarEntry(Float.parseFloat(split[1]), Float.parseFloat(split[0]))); + entries.add(BarEntry(split[1]!!.toFloat(), split[0]!!.toFloat())) - line = reader.readLine(); + line = reader.readLine() } - } catch (IOException e) { - Log.e(LOG, e.toString()); - + } catch (e: IOException) { + Log.e(LOG, e.toString()) } finally { - if (reader != null) { try { - reader.close(); - } catch (IOException e) { - Log.e(LOG, e.toString()); + reader.close() + } catch (e: IOException) { + Log.e(LOG, e.toString()) } } } - return entries; + return entries // String label = null; // ArrayList entries = new ArrayList(); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt index b0699cedba..9403d62b12 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt @@ -1,312 +1,294 @@ -package com.github.mikephil.charting.utils; - -import android.graphics.Canvas; -import android.graphics.LinearGradient; -import android.graphics.Paint; -import android.graphics.Path; -import android.graphics.RectF; -import android.graphics.drawable.Drawable; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -public class Fill { - public enum Type { - EMPTY, COLOR, LINEAR_GRADIENT, DRAWABLE - } - - public enum Direction { - DOWN, UP, RIGHT, LEFT - } - - /** - * the type of fill - */ - private Type mType = Type.EMPTY; - - /** - * the color that is used for filling - */ - @Nullable - private Integer mColor = null; - - private Integer mFinalColor = null; - - /** - * the drawable to be used for filling - */ - @Nullable - protected Drawable mDrawable; - - @Nullable - private int[] mGradientColors; - - @Nullable - private float[] mGradientPositions; - - /** - * transparency used for filling - */ - private int mAlpha = 255; - - public Fill() { - } - - public Fill(int color) { - this.mType = Type.COLOR; - this.mColor = color; - calculateFinalColor(); - } - - public Fill(int startColor, int endColor) { - this.mType = Type.LINEAR_GRADIENT; - this.mGradientColors = new int[]{startColor, endColor}; - } - - public Fill(@NonNull int[] gradientColors) { - this.mType = Type.LINEAR_GRADIENT; - this.mGradientColors = gradientColors; - } - - public Fill(@NonNull int[] gradientColors, @NonNull float[] gradientPositions) { - this.mType = Type.LINEAR_GRADIENT; - this.mGradientColors = gradientColors; - this.mGradientPositions = gradientPositions; - } - - public Fill(@NonNull Drawable drawable) { - this.mType = Type.DRAWABLE; - this.mDrawable = drawable; - } - - public Type getType() { - return mType; - } - - public void setType(Type type) { - this.mType = type; - } - - @Nullable - public Integer getColor() { - return mColor; - } - - public void setColor(int color) { - this.mColor = color; - calculateFinalColor(); - } - - public int[] getGradientColors() { - return mGradientColors; - } - - public void setGradientColors(int[] colors) { - this.mGradientColors = colors; - } - - public float[] getGradientPositions() { - return mGradientPositions; - } - - public void setGradientPositions(float[] positions) { - this.mGradientPositions = positions; - } - - public void setGradientColors(int startColor, int endColor) { - this.mGradientColors = new int[]{startColor, endColor}; - } - - public int getAlpha() { - return mAlpha; - } - - public void setAlpha(int alpha) { - this.mAlpha = alpha; - calculateFinalColor(); - } - - private void calculateFinalColor() { - if (mColor == null) { - mFinalColor = null; - } else { - int alpha = (int) Math.floor(((mColor >> 24) / 255.0) * (mAlpha / 255.0) * 255.0); - mFinalColor = (alpha << 24) | (mColor & 0xffffff); - } - } - - public void fillRect(Canvas c, Paint paint, - float left, float top, float right, float bottom, - Direction gradientDirection, float mRoundedBarRadius) { - switch (mType) { - case EMPTY: - return; - - case COLOR: { +package com.github.mikephil.charting.utils + +import android.graphics.Canvas +import android.graphics.LinearGradient +import android.graphics.Paint +import android.graphics.Path +import android.graphics.RectF +import android.graphics.Shader +import android.graphics.drawable.Drawable +import kotlin.math.floor + +open class Fill { + enum class Type { + EMPTY, COLOR, LINEAR_GRADIENT, DRAWABLE + } + + enum class Direction { + DOWN, UP, RIGHT, LEFT + } + + /** + * the type of fill + */ + var type: Type = Type.EMPTY + + /** + * the color that is used for filling + */ + private var mColor: Int? = null + + private var mFinalColor: Int? = null + + /** + * the drawable to be used for filling + */ + protected var mDrawable: Drawable? = null + + var gradientColors: IntArray? + + val gradientPositions: FloatArray? + + /** + * transparency used for filling + */ + private var mAlpha = 255 + + constructor() { + gradientColors = null + gradientPositions = null + } + + constructor(color: Int) { + this.type = Type.COLOR + this.mColor = color + this.gradientColors = null + this.gradientPositions = null + calculateFinalColor() + } + + constructor(startColor: Int, endColor: Int) { + this.type = Type.LINEAR_GRADIENT + this.gradientColors = intArrayOf(startColor, endColor) + this.gradientPositions = null + } + + constructor(gradientColors: IntArray) { + this.type = Type.LINEAR_GRADIENT + this.gradientColors = gradientColors + this.gradientPositions = null + } + + constructor(gradientColors: IntArray, gradientPositions: FloatArray) { + this.type = Type.LINEAR_GRADIENT + this.gradientColors = gradientColors + this.gradientPositions = gradientPositions + } + + constructor(drawable: Drawable) { + this.type = Type.DRAWABLE + this.mDrawable = drawable + this.gradientColors = null + this.gradientPositions = null + } + + var color: Int? + get() = mColor + set(color) { + this.mColor = color + calculateFinalColor() + } + + fun setGradientColors(startColor: Int, endColor: Int) { + this.gradientColors = intArrayOf(startColor, endColor) + } + + var alpha: Int + get() = mAlpha + set(alpha) { + this.mAlpha = alpha + calculateFinalColor() + } + + private fun calculateFinalColor() { + if (mColor == null) { + mFinalColor = null + } else { + val alpha = floor(((mColor!! shr 24) / 255.0) * (mAlpha / 255.0) * 255.0).toInt() + mFinalColor = (alpha shl 24) or (mColor!! and 0xffffff) + } + } + + fun fillRect( + c: Canvas, paint: Paint, + left: Float, top: Float, right: Float, bottom: Float, + gradientDirection: Direction?, mRoundedBarRadius: Float + ) { + when (this.type) { + Type.EMPTY -> return + + Type.COLOR -> { if (mFinalColor == null) { - return; + return } - if (isClipPathSupported()) { - int save = c.save(); + if (this.isClipPathSupported) { + val save = c.save() - c.clipRect(left, top, right, bottom); - c.drawColor(mFinalColor); + c.clipRect(left, top, right, bottom) + c.drawColor(mFinalColor!!) - c.restoreToCount(save); - } else { - // save - Paint.Style previous = paint.getStyle(); - int previousColor = paint.getColor(); + c.restoreToCount(save) + } else { + // save + val previous = paint.getStyle() + val previousColor = paint.getColor() - // set - paint.setStyle(Paint.Style.FILL); - paint.setColor(mFinalColor); + // set + paint.setStyle(Paint.Style.FILL) + paint.setColor(mFinalColor!!) - c.drawRoundRect(new RectF(left, top, right, bottom), mRoundedBarRadius, mRoundedBarRadius, paint); + c.drawRoundRect(RectF(left, top, right, bottom), mRoundedBarRadius, mRoundedBarRadius, paint) - // restore - paint.setColor(previousColor); - paint.setStyle(previous); - } - } - break; + // restore + paint.setColor(previousColor) + paint.setStyle(previous) + } + } - case LINEAR_GRADIENT: { - if (mGradientColors == null) { - return; + Type.LINEAR_GRADIENT -> { + if (this.gradientColors == null) { + return } - LinearGradient gradient = new LinearGradient( - (int) (gradientDirection == Direction.RIGHT - ? right - : gradientDirection == Direction.LEFT - ? left - : left), - (int) (gradientDirection == Direction.UP - ? bottom - : gradientDirection == Direction.DOWN - ? top - : top), - (int) (gradientDirection == Direction.RIGHT - ? left - : gradientDirection == Direction.LEFT - ? right - : left), - (int) (gradientDirection == Direction.UP - ? top - : gradientDirection == Direction.DOWN - ? bottom - : top), - mGradientColors, - mGradientPositions, - android.graphics.Shader.TileMode.MIRROR); - - paint.setShader(gradient); - - c.drawRoundRect(new RectF(left, top, right, bottom), mRoundedBarRadius, mRoundedBarRadius, paint); - } - break; - - case DRAWABLE: { + val gradient = LinearGradient( + (if (gradientDirection == Direction.RIGHT) + right + else + if (gradientDirection == Direction.LEFT) + left + else + left).toInt().toFloat(), + (if (gradientDirection == Direction.UP) + bottom + else + if (gradientDirection == Direction.DOWN) + top + else + top).toInt().toFloat(), + (if (gradientDirection == Direction.RIGHT) + left + else + if (gradientDirection == Direction.LEFT) + right + else + left).toInt().toFloat(), + (if (gradientDirection == Direction.UP) + top + else + if (gradientDirection == Direction.DOWN) + bottom + else + top).toInt().toFloat(), + this.gradientColors!!, + this.gradientPositions, + Shader.TileMode.MIRROR + ) + + paint.setShader(gradient) + + c.drawRoundRect(RectF(left, top, right, bottom), mRoundedBarRadius, mRoundedBarRadius, paint) + } + + Type.DRAWABLE -> { if (mDrawable == null) { - return; + return } - mDrawable.setBounds((int) left, (int) top, (int) right, (int) bottom); - mDrawable.draw(c); - } - break; - } - } + mDrawable!!.setBounds(left.toInt(), top.toInt(), right.toInt(), bottom.toInt()) + mDrawable!!.draw(c) + } + } + } - public void fillPath(Canvas c, Path path, Paint paint, - @Nullable RectF clipRect) { - switch (mType) { - case EMPTY: - return; + fun fillPath( + c: Canvas, path: Path, paint: Paint, + clipRect: RectF? + ) { + when (this.type) { + Type.EMPTY -> return - case COLOR: { + Type.COLOR -> { if (mFinalColor == null) { - return; + return } - if (clipRect != null && isClipPathSupported()) { - int save = c.save(); + if (clipRect != null && this.isClipPathSupported) { + val save = c.save() - c.clipPath(path); - c.drawColor(mFinalColor); + c.clipPath(path) + c.drawColor(mFinalColor!!) - c.restoreToCount(save); - } else { - // save - Paint.Style previous = paint.getStyle(); - int previousColor = paint.getColor(); + c.restoreToCount(save) + } else { + // save + val previous = paint.getStyle() + val previousColor = paint.getColor() - // set - paint.setStyle(Paint.Style.FILL); - paint.setColor(mFinalColor); + // set + paint.setStyle(Paint.Style.FILL) + paint.setColor(mFinalColor!!) - c.drawPath(path, paint); + c.drawPath(path, paint) - // restore - paint.setColor(previousColor); - paint.setStyle(previous); - } - } - break; + // restore + paint.setColor(previousColor) + paint.setStyle(previous) + } + } - case LINEAR_GRADIENT: { - if (mGradientColors == null) { - return; + Type.LINEAR_GRADIENT -> { + if (this.gradientColors == null) { + return } - LinearGradient gradient = new LinearGradient( - 0, - 0, - c.getWidth(), - c.getHeight(), - mGradientColors, - mGradientPositions, - android.graphics.Shader.TileMode.MIRROR); + val gradient = LinearGradient( + 0f, + 0f, + c.getWidth().toFloat(), + c.getHeight().toFloat(), + this.gradientColors!!, + this.gradientPositions, + Shader.TileMode.MIRROR + ) - paint.setShader(gradient); + paint.setShader(gradient) - c.drawPath(path, paint); - } - break; + c.drawPath(path, paint) + } - case DRAWABLE: { + Type.DRAWABLE -> { if (mDrawable == null) { - return; + return } - ensureClipPathSupported(); - - int save = c.save(); - c.clipPath(path); - - mDrawable.setBounds( - clipRect == null ? 0 : (int) clipRect.left, - clipRect == null ? 0 : (int) clipRect.top, - clipRect == null ? c.getWidth() : (int) clipRect.right, - clipRect == null ? c.getHeight() : (int) clipRect.bottom); - mDrawable.draw(c); - - c.restoreToCount(save); - } - break; - } - } - - private boolean isClipPathSupported() { - return Utils.getSDKInt() >= 18; - } - - private void ensureClipPathSupported() { - if (Utils.getSDKInt() < 18) { - throw new RuntimeException("Fill-drawables not (yet) supported below API level 18, " + - "this code was run on API level " + Utils.getSDKInt() + "."); - } - } + ensureClipPathSupported() + + val save = c.save() + c.clipPath(path) + + mDrawable!!.setBounds( + if (clipRect == null) 0 else clipRect.left.toInt(), + if (clipRect == null) 0 else clipRect.top.toInt(), + if (clipRect == null) c.getWidth() else clipRect.right.toInt(), + if (clipRect == null) c.getHeight() else clipRect.bottom.toInt() + ) + mDrawable!!.draw(c) + + c.restoreToCount(save) + } + } + } + + private val isClipPathSupported: Boolean + get() = Utils.sDKInt >= 18 + + private fun ensureClipPathSupported() { + if (Utils.sDKInt < 18) { + throw RuntimeException( + "Fill-drawables not (yet) supported below API level 18, " + + "this code was run on API level " + Utils.sDKInt + "." + ) + } + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt index 5a415b0477..3a79c48984 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt @@ -1,29 +1,6 @@ - -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.utils /** * ViewPortHandler for HorizontalBarChart. */ -public class HorizontalViewPortHandler extends ViewPortHandler { - - -// @Override -// public void setMinimumScaleX(float xScale) { -// setMinimumScaleY(xScale); -// } -// -// @Override -// public void setMinimumScaleY(float yScale) { -// setMinimumScaleX(yScale); -// } -// -// @Override -// public void setMinMaxScaleX(float minScaleX, float maxScaleX) { -// setMinMaxScaleY(minScaleX, maxScaleX); -// } -// -// @Override -// public void setMinMaxScaleY(float minScaleY, float maxScaleY) { -// setMinMaxScaleX(minScaleY, maxScaleY); -// } -} +class HorizontalViewPortHandler : ViewPortHandler() diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt index f6220a72e9..7a0cd82c78 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt @@ -1,53 +1,44 @@ +package com.github.mikephil.charting.utils -package com.github.mikephil.charting.utils; - -import java.util.List; +import com.github.mikephil.charting.utils.ObjectPool.Poolable /** * Point encapsulating two double values. * * @author Philipp Jahoda */ -public class MPPointD extends ObjectPool.Poolable { - - private static ObjectPool pool; - - static { - pool = ObjectPool.create(64, new MPPointD(0,0)); - pool.setReplenishPercentage(0.5f); +class MPPointD private constructor(var x: Double, var y: Double) : Poolable() { + override fun instantiate(): MPPointD { + return MPPointD(0.0, 0.0) } - public static MPPointD getInstance(double x, double y){ - MPPointD result = pool.get(); - result.x = x; - result.y = y; - return result; - } - - public static void recycleInstance(MPPointD instance){ - pool.recycle(instance); + /** + * returns a string representation of the object + */ + override fun toString(): String { + return "MPPointD, x: $x, y: $y" } - public static void recycleInstances(List instances){ - pool.recycle(instances); - } + companion object { + private val pool: ObjectPool = ObjectPool.Companion.create(64, MPPointD(0.0, 0.0)) - public double x; - public double y; + init { + pool.setReplenishPercentage(0.5f) + } - protected ObjectPool.Poolable instantiate(){ - return new MPPointD(0,0); - } + fun getInstance(x: Double, y: Double): MPPointD { + val result: MPPointD = pool.get() + result.x = x + result.y = y + return result + } - private MPPointD(double x, double y) { - this.x = x; - this.y = y; - } + fun recycleInstance(instance: MPPointD) { + pool.recycle(instance) + } - /** - * returns a string representation of the object - */ - public String toString() { - return "MPPointD, x: " + x + ", y: " + y; + fun recycleInstances(instances: MutableList) { + pool.recycle(instances) + } } } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt index fb5a00f508..b45b45f8db 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt @@ -1,99 +1,88 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.utils -import android.os.Parcel; -import android.os.Parcelable; - -import java.util.List; +import android.os.Parcel +import android.os.Parcelable +import com.github.mikephil.charting.utils.ObjectPool.Poolable /** * Created by Tony Patino on 6/24/16. */ -public class MPPointF extends ObjectPool.Poolable { - - private static ObjectPool pool; +class MPPointF : Poolable { + var x: Float = 0f + var y: Float = 0f - public float x; - public float y; + constructor() - static { - pool = ObjectPool.create(32, new MPPointF(0,0)); - pool.setReplenishPercentage(0.5f); + constructor(x: Float, y: Float) { + this.x = x + this.y = y } - public MPPointF() { + /** + * Set the point's coordinates from the data stored in the specified + * parcel. To write a point to a parcel, call writeToParcel(). + * Provided to support older Android devices. + * + * @param in The parcel to read the point's coordinates from + */ + fun my_readFromParcel(`in`: Parcel) { + x = `in`.readFloat() + y = `in`.readFloat() } - public MPPointF(float x, float y) { - this.x = x; - this.y = y; + override fun instantiate(): MPPointF { + return MPPointF(0f, 0f) } - public static MPPointF getInstance(float x, float y) { - MPPointF result = pool.get(); - result.x = x; - result.y = y; - return result; - } + companion object { + private var pool: ObjectPool = ObjectPool.Companion.create(32, MPPointF(0f, 0f)) - public static MPPointF getInstance() { - return pool.get(); - } + init { + pool.setReplenishPercentage(0.5f) + } - public static MPPointF getInstance(MPPointF copy) { - MPPointF result = pool.get(); - result.x = copy.x; - result.y = copy.y; - return result; - } + fun getInstance(x: Float, y: Float): MPPointF { + val result: MPPointF = pool.get() + result.x = x + result.y = y + return result + } - public static void recycleInstance(MPPointF instance){ - pool.recycle(instance); - } + val instance: MPPointF + get() = pool.get() - public static void recycleInstances(List instances){ - pool.recycle(instances); - } - - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - /** - * Return a new point from the data in the specified parcel. - */ - public MPPointF createFromParcel(Parcel in) { - MPPointF r = new MPPointF(0,0); - r.my_readFromParcel(in); - return r; + fun getInstance(copy: MPPointF): MPPointF { + val result: MPPointF = pool.get() + result.x = copy.x + result.y = copy.y + return result } - /** - * Return an array of rectangles of the specified size. - */ - public MPPointF[] newArray(int size) { - return new MPPointF[size]; + @JvmStatic + fun recycleInstance(instance: MPPointF) { + pool.recycle(instance) } - }; - /** - * Set the point's coordinates from the data stored in the specified - * parcel. To write a point to a parcel, call writeToParcel(). - * Provided to support older Android devices. - * - * @param in The parcel to read the point's coordinates from - */ - public void my_readFromParcel(Parcel in) { - x = in.readFloat(); - y = in.readFloat(); - } - - public float getX(){ - return this.x; - } - - public float getY(){ - return this.y; - } + fun recycleInstances(instances: MutableList) { + pool.recycle(instances) + } - @Override - protected ObjectPool.Poolable instantiate() { - return new MPPointF(0,0); + val CREATOR: Parcelable.Creator = object : Parcelable.Creator { + /** + * Return a new point from the data in the specified parcel. + */ + override fun createFromParcel(`in`: Parcel): MPPointF { + val r = MPPointF(0f, 0f) + r.my_readFromParcel(`in`) + return r + } + + /** + * Return an array of rectangles of the specified size. + */ + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } } } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt index 15e7fc0b88..19961b51d2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt @@ -1,66 +1,43 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.utils -import java.util.List; +import com.github.mikephil.charting.utils.ObjectPool.Poolable /** * An object pool for recycling of object instances extending Poolable. * * * Cost/Benefit : - * Cost - The pool can only contain objects extending Poolable. - * Benefit - The pool can very quickly determine if an object is elligable for storage without iteration. - * Benefit - The pool can also know if an instance of Poolable is already stored in a different pool instance. - * Benefit - The pool can grow as needed, if it is empty - * Cost - However, refilling the pool when it is empty might incur a time cost with sufficiently large capacity. Set the replenishPercentage to a lower number if this is a concern. + * Cost - The pool can only contain objects extending Poolable. + * Benefit - The pool can very quickly determine if an object is elligable for storage without iteration. + * Benefit - The pool can also know if an instance of Poolable is already stored in a different pool instance. + * Benefit - The pool can grow as needed, if it is empty + * Cost - However, refilling the pool when it is empty might incur a time cost with sufficiently large capacity. Set the replenishPercentage to a lower number if this is a concern. * * Created by Tony Patino on 6/20/16. */ -public class ObjectPool { - - private static int ids = 0; - - private int poolId; - private int desiredCapacity; - private Object[] objects; - private int objectsPointer; - private T modelObject; - private float replenishPercentage; - - +class ObjectPool?> private constructor(withCapacity: Int, `object`: T?) { /** * Returns the id of the given pool instance. * * @return an integer ID belonging to this pool instance. */ - public int getPoolId(){ - return poolId; - } - - /** - * Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object. - * - * @param withCapacity A positive integer value. - * @param object An instance of the object that the pool should recycle. - * @return - */ - public static synchronized ObjectPool create(int withCapacity, Poolable object){ - ObjectPool result = new ObjectPool(withCapacity, object); - result.poolId = ids; - ids++; - - return result; - } - - private ObjectPool(int withCapacity, T object){ - if(withCapacity <= 0){ - throw new IllegalArgumentException("Object Pool must be instantiated with a capacity greater than 0!"); - } - this.desiredCapacity = withCapacity; - this.objects = new Object[this.desiredCapacity]; - this.objectsPointer = 0; - this.modelObject = object; - this.replenishPercentage = 1.0f; - this.refillPool(); + var poolId: Int = 0 + private set + private var desiredCapacity: Int + private var objects: Array + private var objectsPointer: Int + private val modelObject: T? + private var replenishPercentage: Float + + + init { + require(withCapacity > 0) { "Object Pool must be instantiated with a capacity greater than 0!" } + this.desiredCapacity = withCapacity + this.objects = arrayOfNulls(this.desiredCapacity) + this.objectsPointer = 0 + this.modelObject = `object` + this.replenishPercentage = 1.0f + this.refillPool() } /** @@ -69,38 +46,33 @@ public class ObjectPool { * * @param percentage a value between 0 and 1, representing the percentage of the pool to replenish. */ - public void setReplenishPercentage(float percentage){ - float p = percentage; - if(p > 1){ - p = 1; - } - else if(p < 0f){ - p = 0f; + fun setReplenishPercentage(percentage: Float) { + var p = percentage + if (p > 1) { + p = 1f + } else if (p < 0f) { + p = 0f } - this.replenishPercentage = p; - } - - public float getReplenishPercentage(){ - return replenishPercentage; + this.replenishPercentage = p } - private void refillPool(){ - this.refillPool(this.replenishPercentage); + fun getReplenishPercentage(): Float { + return replenishPercentage } - private void refillPool(float percentage){ - int portionOfCapacity = (int) (desiredCapacity * percentage); + private fun refillPool(percentage: Float = this.replenishPercentage) { + var portionOfCapacity = (desiredCapacity * percentage).toInt() - if(portionOfCapacity < 1){ - portionOfCapacity = 1; - }else if(portionOfCapacity > desiredCapacity){ - portionOfCapacity = desiredCapacity; + if (portionOfCapacity < 1) { + portionOfCapacity = 1 + } else if (portionOfCapacity > desiredCapacity) { + portionOfCapacity = desiredCapacity } - for(int i = 0 ; i < portionOfCapacity ; i++){ - this.objects[i] = modelObject.instantiate(); + for (i in 0.. { * * @return An instance of Poolable object T */ - public synchronized T get(){ - - if(this.objectsPointer == -1 && this.replenishPercentage > 0.0f){ - this.refillPool(); + @Synchronized + fun get(): T { + if (this.objectsPointer == -1 && this.replenishPercentage > 0.0f) { + this.refillPool() } - T result = (T)objects[this.objectsPointer]; - objects[this.objectsPointer] = null; - result.currentOwnerId = Poolable.NO_OWNER; - this.objectsPointer--; + val result = objects[this.objectsPointer] + objects[this.objectsPointer] = null + result!!.currentOwnerId = Poolable.Companion.NO_OWNER + this.objectsPointer-- - return result; + return result } /** @@ -130,23 +102,20 @@ public class ObjectPool { * * @param object An object of type T to recycle */ - public synchronized void recycle(T object){ - if(object.currentOwnerId != Poolable.NO_OWNER){ - if(object.currentOwnerId == this.poolId){ - throw new IllegalArgumentException("The object passed is already stored in this pool!"); - }else { - throw new IllegalArgumentException("The object to recycle already belongs to poolId " + object.currentOwnerId + ". Object cannot belong to two different pool instances simultaneously!"); - } + @Synchronized + fun recycle(`object`: T?) { + if (`object`!!.currentOwnerId != Poolable.Companion.NO_OWNER) { + require(`object`.currentOwnerId != this.poolId) { "The object passed is already stored in this pool!" } + throw IllegalArgumentException("The object to recycle already belongs to poolId " + `object`.currentOwnerId + ". Object cannot belong to two different pool instances simultaneously!") } - this.objectsPointer++; - if(this.objectsPointer >= objects.length){ - this.resizePool(); + this.objectsPointer++ + if (this.objectsPointer >= objects.size) { + this.resizePool() } - object.currentOwnerId = this.poolId; - objects[this.objectsPointer] = object; - + `object`.currentOwnerId = this.poolId + objects[this.objectsPointer] = `object` } /** @@ -155,65 +124,82 @@ public class ObjectPool { * * @param objects A list of objects of type T to recycle */ - public synchronized void recycle(List objects){ - while(objects.size() + this.objectsPointer + 1 > this.desiredCapacity){ - this.resizePool(); + @Synchronized + fun recycle(objects: MutableList) { + while (objects.size + this.objectsPointer + 1 > this.desiredCapacity) { + this.resizePool() } - final int objectsListSize = objects.size(); + val objectsListSize = objects.size // Not relying on recycle(T object) because this is more performant. - for(int i = 0 ; i < objectsListSize ; i++){ - T object = objects.get(i); - if(object.currentOwnerId != Poolable.NO_OWNER){ - if(object.currentOwnerId == this.poolId){ - throw new IllegalArgumentException("The object passed is already stored in this pool!"); - }else { - throw new IllegalArgumentException("The object to recycle already belongs to poolId " + object.currentOwnerId + ". Object cannot belong to two different pool instances simultaneously!"); - } + for (i in 0.. = arrayOfNulls(this.desiredCapacity) + for (i in 0..?> { + var currentOwnerId: Int = NO_OWNER - public static int NO_OWNER = -1; - int currentOwnerId = NO_OWNER; + abstract fun instantiate(): T? - protected abstract Poolable instantiate(); + companion object { + var NO_OWNER: Int = -1 + } + } + companion object { + private var ids = 0 + + /** + * Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object. + * + * @param withCapacity A positive integer value. + * @param object An instance of the object that the pool should recycle. + * @return + */ + @Synchronized + fun > create(withCapacity: Int, `object`: T): ObjectPool { + val result: ObjectPool = ObjectPool(withCapacity, `object`) + result.poolId = ids + ids++ + + return result + } } } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt index 8a4371802a..d780015fa9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt @@ -1,18 +1,16 @@ - -package com.github.mikephil.charting.utils; - -import android.graphics.Matrix; -import android.graphics.Path; -import android.graphics.RectF; - -import com.github.mikephil.charting.data.CandleEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet; -import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; - -import java.util.List; +package com.github.mikephil.charting.utils + +import android.graphics.Matrix +import android.graphics.Path +import android.graphics.RectF +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet +import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import kotlin.Boolean +import kotlin.FloatArray +import kotlin.Int /** * Transformer class that contains all matrices and is responsible for @@ -20,23 +18,18 @@ import java.util.List; * * @author Philipp Jahoda */ -public class Transformer { - +open class Transformer(protected var mViewPortHandler: ViewPortHandler) { /** * matrix to map the values to the screen pixels */ - protected Matrix mMatrixValueToPx = new Matrix(); + var valueMatrix: Matrix = Matrix() + protected set /** * matrix for handling the different offsets of the chart */ - protected Matrix mMatrixOffset = new Matrix(); - - protected ViewPortHandler mViewPortHandler; - - public Transformer(ViewPortHandler viewPortHandler) { - this.mViewPortHandler = viewPortHandler; - } + var offsetMatrix: Matrix = Matrix() + protected set /** * Prepares the matrix that transforms values to pixels. Calculates the @@ -47,22 +40,21 @@ public class Transformer { * @param deltaY * @param yChartMin */ - public void prepareMatrixValuePx(float xChartMin, float deltaX, float deltaY, float yChartMin) { - - float scaleX = mViewPortHandler.contentWidth() / deltaX; - float scaleY = mViewPortHandler.contentHeight() / deltaY; + fun prepareMatrixValuePx(xChartMin: Float, deltaX: Float, deltaY: Float, yChartMin: Float) { + var scaleX = mViewPortHandler.contentWidth() / deltaX + var scaleY = mViewPortHandler.contentHeight() / deltaY - if (Float.isInfinite(scaleX)) { - scaleX = 0; + if (scaleX.isInfinite()) { + scaleX = 0f } - if (Float.isInfinite(scaleY)) { - scaleY = 0; + if (scaleY.isInfinite()) { + scaleY = 0f } // setup all matrices - mMatrixValueToPx.reset(); - mMatrixValueToPx.postTranslate(-xChartMin, -yChartMin); - mMatrixValueToPx.postScale(scaleX, -scaleY); + valueMatrix.reset() + valueMatrix.postTranslate(-xChartMin, -yChartMin) + valueMatrix.postScale(scaleX, -scaleY) } /** @@ -70,23 +62,22 @@ public class Transformer { * * @param inverted */ - public void prepareMatrixOffset(boolean inverted) { - - mMatrixOffset.reset(); + open fun prepareMatrixOffset(inverted: Boolean) { + offsetMatrix.reset() // offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom); - - if (!inverted) - mMatrixOffset.postTranslate(mViewPortHandler.offsetLeft(), - mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); + if (!inverted) offsetMatrix.postTranslate( + mViewPortHandler.offsetLeft(), + mViewPortHandler.chartHeight - mViewPortHandler.offsetBottom() + ) else { - mMatrixOffset - .setTranslate(mViewPortHandler.offsetLeft(), -mViewPortHandler.offsetTop()); - mMatrixOffset.postScale(1.0f, -1.0f); + offsetMatrix + .setTranslate(mViewPortHandler.offsetLeft(), -mViewPortHandler.offsetTop()) + offsetMatrix.postScale(1.0f, -1.0f) } } - protected float[] valuePointsForGenerateTransformedValuesScatter = new float[1]; + protected var valuePointsForGenerateTransformedValuesScatter: FloatArray = FloatArray(1) /** * Transforms an List of Entry into a float array containing the x and @@ -95,35 +86,37 @@ public class Transformer { * @param data * @return */ - public float[] generateTransformedValuesScatter(IScatterDataSet data, float phaseX, - float phaseY, int from, int to) { - - final int count = (int) ((to - from) * phaseX + 1) * 2; - - if (valuePointsForGenerateTransformedValuesScatter.length != count) { - valuePointsForGenerateTransformedValuesScatter = new float[count]; + fun generateTransformedValuesScatter( + data: IScatterDataSet, phaseX: kotlin.Float, + phaseY: kotlin.Float, from: Int, to: Int + ): FloatArray { + val count = ((to - from) * phaseX + 1).toInt() * 2 + + if (valuePointsForGenerateTransformedValuesScatter.size != count) { + valuePointsForGenerateTransformedValuesScatter = FloatArray(count) } - float[] valuePoints = valuePointsForGenerateTransformedValuesScatter; + val valuePoints = valuePointsForGenerateTransformedValuesScatter - for (int j = 0; j < count; j += 2) { - - Entry e = data.getEntryForIndex(j / 2 + from); + var j = 0 + while (j < count) { + val e = data.getEntryForIndex(j / 2 + from) if (e != null) { - valuePoints[j] = e.getX(); - valuePoints[j + 1] = e.getY() * phaseY; + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY } else { - valuePoints[j] = 0; - valuePoints[j + 1] = 0; + valuePoints[j] = 0f + valuePoints[j + 1] = 0f } + j += 2 } - getValueToPixelMatrix().mapPoints(valuePoints); + this.valueToPixelMatrix.mapPoints(valuePoints) - return valuePoints; + return valuePoints } - protected float[] valuePointsForGenerateTransformedValuesBubble = new float[1]; + protected var valuePointsForGenerateTransformedValuesBubble: FloatArray = FloatArray(1) /** * Transforms an List of Entry into a float array containing the x and @@ -132,34 +125,34 @@ public class Transformer { * @param data * @return */ - public float[] generateTransformedValuesBubble(IBubbleDataSet data, float phaseY, int from, int to) { - - final int count = (to - from + 1) * 2; // (int) Math.ceil((to - from) * phaseX) * 2; + fun generateTransformedValuesBubble(data: IBubbleDataSet, phaseY: kotlin.Float, from: Int, to: Int): FloatArray { + val count = (to - from + 1) * 2 // (int) Math.ceil((to - from) * phaseX) * 2; - if (valuePointsForGenerateTransformedValuesBubble.length != count) { - valuePointsForGenerateTransformedValuesBubble = new float[count]; + if (valuePointsForGenerateTransformedValuesBubble.size != count) { + valuePointsForGenerateTransformedValuesBubble = FloatArray(count) } - float[] valuePoints = valuePointsForGenerateTransformedValuesBubble; - - for (int j = 0; j < count; j += 2) { + val valuePoints = valuePointsForGenerateTransformedValuesBubble - Entry e = data.getEntryForIndex(j / 2 + from); + var j = 0 + while (j < count) { + val e: Entry = data.getEntryForIndex(j / 2 + from) if (e != null) { - valuePoints[j] = e.getX(); - valuePoints[j + 1] = e.getY() * phaseY; + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY } else { - valuePoints[j] = 0; - valuePoints[j + 1] = 0; + valuePoints[j] = 0f + valuePoints[j + 1] = 0f } + j += 2 } - getValueToPixelMatrix().mapPoints(valuePoints); + this.valueToPixelMatrix.mapPoints(valuePoints) - return valuePoints; + return valuePoints } - protected float[] valuePointsForGenerateTransformedValuesLine = new float[1]; + protected var valuePointsForGenerateTransformedValuesLine: FloatArray = FloatArray(1) /** * Transforms an List of Entry into a float array containing the x and @@ -168,38 +161,39 @@ public class Transformer { * @param data * @return */ - public float[] generateTransformedValuesLine(ILineDataSet data, - float phaseX, float phaseY, - int min, int max) { - - int count = ((int) ((max - min) * phaseX) + 1) * 2; - if (count < 0) - count = 0; - - if (valuePointsForGenerateTransformedValuesLine.length != count) { - valuePointsForGenerateTransformedValuesLine = new float[count]; + fun generateTransformedValuesLine( + data: ILineDataSet, + phaseX: kotlin.Float, phaseY: kotlin.Float, + min: Int, max: Int + ): FloatArray { + var count = (((max - min) * phaseX).toInt() + 1) * 2 + if (count < 0) count = 0 + + if (valuePointsForGenerateTransformedValuesLine.size != count) { + valuePointsForGenerateTransformedValuesLine = FloatArray(count) } - float[] valuePoints = valuePointsForGenerateTransformedValuesLine; - - for (int j = 0; j < count; j += 2) { + val valuePoints = valuePointsForGenerateTransformedValuesLine - Entry e = data.getEntryForIndex(j / 2 + min); + var j = 0 + while (j < count) { + val e = data.getEntryForIndex(j / 2 + min) if (e != null) { - valuePoints[j] = e.getX(); - valuePoints[j + 1] = e.getY() * phaseY; + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY } else { - valuePoints[j] = 0; - valuePoints[j + 1] = 0; + valuePoints[j] = 0f + valuePoints[j + 1] = 0f } + j += 2 } - getValueToPixelMatrix().mapPoints(valuePoints); + this.valueToPixelMatrix.mapPoints(valuePoints) - return valuePoints; + return valuePoints } - protected float[] valuePointsForGenerateTransformedValuesCandle = new float[1]; + protected var valuePointsForGenerateTransformedValuesCandle: FloatArray = FloatArray(1) /** * Transforms an List of Entry into a float array containing the x and @@ -208,32 +202,29 @@ public class Transformer { * @param data * @return */ - public float[] generateTransformedValuesCandle(ICandleDataSet data, - float phaseX, float phaseY, int from, int to) { - - final int count = (int) ((to - from) * phaseX + 1) * 2; - - if (valuePointsForGenerateTransformedValuesCandle.length != count) { - valuePointsForGenerateTransformedValuesCandle = new float[count]; + fun generateTransformedValuesCandle( + data: ICandleDataSet, + phaseX: kotlin.Float, phaseY: kotlin.Float, from: Int, to: Int + ): FloatArray { + val count = ((to - from) * phaseX + 1).toInt() * 2 + + if (valuePointsForGenerateTransformedValuesCandle.size != count) { + valuePointsForGenerateTransformedValuesCandle = FloatArray(count) } - float[] valuePoints = valuePointsForGenerateTransformedValuesCandle; - - for (int j = 0; j < count; j += 2) { + val valuePoints = valuePointsForGenerateTransformedValuesCandle - CandleEntry e = data.getEntryForIndex(j / 2 + from); + var j = 0 + while (j < count) { + val e = data.getEntryForIndex(j / 2 + from) - if (e != null) { - valuePoints[j] = e.getX(); - valuePoints[j + 1] = e.getHigh() * phaseY; - } else { - valuePoints[j] = 0; - valuePoints[j + 1] = 0; - } + valuePoints[j] = e.x + valuePoints[j + 1] = e.high * phaseY + j += 2 } - getValueToPixelMatrix().mapPoints(valuePoints); + this.valueToPixelMatrix.mapPoints(valuePoints) - return valuePoints; + return valuePoints } /** @@ -242,11 +233,10 @@ public class Transformer { * * @param path */ - public void pathValueToPixel(Path path) { - - path.transform(mMatrixValueToPx); - path.transform(mViewPortHandler.getMatrixTouch()); - path.transform(mMatrixOffset); + fun pathValueToPixel(path: Path) { + path.transform(this.valueMatrix) + path.transform(mViewPortHandler.matrixTouch) + path.transform(this.offsetMatrix) } /** @@ -254,10 +244,9 @@ public class Transformer { * * @param paths */ - public void pathValuesToPixel(List paths) { - - for (int i = 0; i < paths.size(); i++) { - pathValueToPixel(paths.get(i)); + fun pathValuesToPixel(paths: MutableList) { + for (i in paths.indices) { + pathValueToPixel(paths.get(i)!!) } } @@ -267,11 +256,10 @@ public class Transformer { * * @param pts */ - public void pointValuesToPixel(float[] pts) { - - mMatrixValueToPx.mapPoints(pts); - mViewPortHandler.getMatrixTouch().mapPoints(pts); - mMatrixOffset.mapPoints(pts); + fun pointValuesToPixel(pts: FloatArray?) { + valueMatrix.mapPoints(pts) + mViewPortHandler.matrixTouch.mapPoints(pts) + offsetMatrix.mapPoints(pts) } /** @@ -279,11 +267,10 @@ public class Transformer { * * @param r */ - public void rectValueToPixel(RectF r) { - - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); + fun rectValueToPixel(r: RectF?) { + valueMatrix.mapRect(r) + mViewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) } /** @@ -292,26 +279,26 @@ public class Transformer { * @param r * @param phaseY */ - public void rectToPixelPhase(RectF r, float phaseY) { - + fun rectToPixelPhase(r: RectF, phaseY: kotlin.Float) { // multiply the height of the rect with the phase - r.top *= phaseY; - r.bottom *= phaseY; - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); - } + r.top *= phaseY + r.bottom *= phaseY - public void rectToPixelPhaseHorizontal(RectF r, float phaseY) { + valueMatrix.mapRect(r) + mViewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) + } + fun rectToPixelPhaseHorizontal(r: RectF, phaseY: kotlin.Float) { // multiply the height of the rect with the phase - r.left *= phaseY; - r.right *= phaseY; - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); + r.left *= phaseY + r.right *= phaseY + + valueMatrix.mapRect(r) + mViewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) } /** @@ -319,11 +306,10 @@ public class Transformer { * * @param r */ - public void rectValueToPixelHorizontal(RectF r) { - - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); + fun rectValueToPixelHorizontal(r: RectF?) { + valueMatrix.mapRect(r) + mViewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) } /** @@ -332,15 +318,15 @@ public class Transformer { * @param r * @param phaseY */ - public void rectValueToPixelHorizontal(RectF r, float phaseY) { - + fun rectValueToPixelHorizontal(r: RectF, phaseY: kotlin.Float) { // multiply the height of the rect with the phase - r.left *= phaseY; - r.right *= phaseY; - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); + r.left *= phaseY + r.right *= phaseY + + valueMatrix.mapRect(r) + mViewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) } /** @@ -348,15 +334,13 @@ public class Transformer { * * @param rects */ - public void rectValuesToPixel(List rects) { - - Matrix m = getValueToPixelMatrix(); + fun rectValuesToPixel(rects: MutableList) { + val m = this.valueToPixelMatrix - for (int i = 0; i < rects.size(); i++) - m.mapRect(rects.get(i)); + for (i in rects.indices) m.mapRect(rects.get(i)) } - protected Matrix mPixelToValueMatrixBuffer = new Matrix(); + protected var mPixelToValueMatrixBuffer: Matrix = Matrix() /** * Transforms the given array of touch positions (pixels) (x, y, x, y, ...) @@ -364,26 +348,25 @@ public class Transformer { * * @param pixels */ - public void pixelsToValue(float[] pixels) { - - Matrix tmp = mPixelToValueMatrixBuffer; - tmp.reset(); + fun pixelsToValue(pixels: FloatArray?) { + val tmp = mPixelToValueMatrixBuffer + tmp.reset() // invert all matrixes to convert back to the original value - mMatrixOffset.invert(tmp); - tmp.mapPoints(pixels); + offsetMatrix.invert(tmp) + tmp.mapPoints(pixels) - mViewPortHandler.getMatrixTouch().invert(tmp); - tmp.mapPoints(pixels); + mViewPortHandler.matrixTouch.invert(tmp) + tmp.mapPoints(pixels) - mMatrixValueToPx.invert(tmp); - tmp.mapPoints(pixels); + valueMatrix.invert(tmp) + tmp.mapPoints(pixels) } /** * buffer for performance */ - float[] ptsBuffer = new float[2]; + var ptsBuffer: FloatArray = FloatArray(2) /** * Returns a recyclable MPPointD instance. @@ -396,22 +379,20 @@ public class Transformer { * @param y * @return */ - public MPPointD getValuesByTouchPoint(float x, float y) { - - MPPointD result = MPPointD.getInstance(0, 0); - getValuesByTouchPoint(x, y, result); - return result; + fun getValuesByTouchPoint(x: kotlin.Float, y: kotlin.Float): MPPointD { + val result: MPPointD = MPPointD.Companion.getInstance(0.0, 0.0) + getValuesByTouchPoint(x, y, result) + return result } - public void getValuesByTouchPoint(float x, float y, MPPointD outputPoint) { - - ptsBuffer[0] = x; - ptsBuffer[1] = y; + fun getValuesByTouchPoint(x: kotlin.Float, y: kotlin.Float, outputPoint: MPPointD) { + ptsBuffer[0] = x + ptsBuffer[1] = y - pixelsToValue(ptsBuffer); + pixelsToValue(ptsBuffer) - outputPoint.x = ptsBuffer[0]; - outputPoint.y = ptsBuffer[1]; + outputPoint.x = ptsBuffer[0].toDouble() + outputPoint.y = ptsBuffer[1].toDouble() } /** @@ -422,40 +403,33 @@ public class Transformer { * @param y * @return */ - public MPPointD getPixelForValues(float x, float y) { - - ptsBuffer[0] = x; - ptsBuffer[1] = y; - - pointValuesToPixel(ptsBuffer); - - double xPx = ptsBuffer[0]; - double yPx = ptsBuffer[1]; + fun getPixelForValues(x: kotlin.Float, y: kotlin.Float): MPPointD { + ptsBuffer[0] = x + ptsBuffer[1] = y - return MPPointD.getInstance(xPx, yPx); - } + pointValuesToPixel(ptsBuffer) - public Matrix getValueMatrix() { - return mMatrixValueToPx; - } + val xPx = ptsBuffer[0].toDouble() + val yPx = ptsBuffer[1].toDouble() - public Matrix getOffsetMatrix() { - return mMatrixOffset; + return MPPointD.Companion.getInstance(xPx, yPx) } - private final Matrix mMBuffer1 = new Matrix(); + private val mMBuffer1 = Matrix() - public Matrix getValueToPixelMatrix() { - mMBuffer1.set(mMatrixValueToPx); - mMBuffer1.postConcat(mViewPortHandler.getMatrixTouch()); - mMBuffer1.postConcat(mMatrixOffset); - return mMBuffer1; - } + val valueToPixelMatrix: Matrix + get() { + mMBuffer1.set(this.valueMatrix) + mMBuffer1.postConcat(mViewPortHandler.matrixTouch) + mMBuffer1.postConcat(this.offsetMatrix) + return mMBuffer1 + } - private final Matrix mMBuffer2 = new Matrix(); + private val mMBuffer2 = Matrix() - public Matrix getPixelToValueMatrix() { - getValueToPixelMatrix().invert(mMBuffer2); - return mMBuffer2; - } + val pixelToValueMatrix: Matrix + get() { + this.valueToPixelMatrix.invert(mMBuffer2) + return mMBuffer2 + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt index 05fa82ae29..5802d25e8d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt @@ -1,37 +1,31 @@ - -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.utils /** * Transformer class for the HorizontalBarChart. - * + * * @author Philipp Jahoda */ -public class TransformerHorizontalBarChart extends Transformer { - - public TransformerHorizontalBarChart(ViewPortHandler viewPortHandler) { - super(viewPortHandler); - } - +class TransformerHorizontalBarChart(viewPortHandler: ViewPortHandler) : Transformer(viewPortHandler) { /** * Prepares the matrix that contains all offsets. - * + * * @param inverted */ - public void prepareMatrixOffset(boolean inverted) { - - mMatrixOffset.reset(); + override fun prepareMatrixOffset(inverted: Boolean) { + offsetMatrix.reset() // offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom); - - if (!inverted) - mMatrixOffset.postTranslate(mViewPortHandler.offsetLeft(), - mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); + if (!inverted) offsetMatrix.postTranslate( + mViewPortHandler.offsetLeft(), + mViewPortHandler.chartHeight - mViewPortHandler.offsetBottom() + ) else { - mMatrixOffset - .setTranslate( - -(mViewPortHandler.getChartWidth() - mViewPortHandler.offsetRight()), - mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); - mMatrixOffset.postScale(-1.0f, 1.0f); + offsetMatrix + .setTranslate( + -(mViewPortHandler.chartWidth - mViewPortHandler.offsetRight()), + mViewPortHandler.chartHeight - mViewPortHandler.offsetBottom() + ) + offsetMatrix.postScale(-1.0f, 1.0f) } // mMatrixOffset.set(offset); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt index 0f2296da45..ce52767eec 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt @@ -1,27 +1,49 @@ - -package com.github.mikephil.charting.utils; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.content.res.Resources; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Rect; -import android.graphics.drawable.Drawable; -import android.text.Layout; -import android.text.StaticLayout; -import android.text.TextPaint; -import android.util.DisplayMetrics; -import android.util.Log; -import android.view.MotionEvent; -import android.view.VelocityTracker; -import android.view.View; -import android.view.ViewConfiguration; - -import com.github.mikephil.charting.formatter.DefaultValueFormatter; -import com.github.mikephil.charting.formatter.IValueFormatter; - -import java.util.List; +package com.github.mikephil.charting.utils + +import android.annotation.SuppressLint +import android.content.Context +import android.content.res.Resources +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Paint.Align +import android.graphics.Rect +import android.graphics.drawable.Drawable +import android.os.Build +import android.text.Layout +import android.text.StaticLayout +import android.text.TextPaint +import android.util.DisplayMetrics +import android.util.Log +import android.view.MotionEvent +import android.view.VelocityTracker +import android.view.View +import android.view.ViewConfiguration +import com.github.mikephil.charting.formatter.DefaultValueFormatter +import com.github.mikephil.charting.formatter.IValueFormatter +import kotlin.Array +import kotlin.Boolean +import kotlin.Char +import kotlin.CharArray +import kotlin.Deprecated +import kotlin.Int +import kotlin.IntArray +import kotlin.String +import kotlin.Suppress +import kotlin.arrayOfNulls +import kotlin.code +import kotlin.collections.MutableList +import kotlin.collections.indices +import kotlin.intArrayOf +import kotlin.math.abs +import kotlin.math.ceil +import kotlin.math.cos +import kotlin.math.log10 +import kotlin.math.max +import kotlin.math.min +import kotlin.math.pow +import kotlin.math.roundToInt +import kotlin.math.sin +import androidx.core.graphics.withTranslation /** * Utilities class that has some helper methods. Needs to be initialized by @@ -31,43 +53,43 @@ import java.util.List; * * @author Philipp Jahoda */ -@SuppressWarnings("JavaDoc") -public abstract class Utils { - - private static DisplayMetrics mMetrics; - private static int mMinimumFlingVelocity = 50; - private static int mMaximumFlingVelocity = 8000; - public final static double DEG2RAD = (Math.PI / 180.0); - public final static float FDEG2RAD = ((float) Math.PI / 180.f); +object Utils { + private var mMetrics: DisplayMetrics? = null + var minimumFlingVelocity: Int = 50 + private set + var maximumFlingVelocity: Int = 8000 + private set + val DEG2RAD: Double = (Math.PI / 180.0) + val FDEG2RAD: Float = (Math.PI.toFloat() / 180f) - @SuppressWarnings("unused") - public final static double DOUBLE_EPSILON = Double.longBitsToDouble(1); + @Suppress("unused") + val DOUBLE_EPSILON: Double = java.lang.Double.longBitsToDouble(1) - @SuppressWarnings("unused") - public final static float FLOAT_EPSILON = Float.intBitsToFloat(1); + @Suppress("unused") + val FLOAT_EPSILON: Float = java.lang.Float.intBitsToFloat(1) /** * initialize method, called inside the Chart.init() method. */ - @SuppressWarnings("deprecation") - public static void init(Context context) { - + @Suppress("deprecation") + fun init(context: Context?) { if (context == null) { // noinspection deprecation - mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity(); + minimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity() // noinspection deprecation - mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity(); - - Log.e("MPChartLib-Utils" - , "Utils.init(...) PROVIDED CONTEXT OBJECT IS NULL"); + maximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity() + Log.e( + "MPChartLib-Utils", + "Utils.init(...) PROVIDED CONTEXT OBJECT IS NULL" + ) } else { - ViewConfiguration viewConfiguration = ViewConfiguration.get(context); - mMinimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity(); - mMaximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity(); + val viewConfiguration = ViewConfiguration.get(context) + minimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity() + maximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity() - Resources res = context.getResources(); - mMetrics = res.getDisplayMetrics(); + val res = context.getResources() + mMetrics = res.getDisplayMetrics() } } @@ -77,15 +99,14 @@ public abstract class Utils { * * @param res */ - @Deprecated - public static void init(Resources res) { - - mMetrics = res.getDisplayMetrics(); + @Deprecated("") + fun init(res: Resources) { + mMetrics = res.getDisplayMetrics() // noinspection deprecation - mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity(); + minimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity() // noinspection deprecation - mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity(); + maximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity() } /** @@ -93,22 +114,23 @@ public abstract class Utils { * density. NEEDS UTILS TO BE INITIALIZED BEFORE USAGE. * * @param dp A value in dp (density independent pixels) unit. Which we need - * to convert into pixels + * to convert into pixels * @return A float value to represent px equivalent to dp depending on * device density */ - public static float convertDpToPixel(float dp) { - + @JvmStatic + fun convertDpToPixel(dp: Float): Float { if (mMetrics == null) { - - Log.e("MPChartLib-Utils", - "Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before" + - " calling Utils.convertDpToPixel(...). Otherwise conversion does not " + - "take place."); - return dp; + Log.e( + "MPChartLib-Utils", + "Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before" + + " calling Utils.convertDpToPixel(...). Otherwise conversion does not " + + "take place." + ) + return dp } - return dp * mMetrics.density; + return dp * mMetrics!!.density } /** @@ -118,18 +140,18 @@ public abstract class Utils { * @param px A value in px (pixels) unit. Which we need to convert into db * @return A float value to represent dp equivalent to px value */ - public static float convertPixelsToDp(float px) { - + fun convertPixelsToDp(px: Float): Float { if (mMetrics == null) { - - Log.e("MPChartLib-Utils", - "Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before" + - " calling Utils.convertPixelsToDp(...). Otherwise conversion does not" + - " take place."); - return px; + Log.e( + "MPChartLib-Utils", + "Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before" + + " calling Utils.convertPixelsToDp(...). Otherwise conversion does not" + + " take place." + ) + return px } - return px / mMetrics.density; + return px / mMetrics!!.density } /** @@ -140,11 +162,12 @@ public abstract class Utils { * @param demoText * @return */ - public static int calcTextWidth(Paint paint, String demoText) { - return (int) paint.measureText(demoText); + fun calcTextWidth(paint: Paint, demoText: String?): Int { + return paint.measureText(demoText).toInt() } - private static final Rect mCalcTextHeightRect = new Rect(); + private val mCalcTextHeightRect = Rect() + /** * calculates the approximate height of a text, depending on a demo text * avoid repeated calls (e.g. inside drawing methods) @@ -153,32 +176,31 @@ public abstract class Utils { * @param demoText * @return */ - public static int calcTextHeight(Paint paint, String demoText) { - - Rect r = mCalcTextHeightRect; - r.set(0,0,0,0); - paint.getTextBounds(demoText, 0, demoText.length(), r); - return r.height(); + fun calcTextHeight(paint: Paint, demoText: String): Int { + val r = mCalcTextHeightRect + r.set(0, 0, 0, 0) + paint.getTextBounds(demoText, 0, demoText.length, r) + return r.height() } - private static final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics(); + private val mFontMetrics = Paint.FontMetrics() - public static float getLineHeight(Paint paint) { - return getLineHeight(paint, mFontMetrics); + fun getLineHeight(paint: Paint): Float { + return getLineHeight(paint, mFontMetrics) } - public static float getLineHeight(Paint paint, Paint.FontMetrics fontMetrics){ - paint.getFontMetrics(fontMetrics); - return fontMetrics.descent - fontMetrics.ascent; + fun getLineHeight(paint: Paint, fontMetrics: Paint.FontMetrics): Float { + paint.getFontMetrics(fontMetrics) + return fontMetrics.descent - fontMetrics.ascent } - public static float getLineSpacing(Paint paint) { - return getLineSpacing(paint, mFontMetrics); + fun getLineSpacing(paint: Paint): Float { + return getLineSpacing(paint, mFontMetrics) } - public static float getLineSpacing(Paint paint, Paint.FontMetrics fontMetrics){ - paint.getFontMetrics(fontMetrics); - return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom; + fun getLineSpacing(paint: Paint, fontMetrics: Paint.FontMetrics): Float { + paint.getFontMetrics(fontMetrics) + return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom } /** @@ -190,14 +212,14 @@ public abstract class Utils { * @param demoText * @return A Recyclable FSize instance */ - public static FSize calcTextSize(Paint paint, String demoText) { - - FSize result = FSize.getInstance(0,0); - calcTextSize(paint, demoText, result); - return result; + fun calcTextSize(paint: Paint, demoText: String): FSize { + val result: FSize = FSize.Companion.getInstance(0f, 0f) + calcTextSize(paint, demoText, result) + return result } - private static final Rect mCalcTextSizeRect = new Rect(); + private val mCalcTextSizeRect = Rect() + /** * calculates the approximate size of a text, depending on a demo text * avoid repeated calls (e.g. inside drawing methods) @@ -206,14 +228,12 @@ public abstract class Utils { * @param demoText * @param outputFSize An output variable, modified by the function. */ - public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) { - - Rect r = mCalcTextSizeRect; - r.set(0,0,0,0); - paint.getTextBounds(demoText, 0, demoText.length(), r); - outputFSize.width = r.width(); - outputFSize.height = r.height(); - + fun calcTextSize(paint: Paint, demoText: String, outputFSize: FSize) { + val r = mCalcTextSizeRect + r.set(0, 0, 0, 0) + paint.getTextBounds(demoText, 0, demoText.length, r) + outputFSize.width = r.width().toFloat() + outputFSize.height = r.height().toFloat() } @@ -221,100 +241,91 @@ public abstract class Utils { * Math.pow(...) is very expensive, so avoid calling it and create it * yourself. */ - private static final int[] POW_10 = { - 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 - }; + private val POW_10 = intArrayOf( + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 + ) - private static final IValueFormatter mDefaultValueFormatter = generateDefaultValueFormatter(); - - private static IValueFormatter generateDefaultValueFormatter() { - return new DefaultValueFormatter(1); - } + /** - returns: The default value formatter used for all chart components that needs a default */ + val defaultValueFormatter: IValueFormatter = generateDefaultValueFormatter() - /// - returns: The default value formatter used for all chart components that needs a default - public static IValueFormatter getDefaultValueFormatter() - { - return mDefaultValueFormatter; + private fun generateDefaultValueFormatter(): IValueFormatter { + return DefaultValueFormatter(1) } /** * Formats the given number to the given number of decimals, and returns the - * number as a string, maximum 35 characters. If thousands are separated, the separating - * character is a dot ("."). + * number as a string, maximum 35 characters. * * @param number * @param digitCount * @param separateThousands set this to true to separate thousands values + * @param separateChar a caracter to be paced between the "thousands" * @return */ - public static String formatNumber(float number, int digitCount, boolean separateThousands) { - return formatNumber(number, digitCount, separateThousands, '.'); - } - /** * Formats the given number to the given number of decimals, and returns the - * number as a string, maximum 35 characters. + * number as a string, maximum 35 characters. If thousands are separated, the separating + * character is a dot ("."). * * @param number * @param digitCount * @param separateThousands set this to true to separate thousands values - * @param separateChar a caracter to be paced between the "thousands" * @return */ - public static String formatNumber(float number, int digitCount, boolean separateThousands, - char separateChar) { - - char[] out = new char[35]; - - boolean neg = false; - if (number == 0) { - return "0"; + @JvmOverloads + fun formatNumber( + number: Float, digitCount: Int, separateThousands: Boolean, + separateChar: Char = '.' + ): String { + var number = number + var digitCount = digitCount + val out = CharArray(35) + + var neg = false + if (number == 0f) { + return "0" } - boolean zero = number < 1 && number > -1; + val zero = number < 1 && number > -1 if (number < 0) { - neg = true; - number = -number; + neg = true + number = -number } - if (digitCount > POW_10.length) { - digitCount = POW_10.length - 1; + if (digitCount > POW_10.size) { + digitCount = POW_10.size - 1 } - number *= POW_10[digitCount]; - long lval = Math.round(number); - int ind = out.length - 1; - int charCount = 0; - boolean decimalPointAdded = false; + number *= POW_10[digitCount].toFloat() + var lval = Math.round(number).toLong() + var ind = out.size - 1 + var charCount = 0 + var decimalPointAdded = false - while (lval != 0 || charCount < (digitCount + 1)) { - int digit = (int) (lval % 10); - lval = lval / 10; - out[ind--] = (char) (digit + '0'); - charCount++; + while (lval != 0L || charCount < (digitCount + 1)) { + val digit = (lval % 10).toInt() + lval = lval / 10 + out[ind--] = (digit + '0'.code).toChar() + charCount++ // add decimal point if (charCount == digitCount) { - out[ind--] = ','; - charCount++; - decimalPointAdded = true; + out[ind--] = ',' + charCount++ + decimalPointAdded = true // add thousand separators - } else if (separateThousands && lval != 0 && charCount > digitCount) { - + } else if (separateThousands && lval != 0L && charCount > digitCount) { if (decimalPointAdded) { - if ((charCount - digitCount) % 4 == 0) { - out[ind--] = separateChar; - charCount++; + out[ind--] = separateChar + charCount++ } - } else { - if ((charCount - digitCount) % 4 == 3) { - out[ind--] = separateChar; - charCount++; + out[ind--] = separateChar + charCount++ } } } @@ -322,20 +333,20 @@ public abstract class Utils { // if number around zero (between 1 and -1) if (zero) { - out[ind--] = '0'; - charCount += 1; + out[ind--] = '0' + charCount += 1 } // if the number is negative if (neg) { - out[ind--] = '-'; - charCount += 1; + out[ind--] = '-' + charCount += 1 } - int start = out.length - charCount; + val start = out.size - charCount // use this instead of "new String(...)" because of issue < Android 4.0 - return String.valueOf(out, start, out.length - start); + return String(out, start, out.size - start) } /** @@ -344,17 +355,14 @@ public abstract class Utils { * @param number * @return */ - public static float roundToNextSignificant(double number) { - if (Double.isInfinite(number) || - Double.isNaN(number) || - number == 0.0) - return 0; + fun roundToNextSignificant(number: Double): Float { + if (number.isInfinite() || number.isNaN() || number == 0.0) return 0f - final float d = (float) Math.ceil((float) Math.log10(number < 0 ? -number : number)); - final int pw = 1 - (int) d; - final float magnitude = (float) Math.pow(10, pw); - final long shifted = Math.round(number * magnitude); - return shifted / magnitude; + val d = ceil(log10(if (number < 0) -number else number).toFloat().toDouble()).toFloat() + val pw = 1 - d.toInt() + val magnitude = 10.0.pow(pw.toDouble()).toFloat() + val shifted = (number * magnitude).roundToInt() + return shifted / magnitude } /** @@ -364,14 +372,12 @@ public abstract class Utils { * @param number * @return */ - public static int getDecimals(float number) { + fun getDecimals(number: Float): Int { + val i = roundToNextSignificant(number.toDouble()) - float i = roundToNextSignificant(number); + if (i.isInfinite()) return 0 - if (Float.isInfinite(i)) - return 0; - - return (int) Math.ceil(-Math.log10(i)) + 2; + return ceil(-log10(i.toDouble())).toInt() + 2 } /** @@ -380,19 +386,18 @@ public abstract class Utils { * @param integers * @return */ - public static int[] convertIntegers(List integers) { - - int[] ret = new int[integers.size()]; + fun convertIntegers(integers: MutableList): IntArray { + val ret = IntArray(integers.size) - copyIntegers(integers, ret); + copyIntegers(integers, ret) - return ret; + return ret } - public static void copyIntegers(List from, int[] to){ - int count = Math.min(to.length, from.size()); - for(int i = 0 ; i < count ; i++){ - to[i] = from.get(i); + fun copyIntegers(from: MutableList, to: IntArray) { + val count = min(to.size, from.size) + for (i in 0.. strings) { + fun convertStrings(strings: MutableList): Array { + val ret = arrayOfNulls(strings.size) - String[] ret = new String[strings.size()]; - - for (int i = 0; i < ret.length; i++) { - ret[i] = strings.get(i); + for (i in ret.indices) { + ret[i] = strings.get(i) } - return ret; + return ret } /** @@ -420,13 +424,15 @@ public abstract class Utils { * @param d * @return */ - public static double nextUp(double d) { - if (d == Double.POSITIVE_INFINITY) - return d; + fun nextUp(d: Double): Double { + var d = d + if (d == Double.Companion.POSITIVE_INFINITY) return d else { - d += 0.0d; - return Double.longBitsToDouble(Double.doubleToRawLongBits(d) + - ((d >= 0.0d) ? +1L : -1L)); + d += 0.0 + return java.lang.Double.longBitsToDouble( + java.lang.Double.doubleToRawLongBits(d) + + (if (d >= 0.0) +1L else -1L) + ) } } @@ -440,41 +446,47 @@ public abstract class Utils { * @param angle in degrees, converted to radians internally * @return */ - public static MPPointF getPosition(MPPointF center, float dist, float angle) { - - MPPointF p = MPPointF.getInstance(0,0); - getPosition(center, dist, angle, p); - return p; + fun getPosition(center: MPPointF, dist: Float, angle: Float): MPPointF { + val p: MPPointF = MPPointF.Companion.getInstance(0f, 0f) + getPosition(center, dist, angle, p) + return p } - public static void getPosition(MPPointF center, float dist, float angle, MPPointF outputPoint){ - outputPoint.x = (float) (center.x + dist * Math.cos(Math.toRadians(angle))); - outputPoint.y = (float) (center.y + dist * Math.sin(Math.toRadians(angle))); + fun getPosition(center: MPPointF, dist: Float, angle: Float, outputPoint: MPPointF) { + outputPoint.x = (center.x + dist * cos(Math.toRadians(angle.toDouble()))).toFloat() + outputPoint.y = (center.y + dist * sin(Math.toRadians(angle.toDouble()))).toFloat() } - public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, - VelocityTracker tracker) { - + fun velocityTrackerPointerUpCleanUpIfNecessary( + ev: MotionEvent, + tracker: VelocityTracker + ) { // Check the dot product of current velocities. // If the pointer that left was opposing another velocity vector, clear. - tracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); - final int upIndex = ev.getActionIndex(); - final int id1 = ev.getPointerId(upIndex); - final float x1 = tracker.getXVelocity(id1); - final float y1 = tracker.getYVelocity(id1); - for (int i = 0, count = ev.getPointerCount(); i < count; i++) { - if (i == upIndex) - continue; - - final int id2 = ev.getPointerId(i); - final float x = x1 * tracker.getXVelocity(id2); - final float y = y1 * tracker.getYVelocity(id2); - - final float dot = x + y; + + tracker.computeCurrentVelocity(1000, maximumFlingVelocity.toFloat()) + val upIndex = ev.actionIndex + val id1 = ev.getPointerId(upIndex) + val x1 = tracker.getXVelocity(id1) + val y1 = tracker.getYVelocity(id1) + var i = 0 + val count = ev.pointerCount + while (i < count) { + if (i == upIndex) { + i++ + continue + } + + val id2 = ev.getPointerId(i) + val x = x1 * tracker.getXVelocity(id2) + val y = y1 * tracker.getYVelocity(id2) + + val dot = x + y if (dot < 0) { - tracker.clear(); - break; + tracker.clear() + break } + i++ } } @@ -485,213 +497,209 @@ public abstract class Utils { * @param view */ @SuppressLint("NewApi") - public static void postInvalidateOnAnimation(View view) { - view.postInvalidateOnAnimation(); - } - - public static int getMinimumFlingVelocity() { - return mMinimumFlingVelocity; - } - - public static int getMaximumFlingVelocity() { - return mMaximumFlingVelocity; + fun postInvalidateOnAnimation(view: View) { + view.postInvalidateOnAnimation() } /** * returns an angle between 0.f < 360.f (not less than zero, less than 360) */ - public static float getNormalizedAngle(float angle) { - while (angle < 0.f) - angle += 360.f; - - return angle % 360.f; - } - - private static final Rect mDrawableBoundsCache = new Rect(); - - public static void drawImage(Canvas canvas, - Drawable drawable, - int x, int y, - int width, int height) { - - MPPointF drawOffset = MPPointF.getInstance(); - drawOffset.x = x - (width / 2); - drawOffset.y = y - (height / 2); - - drawable.copyBounds(mDrawableBoundsCache); - drawable.setBounds( - mDrawableBoundsCache.left, - mDrawableBoundsCache.top, - mDrawableBoundsCache.left + width, - mDrawableBoundsCache.top + width); - - int saveId = canvas.save(); - // translate to the correct position and draw - canvas.translate(drawOffset.x, drawOffset.y); - drawable.draw(canvas); - canvas.restoreToCount(saveId); + fun getNormalizedAngle(angle: Float): Float { + var angle = angle + while (angle < 0f) angle += 360f + + return angle % 360f + } + + private val mDrawableBoundsCache = Rect() + + fun drawImage( + canvas: Canvas, + drawable: Drawable?, + x: Int, y: Int, + width: Int, height: Int + ) { + val drawOffset: MPPointF = MPPointF.instance + drawOffset.x = (x - (width / 2)).toFloat() + drawOffset.y = (y - (height / 2)).toFloat() + + drawable?.copyBounds(mDrawableBoundsCache) + drawable?.setBounds( + mDrawableBoundsCache.left, + mDrawableBoundsCache.top, + mDrawableBoundsCache.left + width, + mDrawableBoundsCache.top + width + ) + + canvas.withTranslation(drawOffset.x, drawOffset.y) { + // translate to the correct position and draw + drawable?.draw(canvas) + } } - private static final Rect mDrawTextRectBuffer = new Rect(); - private static final Paint.FontMetrics mFontMetricsBuffer = new Paint.FontMetrics(); - - public static void drawXAxisValue(Canvas c, String text, float x, float y, - Paint paint, - MPPointF anchor, float angleDegrees) { + private val mDrawTextRectBuffer = Rect() + private val mFontMetricsBuffer = Paint.FontMetrics() - float drawOffsetX = 0.f; - float drawOffsetY = 0.f; + fun drawXAxisValue( + c: Canvas?, text: String, x: Float, y: Float, + paint: Paint, + anchor: MPPointF, angleDegrees: Float + ) { + var drawOffsetX = 0f + var drawOffsetY = 0f - final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer); - paint.getTextBounds(text, 0, text.length(), mDrawTextRectBuffer); + val lineHeight = paint.getFontMetrics(mFontMetricsBuffer) + paint.getTextBounds(text, 0, text.length, mDrawTextRectBuffer) // Android sometimes has pre-padding - drawOffsetX -= mDrawTextRectBuffer.left; + drawOffsetX -= mDrawTextRectBuffer.left.toFloat() // Android does not snap the bounds to line boundaries, // and draws from bottom to top. // And we want to normalize it. - drawOffsetY -= mFontMetricsBuffer.ascent; + drawOffsetY -= mFontMetricsBuffer.ascent // To have a consistent point of reference, we always draw left-aligned - Paint.Align originalTextAlign = paint.getTextAlign(); - paint.setTextAlign(Paint.Align.LEFT); - - if (angleDegrees != 0.f) { + val originalTextAlign = paint.getTextAlign() + paint.setTextAlign(Align.LEFT) + if (angleDegrees != 0f) { // Move the text drawing rect in a way that it always rotates around its center - drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f; - drawOffsetY -= lineHeight * 0.5f; - float translateX = x; - float translateY = y; + drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f + drawOffsetY -= lineHeight * 0.5f + + var translateX = x + var translateY = y // Move the "outer" rect relative to the anchor, assuming its centered if (anchor.x != 0.5f || anchor.y != 0.5f) { - final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees( - mDrawTextRectBuffer.width(), - lineHeight, - angleDegrees); - - translateX -= rotatedSize.width * (anchor.x - 0.5f); - translateY -= rotatedSize.height * (anchor.y - 0.5f); - FSize.recycleInstance(rotatedSize); + val rotatedSize = getSizeOfRotatedRectangleByDegrees( + mDrawTextRectBuffer.width().toFloat(), + lineHeight, + angleDegrees + ) + + translateX -= rotatedSize.width * (anchor.x - 0.5f) + translateY -= rotatedSize.height * (anchor.y - 0.5f) + FSize.Companion.recycleInstance(rotatedSize) } - c.save(); - c.translate(translateX, translateY); - c.rotate(angleDegrees); + c?.withTranslation(translateX, translateY) { + rotate(angleDegrees) - c.drawText(text, drawOffsetX, drawOffsetY, paint); + drawText(text, drawOffsetX, drawOffsetY, paint) - c.restore(); + } } else { - if (anchor.x != 0.f || anchor.y != 0.f) { - - drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x; - drawOffsetY -= lineHeight * anchor.y; + if (anchor.x != 0f || anchor.y != 0f) { + drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x + drawOffsetY -= lineHeight * anchor.y } - drawOffsetX += x; - drawOffsetY += y; + drawOffsetX += x + drawOffsetY += y - c.drawText(text, drawOffsetX, drawOffsetY, paint); + c?.drawText(text, drawOffsetX, drawOffsetY, paint) } - paint.setTextAlign(originalTextAlign); + paint.textAlign = originalTextAlign } - public static void drawMultilineText(Canvas c, StaticLayout textLayout, - float x, float y, - TextPaint paint, - MPPointF anchor, float angleDegrees) { + fun drawMultilineText( + c: Canvas, textLayout: StaticLayout, + x: Float, y: Float, + paint: TextPaint, + anchor: MPPointF, angleDegrees: Float + ) { + var drawOffsetX = 0f + var drawOffsetY = 0f + val drawWidth: Float + val drawHeight: Float - float drawOffsetX = 0.f; - float drawOffsetY = 0.f; - float drawWidth; - float drawHeight; + val lineHeight = paint.getFontMetrics(mFontMetricsBuffer) - final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer); - - drawWidth = textLayout.getWidth(); - drawHeight = textLayout.getLineCount() * lineHeight; + drawWidth = textLayout.getWidth().toFloat() + drawHeight = textLayout.getLineCount() * lineHeight // Android sometimes has pre-padding - drawOffsetX -= mDrawTextRectBuffer.left; + drawOffsetX -= mDrawTextRectBuffer.left.toFloat() // Android does not snap the bounds to line boundaries, // and draws from bottom to top. // And we want to normalize it. - drawOffsetY += drawHeight; + drawOffsetY += drawHeight // To have a consistent point of reference, we always draw left-aligned - Paint.Align originalTextAlign = paint.getTextAlign(); - paint.setTextAlign(Paint.Align.LEFT); - - if (angleDegrees != 0.f) { + val originalTextAlign = paint.getTextAlign() + paint.setTextAlign(Align.LEFT) + if (angleDegrees != 0f) { // Move the text drawing rect in a way that it always rotates around its center - drawOffsetX -= drawWidth * 0.5f; - drawOffsetY -= drawHeight * 0.5f; - float translateX = x; - float translateY = y; + drawOffsetX -= drawWidth * 0.5f + drawOffsetY -= drawHeight * 0.5f + + var translateX = x + var translateY = y // Move the "outer" rect relative to the anchor, assuming its centered if (anchor.x != 0.5f || anchor.y != 0.5f) { - final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees( - drawWidth, - drawHeight, - angleDegrees); - - translateX -= rotatedSize.width * (anchor.x - 0.5f); - translateY -= rotatedSize.height * (anchor.y - 0.5f); - FSize.recycleInstance(rotatedSize); + val rotatedSize = getSizeOfRotatedRectangleByDegrees( + drawWidth, + drawHeight, + angleDegrees + ) + + translateX -= rotatedSize.width * (anchor.x - 0.5f) + translateY -= rotatedSize.height * (anchor.y - 0.5f) + FSize.Companion.recycleInstance(rotatedSize) } - c.save(); - c.translate(translateX, translateY); - c.rotate(angleDegrees); + c.save() + c.translate(translateX, translateY) + c.rotate(angleDegrees) - c.translate(drawOffsetX, drawOffsetY); - textLayout.draw(c); + c.translate(drawOffsetX, drawOffsetY) + textLayout.draw(c) - c.restore(); + c.restore() } else { - if (anchor.x != 0.f || anchor.y != 0.f) { - - drawOffsetX -= drawWidth * anchor.x; - drawOffsetY -= drawHeight * anchor.y; + if (anchor.x != 0f || anchor.y != 0f) { + drawOffsetX -= drawWidth * anchor.x + drawOffsetY -= drawHeight * anchor.y } - drawOffsetX += x; - drawOffsetY += y; + drawOffsetX += x + drawOffsetY += y - c.save(); + c.save() - c.translate(drawOffsetX, drawOffsetY); - textLayout.draw(c); + c.translate(drawOffsetX, drawOffsetY) + textLayout.draw(c) - c.restore(); + c.restore() } - paint.setTextAlign(originalTextAlign); + paint.setTextAlign(originalTextAlign) } - public static void drawMultilineText(Canvas c, String text, - float x, float y, - TextPaint paint, - FSize constrainedToSize, - MPPointF anchor, float angleDegrees) { + fun drawMultilineText( + c: Canvas, text: String, + x: Float, y: Float, + paint: TextPaint, + constrainedToSize: FSize, + anchor: MPPointF, angleDegrees: Float + ) { + val textLayout = StaticLayout( + text, 0, text.length, + paint, + max(ceil(constrainedToSize.width.toDouble()), 1.0).toInt(), + Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false + ) - StaticLayout textLayout = new StaticLayout( - text, 0, text.length(), - paint, - (int) Math.max(Math.ceil(constrainedToSize.width), 1.f), - Layout.Alignment.ALIGN_NORMAL, 1.f, 0.f, false); - - drawMultilineText(c, textLayout, x, y, paint, anchor, angleDegrees); + drawMultilineText(c, textLayout, x, y, paint, anchor, angleDegrees) } /** @@ -702,10 +710,12 @@ public abstract class Utils { * @param degrees * @return A Recyclable FSize instance */ - public static FSize getSizeOfRotatedRectangleByDegrees(FSize rectangleSize, float degrees) { - final float radians = degrees * FDEG2RAD; - return getSizeOfRotatedRectangleByRadians(rectangleSize.width, rectangleSize.height, - radians); + fun getSizeOfRotatedRectangleByDegrees(rectangleSize: FSize, degrees: Float): FSize { + val radians = degrees * FDEG2RAD + return getSizeOfRotatedRectangleByRadians( + rectangleSize.width, rectangleSize.height, + radians + ) } /** @@ -716,9 +726,11 @@ public abstract class Utils { * @param radians * @return A Recyclable FSize instance */ - public static FSize getSizeOfRotatedRectangleByRadians(FSize rectangleSize, float radians) { - return getSizeOfRotatedRectangleByRadians(rectangleSize.width, rectangleSize.height, - radians); + fun getSizeOfRotatedRectangleByRadians(rectangleSize: FSize, radians: Float): FSize { + return getSizeOfRotatedRectangleByRadians( + rectangleSize.width, rectangleSize.height, + radians + ) } /** @@ -730,10 +742,9 @@ public abstract class Utils { * @param degrees * @return A Recyclable FSize instance */ - public static FSize getSizeOfRotatedRectangleByDegrees(float rectangleWidth, float - rectangleHeight, float degrees) { - final float radians = degrees * FDEG2RAD; - return getSizeOfRotatedRectangleByRadians(rectangleWidth, rectangleHeight, radians); + fun getSizeOfRotatedRectangleByDegrees(rectangleWidth: Float, rectangleHeight: Float, degrees: Float): FSize { + val radians = degrees * FDEG2RAD + return getSizeOfRotatedRectangleByRadians(rectangleWidth, rectangleHeight, radians) } /** @@ -745,17 +756,13 @@ public abstract class Utils { * @param radians * @return A Recyclable FSize instance */ - public static FSize getSizeOfRotatedRectangleByRadians(float rectangleWidth, float - rectangleHeight, float radians) { - return FSize.getInstance( - Math.abs(rectangleWidth * (float) Math.cos(radians)) + Math.abs(rectangleHeight * - (float) Math.sin(radians)), - Math.abs(rectangleWidth * (float) Math.sin(radians)) + Math.abs(rectangleHeight * - (float) Math.cos(radians)) - ); + fun getSizeOfRotatedRectangleByRadians(rectangleWidth: Float, rectangleHeight: Float, radians: Float): FSize { + return FSize.Companion.getInstance( + abs(rectangleWidth * cos(radians.toDouble()).toFloat()) + abs(rectangleHeight * sin(radians.toDouble()).toFloat()), + abs(rectangleWidth * sin(radians.toDouble()).toFloat()) + abs(rectangleHeight * cos(radians.toDouble()).toFloat()) + ) } - public static int getSDKInt() { - return android.os.Build.VERSION.SDK_INT; - } + val sDKInt: Int + get() = Build.VERSION.SDK_INT } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ViewPortHandler.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ViewPortHandler.kt index 8b3a60022d..028a42a38e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ViewPortHandler.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ViewPortHandler.kt @@ -327,7 +327,7 @@ open class ViewPortHandler { * @param view * @return save */ - fun centerViewPort(transformedPts: FloatArray, view: View) { + fun centerViewPort(transformedPts: FloatArray, view: View?) { val save = mCenterViewPortMatrixBuffer save.reset() save.set(matrixTouch) @@ -351,13 +351,13 @@ open class ViewPortHandler { * @param newMatrix * @return */ - fun refresh(newMatrix: Matrix, chart: View, invalidate: Boolean): Matrix { + fun refresh(newMatrix: Matrix, chart: View?, invalidate: Boolean): Matrix { matrixTouch.set(newMatrix) // make sure scale and translation are within their bounds limitTransAndScale(matrixTouch, contentRect) - if (invalidate) chart.invalidate() + if (invalidate) chart?.invalidate() newMatrix.set(matrixTouch) return newMatrix diff --git a/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.java b/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.java index 16c9568904..58032ce3b8 100644 --- a/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.java +++ b/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.java @@ -141,7 +141,7 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.actionToggleValues: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; @@ -173,7 +173,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleBarBorders: { for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet)set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); + ((BarDataSet)set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivity.java b/app/src/main/java/info/appdev/chartexample/BarChartActivity.java index 74c97f78df..a6d688a5ff 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivity.java @@ -218,14 +218,14 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; } case R.id.actionToggleIcons: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled()); + set.setDrawIcons(!set.isDrawIconsEnabled); chart.invalidate(); break; @@ -253,7 +253,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleBarBorders: { for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); + ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.java b/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.java index 798f454a1a..be6cd0a74b 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.java +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.java @@ -7,6 +7,8 @@ import android.graphics.Color; import android.net.Uri; import android.os.Bundle; + +import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import android.util.Log; import android.view.Menu; @@ -102,6 +104,7 @@ protected void onCreate(Bundle savedInstanceState) { xAxis.setGranularity(1f); xAxis.setCenterAxisLabels(true); xAxis.setValueFormatter(new IAxisValueFormatter() { + @NonNull @Override public String getFormattedValue(float value, AxisBase axis) { return String.valueOf((int) value); @@ -211,7 +214,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IBarDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; @@ -232,7 +235,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleBarBorders: { for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); + ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.java b/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.java index 7b8ce01c2a..6e50e8e709 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.java +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.java @@ -159,7 +159,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IBarDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; @@ -187,7 +187,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleBarBorders: { for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); + ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.java b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.java index c2e34e38ba..013279e541 100644 --- a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.java @@ -170,14 +170,14 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; } case R.id.actionToggleIcons: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled()); + set.setDrawIcons(!set.isDrawIconsEnabled); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.java b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.java index b66d933f3b..01e92f6bd0 100644 --- a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.java @@ -162,14 +162,14 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; } case R.id.actionToggleIcons: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled()); + set.setDrawIcons(!set.isDrawIconsEnabled); chart.invalidate(); break; @@ -197,7 +197,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleMakeShadowSameColorAsCandle: { for (ICandleDataSet set : chart.getData().getDataSets()) { - ((CandleDataSet) set).setShadowColorSameAsCandle(!set.getShadowColorSameAsCandle()); + ((CandleDataSet) set).setShadowColorSameAsCandle(!set.shadowColorSameAsCandle); } chart.invalidate(); diff --git a/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.java b/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.java index bb185a7c76..9e2e4b91a1 100644 --- a/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.java @@ -252,7 +252,7 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.actionToggleLineValues: { for (IDataSet set : chart.getData().getDataSets()) { if (set instanceof LineDataSet) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); } chart.invalidate(); @@ -261,7 +261,7 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.actionToggleBarValues: { for (IDataSet set : chart.getData().getDataSets()) { if (set instanceof BarDataSet) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); } chart.invalidate(); diff --git a/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.java b/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.java index f97b19f950..7651d974d6 100644 --- a/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.java +++ b/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.java @@ -75,11 +75,11 @@ private void addEntry() { int lastDataSetIndex = data.getDataSetCount() - 1; // add data only to the last ILineDataSet lastSet = data.getDataSetByIndex(lastDataSetIndex); - int cycleValue = (int) (lastSet.getEntryCount() % 100.0); + int cycleValue = (int) (lastSet.entryCount % 100.0); float value = (sampleValues[cycleValue].floatValue() * 50) + 50f * (lastDataSetIndex + 1); - data.addEntry(new Entry(lastSet.getEntryCount(), value), lastDataSetIndex); + data.addEntry(new Entry(lastSet.entryCount, value), lastDataSetIndex); data.notifyDataChanged(); // let the chart know it's data has changed @@ -103,7 +103,7 @@ private void removeLastEntry() { if (set != null) { - Entry e = set.getEntryForXValue(set.getEntryCount() - 1, Float.NaN); + Entry e = set.getEntryForXValue(set.entryCount - 1, Float.NaN); data.removeEntry(e, 0); // or remove by index @@ -123,7 +123,7 @@ private void addDataSet() { chart.setData(new LineData()); } else { int count = (data.getDataSetCount() + 1); - int amount = data.getDataSetByIndex(0).getEntryCount(); + int amount = data.getDataSetByIndex(0).entryCount; ArrayList values = new ArrayList<>(); diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.java b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.java index 97bdcf0af0..bcc161e6cf 100644 --- a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.java @@ -176,7 +176,7 @@ public boolean onOptionsItemSelected(MenuItem item) { .getDataSets(); for (IBarDataSet iSet : sets) { - iSet.setDrawValues(!iSet.isDrawValuesEnabled()); + iSet.setDrawValues(!iSet.isDrawValuesEnabled); } chart.invalidate(); @@ -187,7 +187,7 @@ public boolean onOptionsItemSelected(MenuItem item) { .getDataSets(); for (IBarDataSet iSet : sets) { - iSet.setDrawIcons(!iSet.isDrawIconsEnabled()); + iSet.setDrawIcons(!iSet.isDrawIconsEnabled); } chart.invalidate(); @@ -213,7 +213,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleBarBorders: { for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet)set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); + ((BarDataSet)set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); chart.invalidate(); break; @@ -275,8 +275,7 @@ public void onValueSelected(Entry e, Highlight h) { RectF bounds = mOnValueSelectedRectF; chart.getBarBounds((BarEntry) e, bounds); - MPPointF position = chart.getPosition(e, chart.getData().getDataSetByIndex(h.getDataSetIndex()) - .getAxisDependency()); + MPPointF position = chart.getPosition(e, chart.getData().getDataSetByIndex(h.getDataSetIndex()).axisDependency); Log.i("bounds", bounds.toString()); Log.i("position", position.toString()); diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.java b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.java index d64f758a3f..02e920703c 100644 --- a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.java @@ -176,7 +176,7 @@ public boolean onOptionsItemSelected(MenuItem item) { .getDataSets(); for (IBarDataSet iSet : sets) { - iSet.setDrawValues(!iSet.isDrawValuesEnabled()); + iSet.setDrawValues(!iSet.isDrawValuesEnabled); } chart.invalidate(); @@ -187,7 +187,7 @@ public boolean onOptionsItemSelected(MenuItem item) { .getDataSets(); for (IBarDataSet iSet : sets) { - iSet.setDrawIcons(!iSet.isDrawIconsEnabled()); + iSet.setDrawIcons(!iSet.isDrawIconsEnabled); } chart.invalidate(); @@ -213,7 +213,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleBarBorders: { for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet)set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); + ((BarDataSet)set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); chart.invalidate(); break; @@ -275,8 +275,7 @@ public void onValueSelected(Entry e, Highlight h) { RectF bounds = mOnValueSelectedRectF; chart.getBarBounds((BarEntry) e, bounds); - MPPointF position = chart.getPosition(e, chart.getData().getDataSetByIndex(h.getDataSetIndex()) - .getAxisDependency()); + MPPointF position = chart.getPosition(e, chart.getData().getDataSetByIndex(h.getDataSetIndex()).axisDependency); Log.i("bounds", bounds.toString()); Log.i("position", position.toString()); diff --git a/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.java b/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.java index e23552c0c7..2cfed3b849 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.java +++ b/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.java @@ -381,8 +381,7 @@ protected void saveToGallery() { public void onValueSelected(Entry e, Highlight h) { Log.i("Entry selected", e.toString()); - chart.centerViewToAnimated(e.getX(), e.getY(), chart.getData().getDataSetByIndex(h.getDataSetIndex()) - .getAxisDependency(), 500); + chart.centerViewToAnimated(e.getX(), e.getY(), chart.getData().getDataSetByIndex(h.getDataSetIndex()).axisDependency, 500); //chart.zoomAndCenterAnimated(2.5f, 2.5f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex) // .getAxisDependency(), 1000); //chart.zoomAndCenterAnimated(1.8f, 1.8f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex) diff --git a/app/src/main/java/info/appdev/chartexample/PieChartActivity.java b/app/src/main/java/info/appdev/chartexample/PieChartActivity.java index 05b7b83ee1..a75eef0332 100644 --- a/app/src/main/java/info/appdev/chartexample/PieChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/PieChartActivity.java @@ -192,14 +192,14 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; } case R.id.actionToggleIcons: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled()); + set.setDrawIcons(!set.isDrawIconsEnabled); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.java b/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.java index 557bd9d590..f2fb5133c4 100644 --- a/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.java +++ b/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.java @@ -199,14 +199,14 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; } case R.id.actionToggleIcons: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled()); + set.setDrawIcons(!set.isDrawIconsEnabled); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.java b/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.java index b507ee6dc4..fc18a2dee3 100644 --- a/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.java @@ -192,7 +192,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/RadarChartActivity.java b/app/src/main/java/info/appdev/chartexample/RadarChartActivity.java index fa6c21c862..e4a3e9631b 100644 --- a/app/src/main/java/info/appdev/chartexample/RadarChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/RadarChartActivity.java @@ -178,7 +178,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleValues: { for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled()); + set.setDrawValues(!set.isDrawValuesEnabled); chart.invalidate(); break; @@ -201,7 +201,7 @@ public boolean onOptionsItemSelected(MenuItem item) { .getDataSets(); for (IRadarDataSet set : sets) { - set.setDrawFilled(!set.isDrawFilledEnabled()); + set.setDrawFilled(!set.isDrawFilledEnabled); } chart.invalidate(); break; @@ -212,7 +212,7 @@ public boolean onOptionsItemSelected(MenuItem item) { .getDataSets(); for (IRadarDataSet set : sets) { - set.setDrawHighlightCircleEnabled(!set.isDrawHighlightCircleEnabled()); + set.isDrawHighlightCircleEnabled = !set.isDrawHighlightCircleEnabled; } chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.java b/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.java index 179aa688dd..18e9b46b3d 100644 --- a/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.java +++ b/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.java @@ -112,8 +112,8 @@ private void addEntry() { data.addDataSet(set); } - int cycleValue = (int) (set.getEntryCount() % 100.0); - data.addEntry(new Entry(set.getEntryCount(), (sampleValues[cycleValue].floatValue() * 40) + 30f), 0); + int cycleValue = (int) (set.entryCount % 100.0); + data.addEntry(new Entry(set.entryCount, (sampleValues[cycleValue].floatValue() * 40) + 30f), 0); data.notifyDataChanged(); // let the chart know it's data has changed diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.java b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.java index b9fec35d61..5eae9f062c 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.java +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.java @@ -215,7 +215,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleBarBorders: { for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); + ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); chart.invalidate(); break; diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.java b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.java index 5c63f41495..0b144e5516 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.java +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.java @@ -196,7 +196,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.actionToggleBarBorders: { for (IBarDataSet set : chart.getData().getDataSets()) { - ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f); + ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); } chart.invalidate(); diff --git a/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java b/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java index 873f438632..94cf35e259 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java +++ b/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java @@ -19,7 +19,7 @@ public class CustomScatterShapeRenderer implements IShapeRenderer public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, float posX, float posY, Paint renderPaint) { - final float shapeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeSize()) / 2f; + final float shapeHalf = Utils.convertDpToPixel(dataSet.scatterShapeSize) / 2f; c.drawLine( posX - shapeHalf, From b1c3503cf809117e849a195c72310cc55a757093 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sat, 13 Sep 2025 19:01:55 -0400 Subject: [PATCH 3/9] Second pass at Kotlin conversion --- .../mikephil/charting/charts/BarChart.kt | 28 +- .../charting/charts/BarLineChartBase.kt | 296 +++++++++--------- .../github/mikephil/charting/charts/Chart.kt | 205 ++++++------ .../mikephil/charting/charts/CombinedChart.kt | 22 +- .../charting/charts/HorizontalBarChart.kt | 127 ++++---- .../mikephil/charting/charts/LineChart.kt | 4 +- .../mikephil/charting/charts/PieChart.kt | 164 +++++----- .../charting/charts/PieRadarChartBase.kt | 75 ++--- .../mikephil/charting/charts/RadarChart.kt | 65 ++-- .../mikephil/charting/components/AxisBase.kt | 18 +- .../charting/components/Description.kt | 2 +- .../mikephil/charting/components/IMarker.kt | 2 +- .../mikephil/charting/components/Legend.kt | 24 +- .../mikephil/charting/components/LimitLine.kt | 2 +- .../charting/components/LimitRange.kt | 2 +- .../charting/components/MarkerImage.kt | 84 ++--- .../charting/components/MarkerView.kt | 18 +- .../mikephil/charting/components/YAxis.kt | 9 +- .../github/mikephil/charting/data/BarData.kt | 4 +- .../mikephil/charting/data/BarDataSet.kt | 29 +- .../github/mikephil/charting/data/BarEntry.kt | 4 +- .../data/BarLineScatterCandleBubbleDataSet.kt | 2 +- .../mikephil/charting/data/BaseDataSet.kt | 2 +- .../mikephil/charting/data/BubbleData.kt | 2 +- .../mikephil/charting/data/BubbleDataSet.kt | 1 - .../mikephil/charting/data/BubbleEntry.kt | 2 +- .../mikephil/charting/data/CandleDataSet.kt | 2 +- .../mikephil/charting/data/CandleEntry.kt | 2 +- .../mikephil/charting/data/ChartData.kt | 84 ++--- .../mikephil/charting/data/CombinedData.kt | 16 +- .../github/mikephil/charting/data/DataSet.kt | 64 ++-- .../mikephil/charting/data/LineDataSet.kt | 7 +- .../charting/data/LineRadarDataSet.kt | 2 +- .../data/LineScatterCandleRadarDataSet.kt | 4 +- .../github/mikephil/charting/data/PieData.kt | 2 +- .../mikephil/charting/data/PieDataSet.kt | 2 +- .../github/mikephil/charting/data/PieEntry.kt | 2 +- .../mikephil/charting/data/RadarData.kt | 2 +- .../mikephil/charting/data/RadarDataSet.kt | 2 +- .../mikephil/charting/data/RadarEntry.kt | 2 +- .../mikephil/charting/data/ScatterDataSet.kt | 2 +- .../charting/data/filter/Approximator.kt | 3 +- .../charting/data/filter/ApproximatorN.kt | 2 +- .../DrawingDataSetNotCreatedException.kt | 2 + .../formatter/DefaultAxisValueFormatter.kt | 2 +- .../formatter/DefaultFillFormatter.kt | 11 +- .../formatter/DefaultValueFormatter.kt | 2 +- .../charting/formatter/IFillFormatter.kt | 2 +- .../charting/formatter/PercentFormatter.kt | 2 +- .../charting/highlight/BarHighlighter.kt | 48 +-- .../charting/highlight/ChartHighlighter.kt | 15 +- .../charting/highlight/CombinedHighlighter.kt | 10 +- .../highlight/HorizontalBarHighlighter.kt | 26 +- .../charting/highlight/PieHighlighter.kt | 4 +- .../charting/highlight/PieRadarHighlighter.kt | 18 +- .../charting/highlight/RadarHighlighter.kt | 26 +- .../mikephil/charting/highlight/Range.kt | 3 +- .../BarLineScatterCandleBubbleDataProvider.kt | 3 +- .../interfaces/datasets/IBarDataSet.kt | 4 +- .../charting/jobs/AnimatedMoveViewJob.kt | 2 +- .../charting/jobs/AnimatedViewPortJob.kt | 4 +- .../mikephil/charting/jobs/AnimatedZoomJob.kt | 2 +- .../github/mikephil/charting/jobs/ZoomJob.kt | 1 - .../listener/BarLineChartTouchListener.kt | 22 +- .../listener/PieRadarChartTouchListener.kt | 2 +- .../charting/renderer/BarChartRenderer.kt | 124 ++++---- .../BarLineScatterCandleBubbleRenderer.kt | 6 +- .../charting/renderer/BubbleChartRenderer.kt | 35 ++- .../renderer/CandleStickChartRenderer.kt | 14 +- .../renderer/CombinedChartRenderer.kt | 12 +- .../renderer/HorizontalBarChartRenderer.kt | 47 ++- .../charting/renderer/LegendRenderer.kt | 18 +- .../charting/renderer/LineChartRenderer.kt | 136 ++++---- .../charting/renderer/PieChartRenderer.kt | 83 +++-- .../charting/renderer/RadarChartRenderer.kt | 52 +-- .../renderer/RoundedBarChartRenderer.kt | 16 +- .../RoundedHorizontalBarChartRenderer.kt | 12 +- .../charting/renderer/ScatterChartRenderer.kt | 26 +- .../charting/renderer/XAxisRenderer.kt | 41 +-- .../XAxisRendererHorizontalBarChart.kt | 55 ++-- .../renderer/XAxisRendererRadarChart.kt | 8 +- .../charting/renderer/YAxisRenderer.kt | 107 ++++--- .../YAxisRendererHorizontalBarChart.kt | 2 +- .../renderer/YAxisRendererRadarChart.kt | 18 +- .../renderer/scatter/TriangleShapeRenderer.kt | 2 +- .../mikephil/charting/utils/ColorTemplate.kt | 2 +- .../charting/utils/EntryXComparator.kt | 6 +- .../github/mikephil/charting/utils/FSize.kt | 11 +- .../mikephil/charting/utils/FileUtils.kt | 6 +- .../github/mikephil/charting/utils/Fill.kt | 139 ++++---- .../mikephil/charting/utils/MPPointD.kt | 2 +- .../mikephil/charting/utils/MPPointF.kt | 4 +- .../mikephil/charting/utils/ObjectPool.kt | 67 ++-- .../mikephil/charting/utils/Transformer.kt | 58 ++-- .../github/mikephil/charting/utils/Utils.kt | 76 ++--- 95 files changed, 1334 insertions(+), 1455 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt index e9f2a316c6..56cab326e3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt @@ -65,15 +65,17 @@ open class BarChart : BarLineChartBase, BarDataP } override fun calcMinMax() { - if (mFitBars) { - mXAxis?.calculate(mData!!.xMin - mData!!.barWidth / 2f, mData!!.xMax + mData!!.barWidth / 2f) - } else { - mXAxis?.calculate(mData!!.xMin, mData!!.xMax) - } + mData?.let { data -> + if (mFitBars) { + mXAxis.calculate(data.xMin - data.barWidth / 2f, data.xMax + data.barWidth / 2f) + } else { + mXAxis.calculate(data.xMin, data.xMax) + } - // calculate axis range (min / max) according to provided data - mAxisLeft?.calculate(mData!!.getYMin(AxisDependency.LEFT), mData!!.getYMax(AxisDependency.LEFT)) - mAxisRight?.calculate(mData!!.getYMin(AxisDependency.RIGHT), mData!!.getYMax(AxisDependency.RIGHT)) + // calculate axis range (min / max) according to provided data + mAxisLeft.calculate(data.getYMin(AxisDependency.LEFT), data.getYMax(AxisDependency.LEFT)) + mAxisRight.calculate(data.getYMin(AxisDependency.RIGHT), data.getYMax(AxisDependency.RIGHT)) + } } /** @@ -86,7 +88,7 @@ open class BarChart : BarLineChartBase, BarDataP */ override fun getHighlightByTouchPoint(x: Float, y: Float): Highlight? { if (mData == null) { - Log.e(Chart.Companion.LOG_TAG, "Can't select by touch. No data set.") + Log.e(LOG_TAG, "Can't select by touch. No data set.") return null } else { val h = highlighter?.getHighlight(x, y) @@ -121,7 +123,7 @@ open class BarChart : BarLineChartBase, BarDataP * @param barEntry */ open fun getBarBounds(barEntry: BarEntry, outputRect: RectF) { - val set = mData!!.getDataSetForEntry(barEntry) + val set = mData?.getDataSetForEntry(barEntry) if (set == null) { outputRect.set(Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE) @@ -131,7 +133,7 @@ open class BarChart : BarLineChartBase, BarDataP val y = barEntry.y val x = barEntry.x - val barWidth = mData!!.barWidth + val barWidth = mData?.barWidth ?: return val left = x - barWidth / 2f val right = x + barWidth / 2f @@ -257,12 +259,12 @@ open class BarChart : BarLineChartBase, BarDataP // Find the min and max index val yAxisValueFormatter = axisLeft.valueFormatter - val minVal = yAxisValueFormatter!!.getFormattedValue(barData.yMin, null) + val minVal = yAxisValueFormatter.getFormattedValue(barData.yMin, null) val maxVal = yAxisValueFormatter.getFormattedValue(barData.yMax, null) // Data range... val xAxisValueFormatter = xAxis.valueFormatter - val minRange = xAxisValueFormatter!!.getFormattedValue(barData.xMin, null) + val minRange = xAxisValueFormatter.getFormattedValue(barData.xMin, null) val maxRange = xAxisValueFormatter.getFormattedValue(barData.xMax, null) val entries = if (entryCount == 1) "entry" else "entries" diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt index 0c941663cf..776cb2fbb2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt @@ -1,7 +1,6 @@ package com.github.mikephil.charting.charts import android.annotation.SuppressLint -import android.annotation.TargetApi import android.content.Context import android.graphics.Canvas import android.graphics.Color @@ -157,9 +156,9 @@ abstract class BarLineChartBase + mXAxis.calculate(mData.xMin, mData.xMax) - // calculate axis range (min / max) according to provided data - mAxisLeft!!.calculate(mData!!.getYMin(AxisDependency.LEFT), mData!!.getYMax(AxisDependency.LEFT)) - mAxisRight!!.calculate(mData!!.getYMin(AxisDependency.RIGHT), mData!!.getYMax(AxisDependency.RIGHT)) + // calculate axis range (min / max) according to provided data + mAxisLeft.calculate(mData.getYMin(AxisDependency.LEFT), mData.getYMax(AxisDependency.LEFT)) + mAxisRight.calculate(mData.getYMin(AxisDependency.RIGHT), mData.getYMax(AxisDependency.RIGHT)) + } } protected open fun calculateLegendOffsets(offsets: RectF) { @@ -471,47 +459,46 @@ abstract class BarLineChartBase when (legend!!.horizontalAlignment) { + when (legend.orientation) { + LegendOrientation.VERTICAL -> when (legend.horizontalAlignment) { LegendHorizontalAlignment.LEFT -> offsets.left += min( - legend!!.mNeededWidth, - viewPortHandler.chartWidth * legend!!.maxSizePercent - ) + legend!!.xOffset + legend.mNeededWidth, + viewPortHandler.chartWidth * legend.maxSizePercent + ) + legend.xOffset LegendHorizontalAlignment.RIGHT -> offsets.right += min( - legend!!.mNeededWidth, - viewPortHandler.chartWidth * legend!!.maxSizePercent - ) + legend!!.xOffset + legend.mNeededWidth, + viewPortHandler.chartWidth * legend.maxSizePercent + ) + legend.xOffset - LegendHorizontalAlignment.CENTER -> when (legend!!.verticalAlignment) { + LegendHorizontalAlignment.CENTER -> when (legend.verticalAlignment) { LegendVerticalAlignment.TOP -> offsets.top += min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent - ) + legend!!.yOffset + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent + ) + legend.yOffset LegendVerticalAlignment.BOTTOM -> offsets.bottom += min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent - ) + legend!!.yOffset + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent + ) + legend.yOffset else -> {} } - else -> {} } - LegendOrientation.HORIZONTAL -> when (legend!!.verticalAlignment) { + LegendOrientation.HORIZONTAL -> when (legend.verticalAlignment) { LegendVerticalAlignment.TOP -> offsets.top += min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent - ) + legend!!.yOffset + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent + ) + legend.yOffset LegendVerticalAlignment.BOTTOM -> offsets.bottom += min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent - ) + legend!!.yOffset + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent + ) + legend.yOffset else -> {} @@ -536,31 +523,36 @@ abstract class BarLineChartBase { + offsetBottom += xLabelHeight + } + XAxisPosition.TOP -> { + offsetTop += xLabelHeight + } + XAxisPosition.BOTH_SIDED -> { + offsetBottom += xLabelHeight + offsetTop += xLabelHeight + } + else -> {} } } @@ -598,11 +590,11 @@ abstract class BarLineChartBase? { val h = getHighlightByTouchPoint(x, y) if (h != null) { - return mData!!.getDataSetByIndex(h.dataSetIndex) + return mData?.getDataSetByIndex(h.dataSetIndex) } return null } @@ -1271,7 +1261,7 @@ abstract class BarLineChartBase, T : ChartData> : ViewGroup, */ var isLogEnabled: Boolean = false + @Suppress("UsePropertyAccessSyntax") override val width: Int get() = getWidth() + @Suppress("UsePropertyAccessSyntax") override val height: Int get() = getHeight() @@ -123,18 +123,18 @@ abstract class Chart, T : ChartData> : ViewGroup, * paint object used for drawing the description text in the bottom right * corner of the chart */ - protected var mDescPaint: Paint? = null + protected var mDescPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) /** * paint object for drawing the information text when there are no values in * the chart */ - protected var mInfoPaint: Paint? = null + protected var mInfoPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) /** * the object representing the labels on the x-axis */ - protected var mXAxis: XAxis? = null + protected val mXAxis: XAxis = XAxis() /** * if true, touch gestures are enabled on the chart @@ -151,7 +151,7 @@ abstract class Chart, T : ChartData> : ViewGroup, /** * the object responsible for representing the description text */ - var description: Description? = null + var description: Description = Description() /** * Returns the Legend object of the chart. This method can be used to get an @@ -161,8 +161,7 @@ abstract class Chart, T : ChartData> : ViewGroup, /** * the legend object containing all data associated with the legend */ - var legend: Legend? = null - protected set + val legend: Legend = Legend() /** * listener that is called when a value on the chart is selected @@ -188,12 +187,21 @@ abstract class Chart, T : ChartData> : ViewGroup, */ var onChartGestureListener: OnChartGestureListener? = null + /** + * Returns the ViewPortHandler of the chart that is responsible for the + * content area of the chart and its offsets and dimensions. + */ + /** + * object that manages the bounds and drawing constraints of the chart + */ + var viewPortHandler: ViewPortHandler = ViewPortHandler() + protected set + /** * Returns the renderer object responsible for rendering / drawing the * Legend. */ - var legendRenderer: LegendRenderer? = null - protected set + val legendRenderer: LegendRenderer = LegendRenderer(this.viewPortHandler, this.legend) /** * object responsible for rendering the data @@ -203,16 +211,6 @@ abstract class Chart, T : ChartData> : ViewGroup, var highlighter: IHighlighter? = null protected set - /** - * Returns the ViewPortHandler of the chart that is responsible for the - * content area of the chart and its offsets and dimensions. - */ - /** - * object that manages the bounds and drawing constraints of the chart - */ - var viewPortHandler: ViewPortHandler = ViewPortHandler() - protected set - /** * object responsible for animations */ @@ -264,19 +262,9 @@ abstract class Chart, T : ChartData> : ViewGroup, Utils.init(context) mMaxHighlightDistance = Utils.convertDpToPixel(500f) - this.description = Description() - this.legend = Legend() - - this.legendRenderer = LegendRenderer(this.viewPortHandler, this.legend!!) - - mXAxis = XAxis() - - mDescPaint = Paint(Paint.ANTI_ALIAS_FLAG) - - mInfoPaint = Paint(Paint.ANTI_ALIAS_FLAG) - mInfoPaint!!.setColor(Color.rgb(247, 189, 51)) // orange - mInfoPaint!!.setTextAlign(Align.CENTER) - mInfoPaint!!.setTextSize(Utils.convertDpToPixel(12f)) + mInfoPaint.setColor(Color.rgb(247, 189, 51)) // orange + mInfoPaint.textAlign = Align.CENTER + mInfoPaint.textSize = Utils.convertDpToPixel(12f) if (this.isLogEnabled) { Log.i("", "Chart.init()") @@ -350,7 +338,7 @@ abstract class Chart, T : ChartData> : ViewGroup, mData = null mOffsetsCalculated = false this.highlighted = null - mChartTouchListener!!.setLastHighlighted(null) + mChartTouchListener?.setLastHighlighted(null) invalidate() } @@ -359,7 +347,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * chart by calling invalidate(). */ fun clearValues() { - mData!!.clearValues() + mData?.clearValues() invalidate() } @@ -369,11 +357,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * null or contains no entries). */ get() { - if (mData == null) { - return true - } else { - return mData!!.entryCount <= 0 - } + return mData?.let { it.entryCount <= 0 } ?: true } /** @@ -401,12 +385,10 @@ abstract class Chart, T : ChartData> : ViewGroup, * drawn in the chart (if enabled), and creates the default-value-formatter */ protected fun setupDefaultFormatter(min: Float, max: Float) { - val reference: Float - - if (mData == null || mData!!.entryCount < 2) { - reference = max(abs(min), abs(max)) + val reference = if (mData.let { it != null && it.entryCount < 2 }) { + max(abs(min), abs(max)) } else { - reference = abs(max - min) + abs(max - min) } val digits = Utils.getDecimals(reference) @@ -429,18 +411,18 @@ abstract class Chart, T : ChartData> : ViewGroup, if (hasText) { val pt = this.center - when (mInfoPaint!!.textAlign) { + when (mInfoPaint.textAlign) { Align.LEFT -> { pt.x = 0f - canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint!!) + canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint) } Align.RIGHT -> { pt.x *= 2.0.toFloat() - canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint!!) + canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint) } - else -> canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint!!) + else -> canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint) } } @@ -459,27 +441,27 @@ abstract class Chart, T : ChartData> : ViewGroup, protected fun drawDescription(c: Canvas) { // check if description should be drawn - if (this.description != null && description!!.isEnabled) { - val position = description!!.position + if (description.isEnabled) { + val position = description.position - mDescPaint!!.setTypeface(description!!.typeface) - mDescPaint!!.textSize = description!!.textSize - mDescPaint!!.setColor(description!!.textColor) - mDescPaint!!.textAlign = description!!.textAlign + mDescPaint.setTypeface(description.typeface) + mDescPaint.textSize = description.textSize + mDescPaint.setColor(description.textColor) + mDescPaint.textAlign = description.textAlign val x: Float val y: Float // if no position specified, draw on default position if (position == null) { - x = width - viewPortHandler.offsetRight() - description!!.xOffset - y = height - viewPortHandler.offsetBottom() - description!!.yOffset + x = width - viewPortHandler.offsetRight() - description.xOffset + y = height - viewPortHandler.offsetBottom() - description.yOffset } else { x = position.x y = position.y } - c.drawText(description!!.text!!, x, y, mDescPaint!!) + c.drawText(description.text, x, y, mDescPaint) } } @@ -516,7 +498,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * of zero or if the first object is null. */ fun valuesToHighlight(): Boolean { - return this.highlighted != null && highlighted!!.isNotEmpty() + return !highlighted.isNullOrEmpty() } /** @@ -524,9 +506,9 @@ abstract class Chart, T : ChartData> : ViewGroup, */ protected fun setLastHighlighted(highs: Array?) { if (highs == null || highs.isEmpty()) { - mChartTouchListener!!.setLastHighlighted(null) + mChartTouchListener?.setLastHighlighted(null) } else { - mChartTouchListener!!.setLastHighlighted(highs[0]) + mChartTouchListener?.setLastHighlighted(highs[0]) } } @@ -630,7 +612,7 @@ abstract class Chart, T : ChartData> : ViewGroup, */ @JvmOverloads fun highlightValue(x: Float, y: Float, dataSetIndex: Int, dataIndex: Int = -1, callListener: Boolean = true) { - if (dataSetIndex < 0 || dataSetIndex >= mData!!.dataSetCount) { + if (dataSetIndex < 0 || mData.let { it != null && dataSetIndex >= it.dataSetCount }) { highlightValue(null, callListener) } else { highlightValue(Highlight(x, y, dataSetIndex, dataIndex), callListener) @@ -676,17 +658,17 @@ abstract class Chart, T : ChartData> : ViewGroup, this.highlighted = null } else { if (this.isLogEnabled) { - Log.i(LOG_TAG, "Highlighted: " + high) + Log.i(LOG_TAG, "Highlighted: $high") } - e = mData!!.getEntryForHighlight(high) + e = mData?.getEntryForHighlight(high) if (e == null) { this.highlighted = null high = null } else { // set the indices to highlight - this.highlighted = arrayOf(high) + this.highlighted = arrayOf(high) } } @@ -694,10 +676,10 @@ abstract class Chart, T : ChartData> : ViewGroup, if (callListener && mSelectionListener != null) { if (!valuesToHighlight()) { - mSelectionListener!!.onNothingSelected() + mSelectionListener?.onNothingSelected() } else { // notify the listener - mSelectionListener!!.onValueSelected(e, high) + mSelectionListener?.onValueSelected(e, high) } } @@ -715,15 +697,15 @@ abstract class Chart, T : ChartData> : ViewGroup, Log.e(LOG_TAG, "Can't select by touch. No data set.") return null } else { - return this.highlighter!!.getHighlight(x, y) + return this.highlighter?.getHighlight(x, y) } } - var onTouchListener: ChartTouchListener<*> + var onTouchListener: ChartTouchListener<*>? /** * Returns an instance of the currently active touch listener. */ - get() = mChartTouchListener!! + get() = mChartTouchListener /** * Set a new (e.g. custom) ChartTouchListener NOTE: make sure to * setTouchEnabled(true); if you need touch gestures on the chart @@ -749,7 +731,7 @@ abstract class Chart, T : ChartData> : ViewGroup, /** * the view that represents the marker */ - var marker: MutableList? = ArrayList() + var marker: MutableList = ArrayList() protected set /** @@ -758,21 +740,24 @@ abstract class Chart, T : ChartData> : ViewGroup, protected open fun drawMarkers(canvas: Canvas?) { // if there is no marker view or drawing marker is disabled - if (this.marker == null || !this.isDrawMarkersEnabled || !valuesToHighlight()) { + if (!this.isDrawMarkersEnabled || !valuesToHighlight()) { return } - for (i in highlighted!!.indices) { - val highlight = this.highlighted!![i] + val highlighted = highlighted ?: return + val data = mData ?: return + + for (i in highlighted.indices) { + val highlight = highlighted[i] // When changing data sets and calling animation functions, sometimes an erroneous highlight is generated // on the dataset that is removed. Null check to prevent crash - val set = mData!!.getDataSetByIndex(highlight.dataSetIndex) - if (set == null || !set.isVisible) { + val set = data.getDataSetByIndex(highlight.dataSetIndex) + if (!set.isVisible) { continue } - val e = mData!!.getEntryForHighlight(highlight) + val e = data.getEntryForHighlight(highlight) // make sure entry not null before using it if (e == null) { @@ -780,7 +765,7 @@ abstract class Chart, T : ChartData> : ViewGroup, } val entryIndex = set.getEntryIndex(e) - if (entryIndex > set.entryCount * mAnimator!!.phaseX) { + if (entryIndex > set.entryCount * mAnimator.phaseX) { continue } @@ -792,9 +777,9 @@ abstract class Chart, T : ChartData> : ViewGroup, } // callbacks to update the content - if (!marker!!.isEmpty()) { - val markerIndex = i % marker!!.size - val markerItem = marker!!.get(markerIndex) + if (!marker.isEmpty()) { + val markerIndex = i % marker.size + val markerItem = marker[markerIndex] markerItem.refreshContent(e, highlight) // draw the marker @@ -815,7 +800,7 @@ abstract class Chart, T : ChartData> : ViewGroup, /** * Returns the animator responsible for animating chart values. */ - get() = mAnimator!! + get() = mAnimator var dragDecelerationFrictionCoef: Float /** @@ -851,7 +836,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * @param easingY a custom easing function to be used on the animation phase */ fun animateXY(durationMillisX: Int, durationMillisY: Int, easingX: EasingFunction?, easingY: EasingFunction?) { - mAnimator!!.animateXY(durationMillisX, durationMillisY, easingX, easingY) + mAnimator.animateXY(durationMillisX, durationMillisY, easingX, easingY) } /** @@ -863,7 +848,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * @param easing a custom easing function to be used on the animation phase */ fun animateXY(durationMillisX: Int, durationMillisY: Int, easing: EasingFunction?) { - mAnimator!!.animateXY(durationMillisX, durationMillisY, easing) + mAnimator.animateXY(durationMillisX, durationMillisY, easing) } /** @@ -875,7 +860,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * @param easing a custom easing function to be used on the animation phase */ fun animateX(durationMillis: Int, easing: EasingFunction?) { - mAnimator!!.animateX(durationMillis, easing) + mAnimator.animateX(durationMillis, easing) } /** @@ -887,7 +872,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * @param easing a custom easing function to be used on the animation phase */ fun animateY(durationMillis: Int, easing: EasingFunction?) { - mAnimator!!.animateY(durationMillis, easing) + mAnimator.animateY(durationMillis, easing) } /** @@ -897,7 +882,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * API LEVEL 11 (Android 3.0.x) AND HIGHER. */ fun animateX(durationMillis: Int) { - mAnimator!!.animateX(durationMillis) + mAnimator.animateX(durationMillis) } /** @@ -907,7 +892,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * API LEVEL 11 (Android 3.0.x) AND HIGHER. */ fun animateY(durationMillis: Int) { - mAnimator!!.animateY(durationMillis) + mAnimator.animateY(durationMillis) } /** @@ -917,7 +902,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * ONLY WORK FOR API LEVEL 11 (Android 3.0.x) AND HIGHER. */ fun animateXY(durationMillisX: Int, durationMillisY: Int) { - mAnimator!!.animateXY(durationMillisX, durationMillisY) + mAnimator.animateXY(durationMillisX, durationMillisY) } @@ -927,7 +912,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * acquire the XAxis object and modify it (e.g. change the position of the * labels, styling, etc.) */ - get() = mXAxis!! + get() = mXAxis /** * Returns the default IValueFormatter that has been determined by the chart @@ -943,26 +928,26 @@ abstract class Chart, T : ChartData> : ViewGroup, this.mSelectionListener = l } - val yMax: Float + val yMax: Float? /** * returns the current y-max value across all DataSets */ - get() = mData!!.yMax + get() = mData?.yMax - val yMin: Float + val yMin: Float? /** * returns the current y-min value across all DataSets */ - get() = mData!!.yMin + get() = mData?.yMin override val xChartMax: Float - get() = mXAxis!!.mAxisMaximum + get() = mXAxis.mAxisMaximum override val xChartMin: Float - get() = mXAxis!!.axisMinimum + get() = mXAxis.axisMinimum override val xRange: Float - get() = mXAxis!!.mAxisRange + get() = mXAxis.mAxisRange val center: MPPointF /** @@ -1050,21 +1035,21 @@ abstract class Chart, T : ChartData> : ViewGroup, * Sets the color of the no data text. */ fun setNoDataTextColor(color: Int) { - mInfoPaint!!.setColor(color) + mInfoPaint.setColor(color) } /** * Sets the typeface to be used for the no data text. */ fun setNoDataTextTypeface(tf: Typeface?) { - mInfoPaint!!.setTypeface(tf) + mInfoPaint.setTypeface(tf) } /** * alignment of the no data text */ fun setNoDataTextAlignment(align: Align?) { - mInfoPaint!!.textAlign = align + mInfoPaint.textAlign = align } /** @@ -1075,7 +1060,7 @@ abstract class Chart, T : ChartData> : ViewGroup, this.mTouchEnabled = enabled } - fun setMarkers(marker: MutableList?) { + fun setMarkers(marker: MutableList) { this.marker = marker } @@ -1083,12 +1068,12 @@ abstract class Chart, T : ChartData> : ViewGroup, * sets the marker that is displayed when a value is clicked on the chart */ fun setMarker(marker: IMarker?) { - setMarkers(marker?.let { mutableListOf(marker) }) + setMarkers(marker?.let { mutableListOf(it) } ?: mutableListOf()) } @Deprecated("") fun setMarkerView(v: IMarker?) { - setMarker(v) + v?.let { setMarker(it) } ?: setMarkers(mutableListOf()) } @get:Deprecated("") @@ -1238,7 +1223,7 @@ abstract class Chart, T : ChartData> : ViewGroup, val stream: OutputStream? try { - stream = FileOutputStream(Environment.getExternalStorageDirectory().getPath() + pathOnSD + "/" + title + ".png") + stream = FileOutputStream(Environment.getExternalStorageDirectory().path + pathOnSD + "/" + title + ".png") /* * Write bitmap to file using JPEG or PNG and 40% quality hint for @@ -1360,7 +1345,7 @@ abstract class Chart, T : ChartData> : ViewGroup, values.put(MediaStore.Images.Media.DATA, filePath) values.put(MediaStore.Images.Media.SIZE, size) - return getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) != null + return context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) != null } /** @@ -1383,7 +1368,7 @@ abstract class Chart, T : ChartData> : ViewGroup, /** * tasks to be done after the view is setup */ - var jobs: ArrayList = ArrayList() + var jobs: ArrayList = ArrayList() protected set fun removeViewportJob(job: Runnable?) { @@ -1481,8 +1466,8 @@ abstract class Chart, T : ChartData> : ViewGroup, * Link: http://stackoverflow.com/a/6779164/1590502 */ private fun unbindDrawables(view: View) { - if (view.getBackground() != null) { - view.getBackground().setCallback(null) + if (view.background != null) { + view.background.callback = null } if (view is ViewGroup) { for (i in 0.. when (legend!!.horizontalAlignment) { + when (legend.orientation) { + LegendOrientation.VERTICAL -> when (legend.horizontalAlignment) { LegendHorizontalAlignment.LEFT -> offsets.left += min( - legend!!.mNeededWidth, - viewPortHandler.chartWidth * legend!!.maxSizePercent - ) + legend!!.xOffset + legend.mNeededWidth, + viewPortHandler.chartWidth * legend.maxSizePercent + ) + legend.xOffset LegendHorizontalAlignment.RIGHT -> offsets.right += min( - legend!!.mNeededWidth, - viewPortHandler.chartWidth * legend!!.maxSizePercent - ) + legend!!.xOffset + legend.mNeededWidth, + viewPortHandler.chartWidth * legend.maxSizePercent + ) + legend.xOffset - LegendHorizontalAlignment.CENTER -> when (legend!!.verticalAlignment) { + LegendHorizontalAlignment.CENTER -> when (legend.verticalAlignment) { LegendVerticalAlignment.TOP -> offsets.top += min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent - ) + legend!!.yOffset + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent + ) + legend.yOffset LegendVerticalAlignment.BOTTOM -> offsets.bottom += min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent - ) + legend!!.yOffset + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent + ) + legend.yOffset else -> {} } - - else -> {} } - LegendOrientation.HORIZONTAL -> when (legend!!.verticalAlignment) { + LegendOrientation.HORIZONTAL -> when (legend.verticalAlignment) { LegendVerticalAlignment.TOP -> { offsets.top += min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent - ) + legend!!.yOffset + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent + ) + legend.yOffset - if (mAxisLeft!!.isEnabled && mAxisLeft!!.isDrawLabelsEnabled) offsets.top += mAxisLeft!!.getRequiredHeightSpace( - mAxisRendererLeft!!.paintAxisLabels + if (mAxisLeft.isEnabled && mAxisLeft.isDrawLabelsEnabled) offsets.top += mAxisLeft.getRequiredHeightSpace( + mAxisRendererLeft.paintAxisLabels ) } LegendVerticalAlignment.BOTTOM -> { offsets.bottom += min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent - ) + legend!!.yOffset + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent + ) + legend.yOffset - if (mAxisRight!!.isEnabled && mAxisRight!!.isDrawLabelsEnabled) offsets.bottom += mAxisRight!!.getRequiredHeightSpace( - mAxisRendererRight!!.paintAxisLabels + if (mAxisRight.isEnabled && mAxisRight.isDrawLabelsEnabled) offsets.bottom += mAxisRight.getRequiredHeightSpace( + mAxisRendererRight.paintAxisLabels ) } @@ -133,26 +131,31 @@ class HorizontalBarChart : BarChart { offsetBottom += mOffsetsBuffer.bottom // offsets for y-labels - if (mAxisLeft!!.needsOffset()) { - offsetTop += mAxisLeft!!.getRequiredHeightSpace(mAxisRendererLeft!!.paintAxisLabels) + if (mAxisLeft.needsOffset()) { + offsetTop += mAxisLeft.getRequiredHeightSpace(mAxisRendererLeft.paintAxisLabels) } - if (mAxisRight!!.needsOffset()) { - offsetBottom += mAxisRight!!.getRequiredHeightSpace(mAxisRendererRight!!.paintAxisLabels) + if (mAxisRight.needsOffset()) { + offsetBottom += mAxisRight.getRequiredHeightSpace(mAxisRendererRight.paintAxisLabels) } - val xLabelWidth = mXAxis!!.mLabelWidth.toFloat() + val xLabelWidth = mXAxis.mLabelWidth.toFloat() - if (mXAxis!!.isEnabled) { + if (mXAxis.isEnabled) { // offsets for x-labels - if (mXAxis!!.position == XAxisPosition.BOTTOM) { - offsetLeft += xLabelWidth - } else if (mXAxis!!.position == XAxisPosition.TOP) { - offsetRight += xLabelWidth - } else if (mXAxis!!.position == XAxisPosition.BOTH_SIDED) { - offsetLeft += xLabelWidth - offsetRight += xLabelWidth + when (mXAxis.position) { + XAxisPosition.BOTTOM -> { + offsetLeft += xLabelWidth + } + XAxisPosition.TOP -> { + offsetRight += xLabelWidth + } + XAxisPosition.BOTH_SIDED -> { + offsetLeft += xLabelWidth + offsetRight += xLabelWidth + } + else -> {} } } @@ -184,13 +187,13 @@ class HorizontalBarChart : BarChart { } override fun prepareValuePxMatrix() { - mRightAxisTransformer!!.prepareMatrixValuePx( - mAxisRight!!.mAxisMinimum, mAxisRight!!.mAxisRange, mXAxis!!.mAxisRange, - mXAxis!!.mAxisMinimum + mRightAxisTransformer.prepareMatrixValuePx( + mAxisRight.mAxisMinimum, mAxisRight.mAxisRange, mXAxis.mAxisRange, + mXAxis.mAxisMinimum ) - mLeftAxisTransformer!!.prepareMatrixValuePx( - mAxisLeft!!.mAxisMinimum, mAxisLeft!!.mAxisRange, mXAxis!!.mAxisRange, - mXAxis!!.mAxisMinimum + mLeftAxisTransformer.prepareMatrixValuePx( + mAxisLeft.mAxisMinimum, mAxisLeft.mAxisRange, mXAxis.mAxisRange, + mXAxis.mAxisMinimum ) } @@ -200,7 +203,7 @@ class HorizontalBarChart : BarChart { override fun getBarBounds(barEntry: BarEntry, outputRect: RectF) { val bounds = outputRect - val set = mData!!.getDataSetForEntry(barEntry) + val set = mData?.getDataSetForEntry(barEntry) if (set == null) { outputRect.set(Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE, Float.Companion.MIN_VALUE) @@ -210,7 +213,7 @@ class HorizontalBarChart : BarChart { val y = barEntry.y val x = barEntry.x - val barWidth = mData!!.barWidth + val barWidth = mData?.barWidth ?: return val top = x - barWidth / 2f val bottom = x + barWidth / 2f @@ -229,14 +232,14 @@ class HorizontalBarChart : BarChart { * @param axis * @return */ - override fun getPosition(e: Entry, axis: AxisDependency?): MPPointF? { + override fun getPosition(e: Entry, axis: AxisDependency?): MPPointF { val vals = mGetPositionBuffer vals[0] = e.y vals[1] = e.x getTransformer(axis).pointValuesToPixel(vals) - return MPPointF.Companion.getInstance(vals[0], vals[1]) + return MPPointF.getInstance(vals[0], vals[1]) } /** @@ -251,7 +254,7 @@ class HorizontalBarChart : BarChart { if (mData == null) { if (isLogEnabled) Log.e(LOG_TAG, "Can't select by touch. No data set.") return null - } else return highlighter!!.getHighlight(y, x) // switch x and y + } else return highlighter?.getHighlight(y, x) // switch x and y } override val lowestVisibleX: Float @@ -260,7 +263,7 @@ class HorizontalBarChart : BarChart { viewPortHandler.contentLeft(), viewPortHandler.contentBottom(), posForGetLowestVisibleX ) - val result = max(mXAxis!!.mAxisMinimum.toDouble(), posForGetLowestVisibleX.y).toFloat() + val result = max(mXAxis.mAxisMinimum.toDouble(), posForGetLowestVisibleX.y).toFloat() return result } @@ -270,7 +273,7 @@ class HorizontalBarChart : BarChart { viewPortHandler.contentLeft(), viewPortHandler.contentTop(), posForGetHighestVisibleX ) - val result = min(mXAxis!!.mAxisMaximum.toDouble(), posForGetHighestVisibleX.y).toFloat() + val result = min(mXAxis.mAxisMaximum.toDouble(), posForGetHighestVisibleX.y).toFloat() return result } @@ -278,18 +281,18 @@ class HorizontalBarChart : BarChart { * ###### VIEWPORT METHODS BELOW THIS ###### */ override fun setVisibleXRangeMaximum(maxXRange: Float) { - val xScale = mXAxis!!.mAxisRange / (maxXRange) + val xScale = mXAxis.mAxisRange / (maxXRange) viewPortHandler.setMinimumScaleY(xScale) } override fun setVisibleXRangeMinimum(minXRange: Float) { - val xScale = mXAxis!!.mAxisRange / (minXRange) + val xScale = mXAxis.mAxisRange / (minXRange) viewPortHandler.setMaximumScaleY(xScale) } override fun setVisibleXRange(minXRange: Float, maxXRange: Float) { - val minScale = mXAxis!!.mAxisRange / minXRange - val maxScale = mXAxis!!.mAxisRange / maxXRange + val minScale = mXAxis.mAxisRange / minXRange + val maxScale = mXAxis.mAxisRange / maxXRange viewPortHandler.setMinMaxScaleY(minScale, maxScale) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt index 04b82c098b..8dcce889a3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt @@ -37,12 +37,12 @@ open class LineChart : BarLineChartBase, LineData // Min and max values... val yAxisValueFormatter = axisLeft.valueFormatter - val minVal = yAxisValueFormatter!!.getFormattedValue(lineData.yMin, null) + val minVal = yAxisValueFormatter.getFormattedValue(lineData.yMin, null) val maxVal = yAxisValueFormatter.getFormattedValue(lineData.yMax, null) // Data range... val xAxisValueFormatter = xAxis.valueFormatter - val minRange = xAxisValueFormatter!!.getFormattedValue(lineData.xMin, null) + val minRange = xAxisValueFormatter.getFormattedValue(lineData.xMin, null) val maxRange = xAxisValueFormatter.getFormattedValue(lineData.xMax, null) val entries = if (numberOfPoints == 1) "entry" else "entries" return String.format( diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt index c68b6c852c..64ae0bb8a8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt @@ -7,7 +7,6 @@ import android.graphics.Typeface import android.text.TextUtils import android.util.AttributeSet import com.github.mikephil.charting.components.XAxis -import com.github.mikephil.charting.data.ChartData import com.github.mikephil.charting.data.PieData import com.github.mikephil.charting.data.PieEntry import com.github.mikephil.charting.highlight.Highlight @@ -27,7 +26,7 @@ import kotlin.math.sin * * @author Philipp Jahoda */ -class PieChart : PieRadarChartBase { +open class PieChart : PieRadarChartBase { /** * returns the circlebox, the boundingbox of the pie-chart slices * @@ -206,8 +205,7 @@ class PieChart : PieRadarChartBase { override fun init() { super.init() - mRenderer = PieChartRenderer(this, mAnimator!!, viewPortHandler) - mXAxis = null + mRenderer = PieChartRenderer(this, mAnimator, viewPortHandler) highlighter = PieHighlighter(this) } @@ -217,15 +215,15 @@ class PieChart : PieRadarChartBase { if (mData == null) return - mRenderer!!.drawData(canvas) + mRenderer?.drawData(canvas) - if (valuesToHighlight()) mRenderer!!.drawHighlighted(canvas, highlighted!!) + if (valuesToHighlight()) mRenderer?.drawHighlighted(canvas, highlighted!!) - mRenderer!!.drawExtras(canvas) + mRenderer?.drawExtras(canvas) - mRenderer!!.drawValues(canvas) + mRenderer?.drawValues(canvas) - legendRenderer!!.renderLegend(canvas) + legendRenderer.renderLegend(canvas) drawDescription(canvas) @@ -243,12 +241,12 @@ class PieChart : PieRadarChartBase { val c = centerOffsets - val shift = mData!!.dataSet!!.selectionShift + val shift = mData!!.dataSet.selectionShift // create the circle box that will contain the pie-chart (the bounds of // the pie-chart) - circleBox!!.set( - c!!.x - radius + shift, + circleBox.set( + c.x - radius + shift, c.y - radius + shift, c.x + radius - shift, c.y + radius - shift @@ -285,14 +283,14 @@ class PieChart : PieRadarChartBase { * cos( Math.toRadians( ((rotationAngle + this.absoluteAngles[entryIndex] - offset) - * mAnimator!!.phaseY).toDouble() + * mAnimator.phaseY).toDouble() ) ) + center.x).toFloat() val y = (r * sin( Math.toRadians( ((rotationAngle + this.absoluteAngles[entryIndex] - offset) - * mAnimator!!.phaseY).toDouble() + * mAnimator.phaseY).toDouble() ) ) + center.y).toFloat() @@ -304,76 +302,78 @@ class PieChart : PieRadarChartBase { * calculates the needed angles for the chart slices */ private fun calcAngles() { - val entryCount = mData!!.entryCount - - if (drawAngles.size != entryCount) { - this.drawAngles = FloatArray(entryCount) - } else { - for (i in 0.. + val entryCount = mData.entryCount + + if (drawAngles.size != entryCount) { + this.drawAngles = FloatArray(entryCount) + } else { + for (i in 0.. { * @param value * @return */ - private fun calcAngle(value: Float, yValueSum: Float = mData!!.yValueSum): Float { + private fun calcAngle(value: Float, yValueSum: Float = mData?.yValueSum ?: 0f): Float { return value / yValueSum * mMaxAngle } @@ -439,7 +439,7 @@ class PieChart : PieRadarChartBase { * @return */ fun getDataSetIndexForIndex(xIndex: Int): Int { - val dataSets = mData!!.dataSets + val dataSets = mData?.dataSets ?: return -1 for (i in dataSets.indices) { if (dataSets[i].getEntryForXValue(xIndex.toFloat(), Float.Companion.NaN) != null) return i @@ -478,8 +478,7 @@ class PieChart : PieRadarChartBase { * @param text */ set(text) { - if (text == null) mCenterText = "" - else mCenterText = text + mCenterText = text ?: "" } /** @@ -493,14 +492,13 @@ class PieChart : PieRadarChartBase { } override val requiredLegendOffset: Float - get() = legendRenderer!!.labelPaint.textSize * 2f + get() = legendRenderer.labelPaint.textSize * 2f override val requiredBaseOffset: Float get() = 0f override val radius: Float - get() = if (this.circleBox == null) 0f - else min(circleBox.width() / 2f, circleBox.height() / 2f) + get() = min(circleBox.width() / 2f, circleBox.height() / 2f) val centerCircleBox: MPPointF /** @@ -508,7 +506,7 @@ class PieChart : PieRadarChartBase { * * @return */ - get() = MPPointF.Companion.getInstance(circleBox!!.centerX(), circleBox.centerY()) + get() = MPPointF.Companion.getInstance(circleBox.centerX(), circleBox.centerY()) /** * sets the typeface for the center-text paint @@ -525,9 +523,7 @@ class PieChart : PieRadarChartBase { * @param sizeDp */ fun setCenterTextSize(sizeDp: Float) { - (mRenderer as PieChartRenderer).paintCenterText.setTextSize( - Utils.convertDpToPixel(sizeDp) - ) + (mRenderer as PieChartRenderer).paintCenterText.textSize = Utils.convertDpToPixel(sizeDp) } /** @@ -536,7 +532,7 @@ class PieChart : PieRadarChartBase { * @param sizePixels */ fun setCenterTextSizePixels(sizePixels: Float) { - (mRenderer as PieChartRenderer).paintCenterText.setTextSize(sizePixels) + (mRenderer as PieChartRenderer).paintCenterText.textSize = sizePixels } /** @@ -574,7 +570,7 @@ class PieChart : PieRadarChartBase { */ fun setTransparentCircleColor(color: Int) { val p = (mRenderer as PieChartRenderer).paintTransparentCircle - val alpha = p.getAlpha() + val alpha = p.alpha p.setColor(color) p.setAlpha(alpha) } @@ -634,7 +630,7 @@ class PieChart : PieRadarChartBase { * @param size */ fun setEntryLabelTextSize(size: Float) { - (mRenderer as PieChartRenderer).paintEntryLabels.setTextSize(Utils.convertDpToPixel(size)) + (mRenderer as PieChartRenderer).paintEntryLabels.textSize = Utils.convertDpToPixel(size) } /** @@ -697,7 +693,7 @@ class PieChart : PieRadarChartBase { this.mMinAngleForSlices = minAngle } - protected override fun onDetachedFromWindow() { + override fun onDetachedFromWindow() { // releases the bitmap in the renderer to avoid oom error if (mRenderer != null && mRenderer is PieChartRenderer) { (mRenderer as PieChartRenderer).releaseBitmap() @@ -717,8 +713,8 @@ class PieChart : PieRadarChartBase { builder.append(String.format(Locale.getDefault(), "The pie chart has %d entries.", entryCount)) for (i in 0.., T : ChartData> } override val maxVisibleCount: Int - get() = mData!!.entryCount + get() = mData?.entryCount ?: 0 override fun onTouchEvent(event: MotionEvent?): Boolean { // use the pie- and radarchart listener own listener - if (mTouchEnabled && mChartTouchListener != null) return mChartTouchListener!!.onTouch(this, event) - else return super.onTouchEvent(event) + return if (mTouchEnabled && mChartTouchListener != null) mChartTouchListener?.onTouch(this, event) == true + else super.onTouchEvent(event) } override fun computeScroll() { @@ -115,7 +111,7 @@ abstract class PieRadarChartBase, T : ChartData> calcMinMax() - if (legend != null) legendRenderer?.computeLegend(mData!!) + legendRenderer.computeLegend(mData!!) calculateOffsets() } @@ -126,20 +122,20 @@ abstract class PieRadarChartBase, T : ChartData> var legendBottom = 0f var legendTop = 0f - if (legend != null && legend!!.isEnabled && !legend!!.isDrawInsideEnabled) { + if (legend.isEnabled && !legend.isDrawInsideEnabled) { val fullLegendWidth = min( - legend!!.mNeededWidth, - viewPortHandler.chartWidth * legend!!.maxSizePercent + legend.mNeededWidth, + viewPortHandler.chartWidth * legend.maxSizePercent ) - when (legend!!.orientation) { + when (legend.orientation) { LegendOrientation.VERTICAL -> { var xLegendOffset = 0f - if (legend!!.horizontalAlignment == LegendHorizontalAlignment.LEFT - || legend!!.horizontalAlignment == LegendHorizontalAlignment.RIGHT + if (legend.horizontalAlignment == LegendHorizontalAlignment.LEFT + || legend.horizontalAlignment == LegendHorizontalAlignment.RIGHT ) { - if (legend!!.verticalAlignment == LegendVerticalAlignment.CENTER) { + if (legend.verticalAlignment == LegendVerticalAlignment.CENTER) { // this is the space between the legend and the chart val spacing = Utils.convertDpToPixel(13f) @@ -149,11 +145,11 @@ abstract class PieRadarChartBase, T : ChartData> val spacing = Utils.convertDpToPixel(8f) val legendWidth = fullLegendWidth + spacing - val legendHeight = legend!!.mNeededHeight + legend!!.mTextHeightMax + val legendHeight = legend.mNeededHeight + legend.mTextHeightMax val center = center - val bottomX = if (legend!!.horizontalAlignment == + val bottomX = if (legend.horizontalAlignment == LegendHorizontalAlignment.RIGHT ) width - legendWidth + 15f @@ -182,32 +178,30 @@ abstract class PieRadarChartBase, T : ChartData> } } - when (legend!!.horizontalAlignment) { + when (legend.horizontalAlignment) { LegendHorizontalAlignment.LEFT -> legendLeft = xLegendOffset LegendHorizontalAlignment.RIGHT -> legendRight = xLegendOffset - LegendHorizontalAlignment.CENTER -> when (legend!!.verticalAlignment) { + LegendHorizontalAlignment.CENTER -> when (legend.verticalAlignment) { LegendVerticalAlignment.TOP -> legendTop = min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent ) LegendVerticalAlignment.BOTTOM -> legendBottom = min( - legend!!.mNeededHeight, - viewPortHandler.chartHeight * legend!!.maxSizePercent + legend.mNeededHeight, + viewPortHandler.chartHeight * legend.maxSizePercent ) else -> {} } - - else -> {} } } LegendOrientation.HORIZONTAL -> { - var yLegendOffset = 0f + var yLegendOffset: Float - if (legend!!.verticalAlignment == LegendVerticalAlignment.TOP || - legend!!.verticalAlignment == LegendVerticalAlignment.BOTTOM + if (legend.verticalAlignment == LegendVerticalAlignment.TOP || + legend.verticalAlignment == LegendVerticalAlignment.BOTTOM ) { // It's possible that we do not need this offset anymore as it // is available through the extraOffsets, but changing it can mean @@ -216,11 +210,11 @@ abstract class PieRadarChartBase, T : ChartData> val yOffset = this.requiredLegendOffset yLegendOffset = min( - legend!!.mNeededHeight + yOffset, - viewPortHandler.chartHeight * legend!!.maxSizePercent + legend.mNeededHeight + yOffset, + viewPortHandler.chartHeight * legend.maxSizePercent ) - when (legend!!.verticalAlignment) { + when (legend.verticalAlignment) { LegendVerticalAlignment.TOP -> legendTop = yLegendOffset LegendVerticalAlignment.BOTTOM -> legendBottom = yLegendOffset else -> {} @@ -275,7 +269,7 @@ abstract class PieRadarChartBase, T : ChartData> fun getAngleForPoint(x: Float, y: Float): Float { val c = centerOffsets - val tx = (x - c!!.x).toDouble() + val tx = (x - c.x).toDouble() val ty = (y - c.y).toDouble() val length = sqrt(tx * tx + ty * ty) val r = acos(ty / length) @@ -327,21 +321,18 @@ abstract class PieRadarChartBase, T : ChartData> fun distanceToCenter(x: Float, y: Float): Float { val c = centerOffsets - var dist = 0f - - var xDist = 0f - var yDist = 0f + var dist: Float - if (x > c!!.x) { - xDist = x - c.x + val xDist = if (x > c.x) { + x - c.x } else { - xDist = c.x - x + c.x - x } - if (y > c.y) { - yDist = y - c.y + val yDist = if (y > c.y) { + y - c.y } else { - yDist = c.y - y + c.y - y } // pythagoras diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt index 2da9afb62b..058e34446d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt @@ -23,7 +23,7 @@ import kotlin.math.min * * @author Philipp Jahoda */ -class RadarChart : PieRadarChartBase { +open class RadarChart : PieRadarChartBase { /** * width of the main web lines */ @@ -87,12 +87,12 @@ class RadarChart : PieRadarChartBase { /** * the object reprsenting the y-axis labels */ - private var mYAxis: YAxis? = null + private val mYAxis: YAxis = YAxis(AxisDependency.LEFT) private var colorList: MutableList = arrayListOf() - protected var mYAxisRenderer: YAxisRendererRadarChart? = null - protected var mXAxisRenderer: XAxisRendererRadarChart? = null + protected val mYAxisRenderer: YAxisRendererRadarChart = YAxisRendererRadarChart(viewPortHandler, mYAxis, this) + protected val mXAxisRenderer: XAxisRendererRadarChart = XAxisRendererRadarChart(viewPortHandler, mXAxis, this) constructor(context: Context) : super(context) @@ -103,15 +103,12 @@ class RadarChart : PieRadarChartBase { override fun init() { super.init() - mYAxis = YAxis(AxisDependency.LEFT) - mYAxis!!.labelXOffset = 10f + mYAxis.labelXOffset = 10f mWebLineWidth = Utils.convertDpToPixel(1.5f) mInnerWebLineWidth = Utils.convertDpToPixel(0.75f) mRenderer = RadarChartRenderer(this, mAnimator, viewPortHandler) - mYAxisRenderer = YAxisRendererRadarChart(viewPortHandler, mYAxis!!, this) - mXAxisRenderer = XAxisRendererRadarChart(viewPortHandler, mXAxis!!, this) highlighter = RadarHighlighter(this) } @@ -119,8 +116,10 @@ class RadarChart : PieRadarChartBase { override fun calcMinMax() { super.calcMinMax() - mYAxis!!.calculate(mData!!.getYMin(AxisDependency.LEFT), mData!!.getYMax(AxisDependency.LEFT)) - mXAxis!!.calculate(0f, mData!!.maxEntryCountSet!!.entryCount.toFloat()) + mData?.let { mData -> + mYAxis.calculate(mData.getYMin(AxisDependency.LEFT), mData.getYMax(AxisDependency.LEFT)) + mXAxis.calculate(0f, mData.maxEntryCountSet?.entryCount?.toFloat() ?: 0f) + } } override fun notifyDataSetChanged() { @@ -128,40 +127,40 @@ class RadarChart : PieRadarChartBase { calcMinMax() - mYAxisRenderer!!.computeAxis(mYAxis!!.mAxisMinimum, mYAxis!!.mAxisMaximum, mYAxis!!.isInverted) - mXAxisRenderer!!.computeAxis(mXAxis!!.mAxisMinimum, mXAxis!!.mAxisMaximum, false) + mYAxisRenderer.computeAxis(mYAxis.mAxisMinimum, mYAxis.mAxisMaximum, mYAxis.isInverted) + mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false) - if (legend != null && !legend!!.isLegendCustom) legendRenderer!!.computeLegend(mData!!) + if (!legend.isLegendCustom) legendRenderer.computeLegend(mData!!) calculateOffsets() } - protected override fun onDraw(canvas: Canvas) { + override fun onDraw(canvas: Canvas) { super.onDraw(canvas) if (mData == null) return // if (mYAxis.isEnabled()) // mYAxisRenderer.computeAxis(mYAxis.mAxisMinimum, mYAxis.mAxisMaximum, mYAxis.isInverted()); - if (mXAxis!!.isEnabled) mXAxisRenderer!!.computeAxis(mXAxis!!.mAxisMinimum, mXAxis!!.mAxisMaximum, false) + if (mXAxis.isEnabled) mXAxisRenderer.computeAxis(mXAxis.mAxisMinimum, mXAxis.mAxisMaximum, false) - mXAxisRenderer!!.renderAxisLabels(canvas) + mXAxisRenderer.renderAxisLabels(canvas) - if (mDrawWeb) mRenderer!!.drawExtras(canvas) + if (mDrawWeb) mRenderer?.drawExtras(canvas) - if (mYAxis!!.isEnabled && mYAxis!!.isDrawLimitLinesBehindDataEnabled) mYAxisRenderer!!.renderLimitLines(canvas) + if (mYAxis.isEnabled && mYAxis.isDrawLimitLinesBehindDataEnabled) mYAxisRenderer.renderLimitLines(canvas) - mRenderer!!.drawData(canvas) + mRenderer?.drawData(canvas) - if (valuesToHighlight()) mRenderer!!.drawHighlighted(canvas, highlighted!!) + if (valuesToHighlight()) mRenderer?.drawHighlighted(canvas, highlighted!!) - if (mYAxis!!.isEnabled && !mYAxis!!.isDrawLimitLinesBehindDataEnabled) mYAxisRenderer!!.renderLimitLines(canvas) + if (mYAxis.isEnabled && !mYAxis.isDrawLimitLinesBehindDataEnabled) mYAxisRenderer.renderLimitLines(canvas) - mYAxisRenderer!!.renderAxisLabels(canvas) + mYAxisRenderer.renderAxisLabels(canvas) - mRenderer!!.drawValues(canvas) + mRenderer?.drawValues(canvas) - legendRenderer!!.renderLegend(canvas) + legendRenderer.renderLegend(canvas) drawDescription(canvas) @@ -176,7 +175,7 @@ class RadarChart : PieRadarChartBase { */ get() { val content = viewPortHandler.contentRect - return min(content.width() / 2f, content.height() / 2f) / mYAxis!!.mAxisRange + return min(content.width() / 2f, content.height() / 2f) / mYAxis.mAxisRange } val sliceAngle: Float @@ -185,7 +184,7 @@ class RadarChart : PieRadarChartBase { * * @return */ - get() = 360f / mData!!.maxEntryCountSet!!.entryCount.toFloat() + get() = 360f / (mData?.maxEntryCountSet?.entryCount?.toFloat() ?: 1f) val isCustomLayerColorEnable: Boolean @@ -212,7 +211,7 @@ class RadarChart : PieRadarChartBase { val sliceangle = this.sliceAngle - val max = mData!!.maxEntryCountSet!!.entryCount + val max = mData?.maxEntryCountSet?.entryCount ?: return -1 var index = 0 @@ -234,7 +233,7 @@ class RadarChart : PieRadarChartBase { * * @return */ - get() = mYAxis!! + get() = mYAxis var webLineWidth: Float get() = mWebLineWidth @@ -287,10 +286,10 @@ class RadarChart : PieRadarChartBase { } override val requiredLegendOffset: Float - get() = legendRenderer!!.labelPaint.textSize * 4f + get() = legendRenderer.labelPaint.textSize * 4f override val requiredBaseOffset: Float - get() = if (mXAxis!!.isEnabled && mXAxis!!.isDrawLabelsEnabled) mXAxis!!.mLabelWidth.toFloat() else Utils.convertDpToPixel(10f) + get() = if (mXAxis.isEnabled && mXAxis.isDrawLabelsEnabled) mXAxis.mLabelWidth.toFloat() else Utils.convertDpToPixel(10f) override val radius: Float get() { @@ -302,13 +301,13 @@ class RadarChart : PieRadarChartBase { * Returns the maximum value this chart can display on it's y-axis. */ override val yChartMax: Float - get() = mYAxis!!.mAxisMaximum + get() = mYAxis.mAxisMaximum /** * Returns the minimum value this chart can display on it's y-axis. */ override val yChartMin: Float - get() = mYAxis!!.mAxisMinimum + get() = mYAxis.mAxisMinimum val yRange: Float /** @@ -316,7 +315,7 @@ class RadarChart : PieRadarChartBase { * * @return */ - get() = mYAxis!!.mAxisRange + get() = mYAxis.mAxisRange override val accessibilityDescription: String? get() = "This is a Radar chart" diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt index 6a81647608..8d13e64d32 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.kt @@ -529,15 +529,15 @@ abstract class AxisBase : ComponentBase() { * @return */ get() { - var longest: String? = "" + var longest = "" for (i in mEntries.indices) { val text = getFormattedLabel(i) - if (text != null && longest!!.length < text.length) longest = text + if (longest.length < text.length) longest = text } - return longest!! + return longest } /** @@ -551,20 +551,18 @@ abstract class AxisBase : ComponentBase() { if (p == null) { return this.longestLabel } - var longest: String? = "" + var longest = "" val max = 0f for (i in mEntries.indices) { val text = getFormattedLabel(i) - if (text != null) { - val width = p.measureText(text) - if (max < width) { - longest = text - } + val width = p.measureText(text) + if (max < width) { + longest = text } } - return longest!! + return longest } fun getFormattedLabel(index: Int): String { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt index 6bfad3f475..e565b68e83 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Description.kt @@ -22,7 +22,7 @@ class Description : ComponentBase() { /** * the text used in the description */ - var text: String? = "Description Label" + var text: String = "Description Label" /** * Returns the customized position of the description, or null if none set. diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt index 6a27df7c1f..3e36db7a88 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt @@ -11,7 +11,7 @@ interface IMarker { * By returning x: -(width / 2) you will center the IMarker horizontally. * By returning y: -(height / 2) you will center the IMarker vertically. */ - val offset: MPPointF? + val offset: MPPointF /** * @return The offset for drawing at the specific `point`. This allows conditional adjusting of the Marker position. diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt index d277a9c896..5714a1bd42 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt @@ -68,7 +68,7 @@ class Legend() : ComponentBase() { /** * The legend entries array */ - var entries: Array = arrayOf() + var entries: Array = arrayOf() private set /** @@ -446,16 +446,16 @@ class Legend() : ComponentBase() { /** * the total width of the legend (needed width space) */ - var mNeededWidth: kotlin.Float = 0f + var mNeededWidth: Float = 0f /** * the total height of the legend (needed height space) */ - var mNeededHeight: kotlin.Float = 0f + var mNeededHeight: Float = 0f - var mTextHeightMax: kotlin.Float = 0f + var mTextHeightMax: Float = 0f - var mTextWidthMax: kotlin.Float = 0f + var mTextWidthMax: Float = 0f /** * If this is set, then word wrapping the legend is enabled. This means the @@ -604,7 +604,7 @@ class Legend() : ComponentBase() { if (label != null) { calculatedLabelSizes.add(Utils.calcTextSize(labelpaint, label)) requiredWidth += if (drawingForm) formToTextSpace + formSize else 0f - requiredWidth += calculatedLabelSizes[i]!!.width + requiredWidth += calculatedLabelSizes[i].width } else { calculatedLabelSizes.add(FSize.Companion.getInstance(0f, 0f)) requiredWidth += if (drawingForm) formSize else 0f @@ -634,12 +634,10 @@ class Legend() : ComponentBase() { maxLineWidth = max(maxLineWidth, currentLineWidth) // Start a new line - calculatedLabelBreakPoints.set( - if (stackedStartIndex > -1) - stackedStartIndex - else - i, true - ) + calculatedLabelBreakPoints[if (stackedStartIndex > -1) + stackedStartIndex + else + i] = true currentLineWidth = requiredWidth } @@ -657,7 +655,7 @@ class Legend() : ComponentBase() { mNeededWidth = maxLineWidth mNeededHeight = (labelLineHeight * (calculatedLineSizes.size).toFloat() - + labelLineSpacing * (if (calculatedLineSizes.size == 0) + + labelLineSpacing * (if (calculatedLineSizes.isEmpty()) 0 else (calculatedLineSizes.size - 1)).toFloat()) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt index 8e6234e53b..884c404cf7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitLine.kt @@ -166,5 +166,5 @@ class LimitLine : ComponentBase { * * @return */ - get() = if (this.dashPathEffect == null) false else true + get() = this.dashPathEffect != null } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt index 06dc3d98ae..3a538869c0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/LimitRange.kt @@ -209,5 +209,5 @@ class LimitRange : ComponentBase { * * @return */ - get() = if (this.dashPathEffect == null) false else true + get() = this.dashPathEffect != null } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt index 2098798d20..a7d84ebc50 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt @@ -4,14 +4,13 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Rect import android.graphics.drawable.Drawable -import android.os.Build +import androidx.core.graphics.withTranslation import com.github.mikephil.charting.charts.Chart import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.utils.FSize import com.github.mikephil.charting.utils.MPPointF import java.lang.ref.WeakReference -import androidx.core.graphics.withTranslation /** * View that can be displayed when selecting values in the chart. Extend this class to provide custom layouts for your @@ -20,88 +19,67 @@ import androidx.core.graphics.withTranslation * @author Philipp Jahoda */ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMarker { - private var mDrawable: Drawable? = null + private var mDrawable: Drawable = + mContext.resources.getDrawable(drawableResourceId, null) - private var mOffset: MPPointF? = MPPointF() + private var mOffset: MPPointF = MPPointF() private val mOffset2 = MPPointF() private var mWeakChart: WeakReference?>? = null - private var mSize: FSize? = FSize() + private var mSize: FSize = FSize() private val mDrawableBoundsCache = Rect() - /** - * Constructor. Sets up the MarkerView with a custom layout resource. - * - * @param mContext - * @param drawableResourceId the drawable resource to render - */ - init { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - mDrawable = mContext.getResources().getDrawable(drawableResourceId, null) - } else { - mDrawable = mContext.getResources().getDrawable(drawableResourceId) - } - } - fun setOffset(offset: MPPointF?) { - mOffset = offset - - if (mOffset == null) { - mOffset = MPPointF() - } + mOffset = offset ?: MPPointF() } fun setOffset(offsetX: Float, offsetY: Float) { - mOffset!!.x = offsetX - mOffset!!.y = offsetY + mOffset.x = offsetX + mOffset.y = offsetY } - override val offset: MPPointF? + override val offset: MPPointF get() = mOffset var size: FSize? get() = mSize set(size) { - mSize = size - - if (mSize == null) { - mSize = FSize() - } + mSize = size ?: FSize() } var chartView: Chart<*, *, *>? - get() = if (mWeakChart == null) null else mWeakChart!!.get() + get() = mWeakChart?.get() set(chart) { mWeakChart = WeakReference?>(chart) } override fun getOffsetForDrawingAtPoint(posX: Float, posY: Float): MPPointF { - val offset = offset!! + val offset = offset mOffset2.x = offset.x mOffset2.y = offset.y val chart = this.chartView - var width = mSize!!.width - var height = mSize!!.height + var width = mSize.width + var height = mSize.height - if (width == 0f && mDrawable != null) { - width = mDrawable!!.getIntrinsicWidth().toFloat() + if (width == 0f) { + width = mDrawable.intrinsicWidth.toFloat() } - if (height == 0f && mDrawable != null) { - height = mDrawable!!.getIntrinsicHeight().toFloat() + if (height == 0f) { + height = mDrawable.intrinsicHeight.toFloat() } if (posX + mOffset2.x < 0) { mOffset2.x = -posX - } else if (chart != null && posX + width + mOffset2.x > chart.getWidth()) { - mOffset2.x = chart.getWidth() - posX - width + } else if (chart != null && posX + width + mOffset2.x > chart.width) { + mOffset2.x = chart.width - posX - width } if (posY + mOffset2.y < 0) { mOffset2.y = -posY - } else if (chart != null && posY + height + mOffset2.y > chart.getHeight()) { - mOffset2.y = chart.getHeight() - posY - height + } else if (chart != null && posY + height + mOffset2.y > chart.height) { + mOffset2.y = chart.height - posY - height } return mOffset2 @@ -111,22 +89,20 @@ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMar } override fun draw(canvas: Canvas?, posX: Float, posY: Float) { - if (mDrawable == null) return - val offset = getOffsetForDrawingAtPoint(posX, posY) - var width = mSize!!.width - var height = mSize!!.height + var width = mSize.width + var height = mSize.height if (width == 0f) { - width = mDrawable!!.getIntrinsicWidth().toFloat() + width = mDrawable.intrinsicWidth.toFloat() } if (height == 0f) { - height = mDrawable!!.getIntrinsicHeight().toFloat() + height = mDrawable.intrinsicHeight.toFloat() } - mDrawable!!.copyBounds(mDrawableBoundsCache) - mDrawable!!.setBounds( + mDrawable.copyBounds(mDrawableBoundsCache) + mDrawable.setBounds( mDrawableBoundsCache.left, mDrawableBoundsCache.top, mDrawableBoundsCache.left + width.toInt(), @@ -135,9 +111,9 @@ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMar canvas?.withTranslation(posX + offset.x, posY + offset.y) { // translate to the correct position and draw - mDrawable!!.draw(canvas) + mDrawable.draw(canvas) } - mDrawable!!.bounds = mDrawableBoundsCache + mDrawable.bounds = mDrawableBoundsCache } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt index a09634beec..ad64372b13 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt @@ -18,7 +18,7 @@ import androidx.core.graphics.withTranslation * @author Philipp Jahoda */ open class MarkerView(context: Context?, layoutResource: Int) : RelativeLayout(context), IMarker { - private var mOffset: MPPointF? = MPPointF() + private var mOffset: MPPointF = MPPointF() private val mOffset2 = MPPointF() private var mWeakChart: WeakReference?>? = null @@ -48,29 +48,25 @@ open class MarkerView(context: Context?, layoutResource: Int) : RelativeLayout(c } fun setOffset(offset: MPPointF?) { - mOffset = offset - - if (mOffset == null) { - mOffset = MPPointF() - } + mOffset = offset ?: MPPointF() } fun setOffset(offsetX: Float, offsetY: Float) { - mOffset!!.x = offsetX - mOffset!!.y = offsetY + mOffset.x = offsetX + mOffset.y = offsetY } - override val offset: MPPointF? + override val offset: MPPointF get() = mOffset var chartView: Chart<*, *, *>? - get() = if (mWeakChart == null) null else mWeakChart!!.get() + get() = mWeakChart?.get() set(chart) { mWeakChart = WeakReference?>(chart) } override fun getOffsetForDrawingAtPoint(posX: Float, posY: Float): MPPointF { - val offset = offset!! + val offset = offset mOffset2.x = offset.x mOffset2.y = offset.y diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt index 733772e7b6..76531625a6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt @@ -18,7 +18,7 @@ import kotlin.math.min * * @author Philipp Jahoda */ -class YAxis : AxisBase { +open class YAxis : AxisBase { /** * returns true if drawing the bottom y-axis label entry is enabled * @@ -279,7 +279,7 @@ class YAxis : AxisBase { * @return */ fun getRequiredWidthSpace(p: Paint): Float { - p.setTextSize(mTextSize) + p.textSize = mTextSize val label = getLongestLabel(p) var width = Utils.calcTextWidth(p, label).toFloat() + xOffset * 2f @@ -303,7 +303,7 @@ class YAxis : AxisBase { * @return */ fun getRequiredHeightSpace(p: Paint): Float { - p.setTextSize(mTextSize) + p.textSize = mTextSize val label = getLongestLabel(p) return Utils.calcTextHeight(p, label).toFloat() + yOffset * 2f @@ -315,8 +315,7 @@ class YAxis : AxisBase { * @return */ fun needsOffset(): Boolean { - if (isEnabled && isDrawLabelsEnabled && this.labelPosition == YAxisLabelPosition.OUTSIDE_CHART) return true - else return false + return isEnabled && isDrawLabelsEnabled && this.labelPosition == YAxisLabelPosition.OUTSIDE_CHART } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt index f0bd4f7132..7e401c9e65 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.kt @@ -42,8 +42,8 @@ class BarData : BarLineScatterCandleBubbleData { throw RuntimeException("BarData needs to hold at least 2 BarDataSets to allow grouping.") } - val max = maxEntryCountSet - val maxEntryCount = max!!.entryCount + val max = maxEntryCountSet ?: return + val maxEntryCount = max.entryCount val groupSpaceWidthHalf = groupSpace / 2f val barSpaceHalf = barSpace / 2f diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt index 6d839cdd95..aa96c560cc 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarDataSet.kt @@ -46,15 +46,14 @@ open class BarDataSet(yVals: MutableList, label: String) : BarLineScat /** * array of labels used to describe the different values of the stacked bars */ - private var mStackLabels: Array? = arrayOf() + private var mStackLabels: Array? = arrayOf() /** * This method is deprecated. * Use getFills() instead. */ @get:Deprecated("") - var gradients: MutableList? = null - protected set + val gradients: MutableList = mutableListOf() init { highLightColor = Color.rgb(0, 0, 0) @@ -74,7 +73,7 @@ open class BarDataSet(yVals: MutableList, label: String) : BarLineScat } protected fun copy(barDataSet: BarDataSet) { - super.copy((barDataSet as BaseDataSet<*>?)!!) + super.copy(barDataSet) barDataSet.mStackSize = mStackSize barDataSet.mBarShadowColor = mBarShadowColor barDataSet.mBarBorderWidth = mBarBorderWidth @@ -82,11 +81,11 @@ open class BarDataSet(yVals: MutableList, label: String) : BarLineScat barDataSet.mHighLightAlpha = mHighLightAlpha } - override val fills: MutableList? + override val fills: MutableList get() = gradients - override fun getFill(index: Int): Fill? { - return gradients!!.get(index % gradients!!.size) + override fun getFill(index: Int): Fill { + return gradients[index % gradients.size] } /** @@ -96,7 +95,7 @@ open class BarDataSet(yVals: MutableList, label: String) : BarLineScat * @param index */ @Deprecated("") - fun getGradient(index: Int): Fill? { + fun getGradient(index: Int): Fill { return getFill(index) } @@ -107,8 +106,8 @@ open class BarDataSet(yVals: MutableList, label: String) : BarLineScat * @param endColor */ fun setGradientColor(startColor: Int, endColor: Int) { - gradients!!.clear() - gradients!!.add(Fill(startColor, endColor)) + gradients.clear() + gradients.add(Fill(startColor, endColor)) } /** @@ -118,8 +117,9 @@ open class BarDataSet(yVals: MutableList, label: String) : BarLineScat * @param gradientColors */ @Deprecated("") - fun setGradientColors(gradientColors: MutableList?) { - this.gradients = gradientColors + fun setGradientColors(gradientColors: MutableList) { + this.gradients.clear() + this.gradients.addAll(gradientColors) } /** @@ -127,8 +127,9 @@ open class BarDataSet(yVals: MutableList, label: String) : BarLineScat * * @param fills */ - fun setFills(fills: MutableList?) { - this.gradients = fills + fun setFills(fills: MutableList) { + this.gradients.clear() + this.gradients.addAll(fills) } /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt index 043cabdc0f..996584c2d9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarEntry.kt @@ -11,7 +11,7 @@ import kotlin.math.abs * @author Philipp Jahoda */ @SuppressLint("ParcelCreator") -class BarEntry : Entry { +open class BarEntry : Entry { /** * Returns the stacked values this BarEntry represents, or null, if only a single value is represented (then, use * getY()). @@ -148,7 +148,7 @@ class BarEntry : Entry { /** * Returns an exact copy of the BarEntry. */ - public override fun copy(): BarEntry { + override fun copy(): BarEntry { val copied = BarEntry(x, y, data) copied.setVals(this.yVals) return copied diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt index 51dedbdc00..0f16389189 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarLineScatterCandleBubbleDataSet.kt @@ -23,7 +23,7 @@ abstract class BarLineScatterCandleBubbleDataSet override var highLightColor: Int = Color.rgb(255, 187, 115) protected fun copy(barLineScatterCandleBubbleDataSet: BarLineScatterCandleBubbleDataSet<*>) { - super.copy((barLineScatterCandleBubbleDataSet as BaseDataSet<*>?)!!) + super.copy(barLineScatterCandleBubbleDataSet) barLineScatterCandleBubbleDataSet.highLightColor = this.highLightColor } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt index d431e7a6ef..b66d4abd6a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt @@ -226,7 +226,7 @@ abstract class BaseDataSet() : IDataSet { */ override fun getIndexInEntries(xIndex: Int): Int { for (i in 0.. { */ fun setHighlightCircleWidth(width: Float) { for (set in dataSets) { - set?.highlightCircleWidth = width + set.highlightCircleWidth = width } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt index c79e7001ec..58d176263c 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleDataSet.kt @@ -1,7 +1,6 @@ package com.github.mikephil.charting.data import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet -import com.github.mikephil.charting.utils.Utils open class BubbleDataSet(yVals: MutableList, label: String) : BarLineScatterCandleBubbleDataSet(yVals, label), IBubbleDataSet { protected var mMaxSize: Float = 0f diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt index 4c26e571dc..19ac343b2f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BubbleEntry.kt @@ -68,7 +68,7 @@ class BubbleEntry : Entry { this.size = size } - public override fun copy(): BubbleEntry { + override fun copy(): BubbleEntry { val c = BubbleEntry(x, y, this.size, data) return c } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt index 2919121340..e665116b82 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleDataSet.kt @@ -10,7 +10,7 @@ import com.github.mikephil.charting.utils.Utils * * @author Philipp Jahoda */ -class CandleDataSet(yVals: MutableList, label: String) : LineScatterCandleRadarDataSet(yVals, label), ICandleDataSet { +open class CandleDataSet(yVals: MutableList, label: String) : LineScatterCandleRadarDataSet(yVals, label), ICandleDataSet { /** * the width of the shadow of the candle */ diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt index 5c7be61834..2a5e38e751 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CandleEntry.kt @@ -137,7 +137,7 @@ class CandleEntry : Entry { */ get() = abs(this.open - this.close) - public override fun copy(): CandleEntry { + override fun copy(): CandleEntry { val c = CandleEntry( x, this.high, this.low, this.open, this.close, data diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt index 255a129faf..4f677919b8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ChartData.kt @@ -125,7 +125,7 @@ abstract class ChartData>( */ fun calcMinMaxY(fromX: Float, toX: Float) { for (set in this.dataSets) { - set!!.calcMinMaxY(fromX, toX) + set.calcMinMaxY(fromX, toX) } // apply the new data @@ -158,7 +158,7 @@ abstract class ChartData>( mLeftAxisMin = firstLeft.yMin for (dataSet in this.dataSets) { - if (dataSet!!.axisDependency == AxisDependency.LEFT) { + if (dataSet.axisDependency == AxisDependency.LEFT) { if (dataSet.yMin < mLeftAxisMin) mLeftAxisMin = dataSet.yMin if (dataSet.yMax > mLeftAxisMax) mLeftAxisMax = dataSet.yMax @@ -174,7 +174,7 @@ abstract class ChartData>( mRightAxisMin = firstRight.yMin for (dataSet in this.dataSets) { - if (dataSet!!.axisDependency == AxisDependency.RIGHT) { + if (dataSet.axisDependency == AxisDependency.RIGHT) { if (dataSet.yMin < mRightAxisMin) mRightAxisMin = dataSet.yMin if (dataSet.yMax > mRightAxisMax) mRightAxisMax = dataSet.yMax @@ -219,14 +219,14 @@ abstract class ChartData>( * @return */ fun getYMax(axis: AxisDependency?): Float { - if (axis == AxisDependency.LEFT) { + return if (axis == AxisDependency.LEFT) { if (mLeftAxisMax == -Float.Companion.MAX_VALUE) { - return mRightAxisMax - } else return mLeftAxisMax + mRightAxisMax + } else mLeftAxisMax } else { if (mRightAxisMax == -Float.Companion.MAX_VALUE) { - return mLeftAxisMax - } else return mRightAxisMax + mLeftAxisMax + } else mRightAxisMax } } @@ -242,13 +242,13 @@ abstract class ChartData>( * @return */ protected fun getDataSetIndexByLabel( - dataSets: MutableList, label: String, + dataSets: MutableList, label: String, ignorecase: Boolean ): Int { if (ignorecase) { - for (i in dataSets.indices) if (label.equals(dataSets[i]!!.label, ignoreCase = true)) return i + for (i in dataSets.indices) if (label.equals(dataSets[i].label, ignoreCase = true)) return i } else { - for (i in dataSets.indices) if (label == dataSets[i]!!.label) return i + for (i in dataSets.indices) if (label == dataSets[i].label) return i } return -1 @@ -264,7 +264,7 @@ abstract class ChartData>( val types = arrayOfNulls(dataSets.size) for (i in dataSets.indices) { - types[i] = dataSets[i]!!.label + types[i] = dataSets[i].label } return types @@ -279,7 +279,7 @@ abstract class ChartData>( open fun getEntryForHighlight(highlight: Highlight): E? { return if (highlight.dataSetIndex >= dataSets.size) null else { - dataSets[highlight.dataSetIndex]!!.getEntryForXValue(highlight.x, highlight.y) + dataSets[highlight.dataSetIndex].getEntryForXValue(highlight.x, highlight.y) } } @@ -325,10 +325,10 @@ abstract class ChartData>( * * @param d */ - open fun removeDataSet(d: IDataSet?): Boolean { + open fun removeDataSet(d: IDataSet?): Boolean { if (d == null) return false - val removed = dataSets.remove(d) + val removed = dataSets.removeAll { it == d } // if a DataSet was removed if (removed) { @@ -348,8 +348,10 @@ abstract class ChartData>( open fun removeDataSet(index: Int): Boolean { if (index >= dataSets.size || index < 0) return false - val set = dataSets[index] - return removeDataSet(set) + dataSets.removeAt(index) + notifyDataChanged() + + return true } /** @@ -398,8 +400,8 @@ abstract class ChartData>( * * @param d */ - protected fun calcMinMax(d: T?) { - if (this.yMax < d!!.yMax) this.yMax = d.yMax + protected fun calcMinMax(d: T) { + if (this.yMax < d.yMax) this.yMax = d.yMax if (this.yMin > d.yMin) this.yMin = d.yMin if (this.xMax < d.xMax) this.xMax = d.xMax @@ -427,16 +429,14 @@ abstract class ChartData>( val set = dataSets[dataSetIndex] - if (set != null) { - // remove the entry from the dataset - val removed: Boolean = set.removeEntry(e) + // remove the entry from the dataset + val removed: Boolean = set.removeEntry(e) - if (removed) { - notifyDataChanged() - } + if (removed) { + notifyDataChanged() + } - return removed - } else return false + return removed } /** @@ -451,7 +451,7 @@ abstract class ChartData>( open fun removeEntry(xValue: Float, dataSetIndex: Int): Boolean { if (dataSetIndex >= dataSets.size) return false - val dataSet = dataSets[dataSetIndex]!! + val dataSet = dataSets[dataSetIndex] val e = dataSet.getEntryForXValue(xValue, Float.Companion.NaN) if (e == null) return false @@ -472,7 +472,7 @@ abstract class ChartData>( for (i in dataSets.indices) { val set = dataSets[i] - for (j in 0..>( var clrcnt = 0 for (i in dataSets.indices) { - clrcnt += dataSets[i]!!.colors!!.size + clrcnt += dataSets[i].colors.size } val colors = IntArray(clrcnt) var cnt = 0 for (i in dataSets.indices) { - val clrs = dataSets[i]!!.colors!! + val clrs = dataSets[i].colors for (clr in clrs) { - colors[cnt] = clr!! + colors[cnt] = clr cnt++ } } @@ -525,9 +525,9 @@ abstract class ChartData>( * * @return */ - protected fun getFirstLeft(sets: MutableList): T? { + protected fun getFirstLeft(sets: MutableList): T? { for (dataSet in sets) { - if (dataSet!!.axisDependency == AxisDependency.LEFT) return dataSet + if (dataSet.axisDependency == AxisDependency.LEFT) return dataSet } return null } @@ -538,9 +538,9 @@ abstract class ChartData>( * * @return */ - fun getFirstRight(sets: MutableList): T? { + fun getFirstRight(sets: MutableList): T? { for (dataSet in sets) { - if (dataSet!!.axisDependency == AxisDependency.RIGHT) return dataSet + if (dataSet.axisDependency == AxisDependency.RIGHT) return dataSet } return null } @@ -554,7 +554,7 @@ abstract class ChartData>( if (f == null) return else { for (set in this.dataSets) { - set!!.valueFormatter = f + set.valueFormatter = f } } } @@ -567,7 +567,7 @@ abstract class ChartData>( */ fun setValueTextColor(color: Int) { for (set in this.dataSets) { - set!!.valueTextColor = color + set.valueTextColor = color } } @@ -628,7 +628,7 @@ abstract class ChartData>( */ get() { for (set in this.dataSets) { - if (!set!!.isHighlightEnabled) return false + if (!set.isHighlightEnabled) return false } return true } @@ -639,7 +639,7 @@ abstract class ChartData>( */ set(enabled) { for (set in this.dataSets) { - set!!.isHighlightEnabled = enabled + set.isHighlightEnabled = enabled } } @@ -677,7 +677,7 @@ abstract class ChartData>( var count = 0 for (set in this.dataSets) { - count += set!!.entryCount + count += set.entryCount } return count @@ -695,7 +695,7 @@ abstract class ChartData>( var max = dataSets[0] for (set in this.dataSets) { - if (set!!.entryCount > max!!.entryCount) max = set + if (set.entryCount > max.entryCount) max = set } return max diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt index dfb78e4d51..4636bd4d58 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/CombinedData.kt @@ -9,7 +9,6 @@ import java.lang.Float import kotlin.Boolean import kotlin.Deprecated import kotlin.Int -import kotlin.unaryMinus /** * Data object that allows the combination of Line-, Bar-, Scatter-, Bubble- and @@ -72,6 +71,7 @@ class CombinedData : BarLineScatterCandleBubbleData>) if (data.yMax > yMax) yMax = data.yMax @@ -121,11 +121,11 @@ class CombinedData : BarLineScatterCandleBubbleData?): Boolean { + override fun removeDataSet(d: IDataSet?): Boolean { val datas = this.allData var success = false diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt index 5d2e291b04..8039b0c376 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.kt @@ -299,49 +299,47 @@ abstract class DataSet( } } - if (closest != -1) { - val closestEntry: Entry? = mEntries[closest] - if (closestEntry != null) { - val closestXValue = closestEntry.x - if (rounding == Rounding.UP) { - // If rounding up, and found x-value is lower than specified x, and we can go upper... - if (closestXValue < xValue && closest < mEntries.size - 1) { - ++closest - } - } else if (rounding == Rounding.DOWN) { - // If rounding down, and found x-value is upper than specified x, and we can go lower... - if (closestXValue > xValue && closest > 0) { - --closest - } + val closestEntry: Entry? = mEntries[closest] + if (closestEntry != null) { + val closestXValue = closestEntry.x + if (rounding == Rounding.UP) { + // If rounding up, and found x-value is lower than specified x, and we can go upper... + if (closestXValue < xValue && closest < mEntries.size - 1) { + ++closest + } + } else if (rounding == Rounding.DOWN) { + // If rounding down, and found x-value is upper than specified x, and we can go lower... + if (closestXValue > xValue && closest > 0) { + --closest } + } - // Search by closest to y-value - if (!closestToY.isNaN()) { - while (closest > 0 && mEntries[closest - 1].x == closestXValue) closest -= 1 + // Search by closest to y-value + if (!closestToY.isNaN()) { + while (closest > 0 && mEntries[closest - 1].x == closestXValue) closest -= 1 - var closestYValue = closestEntry.y - var closestYIndex = closest + var closestYValue = closestEntry.y + var closestYIndex = closest - while (true) { - closest += 1 - if (closest >= mEntries.size) break + while (true) { + closest += 1 + if (closest >= mEntries.size) break - val value: Entry? = mEntries[closest] + val value: Entry? = mEntries[closest] - if (value == null) { - continue - } + if (value == null) { + continue + } - if (value.x != closestXValue) break + if (value.x != closestXValue) break - if (abs(value.y - closestToY) <= abs(closestYValue - closestToY)) { - closestYValue = closestToY - closestYIndex = closest - } + if (abs(value.y - closestToY) <= abs(closestYValue - closestToY)) { + closestYValue = closestToY + closestYIndex = closest } - - closest = closestYIndex } + + closest = closestYIndex } } return closest diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt index 0bc6b77523..9f47fd7746 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt @@ -10,7 +10,7 @@ import com.github.mikephil.charting.interfaces.datasets.ILineDataSet import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.Utils -class LineDataSet(yVals: MutableList, label: String) : LineRadarDataSet(yVals, label), ILineDataSet { +open class LineDataSet(yVals: MutableList, label: String) : LineRadarDataSet(yVals, label), ILineDataSet { /** * Drawing mode for this line dataset */ @@ -95,7 +95,7 @@ class LineDataSet(yVals: MutableList, label: String) : LineRadarDataSet?)!!) + super.copy(lineDataSet) lineDataSet.circleColors = this.circleColors lineDataSet.mCircleHoleColor = mCircleHoleColor lineDataSet.mCircleHoleRadius = mCircleHoleRadius @@ -340,8 +340,7 @@ class LineDataSet(yVals: MutableList, label: String) : LineRadarDataSet(yVals: MutableList, label: String) get() = mDrawFilled protected fun copy(lineRadarDataSet: LineRadarDataSet<*>) { - super.copy((lineRadarDataSet as BaseDataSet<*>?)!!) + super.copy(lineRadarDataSet) lineRadarDataSet.mDrawFilled = mDrawFilled lineRadarDataSet.mFillAlpha = mFillAlpha lineRadarDataSet.mFillColor = mFillColor diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt index 18dae3ef45..226a910821 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineScatterCandleRadarDataSet.kt @@ -90,10 +90,10 @@ abstract class LineScatterCandleRadarDataSet(yVals: MutableList, l * * @return */ - get() = if (this.dashPathEffectHighlight == null) false else true + get() = this.dashPathEffectHighlight != null protected fun copy(lineScatterCandleRadarDataSet: LineScatterCandleRadarDataSet<*>) { - super.copy((lineScatterCandleRadarDataSet as BaseDataSet<*>?)!!) + super.copy(lineScatterCandleRadarDataSet) lineScatterCandleRadarDataSet.isHorizontalHighlightIndicatorEnabled = this.isHorizontalHighlightIndicatorEnabled lineScatterCandleRadarDataSet.isVerticalHighlightIndicatorEnabled = this.isVerticalHighlightIndicatorEnabled lineScatterCandleRadarDataSet.mHighlightLineWidth = mHighlightLineWidth diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt index 743e1b880a..1c1e7904aa 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieData.kt @@ -80,7 +80,7 @@ class PieData : ChartData { get() { var sum = 0f - for (i in 0.., label: String) : DataSet?)!!) + super.copy(pieDataSet) } override fun calcMinMax(e: PieEntry) { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt index 88fc5872b2..4bc875c457 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/PieEntry.kt @@ -55,7 +55,7 @@ class PieEntry : Entry { Log.i("DEPRECATED", "Pie entries do not have x values") } - public override fun copy(): PieEntry { + override fun copy(): PieEntry { val e = PieEntry(y, label, data) return e } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt index b695d14123..5538e69e85 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarData.kt @@ -32,6 +32,6 @@ class RadarData : ChartData { } override fun getEntryForHighlight(highlight: Highlight): RadarEntry? { - return getDataSetByIndex(highlight.dataSetIndex)!!.getEntryForIndex(highlight.x.toInt()) + return getDataSetByIndex(highlight.dataSetIndex).getEntryForIndex(highlight.x.toInt()) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt index 41c751fd4d..ccc914f91c 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarDataSet.kt @@ -29,7 +29,7 @@ open class RadarDataSet(yVals: MutableList, label: String) : LineRad } protected fun copy(radarDataSet: RadarDataSet) { - super.copy((radarDataSet as BaseDataSet<*>?)!!) + super.copy(radarDataSet) radarDataSet.isDrawHighlightCircleEnabled = isDrawHighlightCircleEnabled radarDataSet.highlightCircleFillColor = highlightCircleFillColor radarDataSet.highlightCircleInnerRadius = highlightCircleInnerRadius diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt index dc17af9706..563a5ee8c2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/RadarEntry.kt @@ -19,7 +19,7 @@ class RadarEntry : Entry { */ get() = y - public override fun copy(): RadarEntry { + override fun copy(): RadarEntry { val e = RadarEntry(y, data) return e } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt index 309dcf35af..239d22de64 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt @@ -47,7 +47,7 @@ open class ScatterDataSet(yVals: MutableList, label: String) : LineScatte } protected fun copy(scatterDataSet: ScatterDataSet) { - super.copy((scatterDataSet as BaseDataSet<*>?)!!) + super.copy(scatterDataSet) scatterDataSet.scatterShapeSize = scatterShapeSize scatterDataSet.shapeRenderer = shapeRenderer scatterDataSet.scatterShapeHoleRadius = scatterShapeHoleRadius diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt index 2d7cb03d3d..ef1d46d726 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/Approximator.kt @@ -1,6 +1,5 @@ package com.github.mikephil.charting.data.filter -import java.util.Arrays import kotlin.math.abs import kotlin.math.sqrt @@ -15,7 +14,7 @@ class Approximator { var greatestIndex = 0 var greatestDistance = 0f - val line: Line = Line(points[0], points[1], points[points.size - 2], points[points.size - 1]) + val line = Line(points[0], points[1], points[points.size - 2], points[points.size - 1]) var i = 2 while (i < points.size - 2) { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt index a9db514106..0c8b0e1faf 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/filter/ApproximatorN.kt @@ -126,7 +126,7 @@ class ApproximatorN { while (!queue.isEmpty()) { val midIndex = min + (max - min) / 2 - val midLine = queue.get(midIndex) + val midLine = queue[midIndex] if (midLine.equals(line)) { return midIndex diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt index 2e8bad3b3d..a6a670e9c8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/exception/DrawingDataSetNotCreatedException.kt @@ -1,6 +1,8 @@ package com.github.mikephil.charting.exception object DrawingDataSetNotCreatedException : RuntimeException() { + private fun readResolve(): Any = DrawingDataSetNotCreatedException + /** * */ diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.kt index 9ad583e3fa..f095c11928 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.kt @@ -31,7 +31,7 @@ open class DefaultAxisValueFormatter(digits: Int) : IAxisValueFormatter { decimalFormat = DecimalFormat("###,###,###,##0$b") } - override fun getFormattedValue(value: Float, axis: AxisBase?): String? { + override fun getFormattedValue(value: Float, axis: AxisBase?): String { // avoid memory allocations here (for performance) return decimalFormat.format(value.toDouble()) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.kt index 9331c910e3..ede441c6ec 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.kt @@ -7,16 +7,15 @@ import com.github.mikephil.charting.interfaces.datasets.ILineDataSet * Default formatter that calculates the position of the filled line. */ open class DefaultFillFormatter : IFillFormatter { - - override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float { + override fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float { val fillMin: Float - val chartMaxY = dataProvider!!.yChartMax + val chartMaxY = dataProvider.yChartMax val chartMinY = dataProvider.yChartMin - val data = dataProvider.lineData - fillMin = if (dataSet!!.yMax > 0 && dataSet.yMin < 0) { + val data = dataProvider.lineData ?: return 0f + fillMin = if (dataSet.yMax > 0 && dataSet.yMin < 0) { 0f } else { - val max: Float = if (data!!.yMax > 0) 0f + val max: Float = if (data.yMax > 0) 0f else chartMaxY val min: Float = if (data.yMin < 0) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.kt index 3d357a9817..c2c14746f3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.kt @@ -48,6 +48,6 @@ open class DefaultValueFormatter(digits: Int) : IValueFormatter { // put more logic here ... // avoid memory allocations here (for performance reasons) - return decimalFormat!!.format(value.toDouble()) + return decimalFormat?.format(value.toDouble()) } } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IFillFormatter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IFillFormatter.kt index 3487a751da..a41951979d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IFillFormatter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IFillFormatter.kt @@ -15,5 +15,5 @@ interface IFillFormatter { * @param dataProvider * @return */ - fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float + fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.kt index ca20515a9e..d13f1f7291 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/formatter/PercentFormatter.kt @@ -31,7 +31,7 @@ open class PercentFormatter : IValueFormatter, IAxisValueFormatter { } // IAxisValueFormatter - override fun getFormattedValue(value: Float, axis: AxisBase?): String? { + override fun getFormattedValue(value: Float, axis: AxisBase?): String { return decimalFormat.format(value.toDouble()) + " %" } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt index 6a9ed053e8..fe017731e7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/BarHighlighter.kt @@ -1,7 +1,5 @@ package com.github.mikephil.charting.highlight -import com.github.mikephil.charting.data.BarData -import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider import com.github.mikephil.charting.interfaces.datasets.IBarDataSet import com.github.mikephil.charting.utils.MPPointD @@ -19,12 +17,12 @@ open class BarHighlighter(chart: BarDataProvider?) : ChartHighlighter?, value: Float): Int { - if (ranges == null || ranges.size == 0) return 0 + if (ranges == null || ranges.isEmpty()) return 0 var stackIndex = 0 @@ -145,5 +147,5 @@ open class BarHighlighter(chart: BarDataProvider?) : ChartHighlighter( /** * buffer for storing previously highlighted values */ - protected var mHighlightBuffer: MutableList = ArrayList() + protected var mHighlightBuffer: MutableList = ArrayList() override fun getHighlight(x: Float, y: Float): Highlight? { - val pos = getValsForTouch(x, y) + val pos = getValsForTouch(x, y) ?: return null val xVal = pos.x.toFloat() MPPointD.Companion.recycleInstance(pos) @@ -41,10 +40,10 @@ open class ChartHighlighter( * @param y * @return */ - protected fun getValsForTouch(x: Float, y: Float): MPPointD { + protected fun getValsForTouch(x: Float, y: Float): MPPointD? { // take any transformer to determine the x-axis value - val pos = mChart!!.getTransformer(AxisDependency.LEFT)!!.getValuesByTouchPoint(x, y) + val pos = mChart?.getTransformer(AxisDependency.LEFT)?.getValuesByTouchPoint(x, y) return pos } @@ -86,7 +85,7 @@ open class ChartHighlighter( var distance = Float.Companion.MAX_VALUE for (i in closestValues.indices) { - val high = closestValues.get(i) + val high = closestValues[i] if (high.axis == axis) { val tempDistance = abs(getHighlightPos(high) - pos) @@ -125,7 +124,7 @@ open class ChartHighlighter( val dataSet = data.getDataSetByIndex(i) // don't include DataSets that cannot be highlighted - if (!dataSet!!.isHighlightEnabled) { + if (!dataSet.isHighlightEnabled) { i++ continue } @@ -163,7 +162,7 @@ open class ChartHighlighter( for (e in entries) { val pixels = mChart!!.getTransformer( set.axisDependency - )!!.getPixelForValues(e.x, e.y) + ).getPixelForValues(e.x, e.y) highlights.add( Highlight( diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt index 970acbfa28..b157f0ff4b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/CombinedHighlighter.kt @@ -1,11 +1,9 @@ package com.github.mikephil.charting.highlight import com.github.mikephil.charting.data.BarData -import com.github.mikephil.charting.data.ChartData import com.github.mikephil.charting.data.DataSet.Rounding import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider -import com.github.mikephil.charting.interfaces.datasets.IDataSet /** * Created by Philipp Jahoda on 12/09/15. @@ -20,14 +18,14 @@ open class CombinedHighlighter(chart: CombinedDataProvider?, barChart: BarDataPr override fun getHighlightsAtXValue(xVal: Float, x: Float, y: Float): MutableList { mHighlightBuffer.clear() - val dataObjects = mChart!!.combinedData!!.allData + val dataObjects = mChart?.combinedData?.allData ?: return mutableListOf() for (i in dataObjects.indices) { - val dataObject = dataObjects.get(i) + val dataObject = dataObjects[i] // in case of BarData, let the BarHighlighter take over if (barHighlighter != null && dataObject is BarData) { - val high = barHighlighter!!.getHighlight(x, y) + val high = barHighlighter?.getHighlight(x, y) if (high != null) { high.dataIndex = i @@ -40,7 +38,7 @@ open class CombinedHighlighter(chart: CombinedDataProvider?, barChart: BarDataPr val dataSet = dataObjects[i].getDataSetByIndex(j) // don't include datasets that cannot be highlighted - if (!dataSet!!.isHighlightEnabled) { + if (!dataSet.isHighlightEnabled) { j++ continue } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt index 64523bfb23..e9c7d4612d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.kt @@ -12,15 +12,15 @@ import kotlin.math.abs */ class HorizontalBarHighlighter(chart: BarDataProvider?) : BarHighlighter(chart) { override fun getHighlight(x: Float, y: Float): Highlight? { - val barData = mChart!!.barData + val barData = mChart?.barData ?: return null - val pos = getValsForTouch(y, x) + val pos = getValsForTouch(y, x) ?: return null val high = getHighlightForX(pos.y.toFloat(), y, x) if (high == null) return null - val set = barData!!.getDataSetByIndex(high.dataSetIndex) - if (set!!.isStacked) { + val set = barData.getDataSetByIndex(high.dataSetIndex) + if (set.isStacked) { return getStackedHighlight( high, set, @@ -49,17 +49,19 @@ class HorizontalBarHighlighter(chart: BarDataProvider?) : BarHighlighter(chart) if (entries.isEmpty()) return highlights for (e in entries) { - val pixels = mChart!!.getTransformer( + val pixels = mChart?.getTransformer( set.axisDependency - )!!.getPixelForValues(e.y, e.x) + )?.getPixelForValues(e.y, e.x) - highlights.add( - Highlight( - e.x, e.y, - pixels.x.toFloat(), pixels.y.toFloat(), - dataSetIndex, set.axisDependency + if (pixels != null) { + highlights.add( + Highlight( + e.x, e.y, + pixels.x.toFloat(), pixels.y.toFloat(), + dataSetIndex, set.axisDependency + ) ) - ) + } } return highlights diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieHighlighter.kt index 362cdf0dd9..d490246f84 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieHighlighter.kt @@ -4,8 +4,8 @@ import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.data.Entry class PieHighlighter(chart: PieChart) : PieRadarHighlighter(chart) { - override fun getClosestHighlight(index: Int, x: Float, y: Float): Highlight { - val set = mChart!!.data!!.dataSet + override fun getClosestHighlight(index: Int, x: Float, y: Float): Highlight? { + val set = mChart?.data?.dataSet ?: return null val entry: Entry = set.getEntryForIndex(index) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt index 07b7dddcb5..960fee4ccc 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/PieRadarHighlighter.kt @@ -13,27 +13,29 @@ abstract class PieRadarHighlighter?>(protected va protected var mHighlightBuffer: MutableList = ArrayList() override fun getHighlight(x: Float, y: Float): Highlight? { - val touchDistanceToCenter = mChart!!.distanceToCenter(x, y) + val chart = mChart ?: return null + + val touchDistanceToCenter = chart.distanceToCenter(x, y) // check if a slice was touched - if (touchDistanceToCenter > mChart!!.radius) { + if (touchDistanceToCenter > chart.radius) { // if no slice was touched, highlight nothing return null } else { - var angle = mChart!!.getAngleForPoint(x, y) + var angle = chart.getAngleForPoint(x, y) if (mChart is PieChart) { - angle /= mChart!!.animator.phaseY + angle /= chart.animator.phaseY } - val index = mChart!!.getIndexForAngle(angle) + val index = chart.getIndexForAngle(angle) // check if the index could be found - if (index < 0 || index >= mChart!!.data!!.maxEntryCountSet!!.entryCount) { - return null + return if (index < 0 || chart.data?.maxEntryCountSet?.entryCount?.let { it >= index } == true) { + null } else { - return getClosestHighlight(index, x, y) + getClosestHighlight(index, x, y) } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt index e5c6528f2a..a557c59ad1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.kt @@ -10,9 +10,10 @@ import kotlin.math.abs */ open class RadarHighlighter(chart: RadarChart) : PieRadarHighlighter(chart) { override fun getClosestHighlight(index: Int, x: Float, y: Float): Highlight? { + val chart = mChart ?: return null val highlights = getHighlightsAtIndex(index) - val distanceToCenter = mChart!!.distanceToCenter(x, y) / mChart!!.factor + val distanceToCenter = chart.distanceToCenter(x, y) / chart.factor var closest: Highlight? = null var distance = Float.Companion.MAX_VALUE @@ -42,22 +43,25 @@ open class RadarHighlighter(chart: RadarChart) : PieRadarHighlighter protected fun getHighlightsAtIndex(index: Int): MutableList { mHighlightBuffer.clear() - val phaseX = mChart!!.animator.phaseX - val phaseY = mChart!!.animator.phaseY - val sliceangle = mChart!!.sliceAngle - val factor = mChart!!.factor + val chart = mChart ?: return mutableListOf() + + val phaseX = chart.animator.phaseX + val phaseY = chart.animator.phaseY + val sliceangle = chart.sliceAngle + val factor = chart.factor val pOut: MPPointF = MPPointF.Companion.getInstance(0f, 0f) - for (i in 0.. from && value <= to) return true - else return false + return value > from && value <= to } fun isLarger(value: Float): Boolean { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarLineScatterCandleBubbleDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarLineScatterCandleBubbleDataProvider.kt index 23129cbc8c..20ff755f72 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarLineScatterCandleBubbleDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarLineScatterCandleBubbleDataProvider.kt @@ -1,12 +1,11 @@ package com.github.mikephil.charting.interfaces.dataprovider import com.github.mikephil.charting.components.YAxis.AxisDependency -import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData import com.github.mikephil.charting.data.ChartData import com.github.mikephil.charting.utils.Transformer interface BarLineScatterCandleBubbleDataProvider : ChartInterface { - fun getTransformer(axis: AxisDependency?): Transformer? + fun getTransformer(axis: AxisDependency?): Transformer fun isInverted(axis: AxisDependency?): Boolean val lowestVisibleX: Float val highestVisibleX: Float diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt index 9531d15d33..d90e5dfb43 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt @@ -7,9 +7,9 @@ import com.github.mikephil.charting.utils.Fill * Created by philipp on 21/10/15. */ interface IBarDataSet : IBarLineScatterCandleBubbleDataSet { - val fills: MutableList? + val fills: MutableList - fun getFill(index: Int): Fill? + fun getFill(index: Int): Fill /** * Returns true if this DataSet is stacked (stacksize > 1) or not. diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt index fde6a44c80..07767cbeac 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt @@ -25,7 +25,7 @@ class AnimatedMoveViewJob( pts[0] = xOrigin + (xValue - xOrigin) * phase pts[1] = yOrigin + (yValue - yOrigin) * phase - mTrans!!.pointValuesToPixel(pts) + mTrans?.pointValuesToPixel(pts) mViewPortHandler.centerViewPort(pts, view) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt index 627cf1efb8..d89940c59f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt @@ -54,7 +54,7 @@ abstract class AnimatedViewPortJob>( override fun onAnimationEnd(animation: Animator) { try { recycleSelf() - } catch (e: IllegalArgumentException) { + } catch (_: IllegalArgumentException) { // don't worry about it. } } @@ -62,7 +62,7 @@ abstract class AnimatedViewPortJob>( override fun onAnimationCancel(animation: Animator) { try { recycleSelf() - } catch (e: IllegalArgumentException) { + } catch (_: IllegalArgumentException) { // don't worry about it. } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt index d19d10513f..9e518a923f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt @@ -80,7 +80,7 @@ open class AnimatedZoomJob @SuppressLint("NewApi") constructor( override fun onAnimationStart(animation: Animator) { } - override fun instantiate(): AnimatedZoomJob? { + override fun instantiate(): AnimatedZoomJob { return AnimatedZoomJob(ViewPortHandler(), null, null, YAxis(), 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt index 544e31d8b0..dc3aef6119 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt @@ -5,7 +5,6 @@ import android.view.View import com.github.mikephil.charting.charts.BarLineChartBase import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.utils.ObjectPool -import com.github.mikephil.charting.utils.ObjectPool.Poolable import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.ViewPortHandler diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt index 09744b6fc9..16658b7589 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt @@ -9,9 +9,6 @@ import android.view.View import android.view.animation.AnimationUtils import com.github.mikephil.charting.charts.BarLineChartBase import com.github.mikephil.charting.charts.HorizontalBarChart -import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData -import com.github.mikephil.charting.data.Entry -import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet import com.github.mikephil.charting.interfaces.datasets.IDataSet import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Utils @@ -104,13 +101,11 @@ class BarLineChartTouchListener( if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain() } - mVelocityTracker!!.addMovement(event) + mVelocityTracker?.addMovement(event) if (event.actionMasked == MotionEvent.ACTION_CANCEL) { - if (mVelocityTracker != null) { - mVelocityTracker!!.recycle() - mVelocityTracker = null - } + mVelocityTracker?.recycle() + mVelocityTracker = null } if (touchMode == NONE || mChart.isFlingEnabled) { @@ -206,9 +201,9 @@ class BarLineChartTouchListener( MotionEvent.ACTION_UP -> { val velocityTracker = mVelocityTracker val pointerId = event.getPointerId(0) - velocityTracker!!.computeCurrentVelocity(1000, Utils.maximumFlingVelocity.toFloat()) - val velocityY = velocityTracker.getYVelocity(pointerId) - val velocityX = velocityTracker.getXVelocity(pointerId) + velocityTracker?.computeCurrentVelocity(1000, Utils.maximumFlingVelocity.toFloat()) + val velocityY = velocityTracker?.getYVelocity(pointerId) ?: 0f + val velocityX = velocityTracker?.getXVelocity(pointerId) ?: 0f if (abs(velocityX.toDouble()) > Utils.minimumFlingVelocity || abs(velocityY.toDouble()) > Utils.minimumFlingVelocity @@ -248,7 +243,7 @@ class BarLineChartTouchListener( } MotionEvent.ACTION_POINTER_UP -> { - Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, mVelocityTracker!!) + mVelocityTracker?.let { Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, it) } touchMode = POST_ZOOM } @@ -468,10 +463,9 @@ class BarLineChartTouchListener( val vph = mChart.viewPortHandler val xTrans = x - vph.offsetLeft() - var yTrans = 0f // check if axis is inverted - yTrans = if (inverted()) { + val yTrans = if (inverted()) { -(y - vph.offsetTop()) } else { -(mChart.measuredHeight - y - vph.offsetBottom()) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt index b8d36584b4..2be79fd686 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt @@ -138,7 +138,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*, *, *>) : ChartTouch var i = 0 var count = _velocitySamples.size while (i < count - 2) { - if (currentTime - _velocitySamples.get(i).time > 1000) { + if (currentTime - _velocitySamples[i].time > 1000) { _velocitySamples.removeAt(0) i-- count-- diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt index dd20a79834..b408272434 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt @@ -29,7 +29,7 @@ open class BarChartRenderer( protected var barRect: RectF = RectF() @JvmField - protected var barBuffers: MutableList = mutableListOf() + protected var barBuffers: MutableList = mutableListOf() @JvmField protected var shadowPaint: Paint @@ -77,7 +77,7 @@ open class BarChartRenderer( val barData = chart.barData ?: return for (i in 0..= 0) (buffer.buffer[j + 1] + posOffset) else (buffer.buffer[j + 3] + negOffset) + icon?.let { + var px = x + var py = if (`val` >= 0) (buffer.buffer[j + 1] + posOffset) else (buffer.buffer[j + 3] + negOffset) - px += iconsOffset.x - py += iconsOffset.y + px += iconsOffset.x + py += iconsOffset.y - Utils.drawImage( - c, - icon, - px.toInt(), - py.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + Utils.drawImage( + c, + icon, + px.toInt(), + py.toInt(), + icon.intrinsicWidth, + icon.intrinsicHeight + ) + } } j += 4 } @@ -351,10 +353,10 @@ open class BarChartRenderer( var index = 0 while (index < dataSet.entryCount * animator.phaseX) { - val entry = dataSet.getEntryForIndex(index) ?: continue + val entry = dataSet.getEntryForIndex(index) val vals = entry.yVals - val x = (buffer!!.buffer[bufferIndex] + buffer.buffer[bufferIndex + 2]) / 2f + val x = (buffer.buffer[bufferIndex] + buffer.buffer[bufferIndex + 2]) / 2f val color = dataSet.getValueTextColor(index) @@ -381,24 +383,26 @@ open class BarChartRenderer( ) } - if (entry.icon != null && dataSet.isDrawIconsEnabled) { + if (dataSet.isDrawIconsEnabled) { val icon = entry.icon - var px = x - var py = buffer.buffer[bufferIndex + 1] + - (if (entry.y >= 0) posOffset else negOffset) + icon?.let { + var px = x + var py = buffer.buffer[bufferIndex + 1] + + (if (entry.y >= 0) posOffset else negOffset) - px += iconsOffset.x - py += iconsOffset.y + px += iconsOffset.x + py += iconsOffset.y - Utils.drawImage( - c, - icon, - px.toInt(), - py.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + Utils.drawImage( + c, + icon, + px.toInt(), + py.toInt(), + icon.intrinsicWidth, + icon.intrinsicHeight + ) + } } // draw stack values @@ -432,7 +436,7 @@ open class BarChartRenderer( } } - trans!!.pointValuesToPixel(transformed) + trans.pointValuesToPixel(transformed) var k = 0 while (k < transformed.size) { @@ -467,17 +471,19 @@ open class BarChartRenderer( ) } - if (entry.icon != null && dataSet.isDrawIconsEnabled) { + if (dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - c, - icon, - (x + iconsOffset.x).toInt(), - (y + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + c, + icon, + (x + iconsOffset.x).toInt(), + (y + iconsOffset.y).toInt(), + icon.intrinsicWidth, + icon.intrinsicHeight + ) + } } k += 2 } @@ -499,7 +505,7 @@ open class BarChartRenderer( for (high in indices) { val set = barData.getDataSetByIndex(high.dataSetIndex) - if (set == null || !set.isHighlightEnabled) { + if (!set.isHighlightEnabled) { continue } @@ -534,7 +540,7 @@ open class BarChartRenderer( y2 = 0f } - prepareBarHighlight(e.x, y1, y2, barData.barWidth / 2f, trans!!) + prepareBarHighlight(e.x, y1, y2, barData.barWidth / 2f, trans) setHighlightDrawPos(high, barRect) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt index 3820f8f128..fda168a3c9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.kt @@ -38,10 +38,10 @@ abstract class BarLineScatterCandleBubbleRenderer(animator: ChartAnimator, viewP val entryIndex = set.getEntryIndex(e).toFloat() - if (entryIndex >= set.entryCount * animator.phaseX) { - return false + return if (entryIndex >= set.entryCount * animator.phaseX) { + false } else { - return true + true } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt index 20ea5d6ed2..f17317a592 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt @@ -13,6 +13,7 @@ import com.github.mikephil.charting.utils.ViewPortHandler import kotlin.math.abs import kotlin.math.max import kotlin.math.min +import kotlin.math.roundToInt import kotlin.math.sqrt @Suppress("MemberVisibilityCanBePrivate") @@ -53,7 +54,7 @@ open class BubbleChartRenderer( sizeBuffer[0] = 0f sizeBuffer[2] = 1f - trans!!.pointValuesToPixel(sizeBuffer) + trans.pointValuesToPixel(sizeBuffer) val normalizeSize = dataSet.isNormalizeSizeEnabled @@ -63,7 +64,7 @@ open class BubbleChartRenderer( val referenceSize = min(maxBubbleHeight.toDouble(), maxBubbleWidth.toDouble()).toFloat() for (j in xBounds.min..xBounds.range + xBounds.min) { - val entry = dataSet.getEntryForIndex(j) ?: continue + val entry = dataSet.getEntryForIndex(j) pointBuffer[0] = entry.x pointBuffer[1] = (entry.y) * phaseY @@ -112,7 +113,7 @@ open class BubbleChartRenderer( xBounds[chart] = dataSet - chart.getTransformer(dataSet.axisDependency)?.let { transformer -> + chart.getTransformer(dataSet.axisDependency).let { transformer -> val positions = transformer.generateTransformedValuesBubble(dataSet, phaseY, xBounds.min, xBounds.max) val alpha = if (phaseX == 1f) @@ -128,7 +129,7 @@ open class BubbleChartRenderer( while (j < positions.size) { var valueTextColor = dataSet.getValueTextColor(j / 2 + xBounds.min) valueTextColor = Color.argb( - Math.round(255f * alpha), Color.red(valueTextColor), + (255f * alpha).roundToInt(), Color.red(valueTextColor), Color.green(valueTextColor), Color.blue(valueTextColor) ) @@ -142,7 +143,7 @@ open class BubbleChartRenderer( continue } - val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) ?: continue + val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) if (dataSet.isDrawValuesEnabled) { drawValue( @@ -151,17 +152,19 @@ open class BubbleChartRenderer( ) } - if (entry.icon != null && dataSet.isDrawIconsEnabled) { + if (dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - c, - icon, - (x + iconsOffset.x).toInt(), - (y + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + c, + icon, + (x + iconsOffset.x).toInt(), + (y + iconsOffset.y).toInt(), + icon.intrinsicWidth, + icon.intrinsicHeight + ) + } } j += 2 } @@ -192,7 +195,7 @@ open class BubbleChartRenderer( for (high in indices) { val set = bubbleData.getDataSetByIndex(high.dataSetIndex) - if (set == null || !set.isHighlightEnabled) continue + if (!set.isHighlightEnabled) continue val entry = set.getEntryForXValue(high.x, high.y) ?: continue @@ -205,7 +208,7 @@ open class BubbleChartRenderer( sizeBuffer[0] = 0f sizeBuffer[2] = 1f - trans!!.pointValuesToPixel(sizeBuffer) + trans.pointValuesToPixel(sizeBuffer) val normalizeSize = set.isNormalizeSizeEnabled diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt index 167447fa46..e9af97ebe8 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt @@ -47,7 +47,7 @@ open class CandleStickChartRenderer( for (j in xBounds.min..xBounds.range + xBounds.min) { // get the entry - val e = dataSet.getEntryForIndex(j) ?: continue + val e = dataSet.getEntryForIndex(j) val xPos = e.x @@ -80,7 +80,7 @@ open class CandleStickChartRenderer( shadowBuffers[7] = shadowBuffers[3] } - trans!!.pointValuesToPixel(shadowBuffers) + trans.pointValuesToPixel(shadowBuffers) // draw the shadows if (dataSet.shadowColorSameAsCandle) { @@ -165,7 +165,7 @@ open class CandleStickChartRenderer( closeBuffers[2] = xPos closeBuffers[3] = close * phaseY - trans!!.pointValuesToPixel(rangeBuffers) + trans.pointValuesToPixel(rangeBuffers) trans.pointValuesToPixel(openBuffers) trans.pointValuesToPixel(closeBuffers) @@ -224,7 +224,7 @@ open class CandleStickChartRenderer( xBounds[chart] = dataSet - val positions = trans!!.generateTransformedValuesCandle( + val positions = trans.generateTransformedValuesCandle( dataSet, animator.phaseX, animator.phaseY, xBounds.min, xBounds.max ) @@ -246,7 +246,7 @@ open class CandleStickChartRenderer( continue } - val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) ?: continue + val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) if (dataSet.isDrawValuesEnabled) { drawValue( @@ -290,7 +290,7 @@ open class CandleStickChartRenderer( for (high in indices) { val set = candleData.getDataSetByIndex(high.dataSetIndex) - if (set == null || !set.isHighlightEnabled) continue + if (!set.isHighlightEnabled) continue val e = set.getEntryForXValue(high.x, high.y) ?: continue @@ -300,7 +300,7 @@ open class CandleStickChartRenderer( val highValue = e.high * animator.phaseY val y = (lowValue + highValue) / 2f - val pix = chart.getTransformer(set.axisDependency)!!.getPixelForValues(e.x, y) + val pix = chart.getTransformer(set.axisDependency).getPixelForValues(e.x, y) high.setDraw(pix.x.toFloat(), pix.y.toFloat()) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.kt index 424a6c6a42..420c0ee452 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CombinedChartRenderer.kt @@ -73,11 +73,13 @@ open class CombinedChartRenderer(chart: CombinedChart, animator: ChartAnimator, for (renderer in dataRenderers) { var data: ChartData<*, *>? = null - if (renderer is BarChartRenderer) data = renderer.chart.barData - else if (renderer is LineChartRenderer) data = renderer.chart.lineData - else if (renderer is CandleStickChartRenderer) data = renderer.chart.candleData - else if (renderer is ScatterChartRenderer) data = renderer.chart.scatterData - else if (renderer is BubbleChartRenderer) data = renderer.chart.bubbleData + when (renderer) { + is BarChartRenderer -> data = renderer.chart.barData + is LineChartRenderer -> data = renderer.chart.lineData + is CandleStickChartRenderer -> data = renderer.chart.candleData + is ScatterChartRenderer -> data = renderer.chart.scatterData + is BubbleChartRenderer -> data = renderer.chart.bubbleData + } val dataIndex = if (data == null) -1 diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt index 3a1f0b8664..df46874451 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt @@ -27,10 +27,8 @@ open class HorizontalBarChartRenderer( ) : BarChartRenderer(chart, animator, viewPortHandler) { override fun initBuffers() { val barData = chart.barData ?: return - barBuffers = arrayOfNulls(barData.dataSetCount).toMutableList() - - for (i in barBuffers.indices) { - val set = barData.getDataSetByIndex(i) ?: continue + for (i in 0 until barData.dataSetCount) { + val set = barData.getDataSetByIndex(i) barBuffers[i] = HorizontalBarBuffer( set.entryCount * 4 * (if (set.isStacked) set.stackSize else 1), barData.dataSetCount, set.isStacked @@ -68,14 +66,14 @@ open class HorizontalBarChartRenderer( var i = 0 val count = min((ceil(((dataSet.entryCount).toFloat() * phaseX).toDouble())).toInt().toDouble(), dataSet.entryCount.toDouble()).toInt() while (i < count) { - val e = dataSet.getEntryForIndex(i) ?: continue + val e = dataSet.getEntryForIndex(i) x = e.x mBarShadowRectBuffer.top = x - barWidthHalf mBarShadowRectBuffer.bottom = x + barWidthHalf - trans!!.rectValueToPixel(mBarShadowRectBuffer) + trans.rectValueToPixel(mBarShadowRectBuffer) if (!viewPortHandler.isInBoundsTop(mBarShadowRectBuffer.bottom)) { i++ @@ -95,7 +93,7 @@ open class HorizontalBarChartRenderer( } // initialize the buffer - val buffer = barBuffers[index]!! + val buffer = barBuffers[index] buffer.setPhases(phaseX, phaseY) buffer.setDataSet(index) buffer.setInverted(chart.isInverted(dataSet.axisDependency)) @@ -103,9 +101,9 @@ open class HorizontalBarChartRenderer( buffer.feed(dataSet) - trans!!.pointValuesToPixel(buffer.buffer) + trans.pointValuesToPixel(buffer.buffer) - val isCustomFill = dataSet.fills != null && !dataSet.fills.isNullOrEmpty() + val isCustomFill = dataSet.fills.isNotEmpty() val isSingleColor = dataSet.colors.size == 1 val isInverted = chart.isInverted(dataSet.axisDependency) @@ -133,15 +131,15 @@ open class HorizontalBarChartRenderer( } if (isCustomFill) { - dataSet.getFill(pos)?.fillRect( - c, paintRender, - buffer.buffer[j], - buffer.buffer[j + 1], - buffer.buffer[j + 2], - buffer.buffer[j + 3], - if (isInverted) Fill.Direction.LEFT else Fill.Direction.RIGHT, - 0f - ) + dataSet.getFill(pos).fillRect( + c, paintRender, + buffer.buffer[j], + buffer.buffer[j + 1], + buffer.buffer[j + 2], + buffer.buffer[j + 3], + if (isInverted) Fill.Direction.LEFT else Fill.Direction.RIGHT, + 0f + ) } else { c.drawRect( buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], @@ -189,7 +187,7 @@ open class HorizontalBarChartRenderer( val formatter = dataSet.valueFormatter // get the buffer - val buffer = barBuffers[i]!! + val buffer = barBuffers[i] val phaseY = animator.phaseY @@ -217,7 +215,7 @@ open class HorizontalBarChartRenderer( continue } - val entry = dataSet.getEntryForIndex(j / 4) ?: continue + val entry = dataSet.getEntryForIndex(j / 4) val `val` = entry.y val formattedValue = formatter.getFormattedValue(`val`, entry, i, viewPortHandler) @@ -271,7 +269,7 @@ open class HorizontalBarChartRenderer( var index = 0 while (index < dataSet.entryCount * animator.phaseX) { - val entry = dataSet.getEntryForIndex(index) ?: continue + val entry = dataSet.getEntryForIndex(index) val color = dataSet.getValueTextColor(index) val vals = entry.yVals @@ -366,7 +364,7 @@ open class HorizontalBarChartRenderer( } } - trans!!.pointValuesToPixel(transformed) + trans.pointValuesToPixel(transformed) var k = 0 while (k < transformed.size) { @@ -459,7 +457,8 @@ open class HorizontalBarChartRenderer( } override fun isDrawingValuesAllowed(chart: ChartInterface): Boolean { - return (chart.data!!.entryCount < chart.maxVisibleCount - * viewPortHandler.scaleY) + return chart.data?.let { + it.entryCount < chart.maxVisibleCount * viewPortHandler.scaleY + } == true } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt index 00da175ea3..0b6e3c8677 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt @@ -47,7 +47,7 @@ open class LegendRenderer( // loop for building up the colors and labels used in the legend for (i in 0.. 0) { - drawLinearFill(c, dataSet, trans!!, xBounds) + drawLinearFill(c, dataSet, trans, xBounds) } // more than 1 color @@ -278,7 +278,7 @@ open class LineChartRenderer( val max = xBounds.min + xBounds.range for (j in xBounds.min.. 0) { - trans!!.pointValuesToPixel(lineBuffer) + lineBuffer[j++] = e2.x + lineBuffer[j++] = e2.y * phaseY + } - val size = (max(((xBounds.range + 1) * pointsPerEntryPair).toDouble(), pointsPerEntryPair.toDouble()) * 2).toInt() + if (j > 0) { + trans.pointValuesToPixel(lineBuffer) - paintRender.color = dataSet.color + val size = (max(((xBounds.range + 1) * pointsPerEntryPair).toDouble(), pointsPerEntryPair.toDouble()) * 2).toInt() - canvas!!.drawLines(lineBuffer, 0, size, paintRender) - } + paintRender.color = dataSet.color + + canvas?.drawLines(lineBuffer, 0, size, paintRender) } } @@ -465,15 +459,13 @@ open class LineChartRenderer( for (x in startIndex + 1..endIndex) { currentEntry = dataSet.getEntryForIndex(x) - if (currentEntry != null) { - if (isDrawSteppedEnabled) { - filled.lineTo(currentEntry.x, previousEntry.y * phaseY) - } + if (isDrawSteppedEnabled) { + filled.lineTo(currentEntry.x, previousEntry.y * phaseY) + } - filled.lineTo(currentEntry.x, currentEntry.y * phaseY) + filled.lineTo(currentEntry.x, currentEntry.y * phaseY) - previousEntry = currentEntry - } + previousEntry = currentEntry } // close up @@ -509,7 +501,7 @@ open class LineChartRenderer( xBounds[chart] = dataSet - val positions = trans!!.generateTransformedValuesLine( + val positions = trans.generateTransformedValuesLine( dataSet, animator.phaseX, animator .phaseY, xBounds.min, xBounds.max ) @@ -532,23 +524,23 @@ open class LineChartRenderer( val entry = dataSet.getEntryForIndex(j / 2 + xBounds.min) - if (entry != null) { - if (dataSet.isDrawValuesEnabled) { - drawValue( - c, dataSet.valueFormatter, entry.y, entry, i, x, - y - valOffset, dataSet.getValueTextColor(j / 2) - ) - } + if (dataSet.isDrawValuesEnabled) { + drawValue( + c, dataSet.valueFormatter, entry.y, entry, i, x, + y - valOffset, dataSet.getValueTextColor(j / 2) + ) + } - if (entry.icon != null && dataSet.isDrawIconsEnabled) { - val icon = entry.icon + if (dataSet.isDrawIconsEnabled) { + val icon = entry.icon + icon?.let { Utils.drawImage( c, icon, (x + iconsOffset.x).toInt(), (y + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, + icon.intrinsicWidth, icon.intrinsicHeight ) } @@ -607,16 +599,9 @@ open class LineChartRenderer( val drawTransparentCircleHole = drawCircleHole && dataSet.circleHoleColor == ColorTemplate.COLOR_NONE - val imageCache: DataSetImageCache? + val imageCache: DataSetImageCache = mImageCaches.getOrPut(dataSet) { DataSetImageCache() } - if (mImageCaches.containsKey(dataSet)) { - imageCache = mImageCaches[dataSet] - } else { - imageCache = DataSetImageCache() - mImageCaches[dataSet] = imageCache - } - - val changeRequired = imageCache!!.init(dataSet) + val changeRequired = imageCache.init(dataSet) // only fill the cache with new bitmaps if a change is required if (changeRequired) { @@ -626,12 +611,12 @@ open class LineChartRenderer( val boundsRangeCount = xBounds.range + xBounds.min for (j in xBounds.min..boundsRangeCount) { - val e = dataSet.getEntryForIndex(j) ?: break + val e = dataSet.getEntryForIndex(j) mCirclesBuffer[0] = e.x mCirclesBuffer[1] = e.y * phaseY - trans!!.pointValuesToPixel(mCirclesBuffer) + trans.pointValuesToPixel(mCirclesBuffer) if (!viewPortHandler.isInBoundsRight(mCirclesBuffer[0])) break @@ -660,7 +645,7 @@ open class LineChartRenderer( if (!isInBoundsX(e, set) || e == null) continue - val pix = chart.getTransformer(set.axisDependency)!!.getPixelForValues( + val pix = chart.getTransformer(set.axisDependency).getPixelForValues( e.x, e.y * animator .phaseY ) @@ -708,7 +693,7 @@ open class LineChartRenderer( private inner class DataSetImageCache { private val mCirclePathBuffer = Path() - private var circleBitmaps: Array? = null + private val circleBitmaps: MutableList = mutableListOf() /** * Sets up the cache, returns true if a change of cache was required. @@ -720,11 +705,8 @@ open class LineChartRenderer( val size = set.circleColorCount var changeRequired = false - if (circleBitmaps == null) { - circleBitmaps = arrayOfNulls(size) - changeRequired = true - } else if (circleBitmaps!!.size != size) { - circleBitmaps = arrayOfNulls(size) + if (circleBitmaps.size != size) { + circleBitmaps.clear() changeRequired = true } @@ -748,7 +730,7 @@ open class LineChartRenderer( val circleBitmap = createBitmap((circleRadius * 2.1).toInt(), (circleRadius * 2.1).toInt(), conf) val canvas = Canvas(circleBitmap) - circleBitmaps!![i] = circleBitmap + circleBitmaps[i] = circleBitmap paintRender.color = set.getCircleColor(i) if (drawTransparentCircleHole) { @@ -799,7 +781,7 @@ open class LineChartRenderer( * @return */ fun getBitmap(index: Int): Bitmap? { - return circleBitmaps!![index % circleBitmaps!!.size] + return circleBitmaps[index % circleBitmaps.size] } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt index 72e9875640..ef0f7df61c 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt @@ -7,7 +7,6 @@ import android.graphics.Paint import android.graphics.Paint.Align import android.graphics.Path import android.graphics.RectF -import android.os.Build import android.text.Layout import android.text.StaticLayout import android.text.TextPaint @@ -32,6 +31,7 @@ import kotlin.math.sqrt import kotlin.math.tan import androidx.core.graphics.withSave import com.github.mikephil.charting.data.PieData +import androidx.core.graphics.createBitmap open class PieChartRenderer( protected var chart: PieChart, animator: ChartAnimator, @@ -87,16 +87,13 @@ open class PieChartRenderer( val width = viewPortHandler.chartWidth.toInt() val height = viewPortHandler.chartHeight.toInt() - var drawBitmap = if (mDrawBitmap == null) - null - else - mDrawBitmap!!.get() + var drawBitmap = mDrawBitmap?.get() if (drawBitmap == null || (drawBitmap.width != width) || (drawBitmap.height != height) ) { if (width > 0 && height > 0) { - drawBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444) + drawBitmap = createBitmap(width, height, Bitmap.Config.ARGB_4444) mDrawBitmap = WeakReference(drawBitmap) bitmapCanvas = Canvas(drawBitmap) } else return @@ -104,9 +101,9 @@ open class PieChartRenderer( drawBitmap.eraseColor(Color.TRANSPARENT) - val pieData = chart.data + val pieData = chart.data ?: return - for (set in pieData!!.dataSets) { + for (set in pieData.dataSets) { if (set.isVisible && set.entryCount > 0) drawDataSet(c, set) } } @@ -350,11 +347,11 @@ open class PieChartRenderer( mPathBuffer.close() - bitmapCanvas!!.drawPath(mPathBuffer, paintRender) + bitmapCanvas?.drawPath(mPathBuffer, paintRender) // Draw rounded corner path with paint object slice with the given radius if (roundedCornerRadius > 0) { - bitmapCanvas!!.drawPath(mPathBuffer, roundedCornerPaint) + bitmapCanvas?.drawPath(mPathBuffer, roundedCornerPaint) } angle += sliceAngle * phaseX @@ -391,9 +388,9 @@ open class PieChartRenderer( val labelRadius = radius - labelRadiusOffset val data = chart.data - val dataSets = data!!.dataSets + val dataSets = data?.dataSets ?: return - val yValueSum = (data as PieData).yValueSum + val yValueSum = data.yValueSum val drawEntryLabels = chart.isDrawEntryLabelsEnabled @@ -577,26 +574,28 @@ open class PieChartRenderer( if (j < data.entryCount && entryLabel != null) { drawEntryLabel(this, entryLabel, x, y + lineHeight / 2f) } - } else if (drawYInside) { + } else { drawValue(this, formatter, value, entry, 0, x, y + lineHeight / 2f, dataSet.getValueTextColor(j)) } } - if (entry.icon != null && dataSet.isDrawIconsEnabled) { + if (dataSet.isDrawIconsEnabled) { val icon = entry.icon - val x = (labelRadius + iconsOffset.y) * sliceXBase + center.x - var y = (labelRadius + iconsOffset.y) * sliceYBase + center.y - y += iconsOffset.x - - Utils.drawImage( - this, - icon, - x.toInt(), - y.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + val x = (labelRadius + iconsOffset.y) * sliceXBase + center.x + var y = (labelRadius + iconsOffset.y) * sliceYBase + center.y + y += iconsOffset.x + + Utils.drawImage( + this, + icon, + x.toInt(), + y.toInt(), + icon.intrinsicWidth, + icon.intrinsicHeight + ) + } } xIndex++ @@ -622,7 +621,7 @@ open class PieChartRenderer( override fun drawExtras(c: Canvas) { drawHole(c) - c.drawBitmap(mDrawBitmap!!.get()!!, 0f, 0f, null) + mDrawBitmap?.get()?.let { c.drawBitmap(it, 0f, 0f, null) } drawCenterText(c) } @@ -640,7 +639,7 @@ open class PieChartRenderer( if (Color.alpha(paintHole.color) > 0) { // draw the hole-circle - bitmapCanvas!!.drawCircle( + bitmapCanvas?.drawCircle( center.x, center.y, holeRadius, paintHole ) @@ -659,7 +658,7 @@ open class PieChartRenderer( mHoleCirclePath.reset() mHoleCirclePath.addCircle(center.x, center.y, secondHoleRadius, Path.Direction.CW) mHoleCirclePath.addCircle(center.x, center.y, holeRadius, Path.Direction.CCW) - bitmapCanvas!!.drawPath(mHoleCirclePath, paintTransparentCircle) + bitmapCanvas?.drawPath(mHoleCirclePath, paintTransparentCircle) // reset alpha paintTransparentCircle.alpha = alpha @@ -723,20 +722,18 @@ open class PieChartRenderer( } //float layoutWidth = Utils.getStaticLayoutMaxWidth(mCenterTextLayout); - val layoutHeight = centerTextLayout!!.height.toFloat() + val layoutHeight = centerTextLayout?.height?.toFloat() ?: 0f - c.save() - if (Build.VERSION.SDK_INT >= 18) { + c.withSave { val path = mDrawCenterTextPathBuffer path.reset() path.addOval(holeRect, Path.Direction.CW) - c.clipPath(path) - } + clipPath(path) - c.translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2f) - centerTextLayout!!.draw(c) + translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2f) + centerTextLayout?.draw(this) - c.restore() + } MPPointF.recycleInstance(center) MPPointF.recycleInstance(offset) @@ -951,7 +948,7 @@ open class PieChartRenderer( mPathBuffer.close() - bitmapCanvas!!.drawPath(mPathBuffer, paintRender) + bitmapCanvas?.drawPath(mPathBuffer, paintRender) } MPPointF.recycleInstance(center) @@ -998,7 +995,7 @@ open class PieChartRenderer( * sin(v) + center.y).toFloat() paintRender.color = dataSet.getColor(j) - bitmapCanvas!!.drawCircle(x, y, circleRadius, paintRender) + bitmapCanvas?.drawCircle(x, y, circleRadius, paintRender) } angle += sliceAngle * phaseX @@ -1010,12 +1007,10 @@ open class PieChartRenderer( * Releases the drawing bitmap. This should be called when . */ fun releaseBitmap() { - if (bitmapCanvas != null) { - bitmapCanvas!!.setBitmap(null) - bitmapCanvas = null - } + bitmapCanvas?.setBitmap(null) + bitmapCanvas = null if (mDrawBitmap != null) { - val drawBitmap = mDrawBitmap!!.get() + val drawBitmap = mDrawBitmap?.get() drawBitmap?.recycle() mDrawBitmap?.clear() mDrawBitmap = null diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt index abb27e5fc2..e74948e132 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt @@ -118,6 +118,7 @@ open class RadarChartRenderer( val phaseY = animator.phaseY val sliceangle = chart.sliceAngle + val chartData = chart.data ?: return // calculate the factor that is needed for transforming the value to // pixels @@ -129,8 +130,8 @@ open class RadarChartRenderer( val yoffset = Utils.convertDpToPixel(5f) - for (i in 0.. 1) { @@ -235,7 +235,7 @@ class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator, v RectF( buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3] - ), roundedNegativeDataSetRadius, roundedNegativeDataSetRadius, true, true, true, true + ), roundedNegativeDataSetRadius, roundedNegativeDataSetRadius, tl = true, tr = true, br = true, bl = true ) c.drawPath(path2, paintRender) } else if ((dataSet.getEntryForIndex(j / 4).y > 0 && roundedPositiveDataSetRadius > 0)) { @@ -243,7 +243,7 @@ class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator, v RectF( buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3] - ), roundedPositiveDataSetRadius, roundedPositiveDataSetRadius, true, true, true, true + ), roundedPositiveDataSetRadius, roundedPositiveDataSetRadius, tl = true, tr = true, br = true, bl = true ) c.drawPath(path2, paintRender) } else { @@ -298,7 +298,7 @@ class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator, v y2 = 0f } - prepareBarHighlight(e.x, y1, y2, barData.barWidth / 2f, trans!!) + prepareBarHighlight(e.x, y1, y2, barData.barWidth / 2f, trans) setHighlightDrawPos(high, barRect) @@ -306,7 +306,7 @@ class RoundedBarChartRenderer(chart: BarDataProvider, animator: ChartAnimator, v RectF( barRect.left, barRect.top, barRect.right, barRect.bottom - ), mRadius, mRadius, true, true, true, true + ), mRadius, mRadius, tl = true, tr = true, br = true, bl = true ) c.drawPath(path2, paintHighlight) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedHorizontalBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedHorizontalBarChartRenderer.kt index b1da10b2a1..ed5aa7e2c6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedHorizontalBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RoundedHorizontalBarChartRenderer.kt @@ -60,7 +60,7 @@ class RoundedHorizontalBarChartRenderer(chart: BarDataProvider, animator: ChartA x = e.x mBarShadowRectBuffer.top = x - barWidthHalf mBarShadowRectBuffer.bottom = x + barWidthHalf - trans!!.rectValueToPixel(mBarShadowRectBuffer) + trans.rectValueToPixel(mBarShadowRectBuffer) if (!viewPortHandler.isInBoundsTop(mBarShadowRectBuffer.bottom)) { i++ continue @@ -80,13 +80,13 @@ class RoundedHorizontalBarChartRenderer(chart: BarDataProvider, animator: ChartA } } - val buffer = barBuffers[index]!! + val buffer = barBuffers[index] buffer.setPhases(phaseX, phaseY) buffer.setDataSet(index) buffer.setInverted(chart.isInverted(dataSet.axisDependency)) - buffer.setBarWidth(chart.barData!!.barWidth) + chart.barData?.barWidth?.let { buffer.setBarWidth(it) } buffer.feed(dataSet) - trans!!.pointValuesToPixel(buffer.buffer) + trans.pointValuesToPixel(buffer.buffer) // if multiple colors has been assigned to Bar Chart if (dataSet.colors.size > 1) { @@ -210,7 +210,7 @@ class RoundedHorizontalBarChartRenderer(chart: BarDataProvider, animator: ChartA RectF( buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3] - ), roundedNegativeDataSetRadius, roundedNegativeDataSetRadius, true, true, true, true + ), roundedNegativeDataSetRadius, roundedNegativeDataSetRadius, tl = true, tr = true, br = true, bl = true ) c.drawPath(path2, paintRender) } else if ((dataSet.getEntryForIndex(j / 4).y > 0 && roundedPositiveDataSetRadius > 0)) { @@ -218,7 +218,7 @@ class RoundedHorizontalBarChartRenderer(chart: BarDataProvider, animator: ChartA RectF( buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3] - ), roundedPositiveDataSetRadius, roundedPositiveDataSetRadius, true, true, true, true + ), roundedPositiveDataSetRadius, roundedPositiveDataSetRadius, tl = true, tr = true, br = true, bl = true ) c.drawPath(path2, paintRender) } else { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt index 91e3e73675..19ddd599d7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt @@ -53,7 +53,7 @@ open class ScatterChartRenderer(@JvmField var chart: ScatterDataProvider, animat pixelBuffer[0] = e.x pixelBuffer[1] = e.y * phaseY - trans!!.pointValuesToPixel(pixelBuffer) + trans.pointValuesToPixel(pixelBuffer) if (!viewPortHandler.isInBoundsRight(pixelBuffer[0])) break @@ -91,7 +91,7 @@ open class ScatterChartRenderer(@JvmField var chart: ScatterDataProvider, animat xBounds[chart] = dataSet - val positions = chart.getTransformer(dataSet.axisDependency)!!.generateTransformedValuesScatter( + val positions = chart.getTransformer(dataSet.axisDependency).generateTransformedValuesScatter( dataSet, animator.phaseX, animator.phaseY, xBounds.min, xBounds.max ) @@ -129,17 +129,19 @@ open class ScatterChartRenderer(@JvmField var chart: ScatterDataProvider, animat ) } - if (entry.icon != null && dataSet.isDrawIconsEnabled) { + if (dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - c, - icon, - (positions[j] + iconsOffset.x).toInt(), - (positions[j + 1] + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + c, + icon, + (positions[j] + iconsOffset.x).toInt(), + (positions[j + 1] + iconsOffset.y).toInt(), + icon.intrinsicWidth, + icon.intrinsicHeight + ) + } } j += 2 } @@ -164,7 +166,7 @@ open class ScatterChartRenderer(@JvmField var chart: ScatterDataProvider, animat if (!isInBoundsX(e, set)) continue - val pix = chart.getTransformer(set.axisDependency)!!.getPixelForValues( + val pix = chart.getTransformer(set.axisDependency).getPixelForValues( e.x, e.y * animator .phaseY ) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt index 86cc53850b..32bfb57588 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt @@ -18,6 +18,7 @@ import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler import androidx.core.graphics.withClip import androidx.core.graphics.withSave +import kotlin.math.roundToInt open class XAxisRenderer( viewPortHandler: ViewPortHandler, @@ -38,19 +39,21 @@ open class XAxisRenderer( var minLocal = min var maxLocal = max if (viewPortHandler.contentWidth() > 10 && !viewPortHandler.isFullyZoomedOutX) { - val p1 = transformer!!.getValuesByTouchPoint(viewPortHandler.contentLeft(), viewPortHandler.contentTop()) - val p2 = transformer!!.getValuesByTouchPoint(viewPortHandler.contentRight(), viewPortHandler.contentTop()) + transformer?.let { + val p1 = it.getValuesByTouchPoint(viewPortHandler.contentLeft(), viewPortHandler.contentTop()) + val p2 = it.getValuesByTouchPoint(viewPortHandler.contentRight(), viewPortHandler.contentTop()) - if (inverted) { - minLocal = p2.x.toFloat() - maxLocal = p1.x.toFloat() - } else { - minLocal = p1.x.toFloat() - maxLocal = p2.x.toFloat() - } + if (inverted) { + minLocal = p2.x.toFloat() + maxLocal = p1.x.toFloat() + } else { + minLocal = p1.x.toFloat() + maxLocal = p2.x.toFloat() + } - MPPointD.recycleInstance(p1) - MPPointD.recycleInstance(p2) + MPPointD.recycleInstance(p1) + MPPointD.recycleInstance(p2) + } } computeAxisValues(minLocal, maxLocal) @@ -80,8 +83,8 @@ open class XAxisRenderer( ) - xAxis.mLabelWidth = Math.round(labelRotatedSize.width) - xAxis.mLabelHeight = Math.round(labelRotatedSize.height) + xAxis.mLabelWidth = labelRotatedSize.width.roundToInt() + xAxis.mLabelHeight = labelRotatedSize.height.roundToInt() FSize.recycleInstance(labelRotatedSize) FSize.recycleInstance(labelSize) @@ -184,7 +187,7 @@ open class XAxisRenderer( } } - transformer!!.pointValuesToPixel(positions) + transformer?.pointValuesToPixel(positions) var i = 0 while (i < positions.size) { @@ -228,7 +231,7 @@ open class XAxisRenderer( override fun renderGridLines(c: Canvas) { if (!xAxis.isDrawGridLinesEnabled || !xAxis.isEnabled) return - c.withClip(gridClippingRect!!) { + c.withClip(gridClippingRect) { if (axis.isShowSpecificPositions) { if (mRenderGridLinesBuffer.size != axis.specificPositions.size * 2) { mRenderGridLinesBuffer = FloatArray(xAxis.specificPositions.size * 2) @@ -254,7 +257,7 @@ open class XAxisRenderer( } } - transformer!!.pointValuesToPixel(positions) + transformer?.pointValuesToPixel(positions) setupGridPaint() @@ -273,7 +276,7 @@ open class XAxisRenderer( @JvmField protected var mGridClippingRect: RectF = RectF() - open val gridClippingRect: RectF? + open val gridClippingRect: RectF get() { mGridClippingRect.set(viewPortHandler.contentRect) mGridClippingRect.inset(-axis.gridLineWidth, 0f) @@ -339,7 +342,7 @@ open class XAxisRenderer( override fun renderLimitLines(c: Canvas) { val limitLines = xAxis.limitLines - if (limitLines == null || limitLines.size <= 0) return + if (limitLines.isEmpty()) return val position = mRenderLimitLinesBuffer position[0] = 0f @@ -358,7 +361,7 @@ open class XAxisRenderer( position[0] = l.limit position[1] = 0f - transformer!!.pointValuesToPixel(position) + transformer?.pointValuesToPixel(position) renderLimitLineLine(c, l, position) renderLimitLineLabel(c, l, position, 2f + l.yOffset) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt index d11334ea23..d28671341a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt @@ -15,6 +15,7 @@ import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler import androidx.core.graphics.withSave +import kotlin.math.roundToInt @Suppress("MemberVisibilityCanBePrivate") open class XAxisRendererHorizontalBarChart( @@ -31,27 +32,29 @@ open class XAxisRendererHorizontalBarChart( var minLocal = min var maxLocal = max if (viewPortHandler.contentWidth() > 10 && !viewPortHandler.isFullyZoomedOutY) { - val p1 = transformer!!.getValuesByTouchPoint(viewPortHandler.contentLeft(), viewPortHandler.contentBottom()) - val p2 = transformer!!.getValuesByTouchPoint(viewPortHandler.contentLeft(), viewPortHandler.contentTop()) - - if (inverted) { - minLocal = p2.y.toFloat() - maxLocal = p1.y.toFloat() - } else { - minLocal = p1.y.toFloat() - maxLocal = p2.y.toFloat() - } + transformer?.let { + val p1 = it.getValuesByTouchPoint(viewPortHandler.contentLeft(), viewPortHandler.contentBottom()) + val p2 = it.getValuesByTouchPoint(viewPortHandler.contentLeft(), viewPortHandler.contentTop()) - MPPointD.recycleInstance(p1) - MPPointD.recycleInstance(p2) + if (inverted) { + minLocal = p2.y.toFloat() + maxLocal = p1.y.toFloat() + } else { + minLocal = p1.y.toFloat() + maxLocal = p2.y.toFloat() + } + + MPPointD.recycleInstance(p1) + MPPointD.recycleInstance(p2) + } } computeAxisValues(minLocal, maxLocal) } override fun computeSize() { - paintAxisLabels!!.setTypeface(xAxis.typeface) - paintAxisLabels!!.textSize = xAxis.textSize + paintAxisLabels.setTypeface(xAxis.typeface) + paintAxisLabels.textSize = xAxis.textSize val longest = xAxis.longestLabel @@ -66,8 +69,8 @@ open class XAxisRendererHorizontalBarChart( xAxis.labelRotationAngle ) - xAxis.mLabelWidth = Math.round(labelRotatedSize.width) - xAxis.mLabelHeight = Math.round(labelRotatedSize.height) + xAxis.mLabelWidth = labelRotatedSize.width.roundToInt() + xAxis.mLabelHeight = labelRotatedSize.height.roundToInt() FSize.recycleInstance(labelRotatedSize) } @@ -77,9 +80,9 @@ open class XAxisRendererHorizontalBarChart( val xOffset = xAxis.xOffset - paintAxisLabels!!.setTypeface(xAxis.typeface) - paintAxisLabels!!.textSize = xAxis.textSize - paintAxisLabels!!.color = xAxis.textColor + paintAxisLabels.setTypeface(xAxis.typeface) + paintAxisLabels.textSize = xAxis.textSize + paintAxisLabels.color = xAxis.textColor val pointF = MPPointF.getInstance(0f, 0f) @@ -136,7 +139,7 @@ open class XAxisRendererHorizontalBarChart( } } - transformer!!.pointValuesToPixel(positions) + transformer?.pointValuesToPixel(positions) var i = 0 while (i < positions.size) { @@ -162,7 +165,7 @@ open class XAxisRendererHorizontalBarChart( gridLinePath.lineTo(viewPortHandler.contentLeft(), y) // draw a path because lines don't support dashing on lower android versions - c.drawPath(gridLinePath, paintGrid!!) + c.drawPath(gridLinePath, paintGrid) gridLinePath.reset() } @@ -170,14 +173,14 @@ open class XAxisRendererHorizontalBarChart( override fun renderAxisLine(c: Canvas) { if (!xAxis.isDrawAxisLineEnabled || !xAxis.isEnabled) return - paintAxisLine!!.color = xAxis.axisLineColor - paintAxisLine!!.strokeWidth = xAxis.axisLineWidth + paintAxisLine.color = xAxis.axisLineColor + paintAxisLine.strokeWidth = xAxis.axisLineWidth if (xAxis.position == XAxisPosition.TOP || xAxis.position == XAxisPosition.TOP_INSIDE || xAxis.position == XAxisPosition.BOTH_SIDED) { c.drawLine( viewPortHandler.contentRight(), viewPortHandler.contentTop(), viewPortHandler.contentRight(), - viewPortHandler.contentBottom(), paintAxisLine!! + viewPortHandler.contentBottom(), paintAxisLine ) } @@ -185,7 +188,7 @@ open class XAxisRendererHorizontalBarChart( c.drawLine( viewPortHandler.contentLeft(), viewPortHandler.contentTop(), viewPortHandler.contentLeft(), - viewPortHandler.contentBottom(), paintAxisLine!! + viewPortHandler.contentBottom(), paintAxisLine ) } } @@ -225,7 +228,7 @@ open class XAxisRendererHorizontalBarChart( pts[1] = l.limit - transformer!!.pointValuesToPixel(pts) + transformer?.pointValuesToPixel(pts) limitLinePath.moveTo(viewPortHandler.contentLeft(), pts[1]) limitLinePath.lineTo(viewPortHandler.contentRight(), pts[1]) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt index 499818b31e..9b89a5fb91 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt @@ -15,9 +15,9 @@ class XAxisRendererRadarChart(viewPortHandler: ViewPortHandler, xAxis: XAxis, pr val labelRotationAngleDegrees = xAxis.labelRotationAngle val drawLabelAnchor = MPPointF.getInstance(0.5f, 0.25f) - paintAxisLabels!!.setTypeface(xAxis.typeface) - paintAxisLabels!!.textSize = xAxis.textSize - paintAxisLabels!!.color = xAxis.textColor + paintAxisLabels.setTypeface(xAxis.typeface) + paintAxisLabels.textSize = xAxis.textSize + paintAxisLabels.color = xAxis.textColor val sliceAngle = chart.sliceAngle @@ -27,7 +27,7 @@ class XAxisRendererRadarChart(viewPortHandler: ViewPortHandler, xAxis: XAxis, pr val center = chart.centerOffsets val pOut = MPPointF.getInstance(0f, 0f) - for (i in 0..<(chart.data!!.maxEntryCountSet?.entryCount ?: 0)) { + for (i in 0..<(chart.data?.maxEntryCountSet?.entryCount ?: 0)) { val label = xAxis.valueFormatter.getFormattedValue(i.toFloat(), xAxis) val angle = (sliceAngle * i + chart.rotationAngle) % 360f diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.kt index 3e0015b702..341650b6d6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.kt @@ -123,7 +123,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v } c.drawText( - text!!, + text, fixedPosition + xOffset, positions[i * 2 + 1] + offset, paintAxisLabels @@ -136,7 +136,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v if (!yAxis.isEnabled) return if (yAxis.isDrawGridLinesEnabled) { - c.withClip(gridClippingRect!!) { + c.withClip(gridClippingRect) { val positions = transformedPositions paintGrid.color = yAxis.gridColor @@ -150,7 +150,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v var i = 0 while (i < positions.size) { // draw a path because lines don't support dashing on lower android versions - c.drawPath(linePath(gridLinePath, i, positions)!!, paintGrid) + c.drawPath(linePath(gridLinePath, i, positions), paintGrid) gridLinePath.reset() i += 2 } @@ -165,7 +165,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v @JvmField protected var mGridClippingRect: RectF = RectF() - open val gridClippingRect: RectF? + open val gridClippingRect: RectF get() { mGridClippingRect.set(viewPortHandler.contentRect) mGridClippingRect.inset(0f, -axis.gridLineWidth) @@ -175,7 +175,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v /** * Calculates the path for a grid line. */ - protected open fun linePath(p: Path, i: Int, positions: FloatArray): Path? { + protected open fun linePath(p: Path, i: Int, positions: FloatArray): Path { p.moveTo(viewPortHandler.offsetLeft(), positions[i + 1]) p.lineTo(viewPortHandler.contentRight(), positions[i + 1]) @@ -225,28 +225,28 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v * Draws the zero line. */ protected open fun drawZeroLine(c: Canvas) { - val clipRestoreCount = c.save() - zeroLineClippingRect.set(viewPortHandler.contentRect) - zeroLineClippingRect.inset(0f, -yAxis.zeroLineWidth) - c.clipRect(zeroLineClippingRect) + c.withSave { + zeroLineClippingRect.set(viewPortHandler.contentRect) + zeroLineClippingRect.inset(0f, -yAxis.zeroLineWidth) + c.clipRect(zeroLineClippingRect) - // draw zero line - val pos = transformer?.getPixelForValues(0f, 0f) - pos?.let { - zeroLinePaint.color = yAxis.zeroLineColor - zeroLinePaint.strokeWidth = yAxis.zeroLineWidth + // draw zero line + val pos = transformer?.getPixelForValues(0f, 0f) + pos?.let { + zeroLinePaint.color = yAxis.zeroLineColor + zeroLinePaint.strokeWidth = yAxis.zeroLineWidth - val zeroLinePath = drawZeroLinePath - zeroLinePath.reset() + val zeroLinePath = drawZeroLinePath + zeroLinePath.reset() - zeroLinePath.moveTo(viewPortHandler.contentLeft(), it.y.toFloat()) - zeroLinePath.lineTo(viewPortHandler.contentRight(), it.y.toFloat()) + zeroLinePath.moveTo(viewPortHandler.contentLeft(), it.y.toFloat()) + zeroLinePath.lineTo(viewPortHandler.contentRight(), it.y.toFloat()) - // draw a path because lines don't support dashing on lower android versions - c.drawPath(zeroLinePath, zeroLinePaint) - } + // draw a path because lines don't support dashing on lower android versions + c.drawPath(zeroLinePath, zeroLinePaint) + } - c.restoreToCount(clipRestoreCount) + } } protected var renderLimitRanges: Path = Path() @@ -268,7 +268,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v */ override fun renderLimitLines(c: Canvas) { val limitLines = yAxis.limitLines - if (limitLines != null && limitLines.size > 0) { + if (limitLines.isNotEmpty()) { val pts = renderLimitLinesBuffer pts[0] = 0f pts[1] = 0f @@ -319,34 +319,39 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v val position = limitLine.labelPosition - if (position == LimitLabelPosition.RIGHT_TOP) { - limitLinePaint.textAlign = Align.RIGHT - c.drawText( - label, - viewPortHandler.contentRight() - xOffset, - pts[1] - yOffset + labelLineHeight, limitLinePaint - ) - } else if (position == LimitLabelPosition.RIGHT_BOTTOM) { - limitLinePaint.textAlign = Align.RIGHT - c.drawText( - label, - viewPortHandler.contentRight() - xOffset, - pts[1] + yOffset, limitLinePaint - ) - } else if (position == LimitLabelPosition.LEFT_TOP) { - limitLinePaint.textAlign = Align.LEFT - c.drawText( - label, - viewPortHandler.contentLeft() + xOffset, - pts[1] - yOffset + labelLineHeight, limitLinePaint - ) - } else { - limitLinePaint.textAlign = Align.LEFT - c.drawText( - label, - viewPortHandler.offsetLeft() + xOffset, - pts[1] + yOffset, limitLinePaint - ) + when (position) { + LimitLabelPosition.RIGHT_TOP -> { + limitLinePaint.textAlign = Align.RIGHT + c.drawText( + label, + viewPortHandler.contentRight() - xOffset, + pts[1] - yOffset + labelLineHeight, limitLinePaint + ) + } + LimitLabelPosition.RIGHT_BOTTOM -> { + limitLinePaint.textAlign = Align.RIGHT + c.drawText( + label, + viewPortHandler.contentRight() - xOffset, + pts[1] + yOffset, limitLinePaint + ) + } + LimitLabelPosition.LEFT_TOP -> { + limitLinePaint.textAlign = Align.LEFT + c.drawText( + label, + viewPortHandler.contentLeft() + xOffset, + pts[1] - yOffset + labelLineHeight, limitLinePaint + ) + } + else -> { + limitLinePaint.textAlign = Align.LEFT + c.drawText( + label, + viewPortHandler.offsetLeft() + xOffset, + pts[1] + yOffset, limitLinePaint + ) + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.kt index 89563449a2..78931dff30 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.kt @@ -220,7 +220,7 @@ open class YAxisRendererHorizontalBarChart( override fun renderLimitLines(c: Canvas) { val limitLines = yAxis.limitLines - if (limitLines == null || limitLines.size <= 0) return + if (limitLines.isEmpty()) return val pts = renderLimitLinesBuffer pts[0] = 0f diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt index 6aacd6c1e6..df965e76e2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt @@ -137,9 +137,9 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr override fun renderAxisLabels(c: Canvas) { if (!yAxis.isEnabled || !yAxis.isDrawLabelsEnabled) return - paintAxisLabels!!.setTypeface(yAxis.typeface) - paintAxisLabels!!.textSize = yAxis.textSize - paintAxisLabels!!.color = yAxis.textColor + paintAxisLabels.setTypeface(yAxis.typeface) + paintAxisLabels.textSize = yAxis.textSize + paintAxisLabels.color = yAxis.textColor val center = chart.centerOffsets val pOut = MPPointF.getInstance(0f, 0f) @@ -160,7 +160,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr val label = yAxis.getFormattedLabel(j) - c.drawText(label, pOut.x + xOffset, pOut.y, paintAxisLabels!!) + c.drawText(label, pOut.x + xOffset, pOut.y, paintAxisLabels) } MPPointF.recycleInstance(center) MPPointF.recycleInstance(pOut) @@ -182,9 +182,9 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr if (!l.isEnabled) continue - limitLinePaint!!.color = l.lineColor - limitLinePaint!!.setPathEffect(l.dashPathEffect) - limitLinePaint!!.strokeWidth = l.lineWidth + limitLinePaint.color = l.lineColor + limitLinePaint.setPathEffect(l.dashPathEffect) + limitLinePaint.strokeWidth = l.lineWidth val r = (l.limit - chart.yChartMin) * factor @@ -192,7 +192,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr limitPath.reset() - for (j in 0..<(chart.data!!.maxEntryCountSet?.entryCount ?: 0)) { + for (j in 0..<(chart.data?.maxEntryCountSet?.entryCount ?: 0)) { Utils.getPosition(center, r, sliceAngle * j + chart.rotationAngle, pOut) if (j == 0) limitPath.moveTo(pOut.x, pOut.y) @@ -200,7 +200,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr } limitPath.close() - c.drawPath(limitPath, limitLinePaint!!) + c.drawPath(limitPath, limitLinePaint) } MPPointF.recycleInstance(center) MPPointF.recycleInstance(pOut) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt index fd55b6f5f4..4e9fbce920 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/TriangleShapeRenderer.kt @@ -12,7 +12,7 @@ import com.github.mikephil.charting.utils.ViewPortHandler * Created by wajdic on 15/06/2016. * Created at Time 09:08 */ -class TriangleShapeRenderer : IShapeRenderer { +open class TriangleShapeRenderer : IShapeRenderer { protected var mTrianglePathBuffer: Path = Path() override fun renderShape( diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt index 336b9d30f3..26ea663b95 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt @@ -100,7 +100,7 @@ object ColorTemplate { * @return */ fun createColors(r: Resources, colors: IntArray): MutableList { - val result: MutableList = ArrayList() + val result: MutableList = ArrayList() for (i in colors) { result.add(r.getColor(i)) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt index b70ef8ee7d..8f6e147a99 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/EntryXComparator.kt @@ -10,10 +10,10 @@ class EntryXComparator : Comparator { override fun compare(entry1: Entry, entry2: Entry): Int { val diff = entry1.x - entry2.x - if (diff == 0f) return 0 + return if (diff == 0f) 0 else { - if (diff > 0f) return 1 - else return -1 + if (diff > 0f) 1 + else -1 } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt index 70c68f0333..a137b53bbf 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt @@ -26,15 +26,14 @@ open class FSize : Poolable { this.height = height } - override fun equals(obj: Any?): Boolean { - if (obj == null) { + override fun equals(other: Any?): Boolean { + if (other == null) { return false } - if (this === obj) { + if (this === other) { return true } - if (obj is FSize) { - val other = obj + if (other is FSize) { return width == other.width && height == other.height } return false @@ -70,7 +69,7 @@ open class FSize : Poolable { pool.recycle(instance) } - fun recycleInstances(instances: MutableList) { + fun recycleInstances(instances: MutableList) { pool.recycle(instances) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt index c577aab3a2..4ad06f6126 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FileUtils.kt @@ -34,7 +34,7 @@ object FileUtils { // Get the text file val file = File(sdcard, path) - val entries: MutableList = ArrayList() + val entries: MutableList = ArrayList() try { val br = BufferedReader(FileReader(file)) @@ -99,7 +99,7 @@ object FileUtils { */ @JvmStatic fun loadEntriesFromAssets(am: AssetManager, path: String): MutableList { - val entries: MutableList = ArrayList() + val entries: MutableList = ArrayList() var reader: BufferedReader? = null try { @@ -211,7 +211,7 @@ object FileUtils { @JvmStatic fun loadBarEntriesFromAssets(am: AssetManager, path: String): MutableList { - val entries: MutableList = ArrayList() + val entries: MutableList = ArrayList() var reader: BufferedReader? = null try { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt index 9403d62b12..21cbd3f239 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Fill.kt @@ -8,6 +8,7 @@ import android.graphics.RectF import android.graphics.Shader import android.graphics.drawable.Drawable import kotlin.math.floor +import androidx.core.graphics.withClip open class Fill { enum class Type { @@ -101,11 +102,13 @@ open class Fill { } private fun calculateFinalColor() { - if (mColor == null) { + val color = mColor + + if (color == null) { mFinalColor = null } else { - val alpha = floor(((mColor!! shr 24) / 255.0) * (mAlpha / 255.0) * 255.0).toInt() - mFinalColor = (alpha shl 24) or (mColor!! and 0xffffff) + val alpha = floor(((color shr 24) / 255.0) * (mAlpha / 255.0) * 255.0).toInt() + mFinalColor = (alpha shl 24) or (color and 0xffffff) } } @@ -118,69 +121,60 @@ open class Fill { Type.EMPTY -> return Type.COLOR -> { - if (mFinalColor == null) { + val finalColor = mFinalColor + if (finalColor == null) { return } if (this.isClipPathSupported) { - val save = c.save() - - c.clipRect(left, top, right, bottom) - c.drawColor(mFinalColor!!) - - c.restoreToCount(save) + c.withClip(left, top, right, bottom) { + c.drawColor(finalColor) + } } else { // save - val previous = paint.getStyle() - val previousColor = paint.getColor() + val previous = paint.style + val previousColor = paint.color // set - paint.setStyle(Paint.Style.FILL) - paint.setColor(mFinalColor!!) + paint.style = Paint.Style.FILL + paint.setColor(finalColor) c.drawRoundRect(RectF(left, top, right, bottom), mRoundedBarRadius, mRoundedBarRadius, paint) // restore paint.setColor(previousColor) - paint.setStyle(previous) + paint.style = previous } } Type.LINEAR_GRADIENT -> { - if (this.gradientColors == null) { + val gradientColors = this.gradientColors + if (gradientColors == null) { return } val gradient = LinearGradient( - (if (gradientDirection == Direction.RIGHT) - right - else - if (gradientDirection == Direction.LEFT) - left - else - left).toInt().toFloat(), - (if (gradientDirection == Direction.UP) - bottom - else - if (gradientDirection == Direction.DOWN) - top - else - top).toInt().toFloat(), - (if (gradientDirection == Direction.RIGHT) - left - else - if (gradientDirection == Direction.LEFT) - right - else - left).toInt().toFloat(), - (if (gradientDirection == Direction.UP) - top - else - if (gradientDirection == Direction.DOWN) - bottom - else - top).toInt().toFloat(), - this.gradientColors!!, + (when (gradientDirection) { + Direction.RIGHT -> right + Direction.LEFT -> left + else -> left + }).toInt().toFloat(), + (when (gradientDirection) { + Direction.UP -> bottom + Direction.DOWN -> top + else -> top + }).toInt().toFloat(), + (when (gradientDirection) { + Direction.RIGHT -> left + Direction.LEFT -> right + else -> left + }).toInt().toFloat(), + (when (gradientDirection) { + Direction.UP -> top + Direction.DOWN -> bottom + else -> top + }).toInt().toFloat(), + gradientColors, this.gradientPositions, Shader.TileMode.MIRROR ) @@ -191,12 +185,8 @@ open class Fill { } Type.DRAWABLE -> { - if (mDrawable == null) { - return - } - - mDrawable!!.setBounds(left.toInt(), top.toInt(), right.toInt(), bottom.toInt()) - mDrawable!!.draw(c) + mDrawable?.setBounds(left.toInt(), top.toInt(), right.toInt(), bottom.toInt()) + mDrawable?.draw(c) } } } @@ -206,48 +196,49 @@ open class Fill { clipRect: RectF? ) { when (this.type) { - Type.EMPTY -> return + Type.EMPTY -> { + return + } Type.COLOR -> { - if (mFinalColor == null) { + val finalColor = mFinalColor + if (finalColor == null) { return } if (clipRect != null && this.isClipPathSupported) { - val save = c.save() - - c.clipPath(path) - c.drawColor(mFinalColor!!) - - c.restoreToCount(save) + c.withClip(path) { + c.drawColor(finalColor) + } } else { // save - val previous = paint.getStyle() - val previousColor = paint.getColor() + val previous = paint.style + val previousColor = paint.color // set - paint.setStyle(Paint.Style.FILL) - paint.setColor(mFinalColor!!) + paint.style = Paint.Style.FILL + paint.setColor(finalColor) c.drawPath(path, paint) // restore paint.setColor(previousColor) - paint.setStyle(previous) + paint.style = previous } } Type.LINEAR_GRADIENT -> { - if (this.gradientColors == null) { + val gradientColors = this.gradientColors + if (gradientColors == null) { return } val gradient = LinearGradient( 0f, 0f, - c.getWidth().toFloat(), - c.getHeight().toFloat(), - this.gradientColors!!, + c.width.toFloat(), + c.height.toFloat(), + gradientColors, this.gradientPositions, Shader.TileMode.MIRROR ) @@ -267,13 +258,13 @@ open class Fill { val save = c.save() c.clipPath(path) - mDrawable!!.setBounds( - if (clipRect == null) 0 else clipRect.left.toInt(), - if (clipRect == null) 0 else clipRect.top.toInt(), - if (clipRect == null) c.getWidth() else clipRect.right.toInt(), - if (clipRect == null) c.getHeight() else clipRect.bottom.toInt() + mDrawable?.setBounds( + clipRect?.left?.toInt() ?: 0, + clipRect?.top?.toInt() ?: 0, + clipRect?.right?.toInt() ?: c.width, + clipRect?.bottom?.toInt() ?: c.height ) - mDrawable!!.draw(c) + mDrawable?.draw(c) c.restoreToCount(save) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt index 7a0cd82c78..f4ce07e3b4 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt @@ -37,7 +37,7 @@ class MPPointD private constructor(var x: Double, var y: Double) : Poolable) { + fun recycleInstances(instances: MutableList) { pool.recycle(instances) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt index b45b45f8db..12b3062fdb 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt @@ -63,7 +63,7 @@ class MPPointF : Poolable { pool.recycle(instance) } - fun recycleInstances(instances: MutableList) { + fun recycleInstances(instances: MutableList) { pool.recycle(instances) } @@ -81,7 +81,7 @@ class MPPointF : Poolable { * Return an array of rectangles of the specified size. */ override fun newArray(size: Int): Array { - return arrayOfNulls(size) + return arrayOfNulls(size) } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt index 19961b51d2..2468c133e2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt @@ -15,7 +15,7 @@ import com.github.mikephil.charting.utils.ObjectPool.Poolable * * Created by Tony Patino on 6/20/16. */ -class ObjectPool?> private constructor(withCapacity: Int, `object`: T?) { +class ObjectPool> private constructor(withCapacity: Int, `object`: T?) { /** * Returns the id of the given pool instance. * @@ -24,8 +24,7 @@ class ObjectPool?> private constructor(withCapacity: Int, `objec var poolId: Int = 0 private set private var desiredCapacity: Int - private var objects: Array - private var objectsPointer: Int + private val objects = ArrayList(withCapacity) private val modelObject: T? private var replenishPercentage: Float @@ -33,8 +32,6 @@ class ObjectPool?> private constructor(withCapacity: Int, `objec init { require(withCapacity > 0) { "Object Pool must be instantiated with a capacity greater than 0!" } this.desiredCapacity = withCapacity - this.objects = arrayOfNulls(this.desiredCapacity) - this.objectsPointer = 0 this.modelObject = `object` this.replenishPercentage = 1.0f this.refillPool() @@ -69,10 +66,11 @@ class ObjectPool?> private constructor(withCapacity: Int, `objec portionOfCapacity = desiredCapacity } - for (i in 0..?> private constructor(withCapacity: Int, `objec */ @Synchronized fun get(): T { - if (this.objectsPointer == -1 && this.replenishPercentage > 0.0f) { + if (objects.isEmpty() && this.replenishPercentage > 0.0f) { this.refillPool() } - val result = objects[this.objectsPointer] - objects[this.objectsPointer] = null - result!!.currentOwnerId = Poolable.Companion.NO_OWNER - this.objectsPointer-- + val result = objects.removeAt(objects.lastIndex) + result.currentOwnerId = Poolable.Companion.NO_OWNER return result } @@ -103,19 +99,18 @@ class ObjectPool?> private constructor(withCapacity: Int, `objec * @param object An object of type T to recycle */ @Synchronized - fun recycle(`object`: T?) { - if (`object`!!.currentOwnerId != Poolable.Companion.NO_OWNER) { + fun recycle(`object`: T) { + if (`object`.currentOwnerId != Poolable.Companion.NO_OWNER) { require(`object`.currentOwnerId != this.poolId) { "The object passed is already stored in this pool!" } throw IllegalArgumentException("The object to recycle already belongs to poolId " + `object`.currentOwnerId + ". Object cannot belong to two different pool instances simultaneously!") } - this.objectsPointer++ - if (this.objectsPointer >= objects.size) { - this.resizePool() - } - `object`.currentOwnerId = this.poolId - objects[this.objectsPointer] = `object` + objects.add(`object`) + + if (objects.size > desiredCapacity) { + resizePool() + } } /** @@ -125,33 +120,27 @@ class ObjectPool?> private constructor(withCapacity: Int, `objec * @param objects A list of objects of type T to recycle */ @Synchronized - fun recycle(objects: MutableList) { - while (objects.size + this.objectsPointer + 1 > this.desiredCapacity) { - this.resizePool() - } + fun recycle(objects: MutableList) { val objectsListSize = objects.size + while (objectsListSize + this.objects.size > this.desiredCapacity) { + resizePool() + } + // Not relying on recycle(T object) because this is more performant. for (i in 0.. = arrayOfNulls(this.desiredCapacity) - for (i in 0..?> private constructor(withCapacity: Int, `objec * * @return The capacity of the pool. */ - get() = this.objects.size + get() = this.desiredCapacity val poolCount: Int /** @@ -170,13 +159,13 @@ class ObjectPool?> private constructor(withCapacity: Int, `objec * * @return The number of objects remaining in the pool. */ - get() = this.objectsPointer + 1 + get() = objects.size - abstract class Poolable?> { + abstract class Poolable> { var currentOwnerId: Int = NO_OWNER - abstract fun instantiate(): T? + abstract fun instantiate(): T companion object { var NO_OWNER: Int = -1 diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt index d780015fa9..a2100201ee 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt @@ -9,7 +9,6 @@ import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet import com.github.mikephil.charting.interfaces.datasets.ILineDataSet import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet import kotlin.Boolean -import kotlin.FloatArray import kotlin.Int /** @@ -87,8 +86,8 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { * @return */ fun generateTransformedValuesScatter( - data: IScatterDataSet, phaseX: kotlin.Float, - phaseY: kotlin.Float, from: Int, to: Int + data: IScatterDataSet, phaseX: Float, + phaseY: Float, from: Int, to: Int ): FloatArray { val count = ((to - from) * phaseX + 1).toInt() * 2 @@ -101,13 +100,8 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { while (j < count) { val e = data.getEntryForIndex(j / 2 + from) - if (e != null) { - valuePoints[j] = e.x - valuePoints[j + 1] = e.y * phaseY - } else { - valuePoints[j] = 0f - valuePoints[j + 1] = 0f - } + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY j += 2 } @@ -125,7 +119,7 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { * @param data * @return */ - fun generateTransformedValuesBubble(data: IBubbleDataSet, phaseY: kotlin.Float, from: Int, to: Int): FloatArray { + fun generateTransformedValuesBubble(data: IBubbleDataSet, phaseY: Float, from: Int, to: Int): FloatArray { val count = (to - from + 1) * 2 // (int) Math.ceil((to - from) * phaseX) * 2; if (valuePointsForGenerateTransformedValuesBubble.size != count) { @@ -137,13 +131,8 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { while (j < count) { val e: Entry = data.getEntryForIndex(j / 2 + from) - if (e != null) { - valuePoints[j] = e.x - valuePoints[j + 1] = e.y * phaseY - } else { - valuePoints[j] = 0f - valuePoints[j + 1] = 0f - } + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY j += 2 } @@ -163,7 +152,7 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { */ fun generateTransformedValuesLine( data: ILineDataSet, - phaseX: kotlin.Float, phaseY: kotlin.Float, + phaseX: Float, phaseY: Float, min: Int, max: Int ): FloatArray { var count = (((max - min) * phaseX).toInt() + 1) * 2 @@ -178,13 +167,8 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { while (j < count) { val e = data.getEntryForIndex(j / 2 + min) - if (e != null) { - valuePoints[j] = e.x - valuePoints[j + 1] = e.y * phaseY - } else { - valuePoints[j] = 0f - valuePoints[j + 1] = 0f - } + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY j += 2 } @@ -204,7 +188,7 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { */ fun generateTransformedValuesCandle( data: ICandleDataSet, - phaseX: kotlin.Float, phaseY: kotlin.Float, from: Int, to: Int + phaseX: Float, phaseY: Float, from: Int, to: Int ): FloatArray { val count = ((to - from) * phaseX + 1).toInt() * 2 @@ -244,9 +228,9 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { * * @param paths */ - fun pathValuesToPixel(paths: MutableList) { + fun pathValuesToPixel(paths: MutableList) { for (i in paths.indices) { - pathValueToPixel(paths.get(i)!!) + pathValueToPixel(paths[i]) } } @@ -279,7 +263,7 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { * @param r * @param phaseY */ - fun rectToPixelPhase(r: RectF, phaseY: kotlin.Float) { + fun rectToPixelPhase(r: RectF, phaseY: Float) { // multiply the height of the rect with the phase r.top *= phaseY @@ -290,7 +274,7 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { offsetMatrix.mapRect(r) } - fun rectToPixelPhaseHorizontal(r: RectF, phaseY: kotlin.Float) { + fun rectToPixelPhaseHorizontal(r: RectF, phaseY: Float) { // multiply the height of the rect with the phase r.left *= phaseY @@ -318,7 +302,7 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { * @param r * @param phaseY */ - fun rectValueToPixelHorizontal(r: RectF, phaseY: kotlin.Float) { + fun rectValueToPixelHorizontal(r: RectF, phaseY: Float) { // multiply the height of the rect with the phase r.left *= phaseY @@ -334,10 +318,10 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { * * @param rects */ - fun rectValuesToPixel(rects: MutableList) { + fun rectValuesToPixel(rects: MutableList) { val m = this.valueToPixelMatrix - for (i in rects.indices) m.mapRect(rects.get(i)) + for (i in rects.indices) m.mapRect(rects[i]) } protected var mPixelToValueMatrixBuffer: Matrix = Matrix() @@ -379,13 +363,13 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { * @param y * @return */ - fun getValuesByTouchPoint(x: kotlin.Float, y: kotlin.Float): MPPointD { + fun getValuesByTouchPoint(x: Float, y: Float): MPPointD { val result: MPPointD = MPPointD.Companion.getInstance(0.0, 0.0) getValuesByTouchPoint(x, y, result) return result } - fun getValuesByTouchPoint(x: kotlin.Float, y: kotlin.Float, outputPoint: MPPointD) { + fun getValuesByTouchPoint(x: Float, y: Float, outputPoint: MPPointD) { ptsBuffer[0] = x ptsBuffer[1] = y @@ -403,7 +387,7 @@ open class Transformer(protected var mViewPortHandler: ViewPortHandler) { * @param y * @return */ - fun getPixelForValues(x: kotlin.Float, y: kotlin.Float): MPPointD { + fun getPixelForValues(x: Float, y: Float): MPPointD { ptsBuffer[0] = x ptsBuffer[1] = y diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt index ce52767eec..1533e9c9f7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt @@ -18,22 +18,9 @@ import android.view.MotionEvent import android.view.VelocityTracker import android.view.View import android.view.ViewConfiguration +import androidx.core.graphics.withTranslation import com.github.mikephil.charting.formatter.DefaultValueFormatter import com.github.mikephil.charting.formatter.IValueFormatter -import kotlin.Array -import kotlin.Boolean -import kotlin.Char -import kotlin.CharArray -import kotlin.Deprecated -import kotlin.Int -import kotlin.IntArray -import kotlin.String -import kotlin.Suppress -import kotlin.arrayOfNulls -import kotlin.code -import kotlin.collections.MutableList -import kotlin.collections.indices -import kotlin.intArrayOf import kotlin.math.abs import kotlin.math.ceil import kotlin.math.cos @@ -43,7 +30,6 @@ import kotlin.math.min import kotlin.math.pow import kotlin.math.roundToInt import kotlin.math.sin -import androidx.core.graphics.withTranslation /** * Utilities class that has some helper methods. Needs to be initialized by @@ -59,8 +45,8 @@ object Utils { private set var maximumFlingVelocity: Int = 8000 private set - val DEG2RAD: Double = (Math.PI / 180.0) - val FDEG2RAD: Float = (Math.PI.toFloat() / 180f) + const val DEG2RAD: Double = (Math.PI / 180.0) + const val FDEG2RAD: Float = (Math.PI.toFloat() / 180f) @Suppress("unused") val DOUBLE_EPSILON: Double = java.lang.Double.longBitsToDouble(1) @@ -85,11 +71,11 @@ object Utils { ) } else { val viewConfiguration = ViewConfiguration.get(context) - minimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity() - maximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity() + minimumFlingVelocity = viewConfiguration.scaledMinimumFlingVelocity + maximumFlingVelocity = viewConfiguration.scaledMaximumFlingVelocity - val res = context.getResources() - mMetrics = res.getDisplayMetrics() + val res = context.resources + mMetrics = res.displayMetrics } } @@ -101,7 +87,7 @@ object Utils { */ @Deprecated("") fun init(res: Resources) { - mMetrics = res.getDisplayMetrics() + mMetrics = res.displayMetrics // noinspection deprecation minimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity() @@ -298,7 +284,7 @@ object Utils { } number *= POW_10[digitCount].toFloat() - var lval = Math.round(number).toLong() + var lval = number.roundToInt().toLong() var ind = out.size - 1 var charCount = 0 var decimalPointAdded = false @@ -386,7 +372,7 @@ object Utils { * @param integers * @return */ - fun convertIntegers(integers: MutableList): IntArray { + fun convertIntegers(integers: MutableList): IntArray { val ret = IntArray(integers.size) copyIntegers(integers, ret) @@ -394,10 +380,10 @@ object Utils { return ret } - fun copyIntegers(from: MutableList, to: IntArray) { + fun copyIntegers(from: MutableList, to: IntArray) { val count = min(to.size, from.size) for (i in 0..(strings.size) for (i in ret.indices) { - ret[i] = strings.get(i) + ret[i] = strings[i] } return ret @@ -560,8 +546,8 @@ object Utils { drawOffsetY -= mFontMetricsBuffer.ascent // To have a consistent point of reference, we always draw left-aligned - val originalTextAlign = paint.getTextAlign() - paint.setTextAlign(Align.LEFT) + val originalTextAlign = paint.textAlign + paint.textAlign = Align.LEFT if (angleDegrees != 0f) { // Move the text drawing rect in a way that it always rotates around its center @@ -614,13 +600,12 @@ object Utils { ) { var drawOffsetX = 0f var drawOffsetY = 0f - val drawWidth: Float val drawHeight: Float val lineHeight = paint.getFontMetrics(mFontMetricsBuffer) - drawWidth = textLayout.getWidth().toFloat() - drawHeight = textLayout.getLineCount() * lineHeight + val drawWidth = textLayout.width.toFloat() + drawHeight = textLayout.lineCount * lineHeight // Android sometimes has pre-padding drawOffsetX -= mDrawTextRectBuffer.left.toFloat() @@ -631,8 +616,8 @@ object Utils { drawOffsetY += drawHeight // To have a consistent point of reference, we always draw left-aligned - val originalTextAlign = paint.getTextAlign() - paint.setTextAlign(Align.LEFT) + val originalTextAlign = paint.textAlign + paint.textAlign = Align.LEFT if (angleDegrees != 0f) { // Move the text drawing rect in a way that it always rotates around its center @@ -656,14 +641,12 @@ object Utils { FSize.Companion.recycleInstance(rotatedSize) } - c.save() - c.translate(translateX, translateY) - c.rotate(angleDegrees) - - c.translate(drawOffsetX, drawOffsetY) - textLayout.draw(c) + c.withTranslation(translateX, translateY) { + rotate(angleDegrees) - c.restore() + translate(drawOffsetX, drawOffsetY) + textLayout.draw(this) + } } else { if (anchor.x != 0f || anchor.y != 0f) { drawOffsetX -= drawWidth * anchor.x @@ -673,15 +656,12 @@ object Utils { drawOffsetX += x drawOffsetY += y - c.save() - - c.translate(drawOffsetX, drawOffsetY) - textLayout.draw(c) - - c.restore() + c.withTranslation(drawOffsetX, drawOffsetY) { + textLayout.draw(this) + } } - paint.setTextAlign(originalTextAlign) + paint.textAlign = originalTextAlign } fun drawMultilineText( From 19568fce82d9093ca1ff9e6f2efa096d39f33cc8 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sat, 13 Sep 2025 21:35:47 -0400 Subject: [PATCH 4/9] Rename .java to .kt --- .../{AnotherBarActivity.java => AnotherBarActivity.kt} | 0 .../chartexample/{BarChartActivity.java => BarChartActivity.kt} | 0 ...tActivityMultiDataset.java => BarChartActivityMultiDataset.kt} | 0 .../{BarChartActivitySinus.java => BarChartActivitySinus.kt} | 0 ...{BarChartPositiveNegative.java => BarChartPositiveNegative.kt} | 0 .../{BubbleChartActivity.java => BubbleChartActivity.kt} | 0 ...{CandleStickChartActivity.java => CandleStickChartActivity.kt} | 0 .../{CombinedChartActivity.java => CombinedChartActivity.kt} | 0 .../{DynamicalAddingActivity.java => DynamicalAddingActivity.kt} | 0 .../{FilledLineActivity.java => FilledLineActivity.kt} | 0 .../{HalfPieChartActivity.java => HalfPieChartActivity.kt} | 0 ...izontalBarChartActivity.java => HorizontalBarChartActivity.kt} | 0 ...veChartActivity.java => HorizontalBarNegativeChartActivity.kt} | 0 ...nvertedLineChartActivity.java => InvertedLineChartActivity.kt} | 0 ...{LineChartActivityColored.java => LineChartActivityColored.kt} | 0 ...ineChartDualAxisActivity.java => LineChartDualAxisActivity.kt} | 0 .../appdev/chartexample/{LineChartTime.java => LineChartTime.kt} | 0 ...{ListViewBarChartActivity.java => ListViewBarChartActivity.kt} | 0 ...tViewMultiChartActivity.java => ListViewMultiChartActivity.kt} | 0 .../{MultiLineChartActivity.java => MultiLineChartActivity.kt} | 0 .../{PerformanceLineChart.java => PerformanceLineChart.kt} | 0 .../chartexample/{PieChartActivity.java => PieChartActivity.kt} | 0 .../{PieChartRoundedActivity.java => PieChartRoundedActivity.kt} | 0 ...{PiePolylineChartActivity.java => PiePolylineChartActivity.kt} | 0 .../{RadarChartActivity.java => RadarChartActivity.kt} | 0 ...ealtimeLineChartActivity.java => RealtimeLineChartActivity.kt} | 0 .../{ScatterChartActivity.java => ScatterChartActivity.kt} | 0 .../{ScrollViewActivity.java => ScrollViewActivity.kt} | 0 .../{StackedBarActivity.java => StackedBarActivity.kt} | 0 ...ckedBarActivityNegative.java => StackedBarActivityNegative.kt} | 0 ...tomScatterShapeRenderer.java => CustomScatterShapeRenderer.kt} | 0 .../chartexample/custom/{MyMarkerView.java => MyMarkerView.kt} | 0 .../custom/{RadarMarkerView.java => RadarMarkerView.kt} | 0 .../{StackedBarsMarkerView.java => StackedBarsMarkerView.kt} | 0 .../chartexample/custom/{XYMarkerView.java => XYMarkerView.kt} | 0 .../chartexample/fragments/{BarChartFrag.java => BarChartFrag.kt} | 0 .../fragments/{ComplexityFragment.java => ComplexityFragment.kt} | 0 .../chartexample/fragments/{PieChartFrag.java => PieChartFrag.kt} | 0 .../fragments/{ScatterChartFrag.java => ScatterChartFrag.kt} | 0 .../fragments/{SimpleFragment.java => SimpleFragment.kt} | 0 .../fragments/{SineCosineFragment.java => SineCosineFragment.kt} | 0 .../listviewitems/{BarChartItem.java => BarChartItem.kt} | 0 .../chartexample/listviewitems/{ChartItem.java => ChartItem.kt} | 0 .../listviewitems/{LineChartItem.java => LineChartItem.kt} | 0 .../listviewitems/{PieChartItem.java => PieChartItem.kt} | 0 45 files changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/info/appdev/chartexample/{AnotherBarActivity.java => AnotherBarActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{BarChartActivity.java => BarChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{BarChartActivityMultiDataset.java => BarChartActivityMultiDataset.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{BarChartActivitySinus.java => BarChartActivitySinus.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{BarChartPositiveNegative.java => BarChartPositiveNegative.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{BubbleChartActivity.java => BubbleChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{CandleStickChartActivity.java => CandleStickChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{CombinedChartActivity.java => CombinedChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{DynamicalAddingActivity.java => DynamicalAddingActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{FilledLineActivity.java => FilledLineActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{HalfPieChartActivity.java => HalfPieChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{HorizontalBarChartActivity.java => HorizontalBarChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{HorizontalBarNegativeChartActivity.java => HorizontalBarNegativeChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{InvertedLineChartActivity.java => InvertedLineChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{LineChartActivityColored.java => LineChartActivityColored.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{LineChartDualAxisActivity.java => LineChartDualAxisActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{LineChartTime.java => LineChartTime.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{ListViewBarChartActivity.java => ListViewBarChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{ListViewMultiChartActivity.java => ListViewMultiChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{MultiLineChartActivity.java => MultiLineChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{PerformanceLineChart.java => PerformanceLineChart.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{PieChartActivity.java => PieChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{PieChartRoundedActivity.java => PieChartRoundedActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{PiePolylineChartActivity.java => PiePolylineChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{RadarChartActivity.java => RadarChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{RealtimeLineChartActivity.java => RealtimeLineChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{ScatterChartActivity.java => ScatterChartActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{ScrollViewActivity.java => ScrollViewActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{StackedBarActivity.java => StackedBarActivity.kt} (100%) rename app/src/main/java/info/appdev/chartexample/{StackedBarActivityNegative.java => StackedBarActivityNegative.kt} (100%) rename app/src/main/java/info/appdev/chartexample/custom/{CustomScatterShapeRenderer.java => CustomScatterShapeRenderer.kt} (100%) rename app/src/main/java/info/appdev/chartexample/custom/{MyMarkerView.java => MyMarkerView.kt} (100%) rename app/src/main/java/info/appdev/chartexample/custom/{RadarMarkerView.java => RadarMarkerView.kt} (100%) rename app/src/main/java/info/appdev/chartexample/custom/{StackedBarsMarkerView.java => StackedBarsMarkerView.kt} (100%) rename app/src/main/java/info/appdev/chartexample/custom/{XYMarkerView.java => XYMarkerView.kt} (100%) rename app/src/main/java/info/appdev/chartexample/fragments/{BarChartFrag.java => BarChartFrag.kt} (100%) rename app/src/main/java/info/appdev/chartexample/fragments/{ComplexityFragment.java => ComplexityFragment.kt} (100%) rename app/src/main/java/info/appdev/chartexample/fragments/{PieChartFrag.java => PieChartFrag.kt} (100%) rename app/src/main/java/info/appdev/chartexample/fragments/{ScatterChartFrag.java => ScatterChartFrag.kt} (100%) rename app/src/main/java/info/appdev/chartexample/fragments/{SimpleFragment.java => SimpleFragment.kt} (100%) rename app/src/main/java/info/appdev/chartexample/fragments/{SineCosineFragment.java => SineCosineFragment.kt} (100%) rename app/src/main/java/info/appdev/chartexample/listviewitems/{BarChartItem.java => BarChartItem.kt} (100%) rename app/src/main/java/info/appdev/chartexample/listviewitems/{ChartItem.java => ChartItem.kt} (100%) rename app/src/main/java/info/appdev/chartexample/listviewitems/{LineChartItem.java => LineChartItem.kt} (100%) rename app/src/main/java/info/appdev/chartexample/listviewitems/{PieChartItem.java => PieChartItem.kt} (100%) diff --git a/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.java b/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/AnotherBarActivity.java rename to app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivity.java b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/BarChartActivity.java rename to app/src/main/java/info/appdev/chartexample/BarChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.java b/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.java rename to app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.java b/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.java rename to app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt diff --git a/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.java b/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.java rename to app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt diff --git a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.java b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/BubbleChartActivity.java rename to app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.java b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.java rename to app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.java b/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/CombinedChartActivity.java rename to app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.java b/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.java rename to app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/FilledLineActivity.java b/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/FilledLineActivity.java rename to app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/HalfPieChartActivity.java b/app/src/main/java/info/appdev/chartexample/HalfPieChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/HalfPieChartActivity.java rename to app/src/main/java/info/appdev/chartexample/HalfPieChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.java b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.java rename to app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.java b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.java rename to app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.java b/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.java rename to app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.java b/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/LineChartActivityColored.java rename to app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt diff --git a/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.java b/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.java rename to app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/LineChartTime.java b/app/src/main/java/info/appdev/chartexample/LineChartTime.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/LineChartTime.java rename to app/src/main/java/info/appdev/chartexample/LineChartTime.kt diff --git a/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.java b/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.java rename to app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.java b/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.java rename to app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.java b/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.java rename to app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.java b/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/PerformanceLineChart.java rename to app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt diff --git a/app/src/main/java/info/appdev/chartexample/PieChartActivity.java b/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/PieChartActivity.java rename to app/src/main/java/info/appdev/chartexample/PieChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.java b/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.java rename to app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.java b/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.java rename to app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/RadarChartActivity.java b/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/RadarChartActivity.java rename to app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.java b/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.java rename to app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.java b/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/ScatterChartActivity.java rename to app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.java b/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/ScrollViewActivity.java rename to app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.java b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/StackedBarActivity.java rename to app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.java b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.java rename to app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt diff --git a/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java b/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java rename to app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt diff --git a/app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.java b/app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.java rename to app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.kt diff --git a/app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.java b/app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.java rename to app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.kt diff --git a/app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.java b/app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.java rename to app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.kt diff --git a/app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.java b/app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.java rename to app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.kt diff --git a/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.java b/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.java rename to app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.java b/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.java rename to app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt diff --git a/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.java b/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.java rename to app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.java b/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.java rename to app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.java b/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.java rename to app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.java b/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.java rename to app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.java b/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.java rename to app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.java b/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.java rename to app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.java b/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.java rename to app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.java b/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt similarity index 100% rename from app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.java rename to app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt From 65c7545140f39f51688c96bdc3ce4693fcfd6200 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sat, 13 Sep 2025 21:35:47 -0400 Subject: [PATCH 5/9] Working prototype --- MPChartLib/build.gradle | 7 +- .../mikephil/charting/charts/BarChart.kt | 10 +- .../charting/charts/BarLineChartBase.kt | 4 +- .../mikephil/charting/charts/BubbleChart.kt | 4 +- .../charting/charts/CandleStickChart.kt | 4 +- .../github/mikephil/charting/charts/Chart.kt | 73 +-- .../mikephil/charting/charts/CombinedChart.kt | 4 +- .../charting/charts/HorizontalBarChart.kt | 4 +- .../mikephil/charting/charts/LineChart.kt | 3 +- .../mikephil/charting/charts/PieChart.kt | 4 +- .../charting/charts/PieRadarChartBase.kt | 4 +- .../mikephil/charting/charts/RadarChart.kt | 4 +- .../mikephil/charting/charts/ScatterChart.kt | 4 +- .../mikephil/charting/components/IMarker.kt | 2 +- .../charting/components/MarkerImage.kt | 2 +- .../charting/components/MarkerView.kt | 4 +- .../mikephil/charting/data/LineDataSet.kt | 1 - .../mikephil/charting/data/ScatterDataSet.kt | 10 - .../interfaces/dataprovider/ChartInterface.kt | 4 +- .../interfaces/datasets/IBarDataSet.kt | 2 - .../renderer/HorizontalBarChartRenderer.kt | 9 +- .../charting/renderer/LineChartRenderer.kt | 4 +- .../mikephil/charting/utils/FileUtils.kt | 8 +- .../mikephil/charting/utils/MPPointF.kt | 2 +- .../mikephil/charting/utils/ObjectPool.kt | 4 +- app/build.gradle | 6 +- .../appdev/chartexample/AnotherBarActivity.kt | 311 +++++----- .../appdev/chartexample/BarChartActivity.kt | 527 ++++++++-------- .../BarChartActivityMultiDataset.kt | 456 +++++++------- .../chartexample/BarChartActivitySinus.kt | 335 +++++------ .../chartexample/BarChartPositiveNegative.kt | 300 +++++----- .../chartexample/BubbleChartActivity.kt | 405 +++++++------ .../chartexample/CandleStickChartActivity.kt | 351 ++++++----- .../chartexample/CombinedChartActivity.kt | 438 +++++++------- .../chartexample/CubicLineChartActivity.kt | 86 +-- .../info/appdev/chartexample/DataTools.kt | 30 +- .../appdev/chartexample/DrawChartActivity.kt | 20 +- .../chartexample/DynamicalAddingActivity.kt | 328 +++++----- .../appdev/chartexample/FilledLineActivity.kt | 255 ++++---- .../chartexample/HalfPieChartActivity.kt | 279 +++++---- .../HorizontalBarChartActivity.kt | 423 +++++++------ .../HorizontalBarNegativeChartActivity.kt | 420 +++++++------ .../chartexample/InvertedLineChartActivity.kt | 350 +++++------ .../appdev/chartexample/LineChartActivity.kt | 16 +- .../chartexample/LineChartActivityColored.kt | 205 +++---- .../chartexample/LineChartDualAxisActivity.kt | 563 +++++++++--------- .../info/appdev/chartexample/LineChartTime.kt | 455 +++++++------- .../chartexample/ListViewBarChartActivity.kt | 221 ++++--- .../ListViewMultiChartActivity.kt | 260 ++++---- .../chartexample/MultiLineChartActivity.kt | 505 ++++++++-------- .../chartexample/PerformanceLineChart.kt | 191 +++--- .../appdev/chartexample/PieChartActivity.kt | 473 +++++++-------- .../chartexample/PieChartRoundedActivity.kt | 484 +++++++-------- .../chartexample/PiePolylineChartActivity.kt | 462 +++++++------- .../appdev/chartexample/RadarChartActivity.kt | 406 ++++++------- .../chartexample/RealtimeLineChartActivity.kt | 327 +++++----- .../chartexample/ScatterChartActivity.kt | 366 ++++++------ .../appdev/chartexample/ScrollViewActivity.kt | 146 +++-- .../SpecificPositionsLineChartActivity.kt | 46 +- .../appdev/chartexample/StackedBarActivity.kt | 399 ++++++------- .../StackedBarActivityNegative.kt | 512 ++++++++-------- .../custom/CustomScatterShapeRenderer.kt | 42 +- .../custom/DayAxisValueFormatter.kt | 2 +- .../chartexample/custom/MyFillFormatter.kt | 2 +- .../chartexample/custom/MyMarkerView.kt | 57 +- .../chartexample/custom/RadarMarkerView.kt | 55 +- .../custom/StackedBarsMarkerView.kt | 68 +-- .../chartexample/custom/XYMarkerView.kt | 58 +- .../chartexample/fragments/BarChartFrag.kt | 148 ++--- .../fragments/ComplexityFragment.kt | 72 +-- .../chartexample/fragments/PieChartFrag.kt | 105 ++-- .../fragments/ScatterChartFrag.kt | 99 ++- .../chartexample/fragments/SimpleFragment.kt | 294 +++++---- .../fragments/SineCosineFragment.kt | 76 ++- .../fragments/ViewPagerSimpleChartDemo.kt | 35 +- .../listviewitems/BarChartItem.kt | 121 ++-- .../chartexample/listviewitems/ChartItem.kt | 31 +- .../listviewitems/LineChartItem.kt | 112 ++-- .../listviewitems/PieChartItem.kt | 152 +++-- .../chartexample/notimportant/DemoBase.kt | 9 +- .../chartexample/notimportant/MainActivity.kt | 9 +- .../chartexample/notimportant/MenuAdapter.kt | 2 +- 82 files changed, 6154 insertions(+), 6933 deletions(-) diff --git a/MPChartLib/build.gradle b/MPChartLib/build.gradle index a77c339e8c..ec437f6692 100644 --- a/MPChartLib/build.gradle +++ b/MPChartLib/build.gradle @@ -28,8 +28,11 @@ android { consumerProguardFiles 'proguard-lib.pro' } compileOptions { - sourceCompatibility JavaVersion.VERSION_17 - targetCompatibility JavaVersion.VERSION_17 + sourceCompatibility JavaVersion.VERSION_21 + targetCompatibility JavaVersion.VERSION_21 + } + kotlinOptions { + jvmTarget = "21" } buildTypes { release { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt index 56cab326e3..b967d839a0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt @@ -53,9 +53,7 @@ open class BarChart : BarLineChartBase, BarDataP constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - override fun init() { - super.init() - + init { mRenderer = BarChartRenderer(this, mAnimator, viewPortHandler, mDrawRoundedBars, mRoundedBarRadius) setHighlighter(BarHighlighter(this)) @@ -201,8 +199,8 @@ open class BarChart : BarLineChartBase, BarDataP * @param dataSetIndex * @param dataIndex the index inside the stack - only relevant for stacked entries */ - override fun highlightValue(x: Float, dataSetIndex: Int, dataIndex: Int) { - highlightValue(Highlight(x, dataSetIndex, dataIndex), false) + override fun highlightValue(x: Float, y: Float, dataSetIndex: Int, dataIndex: Int, callListener: Boolean) { + super.highlightValue(Highlight(x, dataSetIndex, dataIndex), false) } override val barData: BarData? @@ -245,7 +243,7 @@ open class BarChart : BarLineChartBase, BarDataP fun setRoundedBarRadius(mRoundedBarRadius: Float) { this.mRoundedBarRadius = mRoundedBarRadius this.mDrawRoundedBars = true - init() + mRenderer = BarChartRenderer(this, mAnimator, viewPortHandler, mDrawRoundedBars, mRoundedBarRadius) } override val accessibilityDescription: String? diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt index 776cb2fbb2..469d9691c9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt @@ -252,9 +252,7 @@ abstract class BarLineChartBase, B constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - override fun init() { - super.init() - + init { mRenderer = BubbleChartRenderer(this, mAnimator, viewPortHandler) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt index 74d46e7f32..72eb645fe1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt @@ -20,9 +20,7 @@ class CandleStickChart : BarLineChartBase, T : ChartData> : ViewGroup, */ var isLogEnabled: Boolean = false - @Suppress("UsePropertyAccessSyntax") - override val width: Int - get() = getWidth() - - @Suppress("UsePropertyAccessSyntax") - override val height: Int - get() = getHeight() - /** * object that holds all data that was originally set for the chart, before * it was modified or any filtering algorithms had been applied @@ -235,27 +227,29 @@ abstract class Chart, T : ChartData> : ViewGroup, * default constructor for initialization in code */ constructor(context: Context) : super(context) { - init() } /** * constructor for initialization in xml */ constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { - init() } /** * even more awesome constructor */ constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) { - init() } + /** + * The maximum distance in dp away from an entry causing it to highlight. + */ + protected var mMaxHighlightDistance: Float = 0f + /** * initialize all paints and stuff */ - protected open fun init() { + init { setWillNotDraw(false) // initialize the utils @@ -476,11 +470,6 @@ abstract class Chart, T : ChartData> : ViewGroup, var highlighted: Array? = null protected set - /** - * The maximum distance in dp away from an entry causing it to highlight. - */ - protected var mMaxHighlightDistance: Float = 0f - override val maxHighlightDistance: Float get() = mMaxHighlightDistance @@ -535,52 +524,6 @@ abstract class Chart, T : ChartData> : ViewGroup, highlightValues(highs.toTypedArray()) } - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * This method will call the listener. - * @param x The x-value to highlight - * @param dataSetIndex The dataset index to search in - * @param dataIndex The data index to search in (only used in CombinedChartView currently) - */ - open fun highlightValue(x: Float, dataSetIndex: Int, dataIndex: Int) { - highlightValue(x, dataSetIndex, dataIndex, true) - } - - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * - * @param x The x-value to highlight - * @param dataSetIndex The dataset index to search in - * @param dataIndex The data index to search in (only used in CombinedChartView currently) - * @param callListener Should the listener be called for this change - */ - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * This method will call the listener. - * - * @param x The x-value to highlight - * @param dataSetIndex The dataset index to search in - */ - @JvmOverloads - fun highlightValue(x: Float, dataSetIndex: Int, dataIndex: Int = -1, callListener: Boolean = true) { - highlightValue(x, Float.Companion.NaN, dataSetIndex, dataIndex, callListener) - } - - /** - * Highlights any y-value at the given x-value in the given DataSet. - * Provide -1 as the dataSetIndex to undo all highlighting. - * - * @param x The x-value to highlight - * @param dataSetIndex The dataset index to search in - * @param callListener Should the listener be called for this change - */ - fun highlightValue(x: Float, dataSetIndex: Int, callListener: Boolean) { - highlightValue(x, Float.Companion.NaN, dataSetIndex, -1, callListener) - } - /** * Highlights any y-value at the given x-value in the given DataSet. * Provide -1 as the dataSetIndex to undo all highlighting. @@ -611,7 +554,7 @@ abstract class Chart, T : ChartData> : ViewGroup, * @param dataIndex The data index to search in (only used in CombinedChartView currently) */ @JvmOverloads - fun highlightValue(x: Float, y: Float, dataSetIndex: Int, dataIndex: Int = -1, callListener: Boolean = true) { + open fun highlightValue(x: Float, y: Float = Float.NaN, dataSetIndex: Int = -1, dataIndex: Int = -1, callListener: Boolean = true) { if (dataSetIndex < 0 || mData.let { it != null && dataSetIndex >= it.dataSetCount }) { highlightValue(null, callListener) } else { @@ -1153,7 +1096,7 @@ abstract class Chart, T : ChartData> : ViewGroup, /** * Returns the ChartData object that has been set for the chart. */ - override val data: ChartData? + override val data: T? get() = mData var renderer: DataRenderer? diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt index af540b031f..61eb64cd75 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt @@ -61,9 +61,7 @@ open class CombinedChart : BarLineChartBase, LineData constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - override fun init() { - super.init() + init { mRenderer = LineChartRenderer(this, mAnimator, viewPortHandler) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt index 64ae0bb8a8..41ca66ca90 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.kt @@ -202,9 +202,7 @@ open class PieChart : PieRadarChartBase { constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - override fun init() { - super.init() - + init { mRenderer = PieChartRenderer(this, mAnimator, viewPortHandler) highlighter = PieHighlighter(this) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt index 356101e71b..e37864dd0c 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt @@ -83,9 +83,7 @@ abstract class PieRadarChartBase, T : ChartData> constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - override fun init() { - super.init() - + init { mChartTouchListener = PieRadarChartTouchListener(this) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt index 058e34446d..3ff51154f5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.kt @@ -100,9 +100,7 @@ open class RadarChart : PieRadarChartBase constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - override fun init() { - super.init() - + init { mYAxis.labelXOffset = 10f mWebLineWidth = Utils.convertDpToPixel(1.5f) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt index 0da7e85367..cf69e9f440 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt @@ -23,9 +23,7 @@ class ScatterChart : BarLineChartBase, Scat constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - override fun init() { - super.init() - + init { mRenderer = ScatterChartRenderer(this, mAnimator, viewPortHandler) xAxis.spaceMin = 0.5f diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt index 3e36db7a88..3780cd2844 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/IMarker.kt @@ -32,7 +32,7 @@ interface IMarker { * @param highlight The highlight object contains information about the highlighted value such as it's dataset-index, the * selected range or stack-index (only stacked bar entries). */ - fun refreshContent(e: Entry?, highlight: Highlight?) + fun refreshContent(e: Entry, highlight: Highlight) /** * Draws the IMarker on the given position on the screen with the given Canvas object. diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt index a7d84ebc50..285f9f4fd2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt @@ -85,7 +85,7 @@ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMar return mOffset2 } - override fun refreshContent(e: Entry?, highlight: Highlight?) { + override fun refreshContent(e: Entry, highlight: Highlight) { } override fun draw(canvas: Canvas?, posX: Float, posY: Float) { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt index ad64372b13..d9ff82321e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerView.kt @@ -17,7 +17,7 @@ import androidx.core.graphics.withTranslation * * @author Philipp Jahoda */ -open class MarkerView(context: Context?, layoutResource: Int) : RelativeLayout(context), IMarker { +open class MarkerView(context: Context, layoutResource: Int) : RelativeLayout(context), IMarker { private var mOffset: MPPointF = MPPointF() private val mOffset2 = MPPointF() private var mWeakChart: WeakReference?>? = null @@ -90,7 +90,7 @@ open class MarkerView(context: Context?, layoutResource: Int) : RelativeLayout(c return mOffset2 } - override fun refreshContent(e: Entry?, highlight: Highlight?) { + override fun refreshContent(e: Entry, highlight: Highlight) { measure( MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt index 9f47fd7746..e8db4f34b1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt @@ -142,7 +142,6 @@ open class LineDataSet(yVals: MutableList, label: String) : LineRadarData override val cubicIntensity: Float get() = mCubicIntensity - /** * Sets the radius of the drawn circles. * Default radius = 4f, Min = 1f diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt index 239d22de64..d651c07e3b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/ScatterDataSet.kt @@ -64,16 +64,6 @@ open class ScatterDataSet(yVals: MutableList, label: String) : LineScatte shapeRenderer = getRendererForShape(shape) } - /** - * Sets a new IShapeRenderer responsible for drawing this DataSet. - * This can also be used to set a custom IShapeRenderer aside from the default ones. - * - * @param shapeRenderer - */ - fun setShapeRenderer(shapeRenderer: IShapeRenderer?) { - this@ScatterDataSet.shapeRenderer = shapeRenderer - } - companion object { fun getRendererForShape(shape: ScatterShape): IShapeRenderer? { return when (shape) { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt index 2e1e87dc71..343795731c 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.kt @@ -39,9 +39,9 @@ interface ChartInterface { */ val maxHighlightDistance: Float - val width: Int + fun getWidth(): Int - val height: Int + fun getHeight(): Int val centerOfView: MPPointF? diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt index d90e5dfb43..344ab64f78 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.kt @@ -41,8 +41,6 @@ interface IBarDataSet : IBarLineScatterCandleBubbleDataSet { * @return */ val barBorderWidth: Float - @JvmField - set /** * Returns the color drawing borders around the bars. diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt index df46874451..c0a8f04325 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt @@ -26,12 +26,15 @@ open class HorizontalBarChartRenderer( viewPortHandler: ViewPortHandler ) : BarChartRenderer(chart, animator, viewPortHandler) { override fun initBuffers() { + barBuffers.clear() val barData = chart.barData ?: return for (i in 0 until barData.dataSetCount) { val set = barData.getDataSetByIndex(i) - barBuffers[i] = HorizontalBarBuffer( - set.entryCount * 4 * (if (set.isStacked) set.stackSize else 1), - barData.dataSetCount, set.isStacked + barBuffers.add( + HorizontalBarBuffer( + set.entryCount * 4 * (if (set.isStacked) set.stackSize else 1), + barData.dataSetCount, set.isStacked + ) ) } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt index 20252e99f7..8aea1683bd 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt @@ -725,12 +725,14 @@ open class LineChartRenderer( val circleRadius = set.circleRadius val circleHoleRadius = set.circleHoleRadius + circleBitmaps.clear() + for (i in 0.. { - val entries: MutableList = ArrayList() + fun loadEntriesFromAssets(am: AssetManager, path: String): MutableList { + val entries: MutableList = ArrayList() var reader: BufferedReader? = null try { @@ -210,8 +210,8 @@ object FileUtils { } @JvmStatic - fun loadBarEntriesFromAssets(am: AssetManager, path: String): MutableList { - val entries: MutableList = ArrayList() + fun loadBarEntriesFromAssets(am: AssetManager, path: String): MutableList { + val entries: MutableList = ArrayList() var reader: BufferedReader? = null try { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt index 12b3062fdb..461af3d202 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt @@ -59,7 +59,7 @@ class MPPointF : Poolable { } @JvmStatic - fun recycleInstance(instance: MPPointF) { + fun recycleInstance(instance: MPPointF?) { pool.recycle(instance) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt index 2468c133e2..3ee2f5d4f5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt @@ -99,7 +99,9 @@ class ObjectPool> private constructor(withCapacity: Int, `object * @param object An object of type T to recycle */ @Synchronized - fun recycle(`object`: T) { + fun recycle(`object`: T?) { + if (`object` == null) return + if (`object`.currentOwnerId != Poolable.Companion.NO_OWNER) { require(`object`.currentOwnerId != this.poolId) { "The object passed is already stored in this pool!" } throw IllegalArgumentException("The object to recycle already belongs to poolId " + `object`.currentOwnerId + ". Object cannot belong to two different pool instances simultaneously!") diff --git a/app/build.gradle b/app/build.gradle index ae8938734a..90a297924a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,11 +16,11 @@ android { testInstrumentationRunnerArguments useTestStorageService: "true" } compileOptions { - sourceCompatibility JavaVersion.VERSION_17 - targetCompatibility JavaVersion.VERSION_17 + sourceCompatibility JavaVersion.VERSION_21 + targetCompatibility JavaVersion.VERSION_21 } kotlinOptions { - jvmTarget = "17" + jvmTarget = "21" } buildFeatures { viewBinding true diff --git a/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt b/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt index 58032ce3b8..54294e2e53 100644 --- a/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt @@ -1,216 +1,203 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -public class AnotherBarActivity extends DemoBase implements OnSeekBarChangeListener { - - private static final int DEFAULT_VALUE = 10; - private BarChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_barchart); - - setTitle("AnotherBarActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setOnSeekBarChangeListener(this); - - seekBarY = findViewById(R.id.seekBarY); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - - chart.getDescription().setEnabled(false); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase +import androidx.core.net.toUri + +class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { + private var chart: BarChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_barchart) + + setTitle("AnotherBarActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarX?.setOnSeekBarChangeListener(this) + + seekBarY = findViewById(R.id.seekBarY) + seekBarY?.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + + chart?.description?.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart.setMaxVisibleValueCount(60); + chart?.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart?.setPinchZoom(false) - chart.setDrawBarShadow(false); - chart.setDrawGridBackground(false); + chart?.setDrawBarShadow(false) + chart?.setDrawGridBackground(false) - XAxis xAxis = chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setDrawGridLines(false); + val xAxis = chart?.xAxis + xAxis?.position = XAxisPosition.BOTTOM + xAxis?.setDrawGridLines(false) - chart.getAxisLeft().setDrawGridLines(false); + chart?.axisLeft?.setDrawGridLines(false) // setting data - seekBarX.setProgress(DEFAULT_VALUE); - seekBarY.setProgress(100); + seekBarX?.progress = DEFAULT_VALUE + seekBarY?.progress = 100 // add a nice and smooth animation - chart.animateY(1500); + chart?.animateY(1500) - chart.getLegend().setEnabled(false); + chart?.legend?.isEnabled = false } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX?.text = seekBarX?.progress.toString() + tvY?.text = seekBarY?.progress.toString() - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100); + val values = ArrayList() + val sampleValues = getValues(100) - for (int i = 0; i < seekBarX.getProgress(); i++) { - float multi = (seekBarY.getProgress() + 1); - float val = (float) (sampleValues[i].floatValue() * multi) + multi / 3; - values.add(new BarEntry(i, val)); + for (i in 0..<(seekBarX?.progress ?: 0)) { + val multi = ((seekBarY?.progress ?: 0) + 1).toFloat() + val `val` = (sampleValues[i].toFloat() * multi) + multi / 3 + values.add(BarEntry(i.toFloat(), `val`)) } - BarDataSet set1; + val set1: BarDataSet? - if (chart.getData() != null && - chart.getData().getDataSetCount() > 0) { - set1 = (BarDataSet) chart.getData().getDataSetByIndex(0); - set1.setEntries(values); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + if (chart?.data != null && + chart?.data?.let { it.dataSetCount > 0 } == true + ) { + set1 = chart?.data?.getDataSetByIndex(0) as BarDataSet + set1.entries = values + chart?.data?.notifyDataChanged() + chart?.notifyDataSetChanged() } else { - set1 = new BarDataSet(values, "Data Set"); - set1.setColors(ColorTemplate.VORDIPLOM_COLORS); - set1.setDrawValues(false); + set1 = BarDataSet(values, "Data Set") + set1.setColors(*ColorTemplate.VORDIPLOM_COLORS) + set1.isDrawValuesEnabled = false - ArrayList dataSets = new ArrayList<>(); - dataSets.add(set1); + val dataSets = ArrayList() + dataSets.add(set1) - BarData data = new BarData(dataSets); - chart.setData(data); - chart.setFitBars(true); + val data = BarData(dataSets) + chart?.setData(data) + chart?.setFitBars(true) } - chart.invalidate(); + chart?.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bar, menu); - menu.removeItem(R.id.actionToggleIcons); - return true; + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.bar, menu) + menu.removeItem(R.id.actionToggleIcons) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); + R.id.actionToggleValues -> { + chart?.data?.dataSets?.let { + for (set in it) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + } - chart.invalidate(); - break; + chart?.invalidate() } - /* - case R.id.actionToggleIcons: { break; } - */ - case R.id.actionToggleHighlight: { - - if(chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart?.data != null) { + chart?.data?.isHighlightEnabled = chart?.data?.isHighlightEnabled != true + chart?.invalidate() } - break; } - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) - chart.setPinchZoom(false); - else - chart.setPinchZoom(true); - - chart.invalidate(); - break; + + R.id.actionTogglePinch -> { + if (chart?.isPinchZoomEnabled == true) chart?.setPinchZoom(false) + else chart?.setPinchZoom(true) + + chart?.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart?.isAutoScaleMinMaxEnabled = chart?.isAutoScaleMinMaxEnabled != true + chart?.notifyDataSetChanged() } - case R.id.actionToggleBarBorders: { - for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet)set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); - chart.invalidate(); - break; + R.id.actionToggleBarBorders -> { + chart?.data?.dataSets?.let { + for (set in it) (set as BarDataSet).setBarBorderWidth(if (set.barBorderWidth == 1f) 0f else 1f) + } + + chart?.invalidate() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart?.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart?.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + R.id.animateXY -> { + chart?.animateXY(2000, 2000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "AnotherBarActivity"); + override fun saveToGallery() { + saveToGallery(chart, "AnotherBarActivity") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} + + override fun onStopTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + companion object { + private const val DEFAULT_VALUE = 10 + } } diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt index a6d688a5ff..e0a5341a33 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt @@ -1,335 +1,320 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.RectF; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.Legend.LegendForm; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.Fill; -import com.github.mikephil.charting.utils.MPPointF; - -import info.appdev.chartexample.custom.DayAxisValueFormatter; -import info.appdev.chartexample.custom.MyAxisValueFormatter; -import info.appdev.chartexample.custom.XYMarkerView; -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; - -public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private BarChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_barchart); - - setTitle("BarChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarY = findViewById(R.id.seekBarY); - - seekBarY.setOnSeekBarChangeListener(this); - seekBarX.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); - chart.setRoundedBarRadius(50f); - - chart.setDrawBarShadow(false); - chart.setDrawValueAboveBar(true); - - chart.getDescription().setEnabled(false); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.RectF +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.Legend.LegendForm +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.Fill +import com.github.mikephil.charting.utils.MPPointF.Companion.recycleInstance +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.custom.DayAxisValueFormatter +import info.appdev.chartexample.custom.MyAxisValueFormatter +import info.appdev.chartexample.custom.XYMarkerView +import info.appdev.chartexample.notimportant.DemoBase + +class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: BarChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_barchart) + + setTitle("BarChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarY = findViewById(R.id.seekBarY) + + seekBarY?.setOnSeekBarChangeListener(this) + seekBarX?.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart?.setOnChartValueSelectedListener(this) + chart?.setRoundedBarRadius(50f) + + chart?.setDrawBarShadow(false) + chart?.setDrawValueAboveBar(true) + + chart?.description?.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart.setMaxVisibleValueCount(60); + chart?.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart?.setPinchZoom(false) - chart.setDrawGridBackground(false); - // chart.setDrawYLabels(false); + chart?.setDrawGridBackground(false) - IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart); - - XAxis xAxis = chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setTypeface(tfLight); - xAxis.setDrawGridLines(false); - xAxis.setGranularity(1f); // only intervals of 1 day - xAxis.setLabelCount(7); - xAxis.setValueFormatter(xAxisFormatter); - - IAxisValueFormatter custom = new MyAxisValueFormatter(); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tfLight); - leftAxis.setLabelCount(8, false); - leftAxis.setValueFormatter(custom); - leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART); - leftAxis.setSpaceTop(15f); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setDrawGridLines(false); - rightAxis.setTypeface(tfLight); - rightAxis.setLabelCount(8, false); - rightAxis.setValueFormatter(custom); - rightAxis.setSpaceTop(15f); - rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - l.setForm(LegendForm.SQUARE); - l.setFormSize(9f); - l.setTextSize(11f); - l.setXEntrySpace(4f); - - XYMarkerView mv = new XYMarkerView(this, xAxisFormatter); - mv.setChartView(chart); // For bounds control - chart.setMarker(mv); // Set the marker to the chart + // chart.setDrawYLabels(false); + val xAxisFormatter: IAxisValueFormatter = DayAxisValueFormatter(chart!!) + + val xAxis = chart?.xAxis + xAxis?.position = XAxisPosition.BOTTOM + xAxis?.typeface = tfLight + xAxis?.setDrawGridLines(false) + xAxis?.granularity = 1f // only intervals of 1 day + xAxis?.labelCount = 7 + xAxis?.valueFormatter = xAxisFormatter + + val custom: IAxisValueFormatter = MyAxisValueFormatter() + + val leftAxis = chart?.axisLeft + leftAxis?.typeface = tfLight + leftAxis?.setLabelCount(8, false) + leftAxis?.valueFormatter = custom + leftAxis?.setPosition(YAxisLabelPosition.OUTSIDE_CHART) + leftAxis?.spaceTop = 15f + leftAxis?.axisMinimum = 0f // this replaces setStartAtZero(true) + + val rightAxis = chart?.axisRight + rightAxis?.setDrawGridLines(false) + rightAxis?.typeface = tfLight + rightAxis?.setLabelCount(8, false) + rightAxis?.valueFormatter = custom + rightAxis?.spaceTop = 15f + rightAxis?.axisMinimum = 0f // this replaces setStartAtZero(true) + + val l = chart?.legend + l?.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM + l?.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT + l?.orientation = Legend.LegendOrientation.HORIZONTAL + l?.setDrawInside(false) + l?.form = LegendForm.SQUARE + l?.formSize = 9f + l?.textSize = 11f + l?.xEntrySpace = 4f + + val mv = XYMarkerView(this, xAxisFormatter) + mv.chartView = chart // For bounds control + chart?.setMarker(mv) // Set the marker to the chart // setting data - seekBarY.setProgress(50); - seekBarX.setProgress(12); + seekBarY?.progress = 50 + seekBarX?.progress = 12 // chart.setDrawLegend(false); } - private void setData(int count, float range) { + private fun setData(count: Int, range: Float) { + val start = 1f - float start = 1f; + val values = ArrayList() + val sampleValues = getValues(100) - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100); + var i = start.toInt() + while (i < start + count) { + val `val` = (sampleValues[i].toFloat() * (range + 1)) - for (int i = (int) start; i < start + count; i++) { - float val = (float) (sampleValues[i].floatValue() * (range + 1)); - - if (val * 100 < 25) { - values.add(new BarEntry(i, val, getResources().getDrawable(R.drawable.star))); + if (`val` * 100 < 25) { + values.add(BarEntry(i.toFloat(), `val`, getResources().getDrawable(R.drawable.star))) } else { - values.add(new BarEntry(i, val)); + values.add(BarEntry(i.toFloat(), `val`)) } + i++ } - BarDataSet set1; - - if (chart.getData() != null && - chart.getData().getDataSetCount() > 0) { - set1 = (BarDataSet) chart.getData().getDataSetByIndex(0); - set1.setEntries(values); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + val set1: BarDataSet? + if (chart?.data != null && + chart?.data?.let { it.dataSetCount > 0 } == true + ) { + set1 = chart?.data?.getDataSetByIndex(0) as BarDataSet + set1.entries = values + chart?.data?.notifyDataChanged() + chart?.notifyDataSetChanged() } else { - set1 = new BarDataSet(values, "The year 2017"); - - set1.setDrawIcons(false); - - int startColor1 = ContextCompat.getColor(this, android.R.color.holo_orange_light); - int startColor2 = ContextCompat.getColor(this, android.R.color.holo_blue_light); - int startColor3 = ContextCompat.getColor(this, android.R.color.holo_orange_light); - int startColor4 = ContextCompat.getColor(this, android.R.color.holo_green_light); - int startColor5 = ContextCompat.getColor(this, android.R.color.holo_red_light); - int endColor1 = ContextCompat.getColor(this, android.R.color.holo_blue_dark); - int endColor2 = ContextCompat.getColor(this, android.R.color.holo_purple); - int endColor3 = ContextCompat.getColor(this, android.R.color.holo_green_dark); - int endColor4 = ContextCompat.getColor(this, android.R.color.holo_red_dark); - int endColor5 = ContextCompat.getColor(this, android.R.color.holo_orange_dark); - - List gradientFills = new ArrayList<>(); - gradientFills.add(new Fill(startColor1, endColor1)); - gradientFills.add(new Fill(startColor2, endColor2)); - gradientFills.add(new Fill(startColor3, endColor3)); - gradientFills.add(new Fill(startColor4, endColor4)); - gradientFills.add(new Fill(startColor5, endColor5)); - - set1.setFills(gradientFills); - - ArrayList dataSets = new ArrayList<>(); - dataSets.add(set1); - - BarData data = new BarData(dataSets); - data.setValueTextSize(10f); - data.setValueTypeface(tfLight); - data.setBarWidth(0.9f); - - chart.setData(data); + set1 = BarDataSet(values, "The year 2017") + + set1.isDrawIconsEnabled = false + + val startColor1 = ContextCompat.getColor(this, android.R.color.holo_orange_light) + val startColor2 = ContextCompat.getColor(this, android.R.color.holo_blue_light) + val startColor3 = ContextCompat.getColor(this, android.R.color.holo_orange_light) + val startColor4 = ContextCompat.getColor(this, android.R.color.holo_green_light) + val startColor5 = ContextCompat.getColor(this, android.R.color.holo_red_light) + val endColor1 = ContextCompat.getColor(this, android.R.color.holo_blue_dark) + val endColor2 = ContextCompat.getColor(this, android.R.color.holo_purple) + val endColor3 = ContextCompat.getColor(this, android.R.color.holo_green_dark) + val endColor4 = ContextCompat.getColor(this, android.R.color.holo_red_dark) + val endColor5 = ContextCompat.getColor(this, android.R.color.holo_orange_dark) + + val gradientFills: MutableList = ArrayList() + gradientFills.add(Fill(startColor1, endColor1)) + gradientFills.add(Fill(startColor2, endColor2)) + gradientFills.add(Fill(startColor3, endColor3)) + gradientFills.add(Fill(startColor4, endColor4)) + gradientFills.add(Fill(startColor5, endColor5)) + + set1.setFills(gradientFills) + + val dataSets = ArrayList() + dataSets.add(set1) + + val data = BarData(dataSets) + data.setValueTextSize(10f) + data.setValueTypeface(tfLight) + data.barWidth = 0.9f + + chart?.setData(data) } } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bar, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.bar, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + chart?.data?.dataSets?.let { + for (set in it) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + } + + chart?.invalidate() } - case R.id.actionToggleIcons: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled); - chart.invalidate(); - break; + R.id.actionToggleIcons -> { + chart?.data?.dataSets?.let { + for (set in it) set.isDrawIconsEnabled = !set.isDrawIconsEnabled + } + + chart?.invalidate() } - case R.id.actionToggleHighlight: { - if (chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart?.data != null) { + chart?.data?.isHighlightEnabled = chart?.data?.isHighlightEnabled != true + chart?.invalidate() } - break; } - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) - chart.setPinchZoom(false); - else - chart.setPinchZoom(true); - - chart.invalidate(); - break; + + R.id.actionTogglePinch -> { + if (chart?.isPinchZoomEnabled == true) chart?.setPinchZoom(false) + else chart?.setPinchZoom(true) + + chart?.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart?.isAutoScaleMinMaxEnabled = chart?.isAutoScaleMinMaxEnabled != true + chart?.notifyDataSetChanged() } - case R.id.actionToggleBarBorders: { - for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); - chart.invalidate(); - break; + R.id.actionToggleBarBorders -> { + chart?.data?.dataSets?.let { + for (set in it) (set as BarDataSet).setBarBorderWidth(if (set.barBorderWidth == 1f) 0f else 1f) + } + + chart?.invalidate() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart?.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart?.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart?.animateXY(2000, 2000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } - case R.id.actionRotateXAxisLabelsBy45Deg: { - chart.getXAxis().setLabelRotationAngle(45); - chart.notifyDataSetChanged(); - chart.invalidate(); + + R.id.actionRotateXAxisLabelsBy45Deg -> { + chart?.xAxis?.labelRotationAngle = 45f + chart?.notifyDataSetChanged() + chart?.invalidate() } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX?.text = seekBarX?.progress.toString() + tvY?.text = seekBarY?.progress.toString() - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); - - setData(seekBarX.getProgress(), seekBarY.getProgress()); - chart.invalidate(); + setData(seekBarX?.progress ?: 0, seekBarY?.progress?.toFloat() ?: 0f) + chart?.invalidate() } - @Override - protected void saveToGallery() { - saveToGallery(chart, "BarChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "BarChartActivity") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} - - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - private final RectF onValueSelectedRectF = new RectF(); + override fun onStopTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onValueSelected(Entry e, Highlight h) { + private val onValueSelectedRectF = RectF() - if (e == null) - return; + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null) return - RectF bounds = onValueSelectedRectF; - chart.getBarBounds((BarEntry) e, bounds); - MPPointF position = chart.getPosition(e, AxisDependency.LEFT); + val bounds = onValueSelectedRectF + chart?.getBarBounds(e as BarEntry, bounds) + val position = chart?.getPosition(e, AxisDependency.LEFT) - Log.i("bounds", bounds.toString()); - Log.i("position", position.toString()); + Log.i("bounds", bounds.toString()) + Log.i("position", position.toString()) - Log.i("x-index", - "low: " + chart.getLowestVisibleX() + ", high: " - + chart.getHighestVisibleX()); + Log.i( + "x-index", + ("low: " + chart?.lowestVisibleX + ", high: " + + chart?.highestVisibleX) + ) - MPPointF.recycleInstance(position); + recycleInstance(position) } - @Override - public void onNothingSelected() { } + override fun onNothingSelected() {} } diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt b/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt index be6cd0a74b..286471121c 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt @@ -1,294 +1,274 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; - -import androidx.annotation.NonNull; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.AxisBase; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.formatter.LargeValueFormatter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; - -import info.appdev.chartexample.custom.MyMarkerView; -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.Locale; - -public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private BarChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_barchart); - - setTitle("BarChartActivityMultiDataset"); - - tvX = findViewById(R.id.tvXMax); - tvX.setTextSize(10); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setMax(50); - seekBarX.setOnSeekBarChangeListener(this); - - seekBarY = findViewById(R.id.seekBarY); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); - chart.getDescription().setEnabled(false); - -// chart.setDrawBorders(true); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.AxisBase +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.formatter.LargeValueFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.custom.MyMarkerView +import info.appdev.chartexample.notimportant.DemoBase +import java.util.Locale + +class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: BarChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_barchart) + + setTitle("BarChartActivityMultiDataset") + + tvX = findViewById(R.id.tvXMax) + tvX?.textSize = 10f + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarX?.setMax(50) + seekBarX?.setOnSeekBarChangeListener(this) + + seekBarY = findViewById(R.id.seekBarY) + seekBarY?.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart?.setOnChartValueSelectedListener(this) + chart?.description?.isEnabled = false + + // chart.setDrawBorders(true); // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart?.setPinchZoom(false) - chart.setDrawBarShadow(false); + chart?.setDrawBarShadow(false) - chart.setDrawGridBackground(false); + chart?.setDrawGridBackground(false) // create a custom MarkerView (extend MarkerView) and specify the layout // to use for it - MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view); - mv.setChartView(chart); // For bounds control - chart.setMarker(mv); // Set the marker to the chart - - seekBarX.setProgress(10); - seekBarY.setProgress(100); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(true); - l.setTypeface(tfLight); - l.setYOffset(0f); - l.setXOffset(10f); - l.setYEntrySpace(0f); - l.setTextSize(8f); - - XAxis xAxis = chart.getXAxis(); - xAxis.setTypeface(tfLight); - xAxis.setGranularity(1f); - xAxis.setCenterAxisLabels(true); - xAxis.setValueFormatter(new IAxisValueFormatter() { - @NonNull - @Override - public String getFormattedValue(float value, AxisBase axis) { - return String.valueOf((int) value); + val mv = MyMarkerView(this, R.layout.custom_marker_view) + mv.chartView = chart // For bounds control + chart?.setMarker(mv) // Set the marker to the chart + + seekBarX?.progress = 10 + seekBarY?.progress = 100 + + val l = chart?.legend + l?.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l?.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l?.orientation = Legend.LegendOrientation.VERTICAL + l?.setDrawInside(true) + l?.typeface = tfLight + l?.yOffset = 0f + l?.xOffset = 10f + l?.yEntrySpace = 0f + l?.textSize = 8f + + val xAxis = chart?.xAxis + xAxis?.typeface = tfLight + xAxis?.granularity = 1f + xAxis?.setCenterAxisLabels(true) + xAxis?.valueFormatter = object : IAxisValueFormatter { + override fun getFormattedValue(value: Float, axis: AxisBase?): String { + return value.toInt().toString() } - }); + } - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tfLight); - leftAxis.setValueFormatter(new LargeValueFormatter()); - leftAxis.setDrawGridLines(false); - leftAxis.setSpaceTop(35f); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) + val leftAxis = chart?.axisLeft + leftAxis?.typeface = tfLight + leftAxis?.valueFormatter = LargeValueFormatter() + leftAxis?.setDrawGridLines(false) + leftAxis?.spaceTop = 35f + leftAxis?.axisMinimum = 0f // this replaces setStartAtZero(true) - chart.getAxisRight().setEnabled(false); + chart?.axisRight?.isEnabled = false } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + val groupSpace = 0.08f + val barSpace = 0.03f // x4 DataSet + val barWidth = 0.2f // x4 DataSet - float groupSpace = 0.08f; - float barSpace = 0.03f; // x4 DataSet - float barWidth = 0.2f; // x4 DataSet // (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group" - - int groupCount = seekBarX.getProgress() + 1; - int startYear = 1980; - int endYear = startYear + groupCount; - - tvX.setText(String.format(Locale.ENGLISH, "%d-%d", startYear, endYear)); - tvY.setText(String.valueOf(seekBarY.getProgress())); - - ArrayList values1 = new ArrayList<>(); - ArrayList values2 = new ArrayList<>(); - ArrayList values3 = new ArrayList<>(); - ArrayList values4 = new ArrayList<>(); - - float randomMultiplier = seekBarY.getProgress() * 100000f; - Double[] sampleValues = DataTools.Companion.getValues(100 + 2); - - for (int i = startYear; i < endYear; i++) { - values1.add(new BarEntry(i, (float) (sampleValues[i - startYear].floatValue() * randomMultiplier))); - values2.add(new BarEntry(i, (float) (sampleValues[i - startYear + 1].floatValue() * randomMultiplier))); - values3.add(new BarEntry(i, (float) (sampleValues[i - startYear + 2].floatValue() * randomMultiplier))); - values4.add(new BarEntry(i, (float) (sampleValues[i - startYear].floatValue() * randomMultiplier))); + val groupCount = (seekBarX?.progress ?: 0) + 1 + val startYear = 1980 + val endYear = startYear + groupCount + + tvX?.text = String.format(Locale.ENGLISH, "%d-%d", startYear, endYear) + tvY?.text = seekBarY?.progress.toString() + + val values1 = ArrayList() + val values2 = ArrayList() + val values3 = ArrayList() + val values4 = ArrayList() + + val randomMultiplier = (seekBarY?.progress ?: 0) * 100000f + val sampleValues = getValues(100 + 2) + + for (i in startYear.. 0) { - - set1 = (BarDataSet) chart.getData().getDataSetByIndex(0); - set2 = (BarDataSet) chart.getData().getDataSetByIndex(1); - set3 = (BarDataSet) chart.getData().getDataSetByIndex(2); - set4 = (BarDataSet) chart.getData().getDataSetByIndex(3); - set1.setEntries(values1); - set2.setEntries(values2); - set3.setEntries(values3); - set4.setEntries(values4); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); - + val set1: BarDataSet? + val set2: BarDataSet? + val set3: BarDataSet? + val set4: BarDataSet? + + if (chart?.data != null && chart?.data?.let { it.dataSetCount > 0 } == true) { + set1 = chart?.data?.getDataSetByIndex(0) as BarDataSet + set2 = chart?.data?.getDataSetByIndex(1) as BarDataSet + set3 = chart?.data?.getDataSetByIndex(2) as BarDataSet + set4 = chart?.data?.getDataSetByIndex(3) as BarDataSet + set1.entries = values1 + set2.entries = values2 + set3.entries = values3 + set4.entries = values4 + chart?.data?.notifyDataChanged() + chart?.notifyDataSetChanged() } else { // create 4 DataSets - set1 = new BarDataSet(values1, "Company A"); - set1.setColor(Color.rgb(104, 241, 175)); - set2 = new BarDataSet(values2, "Company B"); - set2.setColor(Color.rgb(164, 228, 251)); - set3 = new BarDataSet(values3, "Company C"); - set3.setColor(Color.rgb(242, 247, 158)); - set4 = new BarDataSet(values4, "Company D"); - set4.setColor(Color.rgb(255, 102, 0)); - - BarData data = new BarData(set1, set2, set3, set4); - data.setValueFormatter(new LargeValueFormatter()); - data.setValueTypeface(tfLight); - - chart.setData(data); + set1 = BarDataSet(values1, "Company A") + set1.setColor(Color.rgb(104, 241, 175)) + set2 = BarDataSet(values2, "Company B") + set2.setColor(Color.rgb(164, 228, 251)) + set3 = BarDataSet(values3, "Company C") + set3.setColor(Color.rgb(242, 247, 158)) + set4 = BarDataSet(values4, "Company D") + set4.setColor(Color.rgb(255, 102, 0)) + + val data = BarData(set1, set2, set3, set4) + data.setValueFormatter(LargeValueFormatter()) + data.setValueTypeface(tfLight) + + chart?.setData(data) } // specify the width each bar should have - chart.getBarData().setBarWidth(barWidth); + chart?.barData?.barWidth = barWidth // restrict the x-axis range - chart.getXAxis().setAxisMinimum(startYear); + chart?.xAxis?.axisMinimum = startYear.toFloat() // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters - chart.getXAxis().setAxisMaximum(startYear + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount); - chart.groupBars(startYear, groupSpace, barSpace); - chart.invalidate(); + chart?.xAxis?.axisMaximum = startYear + ((chart?.barData?.getGroupWidth(groupSpace, barSpace) ?: 0f) * groupCount) + chart?.groupBars(startYear.toFloat(), groupSpace, barSpace) + chart?.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bar, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.bar, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IBarDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + chart?.data?.dataSets?.let { + for (set in it) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + } + + chart?.invalidate() } - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) - chart.setPinchZoom(false); - else - chart.setPinchZoom(true); - - chart.invalidate(); - break; + + R.id.actionTogglePinch -> { + if (chart?.isPinchZoomEnabled == true) chart?.setPinchZoom(false) + else chart?.setPinchZoom(true) + + chart?.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart?.isAutoScaleMinMaxEnabled = chart?.isAutoScaleMinMaxEnabled != true + chart?.notifyDataSetChanged() } - case R.id.actionToggleBarBorders: { - for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); - chart.invalidate(); - break; + R.id.actionToggleBarBorders -> { + chart?.data?.dataSets?.let { + for (set in it) (set as BarDataSet).setBarBorderWidth(if (set.barBorderWidth == 1f) 0f else 1f) + } + + chart?.invalidate() } - case R.id.actionToggleHighlight: { - if (chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart?.data != null) { + chart?.data?.isHighlightEnabled = chart?.data?.isHighlightEnabled != true + chart?.invalidate() } - break; } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart?.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart?.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart?.animateXY(2000, 2000) } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "BarChartActivityMultiDataset"); + override fun saveToGallery() { + saveToGallery(chart, "BarChartActivityMultiDataset") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onValueSelected(Entry e, Highlight h) { - Log.i("Activity", "Selected: " + e.toString() + ", dataSet: " + h.getDataSetIndex()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + Log.i("Activity", "Selected: " + e.toString() + ", dataSet: " + h?.dataSetIndex) } - @Override - public void onNothingSelected() { - Log.i("Activity", "Nothing selected."); + override fun onNothingSelected() { + Log.i("Activity", "Nothing selected.") } } diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt b/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt index 6e50e8e709..d1f66338d1 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt @@ -1,240 +1,215 @@ +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.Legend.LegendForm +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.utils.FileUtils.loadBarEntriesFromAssets +import info.appdev.chartexample.notimportant.DemoBase + +class BarChartActivitySinus : DemoBase(), OnSeekBarChangeListener { + private var chart: BarChart? = null + private var seekBarX: SeekBar? = null + private var tvX: TextView? = null + + private val data: MutableList by lazy { + loadBarEntriesFromAssets(assets, "othersine.txt") + } -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.Legend.LegendForm; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.utils.FileUtils; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; - -public class BarChartActivitySinus extends DemoBase implements OnSeekBarChangeListener { - - private BarChart chart; - private SeekBar seekBarX; - private TextView tvX; - - private List data; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_barchart_sinus); - - setTitle("BarChartActivitySinus"); + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_barchart_sinus) - data = FileUtils.loadBarEntriesFromAssets(getAssets(), "othersine.txt"); + setTitle("BarChartActivitySinus") - tvX = findViewById(R.id.tvValueCount); + tvX = findViewById(R.id.tvValueCount) - seekBarX = findViewById(R.id.seekbarValues); + seekBarX = findViewById(R.id.seekbarValues) - chart = findViewById(R.id.chart1); + chart = findViewById(R.id.chart1) - chart.setDrawBarShadow(false); - chart.setDrawValueAboveBar(true); + chart!!.setDrawBarShadow(false) + chart!!.setDrawValueAboveBar(true) - chart.getDescription().setEnabled(false); + chart!!.description.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart.setMaxVisibleValueCount(60); + chart!!.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) // draw shadows for each bar that show the maximum value // chart.setDrawBarShadow(true); // chart.setDrawXLabels(false); + chart!!.setDrawGridBackground(false) - chart.setDrawGridBackground(false); // chart.setDrawYLabels(false); - - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(false); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tfLight); - leftAxis.setLabelCount(6, false); - leftAxis.setAxisMinimum(-2.5f); - leftAxis.setAxisMaximum(2.5f); - leftAxis.setGranularityEnabled(true); - leftAxis.setGranularity(0.1f); - - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setDrawGridLines(false); - rightAxis.setTypeface(tfLight); - rightAxis.setLabelCount(6, false); - rightAxis.setAxisMinimum(-2.5f); - rightAxis.setAxisMaximum(2.5f); - rightAxis.setGranularity(0.1f); - - seekBarX.setOnSeekBarChangeListener(this); - seekBarX.setProgress(150); // set data - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - l.setForm(LegendForm.SQUARE); - l.setFormSize(9f); - l.setTextSize(11f); - l.setXEntrySpace(4f); - - chart.animateXY(1500, 1500); + val xAxis = chart!!.xAxis + xAxis.isEnabled = false + + val leftAxis = chart!!.axisLeft + leftAxis.typeface = tfLight + leftAxis.setLabelCount(6, false) + leftAxis.axisMinimum = -2.5f + leftAxis.axisMaximum = 2.5f + leftAxis.isGranularityEnabled = true + leftAxis.granularity = 0.1f + + val rightAxis = chart!!.axisRight + rightAxis.setDrawGridLines(false) + rightAxis.typeface = tfLight + rightAxis.setLabelCount(6, false) + rightAxis.axisMinimum = -2.5f + rightAxis.axisMaximum = 2.5f + rightAxis.granularity = 0.1f + + seekBarX!!.setOnSeekBarChangeListener(this) + seekBarX!!.progress = 150 // set data + + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM + l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + l.form = LegendForm.SQUARE + l.formSize = 9f + l.textSize = 11f + l.xEntrySpace = 4f + + chart!!.animateXY(1500, 1500) } - private void setData(int count) { - - ArrayList entries = new ArrayList<>(); + private fun setData(count: Int) { + val entries = ArrayList() - for (int i = 0; i < count; i++) { - entries.add(data.get(i)); + for (i in 0.. 0) { - set = (BarDataSet) chart.getData().getDataSetByIndex(0); - set.setEntries(entries); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + if (chart!!.data != null && + chart!!.data!!.dataSetCount > 0 + ) { + set = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set.entries = entries + chart!!.data!!.notifyDataChanged() + chart!!.notifyDataSetChanged() } else { - set = new BarDataSet(entries, "Sinus Function"); - set.setColor(Color.BLUE); + set = BarDataSet(entries, "Sinus Function") + set.setColor(Color.BLUE) } - BarData data = new BarData(set); - data.setValueTextSize(10f); - data.setValueTypeface(tfLight); - data.setDrawValues(false); - data.setBarWidth(0.8f); + val data = BarData(set) + data.setValueTextSize(10f) + data.setValueTypeface(tfLight) + data.setDrawValues(false) + data.barWidth = 0.8f - chart.setData(data); + chart!!.setData(data) } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bar, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.bar, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivitySinus.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartActivitySinus.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IBarDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + for (set in chart!!.data!!.dataSets) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if (chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) - chart.setPinchZoom(false); - else - chart.setPinchZoom(true); - - chart.invalidate(); - break; + + R.id.actionTogglePinch -> { + if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) + else chart!!.setPinchZoom(true) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.actionToggleBarBorders: { - for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); - chart.invalidate(); - break; + R.id.actionToggleBarBorders -> { + for (set in chart!!.data!!.dataSets) (set as BarDataSet).setBarBorderWidth(if (set.barBorderWidth == 1f) 0f else 1f) + + chart!!.invalidate() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() - setData(seekBarX.getProgress()); - chart.invalidate(); + setData(seekBarX!!.progress) + chart!!.invalidate() } - @Override - protected void saveToGallery() { - saveToGallery(chart, "BarChartActivitySinus"); + override fun saveToGallery() { + saveToGallery(chart, "BarChartActivitySinus") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} - - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt b/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt index 65072ece0e..0924d6e7b0 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt @@ -1,201 +1,169 @@ - -package info.appdev.chartexample; - -import android.content.Intent; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.AxisBase; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.formatter.IValueFormatter; -import com.github.mikephil.charting.utils.ViewPortHandler; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.text.DecimalFormat; -import java.util.ArrayList; -import java.util.List; - -public class BarChartPositiveNegative extends DemoBase { - - private BarChart chart; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_barchart_noseekbar); - - setTitle("BarChartPositiveNegative"); - - chart = findViewById(R.id.chart1); - chart.setBackgroundColor(Color.WHITE); - chart.setExtraTopOffset(-30f); - chart.setExtraBottomOffset(10f); - chart.setExtraLeftOffset(70f); - chart.setExtraRightOffset(70f); - - chart.setDrawBarShadow(false); - chart.setDrawValueAboveBar(true); - - chart.getDescription().setEnabled(false); +package info.appdev.chartexample + +import android.content.Intent +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.AxisBase +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.formatter.IValueFormatter +import com.github.mikephil.charting.utils.ViewPortHandler +import info.appdev.chartexample.notimportant.DemoBase +import java.text.DecimalFormat +import kotlin.math.max +import kotlin.math.min + +class BarChartPositiveNegative : DemoBase() { + private var chart: BarChart? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_barchart_noseekbar) + + setTitle("BarChartPositiveNegative") + + chart = findViewById(R.id.chart1) + chart!!.setBackgroundColor(Color.WHITE) + chart!!.extraTopOffset = -30f + chart!!.extraBottomOffset = 10f + chart!!.extraLeftOffset = 70f + chart!!.extraRightOffset = 70f + + chart!!.setDrawBarShadow(false) + chart!!.setDrawValueAboveBar(true) + + chart!!.description.isEnabled = false // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); - - chart.setDrawGridBackground(false); - - XAxis xAxis = chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setTypeface(tfRegular); - xAxis.setDrawGridLines(false); - xAxis.setDrawAxisLine(false); - xAxis.setTextColor(Color.LTGRAY); - xAxis.setTextSize(13f); - xAxis.setLabelCount(5); - xAxis.setCenterAxisLabels(true); - xAxis.setGranularity(1f); - - YAxis left = chart.getAxisLeft(); - left.setDrawLabels(false); - left.setSpaceTop(25f); - left.setSpaceBottom(25f); - left.setDrawAxisLine(false); - left.setDrawGridLines(false); - left.setDrawZeroLine(true); // draw a zero line - left.setZeroLineColor(Color.GRAY); - left.setZeroLineWidth(0.7f); - chart.getAxisRight().setEnabled(false); - chart.getLegend().setEnabled(false); + chart!!.setPinchZoom(false) + + chart!!.setDrawGridBackground(false) + + val xAxis = chart!!.xAxis + xAxis.position = XAxisPosition.BOTTOM + xAxis.typeface = tfRegular + xAxis.setDrawGridLines(false) + xAxis.setDrawAxisLine(false) + xAxis.textColor = Color.LTGRAY + xAxis.textSize = 13f + xAxis.labelCount = 5 + xAxis.setCenterAxisLabels(true) + xAxis.granularity = 1f + + val left = chart!!.axisLeft + left.setDrawLabels(false) + left.spaceTop = 25f + left.spaceBottom = 25f + left.setDrawAxisLine(false) + left.setDrawGridLines(false) + left.setDrawZeroLine(true) // draw a zero line + left.zeroLineColor = Color.GRAY + left.zeroLineWidth = 0.7f + chart!!.axisRight.isEnabled = false + chart!!.legend.isEnabled = false // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT - final List data = new ArrayList<>(); - data.add(new Data(0f, -224.1f, "12-29")); - data.add(new Data(1f, 238.5f, "12-30")); - data.add(new Data(2f, 1280.1f, "12-31")); - data.add(new Data(3f, -442.3f, "01-01")); - data.add(new Data(4f, -2280.1f, "01-02")); - - xAxis.setValueFormatter(new IAxisValueFormatter() { - @Override - public String getFormattedValue(float value, AxisBase axis) { - return data.get(Math.min(Math.max((int) value, 0), data.size()-1)).xAxisValue; + val data: MutableList = ArrayList() + data.add(Data(0f, -224.1f, "12-29")) + data.add(Data(1f, 238.5f, "12-30")) + data.add(Data(2f, 1280.1f, "12-31")) + data.add(Data(3f, -442.3f, "01-01")) + data.add(Data(4f, -2280.1f, "01-02")) + + xAxis.valueFormatter = object : IAxisValueFormatter { + override fun getFormattedValue(value: Float, axis: AxisBase?): String { + return data[min(max(value.toInt(), 0), data.size - 1)].xAxisValue } - }); + } - setData(data); + setData(data) } - private void setData(List dataList) { - - ArrayList values = new ArrayList<>(); - List colors = new ArrayList<>(); - - int green = Color.rgb(110, 190, 102); - int red = Color.rgb(211, 74, 88); + private fun setData(dataList: MutableList) { + val values = ArrayList() + val colors: MutableList = ArrayList() - for (int i = 0; i < dataList.size(); i++) { + val green = Color.rgb(110, 190, 102) + val red = Color.rgb(211, 74, 88) - Data d = dataList.get(i); - BarEntry entry = new BarEntry(d.xValue, d.yValue); - values.add(entry); + for (i in dataList.indices) { + val d = dataList[i] + val entry = BarEntry(d.xValue, d.yValue) + values.add(entry) // specific colors - if (d.yValue >= 0) - colors.add(red); - else - colors.add(green); + if (d.yValue >= 0) colors.add(red) + else colors.add(green) } - BarDataSet set; + val set: BarDataSet? - if (chart.getData() != null && - chart.getData().getDataSetCount() > 0) { - set = (BarDataSet) chart.getData().getDataSetByIndex(0); - set.setEntries(values); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + if (chart!!.data != null && + chart!!.data!!.dataSetCount > 0 + ) { + set = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set.entries = values + chart!!.data!!.notifyDataChanged() + chart!!.notifyDataSetChanged() } else { - set = new BarDataSet(values, "Values"); - set.setColors(colors); - set.setValueTextColors(colors); - - BarData data = new BarData(set); - data.setValueTextSize(13f); - data.setValueTypeface(tfRegular); - data.setValueFormatter(new ValueFormatter()); - data.setBarWidth(0.8f); - - chart.setData(data); - chart.invalidate(); + set = BarDataSet(values, "Values") + set.setColors(colors) + set.setValueTextColors(colors) + + val data = BarData(set) + data.setValueTextSize(13f) + data.setValueTypeface(tfRegular) + data.setValueFormatter(ValueFormatter()) + data.barWidth = 0.8f + + chart!!.setData(data) + chart!!.invalidate() } } /** * Demo class representing data. */ - private class Data { - - final String xAxisValue; - final float yValue; - final float xValue; - - Data(float xValue, float yValue, String xAxisValue) { - this.xAxisValue = xAxisValue; - this.yValue = yValue; - this.xValue = xValue; - } - } - - private class ValueFormatter implements IValueFormatter - { - - private final DecimalFormat mFormat; + private data class Data(val xValue: Float, val yValue: Float, val xAxisValue: String) - ValueFormatter() { - mFormat = new DecimalFormat("######.0"); - } + private class ValueFormatter : IValueFormatter { + private val mFormat = DecimalFormat("######.0") - @Override - public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { - return mFormat.format(value); + override fun getFormattedValue(value: Float, entry: Entry?, dataSetIndex: Int, viewPortHandler: ViewPortHandler?): String { + return mFormat.format(value.toDouble()) } } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.only_github, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.only_github, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartPositiveNegative.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BarChartPositiveNegative.java".toUri()) + startActivity(i) } } - return true; + return true } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } } diff --git a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt index 013279e541..975e2594ed 100644 --- a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt @@ -1,250 +1,245 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.BubbleChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BubbleData; -import com.github.mikephil.charting.data.BubbleDataSet; -import com.github.mikephil.charting.data.BubbleEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.MPPointF; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -public class BubbleChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private BubbleChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_bubblechart); - - setTitle("BubbleChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setOnSeekBarChangeListener(this); - - seekBarY = findViewById(R.id.seekBarY); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.getDescription().setEnabled(false); - - chart.setOnChartValueSelectedListener(this); - - chart.setDrawGridBackground(false); - - chart.setTouchEnabled(true); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.BubbleChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BubbleData +import com.github.mikephil.charting.data.BubbleDataSet +import com.github.mikephil.charting.data.BubbleEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.MPPointF +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: BubbleChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_bubblechart) + + setTitle("BubbleChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarX!!.setOnSeekBarChangeListener(this) + + seekBarY = findViewById(R.id.seekBarY) + seekBarY!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.description.isEnabled = false + + chart!!.setOnChartValueSelectedListener(this) + + chart!!.setDrawGridBackground(false) + + chart!!.setTouchEnabled(true) // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); + chart!!.isDragEnabled = true + chart!!.setScaleEnabled(true) - chart.setMaxVisibleValueCount(200); - chart.setPinchZoom(true); + chart!!.setMaxVisibleValueCount(200) + chart!!.setPinchZoom(true) - seekBarX.setProgress(10); - seekBarY.setProgress(50); + seekBarX!!.progress = 10 + seekBarY!!.progress = 50 - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - l.setTypeface(tfLight); + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) + l.typeface = tfLight - YAxis yl = chart.getAxisLeft(); - yl.setTypeface(tfLight); - yl.setSpaceTop(30f); - yl.setSpaceBottom(30f); - yl.setDrawZeroLine(false); + val yl = chart!!.axisLeft + yl.typeface = tfLight + yl.spaceTop = 30f + yl.spaceBottom = 30f + yl.setDrawZeroLine(false) - chart.getAxisRight().setEnabled(false); + chart!!.axisRight.isEnabled = false - XAxis xl = chart.getXAxis(); - xl.setPosition(XAxis.XAxisPosition.BOTTOM); - xl.setTypeface(tfLight); + val xl = chart!!.xAxis + xl.position = XAxisPosition.BOTTOM + xl.typeface = tfLight } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - int count = seekBarX.getProgress(); - int range = seekBarY.getProgress(); - - tvX.setText(String.valueOf(count)); - tvY.setText(String.valueOf(range)); - - ArrayList values1 = new ArrayList<>(); - ArrayList values2 = new ArrayList<>(); - ArrayList values3 = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100); - - for (int i = 0; i < count; i++) { - values1.add(new BubbleEntry(i, (float) (sampleValues[i+1] * range), (float) (sampleValues[i].floatValue() * range), getResources().getDrawable(R.drawable.star))); - values2.add(new BubbleEntry(i, (float) (sampleValues[i+2] * range), (float) (sampleValues[i+1].floatValue() * range), getResources().getDrawable(R.drawable.star))); - values3.add(new BubbleEntry(i, (float) (sampleValues[i] * range), (float) (sampleValues[i+2].floatValue() * range))); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + val count = seekBarX!!.progress + val range = seekBarY!!.progress + + tvX!!.text = count.toString() + tvY!!.text = range.toString() + + val values1 = ArrayList() + val values2 = ArrayList() + val values3 = ArrayList() + val sampleValues = getValues(100) + + for (i in 0.. dataSets = new ArrayList<>(); - dataSets.add(set1); // add the data sets - dataSets.add(set2); - dataSets.add(set3); + val set1 = BubbleDataSet(values1, "DS 1") + set1.isDrawIconsEnabled = false + set1.setColor(ColorTemplate.COLORFUL_COLORS[0], 130) + set1.isDrawValuesEnabled = true + + val set2 = BubbleDataSet(values2, "DS 2") + set2.isDrawIconsEnabled = false + set2.iconsOffset = MPPointF(0f, 15f) + set2.setColor(ColorTemplate.COLORFUL_COLORS[1], 130) + set2.isDrawValuesEnabled = true + + val set3 = BubbleDataSet(values3, "DS 3") + set3.setColor(ColorTemplate.COLORFUL_COLORS[2], 130) + set3.isDrawValuesEnabled = true + + val dataSets = ArrayList() + dataSets.add(set1) // add the data sets + dataSets.add(set2) + dataSets.add(set3) // create a data object with the data sets - BubbleData data = new BubbleData(dataSets); - data.setDrawValues(false); - data.setValueTypeface(tfLight); - data.setValueTextSize(8f); - data.setValueTextColor(Color.WHITE); - data.setHighlightCircleWidth(1.5f); - - chart.setData(data); - chart.invalidate(); + val data = BubbleData(dataSets) + data.setDrawValues(false) + data.setValueTypeface(tfLight) + data.setValueTextSize(8f) + data.setValueTextColor(Color.WHITE) + data.setHighlightCircleWidth(1.5f) + + chart!!.setData(data) + chart!!.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bubble, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.bubble, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/BubbleChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + for (set in chart!!.data!!.dataSets) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + + chart!!.invalidate() } - case R.id.actionToggleIcons: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled); - chart.invalidate(); - break; + R.id.actionToggleIcons -> { + for (set in chart!!.data!!.dataSets) set.isDrawIconsEnabled = !set.isDrawIconsEnabled + + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if(chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) - chart.setPinchZoom(false); - else - chart.setPinchZoom(true); - - chart.invalidate(); - break; + + R.id.actionTogglePinch -> { + if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) + else chart!!.setPinchZoom(true) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "BubbleChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "BubbleChartActivity") } - @Override - public void onValueSelected(Entry e, Highlight h) { - Log.i("VAL SELECTED", - "Value: " + e.getY() + ", xIndex: " + e.getX() - + ", DataSet index: " + h.getDataSetIndex()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + Log.i( + "VAL SELECTED", + ("Value: " + e?.y + ", xIndex: " + e?.x + + ", DataSet index: " + h?.dataSetIndex) + ) } - @Override - public void onNothingSelected() {} + override fun onNothingSelected() {} - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt index 01e92f6bd0..a0675d977d 100644 --- a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt @@ -1,240 +1,225 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.graphics.Paint; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.CandleStickChart; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.data.CandleData; -import com.github.mikephil.charting.data.CandleDataSet; -import com.github.mikephil.charting.data.CandleEntry; -import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -public class CandleStickChartActivity extends DemoBase implements OnSeekBarChangeListener { - - private CandleStickChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_candlechart); - - setTitle("CandleStickChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setOnSeekBarChangeListener(this); - - seekBarY = findViewById(R.id.seekBarY); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setBackgroundColor(Color.WHITE); - - chart.getDescription().setEnabled(false); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.graphics.Paint +import android.net.Uri +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import com.github.mikephil.charting.charts.CandleStickChart +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.CandleData +import com.github.mikephil.charting.data.CandleDataSet +import com.github.mikephil.charting.data.CandleEntry +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase +import androidx.core.net.toUri + +class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { + private var chart: CandleStickChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_candlechart) + + setTitle("CandleStickChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarX!!.setOnSeekBarChangeListener(this) + + seekBarY = findViewById(R.id.seekBarY) + seekBarY!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setBackgroundColor(Color.WHITE) + + chart!!.description.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart.setMaxVisibleValueCount(60); + chart!!.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) - chart.setDrawGridBackground(false); + chart!!.setDrawGridBackground(false) - XAxis xAxis = chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setDrawGridLines(false); + val xAxis = chart!!.xAxis + xAxis.position = XAxisPosition.BOTTOM + xAxis.setDrawGridLines(false) - YAxis leftAxis = chart.getAxisLeft(); -// leftAxis.setEnabled(false); - leftAxis.setLabelCount(7, false); - leftAxis.setDrawGridLines(false); - leftAxis.setDrawAxisLine(false); + val leftAxis = chart!!.axisLeft + // leftAxis.setEnabled(false); + leftAxis.setLabelCount(7, false) + leftAxis.setDrawGridLines(false) + leftAxis.setDrawAxisLine(false) - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setEnabled(false); -// rightAxis.setStartAtZero(false); + val rightAxis = chart!!.axisRight + rightAxis.isEnabled = false + + // rightAxis.setStartAtZero(false); // setting data - seekBarX.setProgress(40); - seekBarY.setProgress(100); + seekBarX!!.progress = 40 + seekBarY!!.progress = 100 - chart.getLegend().setEnabled(false); + chart!!.legend.isEnabled = false } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - progress = (seekBarX.getProgress()); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + val progress: Int = (seekBarX!!.progress) - tvX.setText(String.valueOf(progress)); - tvY.setText(String.valueOf(seekBarY.getProgress())); + tvX!!.text = progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - chart.resetTracking(); + chart!!.resetTracking() - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100); + val values = ArrayList() + val sampleValues = getValues(100) - for (int i = 0; i < progress; i++) { - float multi = (seekBarY.getProgress() + 1); - float val = (float) (sampleValues[i].floatValue() * 40) + multi; + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/CandleStickChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + for (set in chart!!.data!!.dataSets) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + + chart!!.invalidate() } - case R.id.actionToggleIcons: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled); - chart.invalidate(); - break; + R.id.actionToggleIcons -> { + for (set in chart!!.data!!.dataSets) set.isDrawIconsEnabled = !set.isDrawIconsEnabled + + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if(chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) - chart.setPinchZoom(false); - else - chart.setPinchZoom(true); - - chart.invalidate(); - break; + + R.id.actionTogglePinch -> { + if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) + else chart!!.setPinchZoom(true) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.actionToggleMakeShadowSameColorAsCandle: { - for (ICandleDataSet set : chart.getData().getDataSets()) { - ((CandleDataSet) set).setShadowColorSameAsCandle(!set.shadowColorSameAsCandle); + + R.id.actionToggleMakeShadowSameColorAsCandle -> { + for (set in chart!!.data!!.dataSets) { + (set as CandleDataSet).setShadowColorSameAsCandle(!set.shadowColorSameAsCandle) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "CandleStickChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "CandleStickChartActivity") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt b/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt index 9e2e4b91a1..54c1168e0e 100644 --- a/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt @@ -1,284 +1,274 @@ - -package info.appdev.chartexample; - -import android.content.Intent; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; - -import com.github.mikephil.charting.charts.CombinedChart; -import com.github.mikephil.charting.charts.CombinedChart.DrawOrder; -import com.github.mikephil.charting.components.AxisBase; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.BubbleData; -import com.github.mikephil.charting.data.BubbleDataSet; -import com.github.mikephil.charting.data.BubbleEntry; -import com.github.mikephil.charting.data.CandleData; -import com.github.mikephil.charting.data.CandleDataSet; -import com.github.mikephil.charting.data.CandleEntry; -import com.github.mikephil.charting.data.CombinedData; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.data.ScatterData; -import com.github.mikephil.charting.data.ScatterDataSet; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -public class CombinedChartActivity extends DemoBase { - - private CombinedChart chart; - private final int sampleCount = 12; - Double[] values = DataTools.Companion.getValues(sampleCount * 2); - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_combined); - - setTitle("CombinedChartActivity"); - - chart = findViewById(R.id.chart1); - chart.getDescription().setEnabled(false); - chart.setBackgroundColor(Color.WHITE); - chart.setDrawGridBackground(false); - chart.setDrawBarShadow(false); - chart.setHighlightFullBarEnabled(false); +package info.appdev.chartexample + +import android.content.Intent +import android.graphics.Color +import android.net.Uri +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import com.github.mikephil.charting.charts.CombinedChart +import com.github.mikephil.charting.charts.CombinedChart.DrawOrder +import com.github.mikephil.charting.components.AxisBase +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.BubbleData +import com.github.mikephil.charting.data.BubbleDataSet +import com.github.mikephil.charting.data.BubbleEntry +import com.github.mikephil.charting.data.CandleData +import com.github.mikephil.charting.data.CandleDataSet +import com.github.mikephil.charting.data.CandleEntry +import com.github.mikephil.charting.data.CombinedData +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.data.ScatterData +import com.github.mikephil.charting.data.ScatterDataSet +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase +import kotlin.math.roundToInt +import androidx.core.net.toUri + +class CombinedChartActivity : DemoBase() { + private var chart: CombinedChart? = null + private val sampleCount = 12 + var values: Array = getValues(sampleCount * 2) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_combined) + + setTitle("CombinedChartActivity") + + chart = findViewById(R.id.chart1) + chart!!.description.isEnabled = false + chart!!.setBackgroundColor(Color.WHITE) + chart!!.setDrawGridBackground(false) + chart!!.setDrawBarShadow(false) + chart!!.setHighlightFullBarEnabled(false) // draw bars behind lines - chart.setDrawOrder(new DrawOrder[]{ - DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.CANDLE, DrawOrder.LINE, DrawOrder.SCATTER - }); - - Legend l = chart.getLegend(); - l.setWordWrapEnabled(true); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setDrawGridLines(false); - rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setDrawGridLines(false); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - XAxis xAxis = chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTH_SIDED); - xAxis.setAxisMinimum(0f); - xAxis.setGranularity(1f); - xAxis.setValueFormatter(new IAxisValueFormatter() { - @Override - public String getFormattedValue(float value, AxisBase axis) { - return DemoBase.Companion.getMonths().get((int) value % DemoBase.Companion.getMonths().size()); + chart!!.drawOrder = arrayOf( + DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.CANDLE, DrawOrder.LINE, DrawOrder.SCATTER + ) + + val l = chart!!.legend + l.isWordWrapEnabled = true + l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM + l.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + + val rightAxis = chart!!.axisRight + rightAxis.setDrawGridLines(false) + rightAxis.axisMinimum = 0f // this replaces setStartAtZero(true) + + val leftAxis = chart!!.axisLeft + leftAxis.setDrawGridLines(false) + leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) + + val xAxis = chart!!.xAxis + xAxis.position = XAxisPosition.BOTH_SIDED + xAxis.axisMinimum = 0f + xAxis.granularity = 1f + xAxis.valueFormatter = object : IAxisValueFormatter { + override fun getFormattedValue(value: Float, axis: AxisBase?): String { + return months[value.toInt() % months.size] } - }); + } - CombinedData data = new CombinedData(); + val data = CombinedData() - data.setData(generateLineData()); - data.setData(generateBarData()); - data.setData(generateBubbleData()); - data.setData(generateScatterData()); - data.setData(generateCandleData()); - data.setValueTypeface(tfLight); + data.setData(generateLineData()) + data.setData(generateBarData()) + data.setData(generateBubbleData()) + data.setData(generateScatterData()) + data.setData(generateCandleData()) + data.setValueTypeface(tfLight) - xAxis.setAxisMaximum(data.getXMax() + 0.25f); + xAxis.axisMaximum = data.xMax + 0.25f - chart.setData(data); - chart.invalidate(); + chart!!.setData(data) + chart!!.invalidate() } - private LineData generateLineData() { - - LineData d = new LineData(); + private fun generateLineData(): LineData { + val d = LineData() - ArrayList entries = new ArrayList<>(); + val entries = ArrayList() - for (int index = 0; index < sampleCount; index++) - entries.add(new Entry(index + 0.5f, values[index].floatValue() * 15 + 5)); + for (index in 0..() + val entries2 = ArrayList() - ArrayList entries1 = new ArrayList<>(); - ArrayList entries2 = new ArrayList<>(); - - for (int index = 0; index < sampleCount; index++) { - entries1.add(new BarEntry(0, values[index].floatValue() * 25 + 25)); + for (index in 0.. interval per "group" + val set1 = BarDataSet(entries1, "Bar 1") + set1.setColor(Color.rgb(60, 220, 78)) + set1.valueTextColor = Color.rgb(60, 220, 78) + set1.valueTextSize = 10f + set1.axisDependency = AxisDependency.LEFT - BarData d = new BarData(set1, set2); - d.setBarWidth(barWidth); + val set2 = BarDataSet(entries2, "") + set2.setStackLabels(arrayOf("Stack 1", "Stack 2")) + set2.setColors(Color.rgb(61, 165, 255), Color.rgb(23, 197, 255)) + set2.valueTextColor = Color.rgb(61, 165, 255) + set2.valueTextSize = 10f + set2.axisDependency = AxisDependency.LEFT + + val groupSpace = 0.06f + val barSpace = 0.02f // x2 dataset + val barWidth = 0.45f // x2 dataset + + // (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group" + val d = BarData(set1, set2) + d.barWidth = barWidth // make this BarData object grouped - d.groupBars(0, groupSpace, barSpace); // start at x = 0 + d.groupBars(0f, groupSpace, barSpace) // start at x = 0 - return d; + return d } - private ScatterData generateScatterData() { - - ScatterData d = new ScatterData(); + private fun generateScatterData(): ScatterData { + val d = ScatterData() - ArrayList entries = new ArrayList<>(); + val entries = ArrayList() - for (float index = 0; index < sampleCount; index += 0.5f) - entries.add(new Entry(index + 0.25f, values[Math.round(index*2)].floatValue() * 10 + 55)); + var index = 0f + while (index < sampleCount) { + entries.add(Entry(index + 0.25f, values[(index * 2).roundToInt()].toFloat() * 10 + 55)) + index += 0.5f + } - ScatterDataSet set = new ScatterDataSet(entries, "Scatter DataSet"); - set.setColors(ColorTemplate.MATERIAL_COLORS); - set.setScatterShapeSize(7.5f); - set.setDrawValues(false); - set.setValueTextSize(10f); - d.addDataSet(set); + val set = ScatterDataSet(entries, "Scatter DataSet") + set.setColors(*ColorTemplate.MATERIAL_COLORS) + set.scatterShapeSize = 7.5f + set.isDrawValuesEnabled = false + set.valueTextSize = 10f + d.addDataSet(set) - return d; + return d } - private CandleData generateCandleData() { - - CandleData d = new CandleData(); + private fun generateCandleData(): CandleData { + val d = CandleData() - ArrayList entries = new ArrayList<>(); + val entries = ArrayList() - for (int index = 0; index < sampleCount; index += 2) - entries.add(new CandleEntry(index + 1f, 90, 70, 85, 75f)); + var index = 0 + while (index < sampleCount) { + entries.add(CandleEntry(index + 1f, 90f, 70f, 85f, 75f)) + index += 2 + } - CandleDataSet set = new CandleDataSet(entries, "Candle DataSet"); - set.setDecreasingColor(Color.rgb(142, 150, 175)); - set.setShadowColor(Color.DKGRAY); - set.setBarSpace(0.3f); - set.setValueTextSize(10f); - set.setDrawValues(false); - d.addDataSet(set); + val set = CandleDataSet(entries, "Candle DataSet") + set.setDecreasingColor(Color.rgb(142, 150, 175)) + set.setShadowColor(Color.DKGRAY) + set.setBarSpace(0.3f) + set.valueTextSize = 10f + set.isDrawValuesEnabled = false + d.addDataSet(set) - return d; + return d } - private BubbleData generateBubbleData() { - - BubbleData bd = new BubbleData(); + private fun generateBubbleData(): BubbleData { + val bd = BubbleData() - ArrayList entries = new ArrayList<>(); + val entries = ArrayList() - for (int index = 0; index < sampleCount; index++) { - float y = values[index].floatValue() * 10 + 105; - float size = values[index].floatValue() * 100 + 105; - entries.add(new BubbleEntry(index + 0.5f, y, size)); + for (index in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleLineValues: { - for (IDataSet set : chart.getData().getDataSets()) { - if (set instanceof LineDataSet) - set.setDrawValues(!set.isDrawValuesEnabled); + + R.id.actionToggleLineValues -> { + for (set in chart!!.data!!.dataSets) { + if (set is LineDataSet) set.isDrawValuesEnabled = !set.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleBarValues: { - for (IDataSet set : chart.getData().getDataSets()) { - if (set instanceof BarDataSet) - set.setDrawValues(!set.isDrawValuesEnabled); + + R.id.actionToggleBarValues -> { + for (set in chart!!.data!!.dataSets) { + if (set is BarDataSet) set.isDrawValuesEnabled = !set.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionRemoveDataSet: { - int rnd = (int) values[sampleCount].floatValue() * chart.getData().getDataSetCount(); - chart.getData().removeDataSet(chart.getData().getDataSetByIndex(rnd)); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); - chart.invalidate(); - break; + + R.id.actionRemoveDataSet -> { + val rnd = (values[sampleCount] * chart!!.data!!.dataSetCount).toInt() + chart!!.data!!.removeDataSet(chart!!.data!!.getDataSetByIndex(rnd)) + chart!!.data!!.notifyDataChanged() + chart!!.notifyDataSetChanged() + chart!!.invalidate() } } - return true; + return true } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } } diff --git a/app/src/main/java/info/appdev/chartexample/CubicLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/CubicLineChartActivity.kt index e8b6d6236c..5e8419a654 100644 --- a/app/src/main/java/info/appdev/chartexample/CubicLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/CubicLineChartActivity.kt @@ -5,7 +5,6 @@ import android.annotation.SuppressLint import android.content.Intent import android.content.pm.PackageManager import android.graphics.Color -import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -14,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.github.mikephil.charting.components.YAxis import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineData @@ -50,13 +50,13 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { binding.chart1.setBackgroundColor(Color.rgb(104, 241, 175)) // no description text - binding.chart1.getDescription().isEnabled = false + binding.chart1.description.isEnabled = false // enable touch gestures binding.chart1.setTouchEnabled(true) // enable scaling and dragging - binding.chart1.setDragEnabled(true) + binding.chart1.isDragEnabled = true binding.chart1.setScaleEnabled(true) // if disabled, scaling can be done on x- and y-axis separately @@ -65,10 +65,10 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { binding.chart1.setDrawGridBackground(false) binding.chart1.setMaxHighlightDistance(300f) - val x = binding.chart1.getXAxis() + val x = binding.chart1.xAxis x.isEnabled = false - val y = binding.chart1.getAxisLeft() + val y = binding.chart1.axisLeft y.typeface = tfLight y.setLabelCount(6, false) y.textColor = Color.WHITE @@ -76,7 +76,7 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { y.setDrawGridLines(false) y.axisLineColor = Color.WHITE - binding.chart1.getAxisRight().isEnabled = false + binding.chart1.axisRight.isEnabled = false // add data binding.seekBarY.setOnSeekBarChangeListener(this) @@ -85,10 +85,10 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { // lower max, as cubic runs significantly slower than linear binding.seekBarX.setMax(700) - binding.seekBarX.setProgress(45) - binding.seekBarY.setProgress(100) + binding.seekBarX.progress = 45 + binding.seekBarY.progress = 100 - binding.chart1.getLegend().isEnabled = false + binding.chart1.legend.isEnabled = false binding.chart1.animateXY(2000, 2000) @@ -118,23 +118,23 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { // create a dataset and give it a type set1 = LineDataSet(values, "DataSet 1") - set1.mode = LineDataSet.Mode.CUBIC_BEZIER - set1.cubicIntensity = 0.2f + set1.setMode(LineDataSet.Mode.CUBIC_BEZIER) + set1.setCubicIntensity(0.2f) set1.setDrawFilled(true) set1.setDrawCircles(false) - set1.lineWidth = 1.8f - set1.circleRadius = 4f + set1.setLineWidth(1.8f) + set1.setCircleRadius(4f) set1.setCircleColor(Color.WHITE) - set1.setHighLightColor(Color.rgb(244, 117, 117)) - set1.color = Color.WHITE - set1.fillColor = Color.WHITE - set1.fillAlpha = 100 + set1.highLightColor = Color.rgb(244, 117, 117) + set1.setColor(Color.WHITE) + set1.setFillColor(Color.WHITE) + set1.setFillAlpha(100) set1.setDrawHorizontalHighlightIndicator(false) - set1.fillFormatter = object : IFillFormatter { - override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float { + set1.setFillFormatter(object : IFillFormatter { + override fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float { return binding.chart1.axisLeft.axisMinimum } - } + }) // create a data object with the data sets val data = LineData(set1) @@ -143,7 +143,7 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { data.setDrawValues(false) // set data - binding.chart1.data = data + binding.chart1.setData(data) } } @@ -156,12 +156,12 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { when (item.itemId) { R.id.viewGithub -> { val i = Intent(Intent.ACTION_VIEW) - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java")) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java".toUri()) startActivity(i) } R.id.actionToggleValues -> { - for (set in binding.chart1.data!!.dataSets) set.setDrawValues(!set.isDrawValuesEnabled) + for (set in binding.chart1.data!!.dataSets) set.isDrawValuesEnabled = !set.isDrawValuesEnabled binding.chart1.invalidate() } @@ -174,7 +174,7 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { } R.id.actionToggleFilled -> { - val sets = binding.chart1.data!!.getDataSets() + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet @@ -186,7 +186,7 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { } R.id.actionToggleCircles -> { - val sets = binding.chart1.data!!.getDataSets() + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet @@ -197,40 +197,46 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { } R.id.actionToggleCubic -> { - val sets = binding.chart1.data!!.getDataSets() + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet - set.mode = if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) - LineDataSet.Mode.LINEAR - else - LineDataSet.Mode.CUBIC_BEZIER + set.setMode( + 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!!.getDataSets() + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet - set.mode = if (set.mode == LineDataSet.Mode.STEPPED) - LineDataSet.Mode.LINEAR - else - LineDataSet.Mode.STEPPED + set.setMode( + if (set.mode == LineDataSet.Mode.STEPPED) + LineDataSet.Mode.LINEAR + else + LineDataSet.Mode.STEPPED + ) } binding.chart1.invalidate() } R.id.actionToggleHorizontalCubic -> { - val sets = binding.chart1.data!!.getDataSets() + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet - set.mode = if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) - LineDataSet.Mode.LINEAR - else - LineDataSet.Mode.HORIZONTAL_BEZIER + set.setMode( + if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) + LineDataSet.Mode.LINEAR + else + LineDataSet.Mode.HORIZONTAL_BEZIER + ) } binding.chart1.invalidate() } diff --git a/app/src/main/java/info/appdev/chartexample/DataTools.kt b/app/src/main/java/info/appdev/chartexample/DataTools.kt index fe8a0b27de..9bf18a7316 100644 --- a/app/src/main/java/info/appdev/chartexample/DataTools.kt +++ b/app/src/main/java/info/appdev/chartexample/DataTools.kt @@ -133,7 +133,7 @@ class DataTools { 44.768654, -25.790316, 5.9754066, 99.64748, 141.99321, -17.990795, 38.272446 ) - fun getValues(size: Int) = VAL_102.copyOf(size) + fun getValues(size: Int) = VAL_102.take(size).toTypedArray() fun getMuchValues(size: Int): Array { var result = VAL_102.copyOf(VAL_102.size) @@ -176,25 +176,25 @@ class DataTools { ) { // create a dataset and give it a type val lineDataSet01 = LineDataSet(values, "DataSet 1") - lineDataSet01.setDrawIcons(false) + lineDataSet01.isDrawIconsEnabled = false // draw dashed line lineDataSet01.enableDashedLine(10f, 5f, 0f) // black lines and points - lineDataSet01.color = Color.BLACK + lineDataSet01.setColor(Color.BLACK) lineDataSet01.setCircleColor(Color.BLACK) // line thickness and point size - lineDataSet01.lineWidth = 1f - lineDataSet01.circleRadius = 3f + lineDataSet01.setLineWidth(1f) + lineDataSet01.setCircleRadius(3f) // draw points as solid circles lineDataSet01.setDrawCircleHole(false) // customize legend entry lineDataSet01.formLineWidth = 1f - lineDataSet01.setFormLineDashEffect(DashPathEffect(floatArrayOf(10f, 5f), 0f)) + lineDataSet01.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f) lineDataSet01.formSize = 15f // text size of values @@ -205,19 +205,21 @@ class DataTools { // set the filled area lineDataSet01.setDrawFilled(true) - lineDataSet01.fillFormatter = object : IFillFormatter { - override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float { - return lineChart.axisLeft.axisMinimum + lineDataSet01.setFillFormatter( + object : IFillFormatter { + override fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float { + return lineChart.axisLeft.axisMinimum + } } - } + ) // set color of filled area - if (Utils.getSDKInt() >= 18) { + if (Utils.sDKInt >= 18) { // drawables only supported on api level 18 and above val drawable = ContextCompat.getDrawable(context, R.drawable.fade_blue) - lineDataSet01.fillDrawable = drawable + lineDataSet01.setFillDrawable(drawable) } else { - lineDataSet01.fillColor = Color.BLACK + lineDataSet01.setFillColor(Color.BLACK) } val dataSets = ArrayList() dataSets.add(lineDataSet01) // add the data sets @@ -226,7 +228,7 @@ class DataTools { val data = LineData(dataSets) // set data - lineChart.data = data + lineChart.setData(data) } } } diff --git a/app/src/main/java/info/appdev/chartexample/DrawChartActivity.kt b/app/src/main/java/info/appdev/chartexample/DrawChartActivity.kt index 272514c2cc..a5cf98a906 100644 --- a/app/src/main/java/info/appdev/chartexample/DrawChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/DrawChartActivity.kt @@ -70,13 +70,13 @@ class DrawChartActivity : DemoBase(), OnChartValueSelectedListener, OnDrawListen // create a dataset and give it a type (0) val set1 = LineDataSet(values, "DataSet") - set1.lineWidth = 3f - set1.circleRadius = 5f + set1.setLineWidth(3f) + set1.setCircleRadius(5f) // create a data object with the data sets val data = LineData(set1) - binding.chart1.data = data + binding.chart1.setData(data) } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -91,7 +91,7 @@ class DrawChartActivity : DemoBase(), OnChartValueSelectedListener, OnDrawListen for (iSet in sets) { val set = iSet as LineDataSet - set.setDrawValues(!set.isDrawValuesEnabled) + set.isDrawValuesEnabled = !set.isDrawValuesEnabled } binding.chart1.invalidate() @@ -131,26 +131,26 @@ class DrawChartActivity : DemoBase(), OnChartValueSelectedListener, OnDrawListen saveToGallery(binding.chart1, "DrawChartActivity") } - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i("VAL SELECTED", ("Value: " + e.y + ", xIndex: " + e.x + ", DataSet index: " + h.dataSetIndex)) + override fun onValueSelected(e: Entry?, h: Highlight?) { + Log.i("VAL SELECTED", ("Value: " + e?.y + ", xIndex: " + e?.x + ", DataSet index: " + h?.dataSetIndex)) } override fun onNothingSelected() = Unit /** callback for each new entry drawn with the finger */ - override fun onEntryAdded(entry: Entry) { + override fun onEntryAdded(entry: Entry?) { Log.i(Chart.LOG_TAG, entry.toString()) } /** callback when a DataSet has been drawn (when lifting the finger) */ - override fun onDrawFinished(dataSet: DataSet<*>) { - Log.i(Chart.LOG_TAG, "DataSet drawn. " + dataSet.toSimpleString()) + override fun onDrawFinished(dataSet: DataSet<*>?) { + Log.i(Chart.LOG_TAG, "DataSet drawn. " + dataSet?.toSimpleString()) // prepare the legend again binding.chart1.data?.let { binding.chart1.legendRenderer.computeLegend(it) } } - override fun onEntryMoved(entry: Entry) { + override fun onEntryMoved(entry: Entry?) { Log.i(Chart.LOG_TAG, "Point moved $entry") } } diff --git a/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt b/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt index 7651d974d6..0a7ea0ea57 100644 --- a/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt @@ -1,248 +1,224 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.Toast; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -public class DynamicalAddingActivity extends DemoBase implements OnChartValueSelectedListener { - - private LineChart chart; - Double[] sampleValues = DataTools.Companion.getValues(102); - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_linechart_noseekbar); - - setTitle("DynamicalAddingActivity"); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); - chart.setDrawGridBackground(false); - chart.getDescription().setEnabled(false); - chart.setNoDataText("No chart data available. Use the menu to add entries and data sets!"); - -// chart.getXAxis().setDrawLabels(false); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.Toast +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { + private var chart: LineChart? = null + var sampleValues: Array = getValues(102) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_linechart_noseekbar) + + setTitle("DynamicalAddingActivity") + + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) + chart!!.setDrawGridBackground(false) + chart!!.description.isEnabled = false + chart!!.setNoDataText("No chart data available. Use the menu to add entries and data sets!") + + // chart.getXAxis().setDrawLabels(false); // chart.getXAxis().setDrawGridLines(false); - - chart.invalidate(); + chart!!.invalidate() } - private final int[] colors = ColorTemplate.VORDIPLOM_COLORS; - - private void addEntry() { + private val colors = ColorTemplate.VORDIPLOM_COLORS - LineData data = chart.getData(); + private fun addEntry() { + var data: LineData? = chart!!.data if (data == null) { - data = new LineData(); - chart.setData(data); + data = LineData() + chart!!.setData(data) } - ILineDataSet set = data.getDataSetByIndex(0); - // set.addEntry(...); // can be called as well + var set: LineDataSet - if (set == null) { - set = createSet(); - data.addDataSet(set); + // set.addEntry(...); // can be called as well + if (data.dataSetCount == 0) { + set = createSet() + data.addDataSet(set) } - int lastDataSetIndex = data.getDataSetCount() - 1; // add data only to the last - ILineDataSet lastSet = data.getDataSetByIndex(lastDataSetIndex); + val lastDataSetIndex = data.dataSetCount - 1 // add data only to the last + val lastSet = data.getDataSetByIndex(lastDataSetIndex) - int cycleValue = (int) (lastSet.entryCount % 100.0); + val cycleValue = (lastSet.entryCount % 100.0).toInt() - float value = (sampleValues[cycleValue].floatValue() * 50) + 50f * (lastDataSetIndex + 1); + val value = (sampleValues[cycleValue].toFloat() * 50) + 50f * (lastDataSetIndex + 1) - data.addEntry(new Entry(lastSet.entryCount, value), lastDataSetIndex); - data.notifyDataChanged(); + data.addEntry(Entry(lastSet.entryCount.toFloat(), value), lastDataSetIndex) + data.notifyDataChanged() // let the chart know it's data has changed - chart.notifyDataSetChanged(); + chart!!.notifyDataSetChanged() - chart.setVisibleXRangeMaximum(6); + chart!!.setVisibleXRangeMaximum(6f) //chart.setVisibleYRangeMaximum(15, AxisDependency.LEFT); // // // this automatically refreshes the chart (calls invalidate()) - chart.moveViewTo(data.getEntryCount() - 7, 50f, AxisDependency.LEFT); - + chart!!.moveViewTo((data.entryCount - 7).toFloat(), 50f, AxisDependency.LEFT) } - private void removeLastEntry() { - - LineData data = chart.getData(); + private fun removeLastEntry() { + val data: LineData? = chart!!.data if (data != null) { + val set = data.getDataSetByIndex(0) - ILineDataSet set = data.getDataSetByIndex(0); - - if (set != null) { - - Entry e = set.getEntryForXValue(set.entryCount - 1, Float.NaN); + val e = set.getEntryForXValue(set.entryCount - 1f, Float.Companion.NaN) - data.removeEntry(e, 0); - // or remove by index - // mData.removeEntryByXValue(xIndex, dataSetIndex); - data.notifyDataChanged(); - chart.notifyDataSetChanged(); - chart.invalidate(); - } + data.removeEntry(e, 0) + // or remove by index + // mData.removeEntryByXValue(xIndex, dataSetIndex); + data.notifyDataChanged() + chart!!.notifyDataSetChanged() + chart!!.invalidate() } } - private void addDataSet() { - - LineData data = chart.getData(); + private fun addDataSet() { + val data: LineData? = chart!!.data if (data == null) { - chart.setData(new LineData()); + chart!!.setData(LineData()) } else { - int count = (data.getDataSetCount() + 1); - int amount = data.getDataSetByIndex(0).entryCount; + val count = (data.dataSetCount + 1) + val amount = data.getDataSetByIndex(0).entryCount - ArrayList values = new ArrayList<>(); + val values = ArrayList() - for (int i = 0; i < amount; i++) { - int cycleValue = (int) (i % 100.0); + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java".toUri()) + startActivity(i) } - case R.id.actionAddEntry: { - addEntry(); - Toast.makeText(this, "Entry added!", Toast.LENGTH_SHORT).show(); - break; + + R.id.actionAddEntry -> { + addEntry() + Toast.makeText(this, "Entry added!", Toast.LENGTH_SHORT).show() } - case R.id.actionRemoveEntry: { - removeLastEntry(); - Toast.makeText(this, "Entry removed!", Toast.LENGTH_SHORT).show(); - break; + + R.id.actionRemoveEntry -> { + removeLastEntry() + Toast.makeText(this, "Entry removed!", Toast.LENGTH_SHORT).show() } - case R.id.actionAddDataSet: { - addDataSet(); - Toast.makeText(this, "DataSet added!", Toast.LENGTH_SHORT).show(); - break; + + R.id.actionAddDataSet -> { + addDataSet() + Toast.makeText(this, "DataSet added!", Toast.LENGTH_SHORT).show() } - case R.id.actionRemoveDataSet: { - removeDataSet(); - Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show(); - break; + + R.id.actionRemoveDataSet -> { + removeDataSet() + Toast.makeText(this, "DataSet removed!", Toast.LENGTH_SHORT).show() } - case R.id.actionClear: { - chart.clear(); - Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show(); - break; + + R.id.actionClear -> { + chart!!.clear() + Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show() } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "DynamicalAddingActivity"); + override fun saveToGallery() { + saveToGallery(chart, "DynamicalAddingActivity") } } diff --git a/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt b/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt index f782c20459..00fb2e117f 100644 --- a/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt @@ -1,28 +1,22 @@ - -package info.appdev.chartexample; - -import android.content.Intent; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.formatter.IFillFormatter; -import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; +package info.appdev.chartexample + +import android.content.Intent +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.formatter.IFillFormatter +import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase /** * This works by inverting the background and desired "fill" color. First, we draw the fill color @@ -31,160 +25,155 @@ import java.util.ArrayList; * This method makes it look like we filled the area between the lines, but really we are filling * the area OUTSIDE the lines! */ -@SuppressWarnings("SameParameterValue") -public class FilledLineActivity extends DemoBase { +class FilledLineActivity : DemoBase() { + private var chart: LineChart? = null + private val fillColor = Color.argb(150, 51, 181, 229) - private LineChart chart; - private final int fillColor = Color.argb(150, 51, 181, 229); + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_linechart_noseekbar) - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_linechart_noseekbar); + setTitle("FilledLineActivity") - setTitle("FilledLineActivity"); + chart = findViewById(R.id.chart1) + chart!!.setBackgroundColor(Color.WHITE) + chart!!.setGridBackgroundColor(fillColor) + chart!!.setDrawGridBackground(true) - chart = findViewById(R.id.chart1); - chart.setBackgroundColor(Color.WHITE); - chart.setGridBackgroundColor(fillColor); - chart.setDrawGridBackground(true); - - chart.setDrawBorders(true); + chart!!.setDrawBorders(true) // no description text - chart.getDescription().setEnabled(false); + chart!!.description.isEnabled = false // if disabled, scaling can be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) - Legend l = chart.getLegend(); - l.setEnabled(false); + val l = chart!!.legend + l.isEnabled = false - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(false); + val xAxis = chart!!.xAxis + xAxis.isEnabled = false - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setAxisMaximum(900f); - leftAxis.setAxisMinimum(-250f); - leftAxis.setDrawAxisLine(false); - leftAxis.setDrawZeroLine(false); - leftAxis.setDrawGridLines(false); + val leftAxis = chart!!.axisLeft + leftAxis.axisMaximum = 900f + leftAxis.axisMinimum = -250f + leftAxis.setDrawAxisLine(false) + leftAxis.setDrawZeroLine(false) + leftAxis.setDrawGridLines(false) - chart.getAxisRight().setEnabled(false); + chart!!.axisRight.isEnabled = false // add data - setData(60); + setData(60f) - chart.invalidate(); + chart!!.invalidate() } - private void setData(float range) { - int count = 100; - ArrayList values1 = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(count + 2); + private fun setData(range: Float) { + val count = 100 + val values1 = ArrayList() + val sampleValues = getValues(count + 2) - for (int i = 0; i < count; i++) { - float val = (sampleValues[i].floatValue() * range) + 50; - values1.add(new Entry(i, val)); + for (i in 0.. values2 = new ArrayList<>(); + val values2 = ArrayList() - for (int i = 0; i < count; i++) { - float val = (sampleValues[i+1].floatValue() * range) + 450; - values2.add(new Entry(i, val)); + for (i in 0.. 0) { - set1 = (LineDataSet) chart.getData().getDataSetByIndex(0); - set2 = (LineDataSet) chart.getData().getDataSetByIndex(1); - set1.setEntries(values1); - set2.setEntries(values2); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + val set1: LineDataSet? + val set2: LineDataSet? + + if (chart!!.data != null && + chart!!.data!!.dataSetCount > 0 + ) { + set1 = chart!!.data!!.getDataSetByIndex(0) as LineDataSet + set2 = chart!!.data!!.getDataSetByIndex(1) as LineDataSet + set1.entries = values1 + set2.entries = values2 + chart!!.data!!.notifyDataChanged() + chart!!.notifyDataSetChanged() } else { // create a dataset and give it a type - set1 = new LineDataSet(values1, "DataSet 1"); - - set1.setAxisDependency(YAxis.AxisDependency.LEFT); - set1.setColor(Color.rgb(255, 241, 46)); - set1.setDrawCircles(false); - set1.setLineWidth(2f); - set1.setCircleRadius(3f); - set1.setFillAlpha(255); - set1.setDrawFilled(true); - set1.setFillColor(Color.WHITE); - set1.setHighLightColor(Color.rgb(244, 117, 117)); - set1.setDrawCircleHole(false); - set1.setFillFormatter(new IFillFormatter() { - @Override - public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) { + set1 = LineDataSet(values1, "DataSet 1") + + set1.axisDependency = AxisDependency.LEFT + set1.setColor(Color.rgb(255, 241, 46)) + set1.setDrawCircles(false) + set1.setLineWidth(2f) + set1.setCircleRadius(3f) + set1.setFillAlpha(255) + set1.setDrawFilled(true) + set1.setFillColor(Color.WHITE) + set1.highLightColor = Color.rgb(244, 117, 117) + set1.setDrawCircleHole(false) + set1.setFillFormatter(object : IFillFormatter { + override fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float { // change the return value here to better understand the effect // return 0; - return chart.getAxisLeft().getAxisMinimum(); + return chart!!.axisLeft.axisMinimum } - }); + }) // create a dataset and give it a type - set2 = new LineDataSet(values2, "DataSet 2"); - set2.setAxisDependency(YAxis.AxisDependency.LEFT); - set2.setColor(Color.rgb(255, 241, 46)); - set2.setDrawCircles(false); - set2.setLineWidth(2f); - set2.setCircleRadius(3f); - set2.setFillAlpha(255); - set2.setDrawFilled(true); - set2.setFillColor(Color.WHITE); - set2.setDrawCircleHole(false); - set2.setHighLightColor(Color.rgb(244, 117, 117)); - set2.setFillFormatter(new IFillFormatter() { - @Override - public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) { + set2 = LineDataSet(values2, "DataSet 2") + set2.axisDependency = AxisDependency.LEFT + set2.setColor(Color.rgb(255, 241, 46)) + set2.setDrawCircles(false) + set2.setLineWidth(2f) + set2.setCircleRadius(3f) + set2.setFillAlpha(255) + set2.setDrawFilled(true) + set2.setFillColor(Color.WHITE) + set2.setDrawCircleHole(false) + set2.highLightColor = Color.rgb(244, 117, 117) + set2.setFillFormatter(object : IFillFormatter { + override fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float { // change the return value here to better understand the effect // return 600; - return chart.getAxisLeft().getAxisMaximum(); + return chart!!.axisLeft.axisMaximum } - }); + }) - ArrayList dataSets = new ArrayList<>(); - dataSets.add(set1); // add the data sets - dataSets.add(set2); + val dataSets = ArrayList() + dataSets.add(set1) // add the data sets + dataSets.add(set2) // create a data object with the data sets - LineData data = new LineData(dataSets); - data.setDrawValues(false); + val data = LineData(dataSets) + data.setDrawValues(false) // set data - chart.setData(data); + chart!!.setData(data) } } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.only_github, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.only_github, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/FilledLineActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/FilledLineActivity.java".toUri()) + startActivity(i) } } - return true; + return true } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } } diff --git a/app/src/main/java/info/appdev/chartexample/HalfPieChartActivity.kt b/app/src/main/java/info/appdev/chartexample/HalfPieChartActivity.kt index 5c1c57b40a..38c1a55515 100644 --- a/app/src/main/java/info/appdev/chartexample/HalfPieChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/HalfPieChartActivity.kt @@ -1,170 +1,161 @@ - -package info.appdev.chartexample; - -import android.content.Intent; -import android.graphics.Color; -import android.graphics.Typeface; -import android.net.Uri; -import android.os.Bundle; -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.RelativeSizeSpan; -import android.text.style.StyleSpan; -import android.util.DisplayMetrics; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.RelativeLayout; - -import com.github.mikephil.charting.animation.Easing; -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.data.PieDataSet; -import com.github.mikephil.charting.data.PieEntry; -import com.github.mikephil.charting.formatter.PercentFormatter; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -@SuppressWarnings("SameParameterValue") -public class HalfPieChartActivity extends DemoBase { - - private PieChart chart; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_piechart_half); - - setTitle("HalfPieChartActivity"); - - chart = findViewById(R.id.chart1); - chart.setBackgroundColor(Color.WHITE); - - moveOffScreen(); - - chart.setUsePercentValues(true); - chart.getDescription().setEnabled(false); - - chart.setCenterTextTypeface(tfLight); - chart.setCenterText(generateCenterSpannableText()); - - chart.setDrawHoleEnabled(true); - chart.setHoleColor(Color.WHITE); - - chart.setTransparentCircleColor(Color.WHITE); - chart.setTransparentCircleAlpha(110); - - chart.setHoleRadius(58f); - chart.setTransparentCircleRadius(61f); - - chart.setDrawCenterText(true); - - chart.setRotationEnabled(false); - chart.setHighlightPerTapEnabled(true); - - chart.setMaxAngle(180f); // HALF CHART - chart.setRotationAngle(180f); - chart.setCenterTextOffset(0, -20); - - setData(100); - - chart.animateY(1400, Easing.EaseInOutQuad); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - l.setXEntrySpace(7f); - l.setYEntrySpace(0f); - l.setYOffset(0f); +package info.appdev.chartexample + +import android.content.Intent +import android.graphics.Color +import android.graphics.Typeface +import android.net.Uri +import android.os.Bundle +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.text.style.RelativeSizeSpan +import android.text.style.StyleSpan +import android.util.DisplayMetrics +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.RelativeLayout +import com.github.mikephil.charting.animation.Easing +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.data.PieDataSet +import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.formatter.PercentFormatter +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.ColorTemplate.holoBlue +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase +import androidx.core.net.toUri + +class HalfPieChartActivity : DemoBase() { + private var chart: PieChart? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_piechart_half) + + setTitle("HalfPieChartActivity") + + chart = findViewById(R.id.chart1) + chart!!.setBackgroundColor(Color.WHITE) + + moveOffScreen() + + chart!!.setUsePercentValues(true) + chart!!.description.isEnabled = false + + chart!!.setCenterTextTypeface(tfLight) + chart!!.centerText = generateCenterSpannableText() + + chart!!.isDrawHoleEnabled = true + chart!!.setHoleColor(Color.WHITE) + + chart!!.setTransparentCircleColor(Color.WHITE) + chart!!.setTransparentCircleAlpha(110) + + chart!!.holeRadius = 58f + chart!!.transparentCircleRadius = 61f + + chart!!.setDrawCenterText(true) + + chart!!.isRotationEnabled = false + chart!!.isHighlightPerTapEnabled = true + + chart!!.maxAngle = 180f // HALF CHART + chart!!.rotationAngle = 180f + chart!!.setCenterTextOffset(0f, -20f) + + setData(100f) + + chart!!.animateY(1400, Easing.EaseInOutQuad) + + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + l.xEntrySpace = 7f + l.yEntrySpace = 0f + l.yOffset = 0f // entry label styling - chart.setEntryLabelColor(Color.WHITE); - chart.setEntryLabelTypeface(tfRegular); - chart.setEntryLabelTextSize(12f); + chart!!.setEntryLabelColor(Color.WHITE) + chart!!.setEntryLabelTypeface(tfRegular) + chart!!.setEntryLabelTextSize(12f) } - private void setData(float range) { - int count = 4; - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(count); + private fun setData(range: Float) { + val count = 4 + val values = ArrayList() + val sampleValues = getValues(count) - for (int i = 0; i < count; i++) { - values.add(new PieEntry((sampleValues[i].floatValue() * range) + range / 5, parties[i % parties.length])); + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/HalfPieChartActivity.java".toUri()) + startActivity(i) } } - return true; + return true } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } } diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt index bcc161e6cf..304a5a902a 100644 --- a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt @@ -1,288 +1,273 @@ +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.RectF +import android.net.Uri +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.MPPointF.Companion.recycleInstance +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase +import androidx.core.net.toUri + +class HorizontalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: HorizontalBarChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_horizontalbarchart) + + setTitle("HorizontalBarChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarY = findViewById(R.id.seekBarY) + + seekBarY!!.setOnSeekBarChangeListener(this) + seekBarX!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.RectF; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.HorizontalBarChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.MPPointF; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; - -public class HorizontalBarChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private HorizontalBarChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_horizontalbarchart); - - setTitle("HorizontalBarChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarY = findViewById(R.id.seekBarY); - - seekBarY.setOnSeekBarChangeListener(this); - seekBarX.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); // chart.setHighlightEnabled(false); + chart!!.setDrawBarShadow(false) - chart.setDrawBarShadow(false); + chart!!.setDrawValueAboveBar(true) - chart.setDrawValueAboveBar(true); - - chart.getDescription().setEnabled(false); + chart!!.description.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart.setMaxVisibleValueCount(60); + chart!!.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) // draw shadows for each bar that show the maximum value // chart.setDrawBarShadow(true); - - chart.setDrawGridBackground(false); - - XAxis xl = chart.getXAxis(); - xl.setPosition(XAxisPosition.BOTTOM); - xl.setTypeface(tfLight); - xl.setDrawAxisLine(true); - xl.setDrawGridLines(false); - xl.setGranularity(10f); - - YAxis yl = chart.getAxisLeft(); - yl.setTypeface(tfLight); - yl.setDrawAxisLine(true); - yl.setDrawGridLines(true); - yl.setAxisMinimum(0f); // this replaces setStartAtZero(true) -// yl.setInverted(true); - - YAxis yr = chart.getAxisRight(); - yr.setTypeface(tfLight); - yr.setDrawAxisLine(true); - yr.setDrawGridLines(false); - yr.setAxisMinimum(0f); // this replaces setStartAtZero(true) -// yr.setInverted(true); - - chart.setFitBars(true); - chart.animateY(2500); + chart!!.setDrawGridBackground(false) + + val xl = chart!!.xAxis + xl.position = XAxisPosition.BOTTOM + xl.typeface = tfLight + xl.setDrawAxisLine(true) + xl.setDrawGridLines(false) + xl.granularity = 10f + + val yl = chart!!.axisLeft + yl.typeface = tfLight + yl.setDrawAxisLine(true) + yl.setDrawGridLines(true) + yl.axisMinimum = 0f // this replaces setStartAtZero(true) + + // yl.setInverted(true); + val yr = chart!!.axisRight + yr.typeface = tfLight + yr.setDrawAxisLine(true) + yr.setDrawGridLines(false) + yr.axisMinimum = 0f // this replaces setStartAtZero(true) + + // yr.setInverted(true); + chart!!.setFitBars(true) + chart!!.animateY(2500) // setting data - seekBarY.setProgress(50); - seekBarX.setProgress(12); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - l.setFormSize(8f); - l.setXEntrySpace(4f); + seekBarY!!.progress = 50 + seekBarX!!.progress = 12 + + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM + l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + l.formSize = 8f + l.xEntrySpace = 4f } - private void setData(int count, float range) { - float barWidth = 9f; - float spaceForBar = 10f; - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100); - - for (int i = 0; i < count; i++) { - float val = sampleValues[i].floatValue() * range; - values.add(new BarEntry(i * spaceForBar, val, - getResources().getDrawable(R.drawable.star))); + private fun setData(count: Int, range: Float) { + val barWidth = 9f + val spaceForBar = 10f + val values = ArrayList() + val sampleValues = getValues(100) + + for (i in 0.. 0) { - set1 = (BarDataSet) chart.getData().getDataSetByIndex(0); - set1.setEntries(values); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + if (chart!!.data != null && + chart!!.data!!.dataSetCount > 0 + ) { + set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set1.entries = values + chart!!.data!!.notifyDataChanged() + chart!!.notifyDataSetChanged() } else { - set1 = new BarDataSet(values, "DataSet 1"); + set1 = BarDataSet(values, "DataSet 1") - set1.setDrawIcons(false); + set1.isDrawIconsEnabled = false - ArrayList dataSets = new ArrayList<>(); - dataSets.add(set1); + val dataSets = ArrayList() + dataSets.add(set1) - BarData data = new BarData(dataSets); - data.setValueTextSize(10f); - data.setValueTypeface(tfLight); - data.setBarWidth(barWidth); - chart.setData(data); + val data = BarData(dataSets) + data.setValueTextSize(10f) + data.setValueTypeface(tfLight) + data.barWidth = barWidth + chart!!.setData(data) } } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bar, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.bar, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - List sets = chart.getData() - .getDataSets(); - for (IBarDataSet iSet : sets) { - iSet.setDrawValues(!iSet.isDrawValuesEnabled); + R.id.actionToggleValues -> { + val sets = chart!!.data!! + .dataSets + + for (iSet in sets) { + iSet.isDrawValuesEnabled = !iSet.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleIcons: { - List sets = chart.getData() - .getDataSets(); - for (IBarDataSet iSet : sets) { - iSet.setDrawIcons(!iSet.isDrawIconsEnabled); + R.id.actionToggleIcons -> { + val sets = chart!!.data!! + .dataSets + + for (iSet in sets) { + iSet.isDrawIconsEnabled = !iSet.isDrawIconsEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if(chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionTogglePinch: { - chart.setPinchZoom(!chart.isPinchZoomEnabled()); - chart.invalidate(); - break; + R.id.actionTogglePinch -> { + chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.actionToggleBarBorders: { - for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet)set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); - chart.invalidate(); - break; + R.id.actionToggleBarBorders -> { + for (set in chart!!.data!!.dataSets) (set as BarDataSet).setBarBorderWidth(if (set.barBorderWidth == 1f) 0f else 1f) + + chart!!.invalidate() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); - - setData(seekBarX.getProgress(), seekBarY.getProgress()); - chart.setFitBars(true); - chart.invalidate(); + setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) + chart!!.setFitBars(true) + chart!!.invalidate() } - @Override - protected void saveToGallery() { - saveToGallery(chart, "HorizontalBarChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "HorizontalBarChartActivity") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} - - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - private final RectF mOnValueSelectedRectF = new RectF(); + override fun onStopTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onValueSelected(Entry e, Highlight h) { + private val mOnValueSelectedRectF = RectF() - if (e == null) - return; + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null || h == null) return - RectF bounds = mOnValueSelectedRectF; - chart.getBarBounds((BarEntry) e, bounds); + val bounds = mOnValueSelectedRectF + chart!!.getBarBounds(e as BarEntry, bounds) - MPPointF position = chart.getPosition(e, chart.getData().getDataSetByIndex(h.getDataSetIndex()).axisDependency); + val position = chart!!.getPosition(e, chart!!.data!!.getDataSetByIndex(h.dataSetIndex).axisDependency) - Log.i("bounds", bounds.toString()); - Log.i("position", position.toString()); + Log.i("bounds", bounds.toString()) + Log.i("position", position.toString()) - MPPointF.recycleInstance(position); + recycleInstance(position) } - @Override - public void onNothingSelected() {} + override fun onNothingSelected() {} } diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt index 02e920703c..8322975f97 100644 --- a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt @@ -1,288 +1,270 @@ +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.RectF +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.MPPointF.Companion.recycleInstance +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: HorizontalBarChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_horizontalbarchart) + + setTitle("HorizontalBarChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarY = findViewById(R.id.seekBarY) + + seekBarY!!.setOnSeekBarChangeListener(this) + seekBarX!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.RectF; -import android.net.Uri; -import android.os.Bundle; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import androidx.core.content.ContextCompat; - -import com.github.mikephil.charting.charts.HorizontalBarChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.MPPointF; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; - -public class HorizontalBarNegativeChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private HorizontalBarChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_horizontalbarchart); - - setTitle("HorizontalBarChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarY = findViewById(R.id.seekBarY); - - seekBarY.setOnSeekBarChangeListener(this); - seekBarX.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); // chart.setHighlightEnabled(false); + chart!!.setDrawBarShadow(false) - chart.setDrawBarShadow(false); + chart!!.setDrawValueAboveBar(true) - chart.setDrawValueAboveBar(true); - - chart.getDescription().setEnabled(false); + chart!!.description.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart.setMaxVisibleValueCount(60); + chart!!.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) // draw shadows for each bar that show the maximum value // chart.setDrawBarShadow(true); - - chart.setDrawGridBackground(false); - - XAxis xl = chart.getXAxis(); - xl.setPosition(XAxisPosition.BOTTOM); - xl.setTypeface(tfLight); - xl.setDrawAxisLine(true); - xl.setDrawGridLines(false); - xl.setGranularity(10f); - - YAxis yl = chart.getAxisLeft(); - yl.setTypeface(tfLight); - yl.setDrawAxisLine(true); - yl.setDrawGridLines(true); -// yl.setInverted(true); - - YAxis yr = chart.getAxisRight(); - yr.setTypeface(tfLight); - yr.setDrawAxisLine(true); - yr.setDrawGridLines(false); -// yr.setInverted(true); - - chart.setFitBars(true); - chart.animateY(2500); + chart!!.setDrawGridBackground(false) + + val xl = chart!!.xAxis + xl.position = XAxisPosition.BOTTOM + xl.typeface = tfLight + xl.setDrawAxisLine(true) + xl.setDrawGridLines(false) + xl.granularity = 10f + + val yl = chart!!.axisLeft + yl.typeface = tfLight + yl.setDrawAxisLine(true) + yl.setDrawGridLines(true) + + // yl.setInverted(true); + val yr = chart!!.axisRight + yr.typeface = tfLight + yr.setDrawAxisLine(true) + yr.setDrawGridLines(false) + + // yr.setInverted(true); + chart!!.setFitBars(true) + chart!!.animateY(2500) // setting data - seekBarY.setProgress(50); - seekBarX.setProgress(12); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - l.setFormSize(8f); - l.setXEntrySpace(4f); + seekBarY!!.progress = 50 + seekBarX!!.progress = 12 + + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM + l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + l.formSize = 8f + l.xEntrySpace = 4f } - private void setData(int count, float range) { - - float barWidth = 9f; - float spaceForBar = 10f; - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(count + 2); - - for (int i = 0; i < count; i++) { - float val = sampleValues[i].floatValue() * range - range / 2; - values.add(new BarEntry(i * spaceForBar, val, - getResources().getDrawable(R.drawable.star))); + private fun setData(count: Int, range: Float) { + val barWidth = 9f + val spaceForBar = 10f + val values = ArrayList() + val sampleValues = getValues(count + 2) + + for (i in 0.. 0) { - set1 = (BarDataSet) chart.getData().getDataSetByIndex(0); - set1.setEntries(values); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + if (chart!!.data != null && + chart!!.data!!.dataSetCount > 0 + ) { + set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set1.entries = values + chart!!.data!!.notifyDataChanged() + chart!!.notifyDataSetChanged() } else { - set1 = new BarDataSet(values, "DataSet 1"); + set1 = BarDataSet(values, "DataSet 1") - set1.setDrawIcons(false); + set1.isDrawIconsEnabled = false - ArrayList dataSets = new ArrayList<>(); - dataSets.add(set1); + val dataSets = ArrayList() + dataSets.add(set1) - BarData data = new BarData(dataSets); - data.setValueTextSize(10f); - data.setValueTypeface(tfLight); - data.setBarWidth(barWidth); - chart.setData(data); + val data = BarData(dataSets) + data.setValueTextSize(10f) + data.setValueTypeface(tfLight) + data.barWidth = barWidth + chart!!.setData(data) } } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bar, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.bar, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - List sets = chart.getData() - .getDataSets(); - for (IBarDataSet iSet : sets) { - iSet.setDrawValues(!iSet.isDrawValuesEnabled); + R.id.actionToggleValues -> { + val sets = chart!!.data!! + .dataSets + + for (iSet in sets) { + iSet.isDrawValuesEnabled = !iSet.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleIcons: { - List sets = chart.getData() - .getDataSets(); - for (IBarDataSet iSet : sets) { - iSet.setDrawIcons(!iSet.isDrawIconsEnabled); + R.id.actionToggleIcons -> { + val sets = chart!!.data!! + .dataSets + + for (iSet in sets) { + iSet.isDrawIconsEnabled = !iSet.isDrawIconsEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if(chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionTogglePinch: { - chart.setPinchZoom(!chart.isPinchZoomEnabled()); - chart.invalidate(); - break; + R.id.actionTogglePinch -> { + chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.actionToggleBarBorders: { - for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet)set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); - chart.invalidate(); - break; + R.id.actionToggleBarBorders -> { + for (set in chart!!.data!!.dataSets) (set as BarDataSet).setBarBorderWidth(if (set.barBorderWidth == 1f) 0f else 1f) + + chart!!.invalidate() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); - - setData(seekBarX.getProgress(), seekBarY.getProgress()); - chart.setFitBars(true); - chart.invalidate(); + setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) + chart!!.setFitBars(true) + chart!!.invalidate() } - @Override - protected void saveToGallery() { - saveToGallery(chart, "HorizontalBarChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "HorizontalBarChartActivity") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} - - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - private final RectF mOnValueSelectedRectF = new RectF(); + override fun onStopTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onValueSelected(Entry e, Highlight h) { + private val mOnValueSelectedRectF = RectF() - if (e == null) - return; + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null || h == null) return - RectF bounds = mOnValueSelectedRectF; - chart.getBarBounds((BarEntry) e, bounds); + val bounds = mOnValueSelectedRectF + chart!!.getBarBounds(e as BarEntry, bounds) - MPPointF position = chart.getPosition(e, chart.getData().getDataSetByIndex(h.getDataSetIndex()).axisDependency); + val position = chart!!.getPosition(e, chart!!.data!!.getDataSetByIndex(h.dataSetIndex).axisDependency) - Log.i("bounds", bounds.toString()); - Log.i("position", position.toString()); + Log.i("bounds", bounds.toString()) + Log.i("position", position.toString()) - MPPointF.recycleInstance(position); + recycleInstance(position) } - @Override - public void onNothingSelected() {} + override fun onNothingSelected() {} } diff --git a/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt index 1494083f35..b23221f146 100644 --- a/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt @@ -1,105 +1,98 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.Legend.LegendForm; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.EntryXComparator; - -import info.appdev.chartexample.custom.MyMarkerView; -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class InvertedLineChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private LineChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_linechart); - - setTitle("InvertedLineChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarY = findViewById(R.id.seekBarY); - - seekBarY.setOnSeekBarChangeListener(this); - seekBarX.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); - chart.setDrawGridBackground(false); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.Legend.LegendForm +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.EntryXComparator +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.custom.MyMarkerView +import info.appdev.chartexample.notimportant.DemoBase +import java.util.Collections + +class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: LineChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_linechart) + + setTitle("InvertedLineChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarY = findViewById(R.id.seekBarY) + + seekBarY!!.setOnSeekBarChangeListener(this) + seekBarX!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) + chart!!.setDrawGridBackground(false) // no description text - chart.getDescription().setEnabled(false); + chart!!.description.isEnabled = false // enable touch gestures - chart.setTouchEnabled(true); + chart!!.setTouchEnabled(true) // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); + chart!!.isDragEnabled = true + chart!!.setScaleEnabled(true) // if disabled, scaling can be done on x- and y-axis separately - chart.setPinchZoom(true); + chart!!.setPinchZoom(true) // set an alternative background color // chart.setBackgroundColor(Color.GRAY); // create a custom MarkerView (extend MarkerView) and specify the layout // to use for it - MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view); - mv.setChartView(chart); // For bounds control - chart.setMarker(mv); // Set the marker to the chart + val mv = MyMarkerView(this, R.layout.custom_marker_view) + mv.chartView = chart // For bounds control + chart!!.setMarker(mv) // Set the marker to the chart - XAxis xl = chart.getXAxis(); - xl.setAvoidFirstLastClipping(true); - xl.setAxisMinimum(0f); + val xl = chart!!.xAxis + xl.setAvoidFirstLastClipping(true) + xl.axisMinimum = 0f - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setInverted(true); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) + val leftAxis = chart!!.axisLeft + leftAxis.isInverted = true + leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setEnabled(false); + val rightAxis = chart!!.axisRight + rightAxis.isEnabled = false // add data - seekBarX.setProgress(25); - seekBarY.setProgress(50); + seekBarX!!.progress = 25 + seekBarY!!.progress = 50 // // restrain the maximum scale-out factor // chart.setScaleMinima(3f, 3f); @@ -108,169 +101,154 @@ public class InvertedLineChartActivity extends DemoBase implements OnSeekBarChan // chart.centerViewPort(10, 50); // get the legend (only possible after setting data) - Legend l = chart.getLegend(); + val l = chart!!.legend // modify the legend ... - l.setForm(LegendForm.LINE); + l.form = LegendForm.LINE // don't forget to refresh the drawing - chart.invalidate(); + chart!!.invalidate() } - private void setData(int count, float range) { - - ArrayList entries = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(count + 2); + private fun setData(count: Int, range: Float) { + val entries = ArrayList() + val sampleValues = getValues(count + 2) - for (int i = 0; i < count; i++) { - float xVal = sampleValues[i].floatValue() * range; - float yVal = sampleValues[i + 1].floatValue() * range; - entries.add(new Entry(xVal, yVal)); + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/InvertedLineChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleValues -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - set.setDrawValues(!set.isDrawValuesEnabled()); + for (iSet in sets) { + val set = iSet as LineDataSet + set.isDrawValuesEnabled = !set.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if(chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionToggleFilled: { - - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleFilled -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - set.setDrawFilled(!set.isDrawFilledEnabled()); + for (iSet in sets) { + val set = iSet as LineDataSet + set.setDrawFilled(!set.isDrawFilledEnabled) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleCircles: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleCircles -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - set.setDrawCircles(!set.isDrawCirclesEnabled()); + for (iSet in sets) { + val set = iSet as LineDataSet + set.setDrawCircles(!set.isDrawCirclesEnabled) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } - case R.id.actionTogglePinch: { - chart.setPinchZoom(!chart.isPinchZoomEnabled()); - chart.invalidate(); - break; + R.id.actionTogglePinch -> { + chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - setData(seekBarX.getProgress(), seekBarY.getProgress()); + setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) // redraw - chart.invalidate(); + chart!!.invalidate() } - @Override - protected void saveToGallery() { - saveToGallery(chart, "InvertedLineChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "InvertedLineChartActivity") } - @Override - public void onValueSelected(Entry e, Highlight h) { - Log.i("VAL SELECTED", - "Value: " + e.getY() + ", xIndex: " + e.getX() - + ", DataSet index: " + h.getDataSetIndex()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + Log.i( + "VAL SELECTED", + ("Value: " + e?.y + ", xIndex: " + e?.x + + ", DataSet index: " + h?.dataSetIndex) + ) } - @Override - public void onNothingSelected() {} + override fun onNothingSelected() {} - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/LineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/LineChartActivity.kt index dc49833f77..72a3b19ca1 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/LineChartActivity.kt @@ -4,7 +4,6 @@ import android.Manifest import android.content.Intent import android.content.pm.PackageManager import android.graphics.Color -import android.net.Uri import android.os.Bundle import android.util.Log import android.view.Menu @@ -13,6 +12,7 @@ import android.view.WindowManager import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing import com.github.mikephil.charting.components.Legend.LegendForm import com.github.mikephil.charting.components.LimitLine @@ -159,14 +159,14 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.viewGithub -> { val i = Intent(Intent.ACTION_VIEW) i.data = - Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java") + "https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java".toUri() startActivity(i) } R.id.actionToggleValues -> { binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet - set.setDrawValues(!set.isDrawValuesEnabled) + set.isDrawValuesEnabled = (!set.isDrawValuesEnabled) } binding.chart1.invalidate() } @@ -174,7 +174,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleIcons -> { binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet - set.setDrawIcons(!set.isDrawIconsEnabled) + set.isDrawIconsEnabled = (!set.isDrawIconsEnabled) binding.chart1.invalidate() } } @@ -205,7 +205,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleCubic -> { 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 + set.setMode(if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER) } binding.chart1.invalidate() } @@ -213,7 +213,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleStepped -> { 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 + set.setMode(if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED) } binding.chart1.invalidate() } @@ -221,7 +221,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleHorizontalCubic -> { 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 + set.setMode(if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER) } binding.chart1.invalidate() } @@ -265,7 +265,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec override fun onStartTrackingTouch(seekBar: SeekBar) {} override fun onStopTrackingTouch(seekBar: SeekBar) {} - override fun onValueSelected(e: Entry, h: Highlight) { + override fun onValueSelected(e: Entry?, h: Highlight?) { Log.i("Entry selected", e.toString()) Log.i("LOW HIGH", "low: " + binding.chart1.lowestVisibleX + ", high: " + binding.chart1.highestVisibleX) Log.i( diff --git a/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt b/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt index c8b1980e92..4ad74640e4 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt +++ b/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt @@ -1,157 +1,146 @@ - -package info.appdev.chartexample; - -import android.content.Intent; -import android.graphics.Color; -import android.graphics.Typeface; -import android.net.Uri; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -@SuppressWarnings("SameParameterValue") -public class LineChartActivityColored extends DemoBase { - - private final LineChart[] charts = new LineChart[4]; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_colored_lines); - - setTitle("LineChartActivityColored"); - - charts[0] = findViewById(R.id.chart1); - charts[1] = findViewById(R.id.chart2); - charts[2] = findViewById(R.id.chart3); - charts[3] = findViewById(R.id.chart4); - - Typeface mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf"); - - for (int i = 0; i < charts.length; i++) { - - LineData data = getData(100); - data.setValueTypeface(mTf); +package info.appdev.chartexample + +import android.content.Intent +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class LineChartActivityColored : DemoBase() { + private val charts = arrayOfNulls(4) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_colored_lines) + + setTitle("LineChartActivityColored") + + charts[0] = findViewById(R.id.chart1) + charts[1] = findViewById(R.id.chart2) + charts[2] = findViewById(R.id.chart3) + charts[3] = findViewById(R.id.chart4) + + val mTf = Typeface.createFromAsset(assets, "OpenSans-Bold.ttf") + + for (i in charts.indices) { + val data = getData(100f) + data.setValueTypeface(mTf) // add some transparency to the color with "& 0x90FFFFFF" - setupChart(charts[i], data, colors[i % colors.length]); + setupChart(charts[i]!!, data, colors[i % colors.size]) } } - private final int[] colors = new int[] { - Color.rgb(137, 230, 81), - Color.rgb(240, 240, 30), - Color.rgb(89, 199, 250), - Color.rgb(250, 104, 104) - }; + private val colors = intArrayOf( + Color.rgb(137, 230, 81), + Color.rgb(240, 240, 30), + Color.rgb(89, 199, 250), + Color.rgb(250, 104, 104) + ) - private void setupChart(LineChart chart, LineData data, int color) { - - ((LineDataSet) data.getDataSetByIndex(0)).setCircleHoleColor(color); + private fun setupChart(chart: LineChart, data: LineData, color: Int) { + (data.getDataSetByIndex(0) as LineDataSet).setCircleHoleColor(color) // no description text - chart.getDescription().setEnabled(false); + chart.description.isEnabled = false // chart.setDrawHorizontalGrid(false); // // enable / disable grid background - chart.setDrawGridBackground(false); -// chart.getRenderer().getGridPaint().setGridColor(Color.WHITE & 0x70FFFFFF); + chart.setDrawGridBackground(false) + + // chart.getRenderer().getGridPaint().setGridColor(Color.WHITE & 0x70FFFFFF); // enable touch gestures - chart.setTouchEnabled(true); + chart.setTouchEnabled(true) // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); + chart.isDragEnabled = true + chart.setScaleEnabled(true) // if disabled, scaling can be done on x- and y-axis separately - chart.setPinchZoom(false); + chart.setPinchZoom(false) - chart.setBackgroundColor(color); + chart.setBackgroundColor(color) // set custom chart offsets (automatic offset calculation is hereby disabled) - chart.setViewPortOffsets(10, 0, 10, 0); + chart.setViewPortOffsets(10f, 0f, 10f, 0f) // add data - chart.setData(data); + chart.setData(data) // get the legend (only possible after setting data) - Legend l = chart.getLegend(); - l.setEnabled(false); + val l = chart.legend + l.isEnabled = false - chart.getAxisLeft().setEnabled(false); - chart.getAxisLeft().setSpaceTop(40); - chart.getAxisLeft().setSpaceBottom(40); - chart.getAxisRight().setEnabled(false); + chart.axisLeft.isEnabled = false + chart.axisLeft.spaceTop = 40f + chart.axisLeft.spaceBottom = 40f + chart.axisRight.isEnabled = false - chart.getXAxis().setEnabled(false); + chart.xAxis.isEnabled = false // animate calls invalidate()... - chart.animateX(2500); + chart.animateX(2500) } - private LineData getData(float range) { - int count = 36; - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(count); + private fun getData(range: Float): LineData { + val count = 36 + val values = ArrayList() + val sampleValues = getValues(count) - for (int i = 0; i < count; i++) { - float val = (sampleValues[i].floatValue() * range) + 3; - values.add(new Entry(i, val)); + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivityColored.java".toUri()) + startActivity(i) } } - return true; + return true } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } } diff --git a/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt b/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt index 2cfed3b849..35476b9aa1 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt @@ -1,401 +1,392 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.Legend.LegendForm; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.Legend.LegendForm +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate.colorWithAlpha +import com.github.mikephil.charting.utils.ColorTemplate.holoBlue +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase /** - * Example of a dual axis {@link LineChart} with multiple data sets. + * Example of a dual axis [LineChart] with multiple data sets. * * @since 1.7.4 * @version 3.1.0 */ -public class LineChartDualAxisActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private LineChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; +class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: LineChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_linechart); + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_linechart) - setTitle("LineChart DualAxis"); + setTitle("LineChart DualAxis") - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setOnSeekBarChangeListener(this); + seekBarX = findViewById(R.id.seekBarX) + seekBarX!!.setOnSeekBarChangeListener(this) - seekBarY = findViewById(R.id.seekBarY); - seekBarY.setOnSeekBarChangeListener(this); + seekBarY = findViewById(R.id.seekBarY) + seekBarY!!.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) // no description text - chart.getDescription().setEnabled(false); + chart!!.description.isEnabled = false // enable touch gestures - chart.setTouchEnabled(true); + chart!!.setTouchEnabled(true) - chart.setDragDecelerationFrictionCoef(0.9f); + chart!!.dragDecelerationFrictionCoef = 0.9f // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); - chart.setDrawGridBackground(false); - chart.setHighlightPerDragEnabled(true); + chart!!.isDragEnabled = true + chart!!.setScaleEnabled(true) + chart!!.setDrawGridBackground(false) + chart!!.isHighlightPerDragEnabled = true // if disabled, scaling can be done on x- and y-axis separately - chart.setPinchZoom(true); + chart!!.setPinchZoom(true) // set an alternative background color - chart.setBackgroundColor(Color.LTGRAY); + chart!!.setBackgroundColor(Color.LTGRAY) // add data - seekBarX.setProgress(20); - seekBarY.setProgress(30); + seekBarX!!.progress = 20 + seekBarY!!.progress = 30 - chart.animateX(1500); + chart!!.animateX(1500) // get the legend (only possible after setting data) - Legend l = chart.getLegend(); + val l = chart!!.legend // modify the legend ... - l.setForm(LegendForm.LINE); - l.setTypeface(tfLight); - l.setTextSize(11f); - l.setTextColor(Color.WHITE); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); -// l.setYOffset(11f); - - XAxis xAxis = chart.getXAxis(); - xAxis.setTypeface(tfLight); - xAxis.setTextSize(11f); - xAxis.setTextColor(Color.WHITE); - xAxis.setDrawGridLines(false); - xAxis.setDrawAxisLine(false); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tfLight); - leftAxis.setTextColor(ColorTemplate.getHoloBlue()); - leftAxis.setAxisMaximum(200f); - leftAxis.setAxisMinimum(0f); - leftAxis.setDrawGridLines(true); - leftAxis.setGranularityEnabled(true); - - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setTypeface(tfLight); - rightAxis.setTextColor(Color.MAGENTA); - rightAxis.setAxisMaximum(900); - rightAxis.setAxisMinimum(-200); - rightAxis.setDrawGridLines(false); - rightAxis.setDrawZeroLine(false); - rightAxis.setGranularityEnabled(false); + l.form = LegendForm.LINE + l.typeface = tfLight + l.textSize = 11f + l.textColor = Color.WHITE + l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM + l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + + // l.setYOffset(11f); + val xAxis = chart!!.xAxis + xAxis.typeface = tfLight + xAxis.textSize = 11f + xAxis.textColor = Color.WHITE + xAxis.setDrawGridLines(false) + xAxis.setDrawAxisLine(false) + + val leftAxis = chart!!.axisLeft + leftAxis.typeface = tfLight + leftAxis.textColor = holoBlue + leftAxis.axisMaximum = 200f + leftAxis.axisMinimum = 0f + leftAxis.setDrawGridLines(true) + leftAxis.isGranularityEnabled = true + + val rightAxis = chart!!.axisRight + rightAxis.typeface = tfLight + rightAxis.textColor = Color.MAGENTA + rightAxis.axisMaximum = 900f + rightAxis.axisMinimum = -200f + rightAxis.setDrawGridLines(false) + rightAxis.setDrawZeroLine(false) + rightAxis.isGranularityEnabled = false } - private void setData(int count, float range) { - - ArrayList values1 = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(count); + private fun setData(count: Int, range: Float) { + val values1 = ArrayList() + val sampleValues = getValues(count) - for (int i = 0; i < count; i++) { - float val = (sampleValues[i].floatValue() * (range / 2f)) + 50; - values1.add(new Entry(i, val)); + for (i in 0.. values2 = new ArrayList<>(); + val values2 = ArrayList() - for (int i = 0; i < count; i++) { - float val = (sampleValues[i].floatValue() * range) + 450; - values2.add(new Entry(i, val)); + for (i in 0.. values3 = new ArrayList<>(); + val values3 = ArrayList() - for (int i = 0; i < count; i++) { - float val = (sampleValues[i].floatValue() * range) + 500; - values3.add(new Entry(i, val)); + for (i in 0.. 0) { - set1 = (LineDataSet) chart.getData().getDataSetByIndex(0); - set2 = (LineDataSet) chart.getData().getDataSetByIndex(1); - set3 = (LineDataSet) chart.getData().getDataSetByIndex(2); - set1.setEntries(values1); - set2.setEntries(values2); - set3.setEntries(values3); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + val set1: LineDataSet? + val set2: LineDataSet? + val set3: LineDataSet? + + if (chart!!.data != null && + chart!!.data!!.dataSetCount > 0 + ) { + set1 = chart!!.data!!.getDataSetByIndex(0) as LineDataSet + set2 = chart!!.data!!.getDataSetByIndex(1) as LineDataSet + set3 = chart!!.data!!.getDataSetByIndex(2) as LineDataSet + set1.entries = values1 + set2.entries = values2 + set3.entries = values3 + chart!!.data!!.notifyDataChanged() + chart!!.notifyDataSetChanged() } else { // create a dataset and give it a type - set1 = new LineDataSet(values1, "DataSet 1"); - - set1.setAxisDependency(AxisDependency.LEFT); - set1.setColor(ColorTemplate.getHoloBlue()); - set1.setCircleColor(Color.WHITE); - set1.setLineWidth(2f); - set1.setCircleRadius(3f); - set1.setFillAlpha(65); - set1.setFillColor(ColorTemplate.getHoloBlue()); - set1.setHighLightColor(Color.rgb(244, 117, 117)); - set1.setDrawCircleHole(false); + set1 = LineDataSet(values1, "DataSet 1") + + set1.axisDependency = AxisDependency.LEFT + set1.setColor(holoBlue) + set1.setCircleColor(Color.WHITE) + set1.setLineWidth(2f) + set1.setCircleRadius(3f) + set1.setFillAlpha(65) + set1.setFillColor(holoBlue) + set1.highLightColor = Color.rgb(244, 117, 117) + set1.setDrawCircleHole(false) + //set1.setFillFormatter(new MyFillFormatter(0f)); //set1.setDrawHorizontalHighlightIndicator(false); //set1.setVisible(false); //set1.setCircleHoleColor(Color.WHITE); // create a dataset and give it a type - set2 = new LineDataSet(values2, "DataSet 2"); - set2.setAxisDependency(AxisDependency.RIGHT); - set2.setColor(Color.MAGENTA); - set2.setCircleColor(Color.WHITE); - set2.setLineWidth(2f); - set2.setCircleRadius(3f); - set2.setFillAlpha(65); - set2.setFillColor(Color.BLUE); - set2.setDrawCircleHole(false); - set2.setHighLightColor(Color.rgb(244, 117, 117)); - //set2.setFillFormatter(new MyFillFormatter(900f)); + set2 = LineDataSet(values2, "DataSet 2") + set2.axisDependency = AxisDependency.RIGHT + set2.setColor(Color.MAGENTA) + set2.setCircleColor(Color.WHITE) + set2.setLineWidth(2f) + set2.setCircleRadius(3f) + set2.setFillAlpha(65) + set2.setFillColor(Color.BLUE) + set2.setDrawCircleHole(false) + set2.highLightColor = Color.rgb(244, 117, 117) - set3 = new LineDataSet(values3, "DataSet 3"); - set3.setAxisDependency(AxisDependency.RIGHT); - set3.setColor(Color.YELLOW); - set3.setCircleColor(Color.WHITE); - set3.setLineWidth(2f); - set3.setCircleRadius(3f); - set3.setFillAlpha(65); - set3.setFillColor(ColorTemplate.colorWithAlpha(Color.YELLOW, 200)); - set3.setDrawCircleHole(false); - set3.setHighLightColor(Color.rgb(244, 117, 117)); + //set2.setFillFormatter(new MyFillFormatter(900f)); + set3 = LineDataSet(values3, "DataSet 3") + set3.axisDependency = AxisDependency.RIGHT + set3.setColor(Color.YELLOW) + set3.setCircleColor(Color.WHITE) + set3.setLineWidth(2f) + set3.setCircleRadius(3f) + set3.setFillAlpha(65) + set3.setFillColor(colorWithAlpha(Color.YELLOW, 200)) + set3.setDrawCircleHole(false) + set3.highLightColor = Color.rgb(244, 117, 117) // create a data object with the data sets - LineData data = new LineData(set1, set2, set3); - data.setValueTextColor(Color.WHITE); - data.setValueTextSize(9f); + val data = LineData(set1, set2, set3) + data.setValueTextColor(Color.WHITE) + data.setValueTextSize(9f) // set data - chart.setData(data); + chart!!.setData(data) } } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.line, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.line, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleValues -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - set.setDrawValues(!set.isDrawValuesEnabled()); + for (iSet in sets) { + val set = iSet as LineDataSet + set.isDrawValuesEnabled = (!set.isDrawValuesEnabled) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if (chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionToggleFilled: { - List sets = chart.getData() - .getDataSets(); + R.id.actionToggleFilled -> { + val sets: MutableList = chart!!.data!! + .dataSets - for (ILineDataSet iSet : sets) { - - LineDataSet set = (LineDataSet) iSet; - set.setDrawFilled(!set.isDrawFilledEnabled()); + for (iSet in sets) { + val set = iSet as LineDataSet + set.setDrawFilled(!set.isDrawFilledEnabled) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleCircles: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleCircles -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - set.setDrawCircles(!set.isDrawCirclesEnabled()); + for (iSet in sets) { + val set = iSet as LineDataSet + set.setDrawCircles(!set.isDrawCirclesEnabled) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleCubic: { - List sets = chart.getData() - .getDataSets(); - - for (ILineDataSet iSet : sets) { - LineDataSet set = (LineDataSet) iSet; - set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER - ? LineDataSet.Mode.LINEAR - : LineDataSet.Mode.CUBIC_BEZIER); + R.id.actionToggleCubic -> { + val sets: MutableList = chart!!.data!! + .dataSets + + for (iSet in sets) { + val set = iSet as LineDataSet + set.setMode( + if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) + LineDataSet.Mode.LINEAR + else + LineDataSet.Mode.CUBIC_BEZIER + ) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleStepped: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { - - LineDataSet set = (LineDataSet) iSet; - set.setMode(set.getMode() == LineDataSet.Mode.STEPPED - ? LineDataSet.Mode.LINEAR - : LineDataSet.Mode.STEPPED); + R.id.actionToggleStepped -> { + val sets: MutableList = chart!!.data!! + .dataSets + + for (iSet in sets) { + val set = iSet as LineDataSet + set.setMode( + if (set.mode == LineDataSet.Mode.STEPPED) + LineDataSet.Mode.LINEAR + else + LineDataSet.Mode.STEPPED + ) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHorizontalCubic: { - List sets = chart.getData() - .getDataSets(); - - for (ILineDataSet iSet : sets) { - LineDataSet set = (LineDataSet) iSet; - set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER - ? LineDataSet.Mode.LINEAR - : LineDataSet.Mode.HORIZONTAL_BEZIER); + R.id.actionToggleHorizontalCubic -> { + val sets: MutableList = chart!!.data!! + .dataSets + + for (iSet in sets) { + val set = iSet as LineDataSet + set.setMode( + if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) + LineDataSet.Mode.LINEAR + else + LineDataSet.Mode.HORIZONTAL_BEZIER + ) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionTogglePinch: { - chart.setPinchZoom(!chart.isPinchZoomEnabled()); - chart.invalidate(); - break; + R.id.actionTogglePinch -> { + chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); - - setData(seekBarX.getProgress(), seekBarY.getProgress()); + setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) // redraw - chart.invalidate(); + chart!!.invalidate() } - @Override - protected void saveToGallery() { - saveToGallery(chart, "LineChartActivity2"); + override fun saveToGallery() { + saveToGallery(chart, "LineChartActivity2") } - @Override - public void onValueSelected(Entry e, Highlight h) { - Log.i("Entry selected", e.toString()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null || h == null) return + + Log.i("Entry selected", e.toString()) - chart.centerViewToAnimated(e.getX(), e.getY(), chart.getData().getDataSetByIndex(h.getDataSetIndex()).axisDependency, 500); + chart!!.centerViewToAnimated(e.x, e.y, chart!!.data!!.getDataSetByIndex(h.dataSetIndex).axisDependency, 500) //chart.zoomAndCenterAnimated(2.5f, 2.5f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex) // .getAxisDependency(), 1000); //chart.zoomAndCenterAnimated(1.8f, 1.8f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex) // .getAxisDependency(), 1000); } - @Override - public void onNothingSelected() { - Log.i("Nothing selected", "Nothing selected."); + override fun onNothingSelected() { + Log.i("Nothing selected", "Nothing selected.") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/LineChartTime.kt b/app/src/main/java/info/appdev/chartexample/LineChartTime.kt index d76e39baf5..4f807fc867 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartTime.kt +++ b/app/src/main/java/info/appdev/chartexample/LineChartTime.kt @@ -1,323 +1,290 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.AxisBase; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Locale; -import java.util.concurrent.TimeUnit; - -public class LineChartTime extends DemoBase implements OnSeekBarChangeListener { - - private LineChart chart; - private SeekBar seekBarX; - private TextView tvX; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_linechart_time); - - setTitle("LineChartTime"); - - tvX = findViewById(R.id.tvXMax); - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.AxisBase +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.utils.ColorTemplate.holoBlue +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale +import java.util.concurrent.TimeUnit +import kotlin.math.roundToInt + +class LineChartTime : DemoBase(), OnSeekBarChangeListener { + private var chart: LineChart? = null + private var seekBarX: SeekBar? = null + private var tvX: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_linechart_time) + + setTitle("LineChartTime") + + tvX = findViewById(R.id.tvXMax) + seekBarX = findViewById(R.id.seekBarX) + seekBarX!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) // no description text - chart.getDescription().setEnabled(false); + chart!!.description.isEnabled = false // enable touch gestures - chart.setTouchEnabled(true); + chart!!.setTouchEnabled(true) - chart.setDragDecelerationFrictionCoef(0.9f); + chart!!.dragDecelerationFrictionCoef = 0.9f // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); - chart.setDrawGridBackground(false); - chart.setHighlightPerDragEnabled(true); + chart!!.isDragEnabled = true + chart!!.setScaleEnabled(true) + chart!!.setDrawGridBackground(false) + chart!!.isHighlightPerDragEnabled = true // set an alternative background color - chart.setBackgroundColor(Color.WHITE); - chart.setViewPortOffsets(0f, 0f, 0f, 0f); + chart!!.setBackgroundColor(Color.WHITE) + chart!!.setViewPortOffsets(0f, 0f, 0f, 0f) // add data - seekBarX.setProgress(100); + seekBarX!!.progress = 100 // get the legend (only possible after setting data) - Legend l = chart.getLegend(); - l.setEnabled(false); - - XAxis xAxis = chart.getXAxis(); - xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE); - xAxis.setTypeface(tfLight); - xAxis.setTextSize(10f); - xAxis.setTextColor(Color.WHITE); - xAxis.setDrawAxisLine(false); - xAxis.setDrawGridLines(true); - xAxis.setTextColor(Color.rgb(255, 192, 56)); - xAxis.setCenterAxisLabels(true); - xAxis.setGranularity(1f); // one hour - xAxis.setValueFormatter(new IAxisValueFormatter() { - - private final SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH); - - @Override - public String getFormattedValue(float value, AxisBase axis) { - - long millis = TimeUnit.HOURS.toMillis((long) value); - return mFormat.format(new Date(millis)); + val l = chart!!.legend + l.isEnabled = false + + val xAxis = chart!!.xAxis + xAxis.position = XAxisPosition.TOP_INSIDE + xAxis.typeface = tfLight + xAxis.textSize = 10f + xAxis.textColor = Color.WHITE + xAxis.setDrawAxisLine(false) + xAxis.setDrawGridLines(true) + xAxis.textColor = Color.rgb(255, 192, 56) + xAxis.setCenterAxisLabels(true) + xAxis.granularity = 1f // one hour + xAxis.valueFormatter = object : IAxisValueFormatter { + private val mFormat = SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH) + + override fun getFormattedValue(value: Float, axis: AxisBase?): String { + val millis = TimeUnit.HOURS.toMillis(value.toLong()) + return mFormat.format(Date(millis)) } - }); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART); - leftAxis.setTypeface(tfLight); - leftAxis.setTextColor(ColorTemplate.getHoloBlue()); - leftAxis.setDrawGridLines(true); - leftAxis.setGranularityEnabled(true); - leftAxis.setAxisMinimum(0f); - leftAxis.setAxisMaximum(170f); - leftAxis.setYOffset(-9f); - leftAxis.setTextColor(Color.rgb(255, 192, 56)); - - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setEnabled(false); - } + } - private void setData(int count) { + val leftAxis = chart!!.axisLeft + leftAxis.setPosition(YAxisLabelPosition.INSIDE_CHART) + leftAxis.typeface = tfLight + leftAxis.textColor = holoBlue + leftAxis.setDrawGridLines(true) + leftAxis.isGranularityEnabled = true + leftAxis.axisMinimum = 0f + leftAxis.axisMaximum = 170f + leftAxis.yOffset = -9f + leftAxis.textColor = Color.rgb(255, 192, 56) + + val rightAxis = chart!!.axisRight + rightAxis.isEnabled = false + } + private fun setData(count: Int) { // now in hours - long now = 0; //470044; //TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis()); - ArrayList values = new ArrayList<>(); + val now: Long = 0 //470044; //TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis()); + + val values = ArrayList() // count = hours - float to = now + count; + val to = (now + count).toFloat() - Double[] valuesData = DataTools.Companion.getValues(Math.round(to)); + val valuesData = getValues(to.roundToInt()) // increment by 1 hour - for (float x = now; x < to; x++) { - float y; - if (count == 100) // initial - y = (valuesData[Math.round(x)]).floatValue() * 50 + 50; - else - y = (float) (Math.random() * 50 + 50); // manually triggered - values.add(new Entry(x, y)); // add one entry per hour + var x = now.toFloat() + while (x < to) { + val y = if (count == 100) // initial + (valuesData[x.roundToInt()]).toFloat() * 50 + 50 + else (Math.random() * 50 + 50).toFloat() // manually triggered + + values.add(Entry(x, y)) // add one entry per hour + x++ } // create a dataset and give it a type - LineDataSet set1 = new LineDataSet(values, "DataSet 1"); - set1.setAxisDependency(AxisDependency.LEFT); - set1.setColor(ColorTemplate.getHoloBlue()); - set1.setValueTextColor(ColorTemplate.getHoloBlue()); - set1.setLineWidth(1.5f); - set1.setDrawCircles(false); - set1.setDrawValues(false); - set1.setFillAlpha(65); - set1.setFillColor(ColorTemplate.getHoloBlue()); - set1.setHighLightColor(Color.rgb(244, 117, 117)); - set1.setDrawCircleHole(false); + val set1 = LineDataSet(values, "DataSet 1") + set1.axisDependency = AxisDependency.LEFT + set1.setColor(holoBlue) + set1.valueTextColor = holoBlue + set1.setLineWidth(1.5f) + set1.setDrawCircles(false) + set1.isDrawValuesEnabled = false + set1.setFillAlpha(65) + set1.setFillColor(holoBlue) + set1.highLightColor = Color.rgb(244, 117, 117) + set1.setDrawCircleHole(false) // create a data object with the data sets - LineData data = new LineData(set1); - data.setValueTextColor(Color.WHITE); - data.setValueTextSize(9f); + val data = LineData(set1) + data.setValueTextColor(Color.WHITE) + data.setValueTextSize(9f) // set data - chart.setData(data); + chart!!.setData(data) } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.line, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.line, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/LineChartTime.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleValues -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - set.setDrawValues(!set.isDrawValuesEnabled()); + for (iSet in sets) { + val set = iSet as LineDataSet + set.isDrawValuesEnabled = !set.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if (chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionToggleFilled: { - - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleFilled -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - if (set.isDrawFilledEnabled()) - set.setDrawFilled(false); - else - set.setDrawFilled(true); + for (iSet in sets) { + val set = iSet as LineDataSet + if (set.isDrawFilledEnabled) set.setDrawFilled(false) + else set.setDrawFilled(true) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleCircles: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleCircles -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - if (set.isDrawCirclesEnabled()) - set.setDrawCircles(false); - else - set.setDrawCircles(true); + for (iSet in sets) { + val set = iSet as LineDataSet + if (set.isDrawCirclesEnabled) set.setDrawCircles(false) + else set.setDrawCircles(true) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleCubic: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleCubic -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - if (set.getMode() == LineDataSet.Mode.CUBIC_BEZIER) - set.setMode(LineDataSet.Mode.LINEAR); - else - set.setMode(LineDataSet.Mode.CUBIC_BEZIER); + for (iSet in sets) { + val set = iSet as LineDataSet + if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) set.setMode(LineDataSet.Mode.LINEAR) + else set.setMode(LineDataSet.Mode.CUBIC_BEZIER) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleStepped: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleStepped -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - if (set.getMode() == LineDataSet.Mode.STEPPED) - set.setMode(LineDataSet.Mode.LINEAR); - else - set.setMode(LineDataSet.Mode.STEPPED); + for (iSet in sets) { + val set = iSet as LineDataSet + if (set.mode == LineDataSet.Mode.STEPPED) set.setMode(LineDataSet.Mode.LINEAR) + else set.setMode(LineDataSet.Mode.STEPPED) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) - chart.setPinchZoom(false); - else - chart.setPinchZoom(true); - - chart.invalidate(); - break; + + R.id.actionTogglePinch -> { + if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) + else chart!!.setPinchZoom(true) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } - case R.id.actionSave: { + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() - setData(seekBarX.getProgress()); + setData(seekBarX!!.progress) // redraw - chart.invalidate(); + chart!!.invalidate() } - @Override - protected void saveToGallery() { - saveToGallery(chart, "LineChartTime"); + override fun saveToGallery() { + saveToGallery(chart, "LineChartTime") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt index 32da5e1957..2de9774f99 100644 --- a/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt @@ -1,36 +1,28 @@ - -package info.appdev.chartexample; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.content.Intent; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.annotation.NonNull; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.view.WindowManager; -import android.widget.ArrayAdapter; -import android.widget.ListView; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; +package info.appdev.chartexample + +import android.annotation.SuppressLint +import android.content.Context +import android.content.Intent +import android.graphics.Color +import android.os.Bundle +import android.view.LayoutInflater +import android.view.Menu +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import android.view.WindowManager +import android.widget.ArrayAdapter +import android.widget.ListView +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase /** * Demonstrates the use of charts inside a ListView. IMPORTANT: provide a @@ -38,96 +30,87 @@ import java.util.List; * * @author Philipp Jahoda */ -public class ListViewBarChartActivity extends DemoBase { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_listview_chart); +class ListViewBarChartActivity : DemoBase() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_listview_chart) - setTitle("ListViewBarChartActivity"); + setTitle("ListViewBarChartActivity") - ListView lv = findViewById(R.id.listViewMain); + val lv = findViewById(R.id.listViewMain) - ArrayList list = new ArrayList<>(); + val list = ArrayList() // 20 items - for (int i = 0; i < 20; i++) { - list.add(generateData(i + 1)); + for (i in 0..19) { + list.add(generateData(i + 1)) } - ChartDataAdapter cda = new ChartDataAdapter(getApplicationContext(), list); - lv.setAdapter(cda); + val cda = ChartDataAdapter(applicationContext, list) + lv!!.setAdapter(cda) } - private class ChartDataAdapter extends ArrayAdapter { - - ChartDataAdapter(Context context, List objects) { - super(context, 0, objects); - } - + private inner class ChartDataAdapter(context: Context, objects: MutableList) : ArrayAdapter(context, 0, objects) { @SuppressLint("InflateParams") - @NonNull - @Override - public View getView(int position, View convertView, @NonNull ViewGroup parent) { - - BarData data = getItem(position); + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + var convertView = convertView + val data = getItem(position) - ViewHolder holder; + val holder: ViewHolder if (convertView == null) { + holder = ViewHolder() - holder = new ViewHolder(); - - convertView = LayoutInflater.from(getContext()).inflate( - R.layout.list_item_barchart, null); - holder.chart = convertView.findViewById(R.id.chart); - - convertView.setTag(holder); + convertView = LayoutInflater.from(context).inflate( + R.layout.list_item_barchart, null + ) + holder.chart = convertView.findViewById(R.id.chart) + convertView.tag = holder } else { - holder = (ViewHolder) convertView.getTag(); + holder = convertView.tag as ViewHolder } // apply styling if (data != null) { - data.setValueTypeface(tfLight); - data.setValueTextColor(Color.BLACK); + data.setValueTypeface(tfLight) + data.setValueTextColor(Color.BLACK) } - holder.chart.getDescription().setEnabled(false); - holder.chart.setDrawGridBackground(false); + holder.chart!!.description.isEnabled = false + holder.chart!!.setDrawGridBackground(false) - XAxis xAxis = holder.chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setTypeface(tfLight); - xAxis.setDrawGridLines(false); + val xAxis = holder.chart!!.xAxis + xAxis.position = XAxisPosition.BOTTOM + xAxis.typeface = tfLight + xAxis.setDrawGridLines(false) - YAxis leftAxis = holder.chart.getAxisLeft(); - leftAxis.setTypeface(tfLight); - leftAxis.setLabelCount(5, false); - leftAxis.setSpaceTop(15f); + val leftAxis = holder.chart!!.axisLeft + leftAxis.typeface = tfLight + leftAxis.setLabelCount(5, false) + leftAxis.spaceTop = 15f - YAxis rightAxis = holder.chart.getAxisRight(); - rightAxis.setTypeface(tfLight); - rightAxis.setLabelCount(5, false); - rightAxis.setSpaceTop(15f); + val rightAxis = holder.chart!!.axisRight + rightAxis.typeface = tfLight + rightAxis.setLabelCount(5, false) + rightAxis.spaceTop = 15f // set data - holder.chart.setData(data); - holder.chart.setFitBars(true); + holder.chart!!.setData(data) + holder.chart!!.setFitBars(true) // do not forget to refresh the chart // holder.chart.invalidate(); - holder.chart.animateY(700); + holder.chart!!.animateY(700) - return convertView; + return convertView } - private class ViewHolder { - - BarChart chart; + private inner class ViewHolder { + var chart: BarChart? = null } } @@ -136,48 +119,44 @@ public class ListViewBarChartActivity extends DemoBase { * * @return Bar data */ - private BarData generateData(int cnt) { - int count = 12; - ArrayList entries = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(count); + private fun generateData(cnt: Int): BarData { + val count = 12 + val entries = ArrayList() + val sampleValues = getValues(count) - for (int i = 0; i < count; i++) { - entries.add(new BarEntry(i, (sampleValues[i].floatValue() * 70) + 30)); + for (i in 0.. sets = new ArrayList<>(); - sets.add(d); + val sets = ArrayList() + sets.add(d) - BarData cd = new BarData(sets); - cd.setBarWidth(0.9f); - return cd; + val cd = BarData(sets) + cd.barWidth = 0.9f + return cd } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.only_github, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.only_github, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java".toUri()) + startActivity(i) } } - return true; + return true } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } } diff --git a/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt b/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt index 3f086359a3..2bf1bde94c 100644 --- a/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt @@ -1,40 +1,34 @@ - -package info.appdev.chartexample; - -import android.content.Context; -import android.content.Intent; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.annotation.NonNull; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.view.WindowManager; -import android.widget.ArrayAdapter; -import android.widget.ListView; - -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.data.PieDataSet; -import com.github.mikephil.charting.data.PieEntry; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.listviewitems.BarChartItem; -import info.appdev.chartexample.listviewitems.ChartItem; -import info.appdev.chartexample.listviewitems.LineChartItem; -import info.appdev.chartexample.listviewitems.PieChartItem; -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; +package info.appdev.chartexample + +import android.content.Context +import android.content.Intent +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import android.view.WindowManager +import android.widget.ArrayAdapter +import android.widget.ListView +import androidx.core.net.toUri +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.data.PieDataSet +import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.listviewitems.BarChartItem +import info.appdev.chartexample.listviewitems.ChartItem +import info.appdev.chartexample.listviewitems.LineChartItem +import info.appdev.chartexample.listviewitems.PieChartItem +import info.appdev.chartexample.notimportant.DemoBase /** * Demonstrates the use of charts inside a ListView. IMPORTANT: provide a @@ -42,61 +36,50 @@ import java.util.List; * * @author Philipp Jahoda */ -public class ListViewMultiChartActivity extends DemoBase { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_listview_chart); +class ListViewMultiChartActivity : DemoBase() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_listview_chart) - setTitle("ListViewMultiChartActivity"); + setTitle("ListViewMultiChartActivity") - ListView lv = findViewById(R.id.listViewMain); + val lv = findViewById(R.id.listViewMain) - ArrayList list = new ArrayList<>(); + val list = ArrayList() // 30 items - for (int i = 0; i < 30; i++) { - - if(i % 3 == 0) { - list.add(new LineChartItem(generateDataLine(i + 1), getApplicationContext())); - } else if(i % 3 == 1) { - list.add(new BarChartItem(generateDataBar(i + 1), getApplicationContext())); - } else if(i % 3 == 2) { - list.add(new PieChartItem(generateDataPie(), getApplicationContext())); + for (i in 0..29) { + if (i % 3 == 0) { + list.add(LineChartItem(generateDataLine(i + 1), applicationContext)) + } else if (i % 3 == 1) { + list.add(BarChartItem(generateDataBar(i + 1), applicationContext)) + } else { + list.add(PieChartItem(generateDataPie(), applicationContext)) } } - ChartDataAdapter cda = new ChartDataAdapter(getApplicationContext(), list); - lv.setAdapter(cda); + val cda = ChartDataAdapter(applicationContext, list) + lv!!.setAdapter(cda) } - /** adapter that supports 3 different item types */ - private class ChartDataAdapter extends ArrayAdapter { - - ChartDataAdapter(Context context, List objects) { - super(context, 0, objects); - } - - @NonNull - @Override - public View getView(int position, View convertView, @NonNull ViewGroup parent) { - //noinspection ConstantConditions - return getItem(position).getView(position, convertView, getContext()); + /** adapter that supports 3 different item types */ + private inner class ChartDataAdapter(context: Context, objects: MutableList) : ArrayAdapter(context, 0, objects) { + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + return getItem(position)!!.getView(position, convertView, context) } - @Override - public int getItemViewType(int position) { + override fun getItemViewType(position: Int): Int { // return the views type - ChartItem ci = getItem(position); - return ci != null ? ci.getItemType() : 0; + val ci = getItem(position) + return ci?.itemType ?: 0 } - @Override - public int getViewTypeCount() { - return 3; // we have 3 different item-types + override fun getViewTypeCount(): Int { + return 3 // we have 3 different item-types } } @@ -105,41 +88,40 @@ public class ListViewMultiChartActivity extends DemoBase { * * @return Line data */ - private LineData generateDataLine(int cnt) { - - ArrayList values1 = new ArrayList<>(); - int count = 12; - Double[] sampleValues = DataTools.Companion.getValues(count); + private fun generateDataLine(cnt: Int): LineData { + val values1 = ArrayList() + val count = 12 + val sampleValues = getValues(count) - for (int i = 0; i < count; i++) { - values1.add(new Entry(i, (int) (sampleValues[i].floatValue() * 65) + 40)); + for (i in 0.. values2 = new ArrayList<>(); + val values2 = ArrayList() - for (int i = 0; i < count; i++) { - values2.add(new Entry(i, values1.get(i).getY() - 30)); + for (i in 0.. sets = new ArrayList<>(); - sets.add(d1); - sets.add(d2); + val sets = ArrayList() + sets.add(d1) + sets.add(d2) - return new LineData(sets); + return LineData(sets) } /** @@ -147,22 +129,22 @@ public class ListViewMultiChartActivity extends DemoBase { * * @return Bar data */ - private BarData generateDataBar(int cnt) { - int count = 12; - ArrayList entries = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(count); + private fun generateDataBar(cnt: Int): BarData { + val count = 12 + val entries = ArrayList() + val sampleValues = getValues(count) - for (int i = 0; i < count; i++) { - entries.add(new BarEntry(i, (int) (sampleValues[i].floatValue() * 70) + 30)); + for (i in 0.. entries = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(cnt); + private fun generateDataPie(): PieData { + val cnt = 4 + val entries = ArrayList() + val sampleValues = getValues(cnt) - for (int i = 0; i < cnt; i++) { - entries.add(new PieEntry((sampleValues[i].floatValue() * 70) + 30, "Quarter " + (i+1))); + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/ListViewMultiChartActivity.java".toUri()) + startActivity(i) } } - return true; + return true } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } } diff --git a/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt index 83c8e8989c..2128af91f8 100644 --- a/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt @@ -1,356 +1,327 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.MotionEvent; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.listener.ChartTouchListener; -import com.github.mikephil.charting.listener.OnChartGestureListener; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; - -public class MultiLineChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartGestureListener, OnChartValueSelectedListener { - - private LineChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_linechart); - - setTitle("MultiLineChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setOnSeekBarChangeListener(this); - - seekBarY = findViewById(R.id.seekBarY); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); - - chart.setDrawGridBackground(false); - chart.getDescription().setEnabled(false); - chart.setDrawBorders(false); - - chart.getAxisLeft().setEnabled(false); - chart.getAxisRight().setDrawAxisLine(false); - chart.getAxisRight().setDrawGridLines(false); - chart.getXAxis().setDrawAxisLine(false); - chart.getXAxis().setDrawGridLines(false); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.MotionEvent +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture +import com.github.mikephil.charting.listener.OnChartGestureListener +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestureListener, OnChartValueSelectedListener { + private var chart: LineChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_linechart) + + setTitle("MultiLineChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarX!!.setOnSeekBarChangeListener(this) + + seekBarY = findViewById(R.id.seekBarY) + seekBarY!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) + + chart!!.setDrawGridBackground(false) + chart!!.description.isEnabled = false + chart!!.setDrawBorders(false) + + chart!!.axisLeft.isEnabled = false + chart!!.axisRight.setDrawAxisLine(false) + chart!!.axisRight.setDrawGridLines(false) + chart!!.xAxis.setDrawAxisLine(false) + chart!!.xAxis.setDrawGridLines(false) // enable touch gestures - chart.setTouchEnabled(true); + chart!!.setTouchEnabled(true) // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); + chart!!.isDragEnabled = true + chart!!.setScaleEnabled(true) // if disabled, scaling can be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) - seekBarX.setProgress(20); - seekBarY.setProgress(100); + seekBarX!!.progress = 20 + seekBarY!!.progress = 100 - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) } - private final int[] colors = new int[] { - ColorTemplate.VORDIPLOM_COLORS[0], - ColorTemplate.VORDIPLOM_COLORS[1], - ColorTemplate.VORDIPLOM_COLORS[2] - }; - - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + private val colors = intArrayOf( + ColorTemplate.VORDIPLOM_COLORS[0], + ColorTemplate.VORDIPLOM_COLORS[1], + ColorTemplate.VORDIPLOM_COLORS[2] + ) - chart.resetTracking(); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + chart!!.resetTracking() - progress = seekBarX.getProgress(); + val progress: Int = seekBarX!!.progress - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - ArrayList dataSets = new ArrayList<>(); + val dataSets = ArrayList() - for (int z = 0; z < 3; z++) { + for (z in 0..2) { + val values = ArrayList() + val sampleValues = getValues(100) - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100); - - for (int i = 0; i < progress; i++) { - double val = (sampleValues[i].floatValue() * seekBarY.getProgress()) + 3; - values.add(new Entry(i, (float) val)); + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/MultiLineChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleValues -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - set.setDrawValues(!set.isDrawValuesEnabled()); + for (iSet in sets) { + val set = iSet as LineDataSet + set.isDrawValuesEnabled = !set.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - /* - case R.id.actionToggleIcons: { break; } - */ - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) - chart.setPinchZoom(false); - else - chart.setPinchZoom(true); - - chart.invalidate(); - break; + + R.id.actionTogglePinch -> { + if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) + else chart!!.setPinchZoom(true) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.actionToggleHighlight: { - if(chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionToggleFilled: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleFilled -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - if (set.isDrawFilledEnabled()) - set.setDrawFilled(false); - else - set.setDrawFilled(true); + for (iSet in sets) { + val set = iSet as LineDataSet + if (set.isDrawFilledEnabled) set.setDrawFilled(false) + else set.setDrawFilled(true) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleCircles: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { + R.id.actionToggleCircles -> { + val sets: MutableList = chart!!.data!! + .dataSets - LineDataSet set = (LineDataSet) iSet; - if (set.isDrawCirclesEnabled()) - set.setDrawCircles(false); - else - set.setDrawCircles(true); + for (iSet in sets) { + val set = iSet as LineDataSet + if (set.isDrawCirclesEnabled) set.setDrawCircles(false) + else set.setDrawCircles(true) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleCubic: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { - - LineDataSet set = (LineDataSet) iSet; - set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER - ? LineDataSet.Mode.LINEAR - : LineDataSet.Mode.CUBIC_BEZIER); + R.id.actionToggleCubic -> { + val sets: MutableList = chart!!.data!! + .dataSets + + for (iSet in sets) { + val set = iSet as LineDataSet + set.setMode( + if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) + LineDataSet.Mode.LINEAR + else + LineDataSet.Mode.CUBIC_BEZIER + ) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleStepped: { - List sets = chart.getData() - .getDataSets(); - - for (ILineDataSet iSet : sets) { - LineDataSet set = (LineDataSet) iSet; - set.setMode(set.getMode() == LineDataSet.Mode.STEPPED - ? LineDataSet.Mode.LINEAR - : LineDataSet.Mode.STEPPED); + R.id.actionToggleStepped -> { + val sets: MutableList = chart!!.data!! + .dataSets + + for (iSet in sets) { + val set = iSet as LineDataSet + set.setMode( + if (set.mode == LineDataSet.Mode.STEPPED) + LineDataSet.Mode.LINEAR + else + LineDataSet.Mode.STEPPED + ) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHorizontalCubic: { - List sets = chart.getData() - .getDataSets(); - for (ILineDataSet iSet : sets) { - - LineDataSet set = (LineDataSet) iSet; - set.setMode(set.getMode() == LineDataSet.Mode.HORIZONTAL_BEZIER - ? LineDataSet.Mode.LINEAR - : LineDataSet.Mode.HORIZONTAL_BEZIER); + R.id.actionToggleHorizontalCubic -> { + val sets: MutableList = chart!!.data!! + .dataSets + + for (iSet in sets) { + val set = iSet as LineDataSet + set.setMode( + if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) + LineDataSet.Mode.LINEAR + else + LineDataSet.Mode.HORIZONTAL_BEZIER + ) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "MultiLineChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "MultiLineChartActivity") } - @Override - public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { - Log.i("Gesture", "START, x: " + me.getX() + ", y: " + me.getY()); + override fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + Log.i("Gesture", "START, x: " + me?.x + ", y: " + me?.y) } - @Override - public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { - Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture); + override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + Log.i("Gesture", "END, lastGesture: $lastPerformedGesture") // un-highlight values after the gesture is finished and no single-tap - if(lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP) - chart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(...) + if (lastPerformedGesture != ChartGesture.SINGLE_TAP) chart!!.highlightValues(null) // or highlightTouch(null) for callback to onNothingSelected(...) } - @Override - public void onChartLongPressed(MotionEvent me) { - Log.i("LongPress", "Chart long pressed."); + override fun onChartLongPressed(me: MotionEvent?) { + Log.i("LongPress", "Chart long pressed.") } - @Override - public void onChartDoubleTapped(MotionEvent me) { - Log.i("DoubleTap", "Chart double-tapped."); + override fun onChartDoubleTapped(me: MotionEvent?) { + Log.i("DoubleTap", "Chart double-tapped.") } - @Override - public void onChartSingleTapped(MotionEvent me) { - Log.i("SingleTap", "Chart single-tapped."); + override fun onChartSingleTapped(me: MotionEvent?) { + Log.i("SingleTap", "Chart single-tapped.") } - @Override - public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { - Log.i("Fling", "Chart fling. VelocityX: " + velocityX + ", VelocityY: " + velocityY); + override fun onChartFling(me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float) { + Log.i("Fling", "Chart fling. VelocityX: $velocityX, VelocityY: $velocityY") } - @Override - public void onChartScale(MotionEvent me, float scaleX, float scaleY) { - Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY); + override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) { + Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY") } - @Override - public void onChartTranslate(MotionEvent me, float dX, float dY) { - Log.i("Translate / Move", "dX: " + dX + ", dY: " + dY); + override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) { + Log.i("Translate / Move", "dX: $dX, dY: $dY") } - @Override - public void onValueSelected(Entry e, Highlight h) { - Log.i("VAL SELECTED", - "Value: " + e.getY() + ", xIndex: " + e.getX() - + ", DataSet index: " + h.getDataSetIndex()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + Log.i( + "VAL SELECTED", + ("Value: " + e?.y + ", xIndex: " + e?.x + + ", DataSet index: " + h?.dataSetIndex) + ) } - @Override - public void onNothingSelected() {} + override fun onNothingSelected() {} - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt b/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt index 8569193f9b..c3dae688eb 100644 --- a/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt +++ b/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt @@ -1,146 +1,131 @@ - -package info.appdev.chartexample; - -import android.content.Intent; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -@SuppressWarnings("SameParameterValue") -public class PerformanceLineChart extends DemoBase implements OnSeekBarChangeListener { - - private LineChart chart; - private SeekBar seekBarValues; - private TextView tvCount; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_performance_linechart); - - setTitle("PerformanceLineChart"); - - tvCount = findViewById(R.id.tvValueCount); - seekBarValues = findViewById(R.id.seekbarValues); - seekBarValues.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setDrawGridBackground(false); +package info.appdev.chartexample + +import android.content.Intent +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import info.appdev.chartexample.DataTools.Companion.getMuchValues +import info.appdev.chartexample.notimportant.DemoBase + +class PerformanceLineChart : DemoBase(), OnSeekBarChangeListener { + private var chart: LineChart? = null + private var seekBarValues: SeekBar? = null + private var tvCount: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_performance_linechart) + + setTitle("PerformanceLineChart") + + tvCount = findViewById(R.id.tvValueCount) + seekBarValues = findViewById(R.id.seekbarValues) + seekBarValues!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setDrawGridBackground(false) // no description text - chart.getDescription().setEnabled(false); + chart!!.description.isEnabled = false // enable touch gestures - chart.setTouchEnabled(true); + chart!!.setTouchEnabled(true) // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); + chart!!.isDragEnabled = true + chart!!.setScaleEnabled(true) // if disabled, scaling can be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) - chart.getAxisLeft().setDrawGridLines(false); - chart.getAxisRight().setEnabled(false); - chart.getXAxis().setDrawGridLines(true); - chart.getXAxis().setDrawAxisLine(false); + chart!!.axisLeft.setDrawGridLines(false) + chart!!.axisRight.isEnabled = false + chart!!.xAxis.setDrawGridLines(true) + chart!!.xAxis.setDrawAxisLine(false) - seekBarValues.setProgress(9000); + seekBarValues!!.progress = 9000 // don't forget to refresh the drawing - chart.invalidate(); + chart!!.invalidate() } - private void setData(int count, float range) { - - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getMuchValues(count); + private fun setData(count: Int, range: Float) { + val values = ArrayList() + val sampleValues = getMuchValues(count) - for (int i = 0; i < count; i++) { - float val = (sampleValues[i].floatValue() * (range + 1)) + 3; - values.add(new Entry(i * 0.001f, val)); + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/PerformanceLineChart.java".toUri()) + startActivity(i) } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - int count = seekBarValues.getProgress() + 1000; - tvCount.setText(String.valueOf(count)); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + val count = seekBarValues!!.progress + 1000 + tvCount!!.text = count.toString() - chart.resetTracking(); + chart!!.resetTracking() - setData(count, 500f); + setData(count, 500f) // redraw - chart.invalidate(); + chart!!.invalidate() } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt b/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt index a75eef0332..8e73817014 100644 --- a/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt @@ -1,322 +1,301 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.graphics.Typeface; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.RelativeSizeSpan; -import android.text.style.StyleSpan; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.animation.Easing; -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.data.PieDataSet; -import com.github.mikephil.charting.data.PieEntry; -import com.github.mikephil.charting.formatter.PercentFormatter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.MPPointF; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -public class PieChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private PieChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_piechart); - - setTitle("PieChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarY = findViewById(R.id.seekBarY); - - seekBarX.setOnSeekBarChangeListener(this); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setUsePercentValues(true); - chart.getDescription().setEnabled(false); - chart.setExtraOffsets(5, 10, 5, 5); - - chart.setDragDecelerationFrictionCoef(0.95f); - - chart.setCenterTextTypeface(tfLight); - chart.setCenterText(generateCenterSpannableText()); - - chart.setDrawHoleEnabled(true); - chart.setHoleColor(Color.WHITE); - - chart.setTransparentCircleColor(Color.WHITE); - chart.setTransparentCircleAlpha(110); - - chart.setHoleRadius(58f); - chart.setTransparentCircleRadius(61f); - - chart.setDrawCenterText(true); - - chart.setRotationAngle(0); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.text.style.RelativeSizeSpan +import android.text.style.StyleSpan +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.animation.Easing +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.data.PieDataSet +import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.formatter.PercentFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.ColorTemplate.holoBlue +import com.github.mikephil.charting.utils.MPPointF +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: PieChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_piechart) + + setTitle("PieChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarY = findViewById(R.id.seekBarY) + + seekBarX!!.setOnSeekBarChangeListener(this) + seekBarY!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setUsePercentValues(true) + chart!!.description.isEnabled = false + chart!!.setExtraOffsets(5f, 10f, 5f, 5f) + + chart!!.dragDecelerationFrictionCoef = 0.95f + + chart!!.setCenterTextTypeface(tfLight) + chart!!.centerText = generateCenterSpannableText() + + chart!!.isDrawHoleEnabled = true + chart!!.setHoleColor(Color.WHITE) + + chart!!.setTransparentCircleColor(Color.WHITE) + chart!!.setTransparentCircleAlpha(110) + + chart!!.holeRadius = 58f + chart!!.transparentCircleRadius = 61f + + chart!!.setDrawCenterText(true) + + chart!!.rotationAngle = 0f // enable rotation of the chart by touch - chart.setRotationEnabled(true); - chart.setHighlightPerTapEnabled(true); + chart!!.isRotationEnabled = true + chart!!.isHighlightPerTapEnabled = true // chart.setUnit(" €"); // chart.setDrawUnitsInChart(true); // add a selection listener - chart.setOnChartValueSelectedListener(this); + chart!!.setOnChartValueSelectedListener(this) - seekBarX.setProgress(4); - seekBarY.setProgress(10); + seekBarX!!.progress = 4 + seekBarY!!.progress = 10 - chart.animateY(1400, Easing.EaseInOutQuad); - // chart.spin(2000, 0, 360); + chart!!.animateY(1400, Easing.EaseInOutQuad) - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - l.setXEntrySpace(7f); - l.setYEntrySpace(0f); - l.setYOffset(0f); + // chart.spin(2000, 0, 360); + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) + l.xEntrySpace = 7f + l.yEntrySpace = 0f + l.yOffset = 0f // entry label styling - chart.setEntryLabelColor(Color.WHITE); - chart.setEntryLabelTypeface(tfRegular); - chart.setEntryLabelTextSize(12f); + chart!!.setEntryLabelColor(Color.WHITE) + chart!!.setEntryLabelTypeface(tfRegular) + chart!!.setEntryLabelTextSize(12f) } - private void setData(int count, float range) { - ArrayList entries = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100); + private fun setData(count: Int, range: Float) { + val entries = ArrayList() + val sampleValues = getValues(100) // NOTE: The order of the entries when being added to the entries array determines their position around the center of // the chart. - for (int i = 0; i < count ; i++) { - entries.add(new PieEntry((sampleValues[i].floatValue() * range) + range / 5, - parties[i % parties.length], - getResources().getDrawable(R.drawable.star))); + for (i in 0..() - ArrayList colors = new ArrayList<>(); + for (c in ColorTemplate.VORDIPLOM_COLORS) colors.add(c) - for (int c : ColorTemplate.VORDIPLOM_COLORS) - colors.add(c); + for (c in ColorTemplate.JOYFUL_COLORS) colors.add(c) - for (int c : ColorTemplate.JOYFUL_COLORS) - colors.add(c); + for (c in ColorTemplate.COLORFUL_COLORS) colors.add(c) - for (int c : ColorTemplate.COLORFUL_COLORS) - colors.add(c); + for (c in ColorTemplate.LIBERTY_COLORS) colors.add(c) - for (int c : ColorTemplate.LIBERTY_COLORS) - colors.add(c); + for (c in ColorTemplate.PASTEL_COLORS) colors.add(c) - for (int c : ColorTemplate.PASTEL_COLORS) - colors.add(c); + colors.add(holoBlue) - colors.add(ColorTemplate.getHoloBlue()); + dataSet.setColors(colors) - dataSet.setColors(colors); //dataSet.setSelectionShift(0f); - - PieData data = new PieData(dataSet); - data.setValueFormatter(new PercentFormatter()); - data.setValueTextSize(11f); - data.setValueTextColor(Color.WHITE); - data.setValueTypeface(tfLight); - chart.setData(data); + val data = PieData(dataSet) + data.setValueFormatter(PercentFormatter()) + data.setValueTextSize(11f) + data.setValueTextColor(Color.WHITE) + data.setValueTypeface(tfLight) + chart!!.setData(data) // undo all highlights - chart.highlightValues(null); + chart!!.highlightValues(null) - chart.invalidate(); + chart!!.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.pie, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.pie, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + for (set in chart!!.data!!.dataSets) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + + chart!!.invalidate() } - case R.id.actionToggleIcons: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled); - chart.invalidate(); - break; + R.id.actionToggleIcons -> { + for (set in chart!!.data!!.dataSets) set.isDrawIconsEnabled = !set.isDrawIconsEnabled + + chart!!.invalidate() } - case R.id.actionToggleHole: { - chart.setDrawHoleEnabled(!chart.isDrawHoleEnabled()); - chart.invalidate(); - break; + + R.id.actionToggleHole -> { + chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled + chart!!.invalidate() } - case R.id.actionToggleMinAngles: { - if (chart.getMinAngleForSlices() == 0f) - chart.setMinAngleForSlices(36f); - else - chart.setMinAngleForSlices(0f); - chart.notifyDataSetChanged(); - chart.invalidate(); - break; + + R.id.actionToggleMinAngles -> { + if (chart!!.minAngleForSlices == 0f) chart!!.minAngleForSlices = 36f + else chart!!.minAngleForSlices = 0f + chart!!.notifyDataSetChanged() + chart!!.invalidate() } - case R.id.actionToggleCurvedSlices: { - boolean toSet = !chart.isDrawRoundedSlicesEnabled() || !chart.isDrawHoleEnabled(); - chart.setDrawRoundedSlices(toSet); - if (toSet && !chart.isDrawHoleEnabled()) { - chart.setDrawHoleEnabled(true); + + R.id.actionToggleCurvedSlices -> { + val toSet = !chart!!.isDrawRoundedSlicesEnabled || !chart!!.isDrawHoleEnabled + chart!!.setDrawRoundedSlices(toSet) + if (toSet && !chart!!.isDrawHoleEnabled) { + chart!!.isDrawHoleEnabled = true } - if (toSet && chart.isDrawSlicesUnderHoleEnabled()) { - chart.setDrawSlicesUnderHole(false); + if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { + chart!!.setDrawSlicesUnderHole(false) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionDrawCenter: { - chart.setDrawCenterText(!chart.isDrawCenterTextEnabled()); - chart.invalidate(); - break; + + R.id.actionDrawCenter -> { + chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) + chart!!.invalidate() } - case R.id.actionToggleXValues: { - chart.setDrawEntryLabels(!chart.isDrawEntryLabelsEnabled()); - chart.invalidate(); - break; + R.id.actionToggleXValues -> { + chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) + chart!!.invalidate() } - case R.id.actionTogglePercent: - chart.setUsePercentValues(!chart.isUsePercentValuesEnabled()); - chart.invalidate(); - break; - case R.id.animateX: { - chart.animateX(1400); - break; + + R.id.actionTogglePercent -> { + chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) + chart!!.invalidate() } - case R.id.animateY: { - chart.animateY(1400); - break; + + R.id.animateX -> { + chart!!.animateX(1400) } - case R.id.animateXY: { - chart.animateXY(1400, 1400); - break; + + R.id.animateY -> { + chart!!.animateY(1400) + } + + R.id.animateXY -> { + chart!!.animateXY(1400, 1400) } - case R.id.actionToggleSpin: { - chart.spin(1000, chart.getRotationAngle(), chart.getRotationAngle() + 360, Easing.EaseInOutCubic); - break; + + R.id.actionToggleSpin -> { + chart!!.spin(1000, chart!!.rotationAngle, chart!!.rotationAngle + 360, Easing.EaseInOutCubic) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - setData(seekBarX.getProgress(), seekBarY.getProgress()); + setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) } - @Override - protected void saveToGallery() { - saveToGallery(chart, "PieChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "PieChartActivity") } - private SpannableString generateCenterSpannableText() { - - SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda"); - s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0); - s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); - s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); - s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0); - s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); - s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0); - return s; + private fun generateCenterSpannableText(): SpannableString { + val s = SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda") + s.setSpan(RelativeSizeSpan(1.7f), 0, 14, 0) + s.setSpan(StyleSpan(Typeface.NORMAL), 14, s.length - 15, 0) + s.setSpan(ForegroundColorSpan(Color.GRAY), 14, s.length - 15, 0) + s.setSpan(RelativeSizeSpan(.8f), 14, s.length - 15, 0) + s.setSpan(StyleSpan(Typeface.ITALIC), s.length - 14, s.length, 0) + s.setSpan(ForegroundColorSpan(holoBlue), s.length - 14, s.length, 0) + return s } - @Override - public void onValueSelected(Entry e, Highlight h) { - - if (e == null) - return; - Log.i("VAL SELECTED", - "Value: " + e.getY() + ", index: " + h.getX() - + ", DataSet index: " + h.getDataSetIndex()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null) return + Log.i( + "VAL SELECTED", + ("Value: " + e.y + ", index: " + h?.x + + ", DataSet index: " + h?.dataSetIndex) + ) } - @Override - public void onNothingSelected() { - Log.i("PieChart", "nothing selected"); + override fun onNothingSelected() { + Log.i("PieChart", "nothing selected") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt b/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt index f2fb5133c4..0c3abcc7d1 100644 --- a/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt @@ -1,329 +1,307 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.graphics.Typeface; -import android.net.Uri; -import android.os.Bundle; -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.RelativeSizeSpan; -import android.text.style.StyleSpan; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.animation.Easing; -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.data.PieDataSet; -import com.github.mikephil.charting.data.PieEntry; -import com.github.mikephil.charting.formatter.PercentFormatter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.renderer.PieChartRenderer; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.MPPointF; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -import androidx.core.content.ContextCompat; - -public class PieChartRoundedActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private PieChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_piechart); - - setTitle("PieChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarY = findViewById(R.id.seekBarY); - - seekBarX.setOnSeekBarChangeListener(this); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setUsePercentValues(true); - chart.getDescription().setEnabled(false); - chart.setExtraOffsets(5, 10, 5, 5); - - chart.setDragDecelerationFrictionCoef(0.95f); - - chart.setCenterTextTypeface(tfLight); - chart.setCenterText(generateCenterSpannableText()); - - chart.setDrawHoleEnabled(true); - chart.setHoleColor(Color.TRANSPARENT); - - chart.setTransparentCircleColor(Color.TRANSPARENT); - chart.setTransparentCircleAlpha(110); - - chart.setHoleRadius(50f); - - chart.setTransparentCircleRadius(0f); - - chart.setDrawCenterText(true); - - chart.setRotationAngle(0); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.text.style.RelativeSizeSpan +import android.text.style.StyleSpan +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.animation.Easing +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.data.PieDataSet +import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.formatter.PercentFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.renderer.PieChartRenderer +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.ColorTemplate.holoBlue +import com.github.mikephil.charting.utils.MPPointF +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: PieChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_piechart) + + setTitle("PieChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarY = findViewById(R.id.seekBarY) + + seekBarX!!.setOnSeekBarChangeListener(this) + seekBarY!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setUsePercentValues(true) + chart!!.description.isEnabled = false + chart!!.setExtraOffsets(5f, 10f, 5f, 5f) + + chart!!.dragDecelerationFrictionCoef = 0.95f + + chart!!.setCenterTextTypeface(tfLight) + chart!!.centerText = generateCenterSpannableText() + + chart!!.isDrawHoleEnabled = true + chart!!.setHoleColor(Color.TRANSPARENT) + + chart!!.setTransparentCircleColor(Color.TRANSPARENT) + chart!!.setTransparentCircleAlpha(110) + + chart!!.holeRadius = 50f + + chart!!.transparentCircleRadius = 0f + + chart!!.setDrawCenterText(true) + + chart!!.rotationAngle = 0f // enable rotation of the chart by touch - chart.setRotationEnabled(true); - chart.setHighlightPerTapEnabled(true); + chart!!.isRotationEnabled = true + chart!!.isHighlightPerTapEnabled = true // chart.setUnit(" €"); // chart.setDrawUnitsInChart(true); // add a selection listener - chart.setOnChartValueSelectedListener(this); + chart!!.setOnChartValueSelectedListener(this) - seekBarX.setProgress(4); - seekBarY.setProgress(10); + seekBarX!!.progress = 4 + seekBarY!!.progress = 10 - chart.animateY(1400, Easing.EaseInOutQuad); - // chart.spin(2000, 0, 360); + chart!!.animateY(1400, Easing.EaseInOutQuad) - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - l.setXEntrySpace(7f); - l.setYEntrySpace(0f); - l.setYOffset(0f); + // chart.spin(2000, 0, 360); + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) + l.xEntrySpace = 7f + l.yEntrySpace = 0f + l.yOffset = 0f // entry label styling - chart.setEntryLabelColor(Color.WHITE); - chart.setEntryLabelTypeface(tfRegular); - chart.setEntryLabelTextSize(12f); + chart!!.setEntryLabelColor(Color.WHITE) + chart!!.setEntryLabelTypeface(tfRegular) + chart!!.setEntryLabelTextSize(12f) } - private void setData(int count, float range) { - ArrayList entries = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100); + private fun setData(count: Int, range: Float) { + val entries = ArrayList() + val sampleValues = getValues(100) // NOTE: The order of the entries when being added to the entries array determines their position around the center of // the chart. - for (int i = 0; i < count ; i++) { - entries.add(new PieEntry((sampleValues[i].floatValue() * range) + range / 5, - parties[i % parties.length], - getResources().getDrawable(R.drawable.star))); + for (i in 0..() - ArrayList colors = new ArrayList<>(); + for (c in ColorTemplate.VORDIPLOM_COLORS) colors.add(c) - for (int c : ColorTemplate.VORDIPLOM_COLORS) - colors.add(c); + for (c in ColorTemplate.JOYFUL_COLORS) colors.add(c) - for (int c : ColorTemplate.JOYFUL_COLORS) - colors.add(c); + for (c in ColorTemplate.COLORFUL_COLORS) colors.add(c) - for (int c : ColorTemplate.COLORFUL_COLORS) - colors.add(c); + for (c in ColorTemplate.LIBERTY_COLORS) colors.add(c) - for (int c : ColorTemplate.LIBERTY_COLORS) - colors.add(c); + for (c in ColorTemplate.PASTEL_COLORS) colors.add(c) - for (int c : ColorTemplate.PASTEL_COLORS) - colors.add(c); + colors.add(holoBlue) - colors.add(ColorTemplate.getHoloBlue()); + dataSet.setColors(colors) - dataSet.setColors(colors); //dataSet.setSelectionShift(0f); - - PieData data = new PieData(dataSet); - data.setValueFormatter(new PercentFormatter()); - data.setValueTextSize(11f); - data.setValueTextColor(Color.WHITE); - data.setValueTypeface(tfLight); - chart.setData(data); + val data = PieData(dataSet) + data.setValueFormatter(PercentFormatter()) + data.setValueTextSize(11f) + data.setValueTextColor(Color.WHITE) + data.setValueTypeface(tfLight) + chart!!.setData(data) // undo all highlights - chart.highlightValues(null); + chart!!.highlightValues(null) - PieChartRenderer renderer =(PieChartRenderer) chart.getRenderer(); - renderer.setRoundedCornerRadius(30f); - dataSet.setSliceSpace(renderer.getRoundedCornerRadius()/2); + val renderer = chart!!.renderer as PieChartRenderer? + renderer!!.roundedCornerRadius = 30f + dataSet.setSliceSpace(renderer.roundedCornerRadius / 2) - chart.invalidate(); + chart!!.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.pie, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.pie, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + for (set in chart!!.data!!.dataSets) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + + chart!!.invalidate() } - case R.id.actionToggleIcons: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawIcons(!set.isDrawIconsEnabled); - chart.invalidate(); - break; + R.id.actionToggleIcons -> { + for (set in chart!!.data!!.dataSets) set.isDrawIconsEnabled = !set.isDrawIconsEnabled + + chart!!.invalidate() } - case R.id.actionToggleHole: { - chart.setDrawHoleEnabled(!chart.isDrawHoleEnabled()); - chart.invalidate(); - break; + + R.id.actionToggleHole -> { + chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled + chart!!.invalidate() } - case R.id.actionToggleMinAngles: { - if (chart.getMinAngleForSlices() == 0f) - chart.setMinAngleForSlices(36f); - else - chart.setMinAngleForSlices(0f); - chart.notifyDataSetChanged(); - chart.invalidate(); - break; + + R.id.actionToggleMinAngles -> { + if (chart!!.minAngleForSlices == 0f) chart!!.minAngleForSlices = 36f + else chart!!.minAngleForSlices = 0f + chart!!.notifyDataSetChanged() + chart!!.invalidate() } - case R.id.actionToggleCurvedSlices: { - boolean toSet = !chart.isDrawRoundedSlicesEnabled() || !chart.isDrawHoleEnabled(); - chart.setDrawRoundedSlices(toSet); - if (toSet && !chart.isDrawHoleEnabled()) { - chart.setDrawHoleEnabled(true); + + R.id.actionToggleCurvedSlices -> { + val toSet = !chart!!.isDrawRoundedSlicesEnabled || !chart!!.isDrawHoleEnabled + chart!!.setDrawRoundedSlices(toSet) + if (toSet && !chart!!.isDrawHoleEnabled) { + chart!!.isDrawHoleEnabled = true } - if (toSet && chart.isDrawSlicesUnderHoleEnabled()) { - chart.setDrawSlicesUnderHole(false); + if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { + chart!!.setDrawSlicesUnderHole(false) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionDrawCenter: { - chart.setDrawCenterText(!chart.isDrawCenterTextEnabled()); - chart.invalidate(); - break; + + R.id.actionDrawCenter -> { + chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) + chart!!.invalidate() } - case R.id.actionToggleXValues: { - chart.setDrawEntryLabels(!chart.isDrawEntryLabelsEnabled()); - chart.invalidate(); - break; + R.id.actionToggleXValues -> { + chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) + chart!!.invalidate() } - case R.id.actionTogglePercent: - chart.setUsePercentValues(!chart.isUsePercentValuesEnabled()); - chart.invalidate(); - break; - case R.id.animateX: { - chart.animateX(1400); - break; + + R.id.actionTogglePercent -> { + chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) + chart!!.invalidate() } - case R.id.animateY: { - chart.animateY(1400); - break; + + R.id.animateX -> { + chart!!.animateX(1400) } - case R.id.animateXY: { - chart.animateXY(1400, 1400); - break; + + R.id.animateY -> { + chart!!.animateY(1400) + } + + R.id.animateXY -> { + chart!!.animateXY(1400, 1400) } - case R.id.actionToggleSpin: { - chart.spin(1000, chart.getRotationAngle(), chart.getRotationAngle() + 360, Easing.EaseInOutCubic); - break; + + R.id.actionToggleSpin -> { + chart!!.spin(1000, chart!!.rotationAngle, chart!!.rotationAngle + 360, Easing.EaseInOutCubic) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - setData(seekBarX.getProgress(), seekBarY.getProgress()); + setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) } - @Override - protected void saveToGallery() { - saveToGallery(chart, "PieChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "PieChartActivity") } - private SpannableString generateCenterSpannableText() { - - SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda"); - s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0); - s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); - s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); - s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0); - s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); - s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0); - return s; + private fun generateCenterSpannableText(): SpannableString { + val s = SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda") + s.setSpan(RelativeSizeSpan(1.7f), 0, 14, 0) + s.setSpan(StyleSpan(Typeface.NORMAL), 14, s.length - 15, 0) + s.setSpan(ForegroundColorSpan(Color.GRAY), 14, s.length - 15, 0) + s.setSpan(RelativeSizeSpan(.8f), 14, s.length - 15, 0) + s.setSpan(StyleSpan(Typeface.ITALIC), s.length - 14, s.length, 0) + s.setSpan(ForegroundColorSpan(holoBlue), s.length - 14, s.length, 0) + return s } - @Override - public void onValueSelected(Entry e, Highlight h) { - - if (e == null) - return; - Log.i("VAL SELECTED", - "Value: " + e.getY() + ", index: " + h.getX() - + ", DataSet index: " + h.getDataSetIndex()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null) return + Log.i( + "VAL SELECTED", + ("Value: " + e.y + ", index: " + h?.x + + ", DataSet index: " + h?.dataSetIndex) + ) } - @Override - public void onNothingSelected() { - Log.i("PieChart", "nothing selected"); + override fun onNothingSelected() { + Log.i("PieChart", "nothing selected") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.kt index fc18a2dee3..90fedc0758 100644 --- a/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/PiePolylineChartActivity.kt @@ -1,315 +1,291 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.graphics.Typeface; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.RelativeSizeSpan; -import android.text.style.StyleSpan; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.animation.Easing; -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.data.PieDataSet; -import com.github.mikephil.charting.data.PieEntry; -import com.github.mikephil.charting.formatter.PercentFormatter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private PieChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - private Typeface tf; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_piechart); - - setTitle("PiePolylineChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarY = findViewById(R.id.seekBarY); - - seekBarX.setOnSeekBarChangeListener(this); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setUsePercentValues(true); - chart.getDescription().setEnabled(false); - chart.setExtraOffsets(5, 10, 5, 5); - - chart.setDragDecelerationFrictionCoef(0.95f); - - tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); - - chart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf")); - chart.setCenterText(generateCenterSpannableText()); - - chart.setExtraOffsets(20.f, 0.f, 20.f, 0.f); - - chart.setDrawHoleEnabled(true); - chart.setHoleColor(Color.WHITE); - - chart.setTransparentCircleColor(Color.WHITE); - chart.setTransparentCircleAlpha(110); - - chart.setHoleRadius(58f); - chart.setTransparentCircleRadius(61f); - - chart.setDrawCenterText(true); - - chart.setRotationAngle(0); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.text.style.RelativeSizeSpan +import android.text.style.StyleSpan +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.animation.Easing +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.data.PieDataSet +import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.formatter.PercentFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.ColorTemplate.holoBlue +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: PieChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + private var tf: Typeface? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_piechart) + + setTitle("PiePolylineChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarY = findViewById(R.id.seekBarY) + + seekBarX!!.setOnSeekBarChangeListener(this) + seekBarY!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setUsePercentValues(true) + chart!!.description.isEnabled = false + chart!!.setExtraOffsets(5f, 10f, 5f, 5f) + + chart!!.dragDecelerationFrictionCoef = 0.95f + + tf = Typeface.createFromAsset(assets, "OpenSans-Regular.ttf") + + chart!!.setCenterTextTypeface(Typeface.createFromAsset(assets, "OpenSans-Light.ttf")) + chart!!.centerText = generateCenterSpannableText() + + chart!!.setExtraOffsets(20f, 0f, 20f, 0f) + + chart!!.isDrawHoleEnabled = true + chart!!.setHoleColor(Color.WHITE) + + chart!!.setTransparentCircleColor(Color.WHITE) + chart!!.setTransparentCircleAlpha(110) + + chart!!.holeRadius = 58f + chart!!.transparentCircleRadius = 61f + + chart!!.setDrawCenterText(true) + + chart!!.rotationAngle = 0f // enable rotation of the chart by touch - chart.setRotationEnabled(true); - chart.setHighlightPerTapEnabled(true); + chart!!.isRotationEnabled = true + chart!!.isHighlightPerTapEnabled = true // chart.setUnit(" €"); // chart.setDrawUnitsInChart(true); // add a selection listener - chart.setOnChartValueSelectedListener(this); + chart!!.setOnChartValueSelectedListener(this) - seekBarX.setProgress(4); - seekBarY.setProgress(100); + seekBarX!!.progress = 4 + seekBarY!!.progress = 100 - chart.animateY(1400, Easing.EaseInOutQuad); - // chart.spin(2000, 0, 360); + chart!!.animateY(1400, Easing.EaseInOutQuad) - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - l.setEnabled(false); + // chart.spin(2000, 0, 360); + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) + l.isEnabled = false } - private void setData(int count, float range) { - Double[] sampleValues = DataTools.Companion.getValues(count); - ArrayList entries = new ArrayList<>(); + private fun setData(count: Int, range: Float) { + val sampleValues = getValues(count) + val entries = ArrayList() // NOTE: The order of the entries when being added to the entries array determines their position around the center of // the chart. - for (int i = 0; i < count; i++) { - entries.add(new PieEntry((sampleValues[i].floatValue() * range) + range / 5, parties[i % parties.length])); + for (i in 0..() - ArrayList colors = new ArrayList<>(); + for (c in ColorTemplate.VORDIPLOM_COLORS) colors.add(c) - for (int c : ColorTemplate.VORDIPLOM_COLORS) - colors.add(c); + for (c in ColorTemplate.JOYFUL_COLORS) colors.add(c) - for (int c : ColorTemplate.JOYFUL_COLORS) - colors.add(c); + for (c in ColorTemplate.COLORFUL_COLORS) colors.add(c) - for (int c : ColorTemplate.COLORFUL_COLORS) - colors.add(c); + for (c in ColorTemplate.LIBERTY_COLORS) colors.add(c) - for (int c : ColorTemplate.LIBERTY_COLORS) - colors.add(c); + for (c in ColorTemplate.PASTEL_COLORS) colors.add(c) - for (int c : ColorTemplate.PASTEL_COLORS) - colors.add(c); + colors.add(holoBlue) - colors.add(ColorTemplate.getHoloBlue()); - - dataSet.setColors(colors); - //dataSet.setSelectionShift(0f); + dataSet.setColors(colors) - dataSet.setValueLinePart1OffsetPercentage(80.f); - dataSet.setValueLinePart1Length(0.2f); - dataSet.setValueLinePart2Length(0.4f); + //dataSet.setSelectionShift(0f); + dataSet.setValueLinePart1OffsetPercentage(80f) + dataSet.setValueLinePart1Length(0.2f) + dataSet.setValueLinePart2Length(0.4f) //dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); - dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); + dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE) - PieData data = new PieData(dataSet); - data.setValueFormatter(new PercentFormatter()); - data.setValueTextSize(11f); - data.setValueTextColor(Color.BLACK); - data.setValueTypeface(tf); - chart.setData(data); + val data = PieData(dataSet) + data.setValueFormatter(PercentFormatter()) + data.setValueTextSize(11f) + data.setValueTextColor(Color.BLACK) + data.setValueTypeface(tf) + chart!!.setData(data) // undo all highlights - chart.highlightValues(null); + chart!!.highlightValues(null) - chart.invalidate(); + chart!!.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.pie, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.pie, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/PiePolylineChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/PiePolylineChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + for (set in chart!!.data!!.dataSets) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + + chart!!.invalidate() } - case R.id.actionToggleHole: { - chart.setDrawHoleEnabled(!chart.isDrawHoleEnabled()); - chart.invalidate(); - break; + + R.id.actionToggleHole -> { + chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled + chart!!.invalidate() } - case R.id.actionToggleMinAngles: { - if (chart.getMinAngleForSlices() == 0f) - chart.setMinAngleForSlices(36f); - else - chart.setMinAngleForSlices(0f); - chart.notifyDataSetChanged(); - chart.invalidate(); - break; + + R.id.actionToggleMinAngles -> { + if (chart!!.minAngleForSlices == 0f) chart!!.minAngleForSlices = 36f + else chart!!.minAngleForSlices = 0f + chart!!.notifyDataSetChanged() + chart!!.invalidate() } - case R.id.actionToggleCurvedSlices: { - boolean toSet = !chart.isDrawRoundedSlicesEnabled() || !chart.isDrawHoleEnabled(); - chart.setDrawRoundedSlices(toSet); - if (toSet && !chart.isDrawHoleEnabled()) { - chart.setDrawHoleEnabled(true); + + R.id.actionToggleCurvedSlices -> { + val toSet = !chart!!.isDrawRoundedSlicesEnabled || !chart!!.isDrawHoleEnabled + chart!!.setDrawRoundedSlices(toSet) + if (toSet && !chart!!.isDrawHoleEnabled) { + chart!!.isDrawHoleEnabled = true } - if (toSet && chart.isDrawSlicesUnderHoleEnabled()) { - chart.setDrawSlicesUnderHole(false); + if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { + chart!!.setDrawSlicesUnderHole(false) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionDrawCenter: { - chart.setDrawCenterText(!chart.isDrawCenterTextEnabled()); - chart.invalidate(); - break; + + R.id.actionDrawCenter -> { + chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) + chart!!.invalidate() } - case R.id.actionToggleXValues: { - chart.setDrawEntryLabels(!chart.isDrawEntryLabelsEnabled()); - chart.invalidate(); - break; + R.id.actionToggleXValues -> { + chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) + chart!!.invalidate() } - case R.id.actionTogglePercent: - chart.setUsePercentValues(!chart.isUsePercentValuesEnabled()); - chart.invalidate(); - break; - case R.id.animateX: { - chart.animateX(1400); - break; + + R.id.actionTogglePercent -> { + chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) + chart!!.invalidate() } - case R.id.animateY: { - chart.animateY(1400); - break; + + R.id.animateX -> { + chart!!.animateX(1400) } - case R.id.animateXY: { - chart.animateXY(1400, 1400); - break; + + R.id.animateY -> { + chart!!.animateY(1400) } - case R.id.actionToggleSpin: { - chart.spin(1000, chart.getRotationAngle(), chart.getRotationAngle() + 360, Easing.EaseInOutCubic); - break; + + R.id.animateXY -> { + chart!!.animateXY(1400, 1400) } - case R.id.actionSave: { + + R.id.actionToggleSpin -> { + chart!!.spin(1000, chart!!.rotationAngle, chart!!.rotationAngle + 360, Easing.EaseInOutCubic) + } + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - setData(seekBarX.getProgress(), seekBarY.getProgress()); + setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) } - @Override - protected void saveToGallery() { - saveToGallery(chart, "PiePolylineChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "PiePolylineChartActivity") } - private SpannableString generateCenterSpannableText() { - - SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda"); - s.setSpan(new RelativeSizeSpan(1.5f), 0, 14, 0); - s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); - s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); - s.setSpan(new RelativeSizeSpan(.65f), 14, s.length() - 15, 0); - s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); - s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0); - return s; + private fun generateCenterSpannableText(): SpannableString { + val s = SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda") + s.setSpan(RelativeSizeSpan(1.5f), 0, 14, 0) + s.setSpan(StyleSpan(Typeface.NORMAL), 14, s.length - 15, 0) + s.setSpan(ForegroundColorSpan(Color.GRAY), 14, s.length - 15, 0) + s.setSpan(RelativeSizeSpan(.65f), 14, s.length - 15, 0) + s.setSpan(StyleSpan(Typeface.ITALIC), s.length - 14, s.length, 0) + s.setSpan(ForegroundColorSpan(holoBlue), s.length - 14, s.length, 0) + return s } - @Override - public void onValueSelected(Entry e, Highlight h) { - - if (e == null) - return; - Log.i("VAL SELECTED", - "Value: " + e.getY() + ", xIndex: " + e.getX() - + ", DataSet index: " + h.getDataSetIndex()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null) return + Log.i( + "VAL SELECTED", + ("Value: " + e.y + ", xIndex: " + e.x + + ", DataSet index: " + h?.dataSetIndex) + ) } - @Override - public void onNothingSelected() { - Log.i("PieChart", "nothing selected"); + override fun onNothingSelected() { + Log.i("PieChart", "nothing selected") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt index e4a3e9631b..afaebbd7f1 100644 --- a/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt @@ -1,264 +1,244 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; - -import com.github.mikephil.charting.animation.Easing; -import com.github.mikephil.charting.charts.RadarChart; -import com.github.mikephil.charting.components.AxisBase; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.MarkerView; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.RadarData; -import com.github.mikephil.charting.data.RadarDataSet; -import com.github.mikephil.charting.data.RadarEntry; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.interfaces.datasets.IDataSet; -import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet; - -import info.appdev.chartexample.custom.RadarMarkerView; -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; - -public class RadarChartActivity extends DemoBase { - - private RadarChart chart; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_radarchart); - - setTitle("RadarChartActivity"); - - chart = findViewById(R.id.chart1); - chart.setBackgroundColor(Color.rgb(60, 65, 82)); - - chart.getDescription().setEnabled(false); - - chart.setWebLineWidth(1f); - chart.setWebColor(Color.LTGRAY); - chart.setWebLineWidthInner(1f); - chart.setWebColorInner(Color.LTGRAY); - chart.setWebAlpha(100); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.animation.Easing +import com.github.mikephil.charting.charts.RadarChart +import com.github.mikephil.charting.components.AxisBase +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.RadarData +import com.github.mikephil.charting.data.RadarDataSet +import com.github.mikephil.charting.data.RadarEntry +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.custom.RadarMarkerView +import info.appdev.chartexample.notimportant.DemoBase + +class RadarChartActivity : DemoBase() { + private var chart: RadarChart? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_radarchart) + + setTitle("RadarChartActivity") + + chart = findViewById(R.id.chart1) + chart!!.setBackgroundColor(Color.rgb(60, 65, 82)) + + chart!!.description.isEnabled = false + + chart!!.webLineWidth = 1f + chart!!.webColor = Color.LTGRAY + chart!!.webLineWidthInner = 1f + chart!!.webColorInner = Color.LTGRAY + chart!!.webAlpha = 100 // create a custom MarkerView (extend MarkerView) and specify the layout // to use for it - MarkerView mv = new RadarMarkerView(this, R.layout.radar_markerview); - mv.setChartView(chart); // For bounds control - chart.setMarker(mv); // Set the marker to the chart - - setData(); + val mv: MarkerView = RadarMarkerView(this, R.layout.radar_markerview) + mv.chartView = chart // For bounds control + chart!!.setMarker(mv) // Set the marker to the chart - chart.animateXY(1400, 1400, Easing.EaseInOutQuad); + setData() - XAxis xAxis = chart.getXAxis(); - xAxis.setTypeface(tfLight); - xAxis.setTextSize(9f); - xAxis.setYOffset(0f); - xAxis.setXOffset(0f); - xAxis.setValueFormatter(new IAxisValueFormatter() { + chart!!.animateXY(1400, 1400, Easing.EaseInOutQuad) - private final String[] mActivities = new String[]{"Burger", "Steak", "Salad", "Pasta", "Pizza"}; + val xAxis = chart!!.xAxis + xAxis.typeface = tfLight + xAxis.textSize = 9f + xAxis.yOffset = 0f + xAxis.xOffset = 0f + xAxis.valueFormatter = object : IAxisValueFormatter { + private val mActivities = arrayOf("Burger", "Steak", "Salad", "Pasta", "Pizza") - @Override - public String getFormattedValue(float value, AxisBase axis) { - return mActivities[(int) value % mActivities.length]; + override fun getFormattedValue(value: Float, axis: AxisBase?): String { + return mActivities[value.toInt() % mActivities.size] } - }); - xAxis.setTextColor(Color.WHITE); - - YAxis yAxis = chart.getYAxis(); - yAxis.setTypeface(tfLight); - yAxis.setLabelCount(5, false); - yAxis.setTextSize(9f); - yAxis.setAxisMinimum(0f); - yAxis.setAxisMaximum(80f); - yAxis.setDrawLabels(false); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - l.setTypeface(tfLight); - l.setXEntrySpace(7f); - l.setYEntrySpace(5f); - l.setTextColor(Color.WHITE); + } + xAxis.textColor = Color.WHITE + + val yAxis = chart!!.yAxis + yAxis.typeface = tfLight + yAxis.setLabelCount(5, false) + yAxis.textSize = 9f + yAxis.axisMinimum = 0f + yAxis.axisMaximum = 80f + yAxis.setDrawLabels(false) + + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + l.typeface = tfLight + l.xEntrySpace = 7f + l.yEntrySpace = 5f + l.textColor = Color.WHITE } - private void setData() { - - float mul = 80; - float min = 20; - int cnt = 5; - Double[] sampleValues = DataTools.Companion.getValues(cnt + 1); + private fun setData() { + val mul = 80f + val min = 20f + val cnt = 5 + val sampleValues = getValues(cnt + 1) - ArrayList entries1 = new ArrayList<>(); - ArrayList entries2 = new ArrayList<>(); + val entries1 = ArrayList() + val entries2 = ArrayList() // NOTE: The order of the entries when being added to the entries array determines their position around the center of // the chart. - for (int i = 0; i < cnt; i++) { - float val1 = (sampleValues[i].floatValue() * mul) + min; - entries1.add(new RadarEntry(val1)); + for (i in 0.. sets = new ArrayList<>(); - sets.add(set1); - sets.add(set2); - - RadarData data = new RadarData(sets); - data.setValueTypeface(tfLight); - data.setValueTextSize(8f); - data.setDrawValues(false); - data.setValueTextColor(Color.WHITE); - - chart.setData(data); - List colorList = new ArrayList<>(); - colorList.add(Color.rgb(222, 166, 111)); - colorList.add(Color.rgb(220, 206, 138)); - colorList.add(Color.rgb(243, 255, 192)); - colorList.add(Color.rgb(240, 255, 240)); - colorList.add(Color.rgb(250, 255, 250)); - chart.setLayerColorList(colorList); - chart.invalidate(); + val set1 = RadarDataSet(entries1, "Last Week") + set1.setColor(Color.rgb(103, 110, 129)) + set1.setFillColor(Color.rgb(103, 110, 129)) + set1.setDrawFilled(true) + set1.setFillAlpha(180) + set1.setLineWidth(2f) + set1.isDrawHighlightCircleEnabled = true + set1.setDrawHighlightIndicators(false) + + val set2 = RadarDataSet(entries2, "This Week") + set2.setColor(Color.rgb(121, 162, 175)) + set2.setFillColor(Color.rgb(121, 162, 175)) + set2.setDrawFilled(true) + set2.setFillAlpha(180) + set2.setLineWidth(2f) + set2.isDrawHighlightCircleEnabled = true + set2.setDrawHighlightIndicators(false) + + val sets = ArrayList() + sets.add(set1) + sets.add(set2) + + val data = RadarData(sets) + data.setValueTypeface(tfLight) + data.setValueTextSize(8f) + data.setDrawValues(false) + data.setValueTextColor(Color.WHITE) + + chart!!.setData(data) + val colorList: MutableList = ArrayList() + colorList.add(Color.rgb(222, 166, 111)) + colorList.add(Color.rgb(220, 206, 138)) + colorList.add(Color.rgb(243, 255, 192)) + colorList.add(Color.rgb(240, 255, 240)) + colorList.add(Color.rgb(250, 255, 250)) + chart!!.layerColorList = colorList + chart!!.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.radar, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.radar, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/RadarChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/RadarChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - for (IDataSet set : chart.getData().getDataSets()) - set.setDrawValues(!set.isDrawValuesEnabled); - chart.invalidate(); - break; + R.id.actionToggleValues -> { + for (set in chart!!.data!!.dataSets) set.isDrawValuesEnabled = !set.isDrawValuesEnabled + + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if (chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionToggleRotate: { - chart.setRotationEnabled(!chart.isRotationEnabled()); - chart.invalidate(); - break; + + R.id.actionToggleRotate -> { + chart!!.isRotationEnabled = !chart!!.isRotationEnabled + chart!!.invalidate() } - case R.id.actionToggleFilled: { - ArrayList sets = (ArrayList) chart.getData() - .getDataSets(); + R.id.actionToggleFilled -> { + val sets = chart!!.data!! + .dataSets as ArrayList - for (IRadarDataSet set : sets) { - set.setDrawFilled(!set.isDrawFilledEnabled); + for (set in sets) { + set.setDrawFilled(!set.isDrawFilledEnabled) } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHighlightCircle: { - ArrayList sets = (ArrayList) chart.getData() - .getDataSets(); + R.id.actionToggleHighlightCircle -> { + val sets = chart!!.data!! + .dataSets as ArrayList - for (IRadarDataSet set : sets) { - set.isDrawHighlightCircleEnabled = !set.isDrawHighlightCircleEnabled; + for (set in sets) { + set.isDrawHighlightCircleEnabled = !set.isDrawHighlightCircleEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleXLabels: { - chart.getXAxis().setEnabled(!chart.getXAxis().isEnabled()); - chart.notifyDataSetChanged(); - chart.invalidate(); - break; + + R.id.actionToggleXLabels -> { + chart!!.xAxis.isEnabled = !chart!!.xAxis.isEnabled + chart!!.notifyDataSetChanged() + chart!!.invalidate() } - case R.id.actionToggleYLabels: { - chart.getYAxis().setEnabled(!chart.getYAxis().isEnabled()); - chart.invalidate(); - break; + R.id.actionToggleYLabels -> { + chart!!.yAxis.isEnabled = !chart!!.yAxis.isEnabled + chart!!.invalidate() } - case R.id.animateX: { - chart.animateX(1400); - break; + + R.id.animateX -> { + chart!!.animateX(1400) } - case R.id.animateY: { - chart.animateY(1400); - break; + + R.id.animateY -> { + chart!!.animateY(1400) } - case R.id.animateXY: { - chart.animateXY(1400, 1400); - break; + + R.id.animateXY -> { + chart!!.animateXY(1400, 1400) } - case R.id.actionToggleSpin: { - chart.spin(2000, chart.getRotationAngle(), chart.getRotationAngle() + 360, Easing.EaseInOutCubic); - break; + + R.id.actionToggleSpin -> { + chart!!.spin(2000, chart!!.rotationAngle, chart!!.rotationAngle + 360, Easing.EaseInOutCubic) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "RadarChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "RadarChartActivity") } } diff --git a/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt index 18e9b46b3d..8fa09c18b1 100644 --- a/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt @@ -1,130 +1,124 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.Toast; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.Legend.LegendForm; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.components.YAxis.AxisDependency; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -public class RealtimeLineChartActivity extends DemoBase implements - OnChartValueSelectedListener { - - private LineChart chart; - Double[] sampleValues = DataTools.Companion.getValues(102); - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_realtime_linechart); - - setTitle("RealtimeLineChartActivity"); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.Toast +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.Legend.LegendForm +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate.holoBlue +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { + private var chart: LineChart? = null + var sampleValues: Array = getValues(102) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_realtime_linechart) + + setTitle("RealtimeLineChartActivity") + + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) // enable description text - chart.getDescription().setEnabled(true); + chart!!.description.isEnabled = true // enable touch gestures - chart.setTouchEnabled(true); + chart!!.setTouchEnabled(true) // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); - chart.setDrawGridBackground(false); + chart!!.isDragEnabled = true + chart!!.setScaleEnabled(true) + chart!!.setDrawGridBackground(false) // if disabled, scaling can be done on x- and y-axis separately - chart.setPinchZoom(true); + chart!!.setPinchZoom(true) // set an alternative background color - chart.setBackgroundColor(Color.LTGRAY); + chart!!.setBackgroundColor(Color.LTGRAY) - LineData data = new LineData(); - data.setValueTextColor(Color.WHITE); + val data = LineData() + data.setValueTextColor(Color.WHITE) // add empty data - chart.setData(data); + chart!!.setData(data) // get the legend (only possible after setting data) - Legend l = chart.getLegend(); + val l = chart!!.legend // modify the legend ... - l.setForm(LegendForm.LINE); - l.setTypeface(tfLight); - l.setTextColor(Color.WHITE); - - XAxis xl = chart.getXAxis(); - xl.setTypeface(tfLight); - xl.setTextColor(Color.WHITE); - xl.setDrawGridLines(false); - xl.setAvoidFirstLastClipping(true); - xl.setEnabled(true); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tfLight); - leftAxis.setTextColor(Color.WHITE); - leftAxis.setAxisMaximum(100f); - leftAxis.setAxisMinimum(0f); - leftAxis.setDrawGridLines(true); - - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setEnabled(false); - + l.form = LegendForm.LINE + l.typeface = tfLight + l.textColor = Color.WHITE + + val xl = chart!!.xAxis + xl.typeface = tfLight + xl.textColor = Color.WHITE + xl.setDrawGridLines(false) + xl.setAvoidFirstLastClipping(true) + xl.isEnabled = true + + val leftAxis = chart!!.axisLeft + leftAxis.typeface = tfLight + leftAxis.textColor = Color.WHITE + leftAxis.axisMaximum = 100f + leftAxis.axisMinimum = 0f + leftAxis.setDrawGridLines(true) + + val rightAxis = chart!!.axisRight + rightAxis.isEnabled = false } - private void addEntry() { - - LineData data = chart.getData(); + private fun addEntry() { + val data: LineData? = chart!!.data if (data != null) { + var set: ILineDataSet - ILineDataSet set = data.getDataSetByIndex(0); // set.addEntry(...); // can be called as well - - if (set == null) { - set = createSet(); - data.addDataSet(set); + if (data.dataSetCount == 0) { + set = createSet() + data.addDataSet(set) + } else { + set = data.getDataSetByIndex(0) } - int cycleValue = (int) (set.entryCount % 100.0); - data.addEntry(new Entry(set.entryCount, (sampleValues[cycleValue].floatValue() * 40) + 30f), 0); - data.notifyDataChanged(); + val cycleValue = (set.entryCount % 100.0) + data.addEntry(Entry(set.entryCount.toFloat(), (sampleValues[cycleValue.toInt()].toFloat() * 40) + 30f), 0) + data.notifyDataChanged() // let the chart know it's data has changed - chart.notifyDataSetChanged(); + chart!!.notifyDataSetChanged() // limit the number of visible entries - chart.setVisibleXRangeMaximum(120); + chart!!.setVisibleXRangeMaximum(120f) + // chart.setVisibleYRange(30, AxisDependency.LEFT); // move to the latest entry - chart.moveViewToX(data.getEntryCount()); + chart!!.moveViewToX(data.entryCount.toFloat()) // this automatically refreshes the chart (calls invalidate()) // chart.moveViewTo(data.getXValCount()-7, 55f, @@ -132,121 +126,100 @@ public class RealtimeLineChartActivity extends DemoBase implements } } - private LineDataSet createSet() { - - LineDataSet set = new LineDataSet(null, "Dynamic Data"); - set.setAxisDependency(AxisDependency.LEFT); - set.setColor(ColorTemplate.getHoloBlue()); - set.setCircleColor(Color.WHITE); - set.setLineWidth(2f); - set.setCircleRadius(4f); - set.setFillAlpha(65); - set.setFillColor(ColorTemplate.getHoloBlue()); - set.setHighLightColor(Color.rgb(244, 117, 117)); - set.setValueTextColor(Color.WHITE); - set.setValueTextSize(9f); - set.setDrawValues(false); - return set; + private fun createSet(): LineDataSet { + val set = LineDataSet(mutableListOf(), "Dynamic Data") + set.axisDependency = AxisDependency.LEFT + set.setColor(holoBlue) + set.setCircleColor(Color.WHITE) + set.setLineWidth(2f) + set.setCircleRadius(4f) + set.setFillAlpha(65) + set.setFillColor(holoBlue) + set.highLightColor = Color.rgb(244, 117, 117) + set.valueTextColor = Color.WHITE + set.valueTextSize = 9f + set.isDrawValuesEnabled = false + return set } - private Thread thread; - - private void feedMultiple() { + private var thread: Thread? = null - if (thread != null) - thread.interrupt(); + private fun feedMultiple() { + if (thread != null) thread!!.interrupt() - final Runnable runnable = new Runnable() { - - @Override - public void run() { - addEntry(); - } - }; + val runnable = Runnable { addEntry() } - thread = new Thread(new Runnable() { + thread = Thread { + for (i in 0..999) { + // Don't generate garbage runnables inside the loop. - @Override - public void run() { - for (int i = 0; i < 1000; i++) { + runOnUiThread(runnable) - // Don't generate garbage runnables inside the loop. - runOnUiThread(runnable); - - try { - Thread.sleep(25); - } catch (InterruptedException e) { - e.printStackTrace(); - } + try { + Thread.sleep(25) + } catch (e: InterruptedException) { + e.printStackTrace() } } - }); + } - thread.start(); + thread!!.start() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.realtime, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.realtime, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/RealtimeLineChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/RealtimeLineChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionAdd: { - addEntry(); - break; + + R.id.actionAdd -> { + addEntry() } - case R.id.actionClear: { - chart.clearValues(); - Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show(); - break; + + R.id.actionClear -> { + chart!!.clearValues() + Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show() } - case R.id.actionFeedMultiple: { - feedMultiple(); - break; + + R.id.actionFeedMultiple -> { + feedMultiple() } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "RealtimeLineChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "RealtimeLineChartActivity") } - @Override - public void onValueSelected(Entry e, Highlight h) { - Log.i("Entry selected", e.toString()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + Log.i("Entry selected", e.toString()) } - @Override - public void onNothingSelected() { - Log.i("Nothing selected", "Nothing selected."); + override fun onNothingSelected() { + Log.i("Nothing selected", "Nothing selected.") } - @Override - protected void onPause() { - super.onPause(); + override fun onPause() { + super.onPause() if (thread != null) { - thread.interrupt(); + thread!!.interrupt() } } } diff --git a/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt b/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt index 0d12764537..1659d7bd83 100644 --- a/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt @@ -1,246 +1,230 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.ScatterChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.ScatterData; -import com.github.mikephil.charting.data.ScatterDataSet; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.custom.CustomScatterShapeRenderer; -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; - -public class ScatterChartActivity extends DemoBase implements OnSeekBarChangeListener, - OnChartValueSelectedListener { - - private ScatterChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_scatterchart); - - setTitle("ScatterChartActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setOnSeekBarChangeListener(this); - - seekBarY = findViewById(R.id.seekBarY); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.getDescription().setEnabled(false); - chart.setOnChartValueSelectedListener(this); - - chart.setDrawGridBackground(false); - chart.setTouchEnabled(true); - chart.setMaxHighlightDistance(50f); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.ScatterChart +import com.github.mikephil.charting.charts.ScatterChart.ScatterShape +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.ScatterData +import com.github.mikephil.charting.data.ScatterDataSet +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.custom.CustomScatterShapeRenderer +import info.appdev.chartexample.notimportant.DemoBase + +class ScatterChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: ScatterChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_scatterchart) + + setTitle("ScatterChartActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarX!!.setOnSeekBarChangeListener(this) + + seekBarY = findViewById(R.id.seekBarY) + seekBarY!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.description.isEnabled = false + chart!!.setOnChartValueSelectedListener(this) + + chart!!.setDrawGridBackground(false) + chart!!.setTouchEnabled(true) + chart!!.setMaxHighlightDistance(50f) // enable scaling and dragging - chart.setDragEnabled(true); - chart.setScaleEnabled(true); + chart!!.isDragEnabled = true + chart!!.setScaleEnabled(true) - chart.setMaxVisibleValueCount(200); - chart.setPinchZoom(true); + chart!!.setMaxVisibleValueCount(200) + chart!!.setPinchZoom(true) - seekBarX.setProgress(45); - seekBarY.setProgress(100); + seekBarX!!.progress = 45 + seekBarY!!.progress = 100 - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - l.setTypeface(tfLight); - l.setXOffset(5f); + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) + l.typeface = tfLight + l.xOffset = 5f - YAxis yl = chart.getAxisLeft(); - yl.setTypeface(tfLight); - yl.setAxisMinimum(0f); // this replaces setStartAtZero(true) + val yl = chart!!.axisLeft + yl.typeface = tfLight + yl.axisMinimum = 0f // this replaces setStartAtZero(true) - chart.getAxisRight().setEnabled(false); + chart!!.axisRight.isEnabled = false - XAxis xl = chart.getXAxis(); - xl.setTypeface(tfLight); - xl.setDrawGridLines(false); + val xl = chart!!.xAxis + xl.typeface = tfLight + xl.setDrawGridLines(false) } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() - ArrayList values1 = new ArrayList<>(); - ArrayList values2 = new ArrayList<>(); - ArrayList values3 = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100+2); + val values1 = ArrayList() + val values2 = ArrayList() + val values3 = ArrayList() + val sampleValues = getValues(100 + 2) - for (int i = 0; i < seekBarX.getProgress(); i++) { - float val = (sampleValues[i].floatValue() * seekBarY.getProgress()) + 3; - values1.add(new Entry(i, val)); + for (i in 0.. dataSets = new ArrayList<>(); - dataSets.add(set1); // add the data sets - dataSets.add(set2); - dataSets.add(set3); + val set1 = ScatterDataSet(values1, "DS 1") + set1.setScatterShape(ScatterShape.SQUARE) + set1.setColor(ColorTemplate.COLORFUL_COLORS[0]) + val set2 = ScatterDataSet(values2, "DS 2") + set2.setScatterShape(ScatterShape.CIRCLE) + set2.scatterShapeHoleColor = ColorTemplate.COLORFUL_COLORS[3] + set2.scatterShapeHoleRadius = 3f + set2.setColor(ColorTemplate.COLORFUL_COLORS[1]) + val set3 = ScatterDataSet(values3, "DS 3") + set3.shapeRenderer = CustomScatterShapeRenderer() + set3.setColor(ColorTemplate.COLORFUL_COLORS[2]) + + set1.scatterShapeSize = 8f + set2.scatterShapeSize = 8f + set3.scatterShapeSize = 8f + + val dataSets = ArrayList() + dataSets.add(set1) // add the data sets + dataSets.add(set2) + dataSets.add(set3) // create a data object with the data sets - ScatterData data = new ScatterData(dataSets); - data.setValueTypeface(tfLight); + val data = ScatterData(dataSets) + data.setValueTypeface(tfLight) - chart.setData(data); - chart.invalidate(); + chart!!.setData(data) + chart!!.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.scatter, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.scatter, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - List sets = chart.getData() - .getDataSets(); - for (IScatterDataSet iSet : sets) { + R.id.actionToggleValues -> { + val sets: MutableList = chart!!.data!! + .dataSets - ScatterDataSet set = (ScatterDataSet) iSet; - set.setDrawValues(!set.isDrawValuesEnabled()); + for (iSet in sets) { + val set = iSet as ScatterDataSet + set.isDrawValuesEnabled = !set.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if(chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionTogglePinch: { - chart.setPinchZoom(!chart.isPinchZoomEnabled()); - chart.invalidate(); - break; + R.id.actionTogglePinch -> { + chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.animateX: { - chart.animateX(3000); - break; + + R.id.animateX -> { + chart!!.animateX(3000) } - case R.id.animateY: { - chart.animateY(3000); - break; + + R.id.animateY -> { + chart!!.animateY(3000) } - case R.id.animateXY: { - chart.animateXY(3000, 3000); - break; + R.id.animateXY -> { + chart!!.animateXY(3000, 3000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "ScatterChartActivity"); + override fun saveToGallery() { + saveToGallery(chart, "ScatterChartActivity") } - @Override - public void onValueSelected(Entry e, Highlight h) { - Log.i("VAL SELECTED", - "Value: " + e.getY() + ", xIndex: " + e.getX() - + ", DataSet index: " + h.getDataSetIndex()); + override fun onValueSelected(e: Entry?, h: Highlight?) { + Log.i( + "VAL SELECTED", + ("Value: " + e?.y + ", xIndex: " + e?.x + + ", DataSet index: " + h?.dataSetIndex) + ) } - @Override - public void onNothingSelected() {} + override fun onNothingSelected() {} - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} } diff --git a/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt b/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt index 02263dbe51..6b2880aa30 100644 --- a/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt @@ -1,102 +1,92 @@ - -package info.appdev.chartexample; - -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; - -@SuppressWarnings("SameParameterValue") -public class ScrollViewActivity extends DemoBase { - - private BarChart chart; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_scrollview); - - setTitle("ScrollViewActivity"); - - chart = findViewById(R.id.chart1); - - chart.getDescription().setEnabled(false); +package info.appdev.chartexample + +import android.content.Intent +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import androidx.core.net.toUri +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.notimportant.DemoBase + +class ScrollViewActivity : DemoBase() { + private var chart: BarChart? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_scrollview) + + setTitle("ScrollViewActivity") + + chart = findViewById(R.id.chart1) + + chart!!.description.isEnabled = false // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) - chart.setDrawBarShadow(false); - chart.setDrawGridBackground(false); + chart!!.setDrawBarShadow(false) + chart!!.setDrawGridBackground(false) - XAxis xAxis = chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setDrawGridLines(false); + val xAxis = chart!!.xAxis + xAxis.position = XAxisPosition.BOTTOM + xAxis.setDrawGridLines(false) - chart.getAxisLeft().setDrawGridLines(false); + chart!!.axisLeft.setDrawGridLines(false) - chart.getLegend().setEnabled(false); + chart!!.legend.isEnabled = false - setData(10); - chart.setFitBars(true); + setData(10) + chart!!.setFitBars(true) } - private void setData(int count) { - Double[] sampleValues = DataTools.Companion.getValues(count); - ArrayList values = new ArrayList<>(); + private fun setData(count: Int) { + val sampleValues = getValues(count) + val values = ArrayList() - for (int i = 0; i < count; i++) { - float val = (sampleValues[i].floatValue() * count) + 15; - values.add(new BarEntry(i, (int) val)); + for (i in 0.. { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/ScrollViewActivity.java".toUri()) + startActivity(i) } } - return true; + return true } - @Override - public void saveToGallery() { /* Intentionally left empty */ } + public override fun saveToGallery() { /* Intentionally left empty */ + } } diff --git a/app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt index 6548e8ca2a..ba3d4fa564 100644 --- a/app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt @@ -142,7 +142,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleValues -> { mChart!!.data?.dataSets?.forEach { val set = it as LineDataSet - set.setDrawValues(!set.isDrawValuesEnabled) + set.isDrawValuesEnabled = !set.isDrawValuesEnabled } mChart!!.invalidate() } @@ -173,7 +173,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleCubic -> { 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 + set.setMode(if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER) } mChart!!.invalidate() } @@ -181,7 +181,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleStepped -> { mChart!!.data?.dataSets?.forEach { val set = it as LineDataSet - set.mode = if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED + set.setMode(if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED) } mChart!!.invalidate() } @@ -189,7 +189,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleHorizontalCubic -> { 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 + set.setMode(if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER) } mChart!!.invalidate() } @@ -242,7 +242,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, val values = ArrayList() val sampleValues = getValues(100) for (i in 0 until count) { - val `val` = (sampleValues[i]!!.toFloat() * range) + 3 + val `val` = (sampleValues[i].toFloat() * range) + 3 values.add(Entry(i.toFloat(), `val`)) } mChart!!.data?.let { @@ -265,22 +265,22 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, // set the line to be drawn like this "- - - - - -" set11.enableDashedLine(10f, 5f, 0f) set11.enableDashedHighlightLine(10f, 5f, 0f) - set11.color = Color.BLACK + set11.setColor(Color.BLACK) set11.setCircleColor(Color.BLACK) - set11.lineWidth = 1f - set11.circleRadius = 3f + set11.setLineWidth(1f) + set11.setCircleRadius(3f) set11.setDrawCircleHole(false) set11.valueTextSize = 9f set11.setDrawFilled(true) set11.formLineWidth = 1f - set11.setFormLineDashEffect(DashPathEffect(floatArrayOf(10f, 5f), 0f)) + set11.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f) set11.formSize = 15f - if (Utils.getSDKInt() >= 18) { + if (Utils.sDKInt >= 18) { // fill drawable only supported on api level 18 and above val drawable = ContextCompat.getDrawable(this, R.drawable.fade_blue) - set11.fillDrawable = drawable + set11.setFillDrawable(drawable) } else { - set11.fillColor = Color.BLACK + set11.setFillColor(Color.BLACK) } val dataSets = ArrayList() dataSets.add(set11) // add the datasets @@ -289,14 +289,14 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, val data = LineData(dataSets) // set data - mChart!!.data = data + mChart!!.setData(data) } - override fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture) { - Log.i("Gesture", "START, x: " + me.x + ", y: " + me.y) + override fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + Log.i("Gesture", "START, x: " + me?.x + ", y: " + me?.y) } - override fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartGesture) { + override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { Log.i("Gesture", "END, lastGesture: $lastPerformedGesture") // un-highlight values after the gesture is finished and no single-tap @@ -305,31 +305,31 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, } } - override fun onChartLongPressed(me: MotionEvent) { + override fun onChartLongPressed(me: MotionEvent?) { Log.i("LongPress", "Chart longpressed.") } - override fun onChartDoubleTapped(me: MotionEvent) { + override fun onChartDoubleTapped(me: MotionEvent?) { Log.i("DoubleTap", "Chart double-tapped.") } - override fun onChartSingleTapped(me: MotionEvent) { + override fun onChartSingleTapped(me: MotionEvent?) { Log.i("SingleTap", "Chart single-tapped.") } - override fun onChartFling(me1: MotionEvent, me2: MotionEvent, velocityX: Float, velocityY: Float) { + override fun onChartFling(me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float) { Log.i("Fling", "Chart flinged. VeloX: $velocityX, VeloY: $velocityY") } - override fun onChartScale(me: MotionEvent, scaleX: Float, scaleY: Float) { + override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) { Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY") } - override fun onChartTranslate(me: MotionEvent, dX: Float, dY: Float) { + override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) { Log.i("Translate / Move", "dX: $dX, dY: $dY") } - override fun onValueSelected(e: Entry, h: Highlight) { + override fun onValueSelected(e: Entry?, h: Highlight?) { Log.i("Entry selected", e.toString()) Log.i("LOWHIGH", "low: " + mChart!!.lowestVisibleX + ", high: " + mChart!!.highestVisibleX) Log.i("MIN MAX", "xmin: " + mChart!!.xChartMin + ", xmax: " + mChart!!.xChartMax + ", ymin: " + mChart!!.yChartMin + ", ymax: " + mChart!!.yChartMax) diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt index 5eae9f062c..0d30faee8e 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt @@ -1,282 +1,265 @@ -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; -import android.widget.SeekBar; -import android.widget.SeekBar.OnSeekBarChangeListener; -import android.widget.TextView; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.custom.MyAxisValueFormatter; -import info.appdev.chartexample.custom.MyValueFormatter; -import info.appdev.chartexample.notimportant.DemoBase; - -import java.util.ArrayList; -import java.util.List; - -public class StackedBarActivity extends DemoBase implements OnSeekBarChangeListener, OnChartValueSelectedListener { - - private BarChart chart; - private SeekBar seekBarX, seekBarY; - private TextView tvX, tvY; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_barchart); - - setTitle("StackedBarActivity"); - - tvX = findViewById(R.id.tvXMax); - tvY = findViewById(R.id.tvYMax); - - seekBarX = findViewById(R.id.seekBarX); - seekBarX.setOnSeekBarChangeListener(this); - - seekBarY = findViewById(R.id.seekBarY); - seekBarY.setOnSeekBarChangeListener(this); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); - - chart.getDescription().setEnabled(false); +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import android.widget.SeekBar +import android.widget.SeekBar.OnSeekBarChangeListener +import android.widget.TextView +import androidx.core.content.ContextCompat +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.custom.MyAxisValueFormatter +import info.appdev.chartexample.custom.MyValueFormatter +import info.appdev.chartexample.notimportant.DemoBase +import androidx.core.net.toUri + +class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { + private var chart: BarChart? = null + private var seekBarX: SeekBar? = null + private var seekBarY: SeekBar? = null + private var tvX: TextView? = null + private var tvY: TextView? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) + setContentView(R.layout.activity_barchart) + + setTitle("StackedBarActivity") + + tvX = findViewById(R.id.tvXMax) + tvY = findViewById(R.id.tvYMax) + + seekBarX = findViewById(R.id.seekBarX) + seekBarX!!.setOnSeekBarChangeListener(this) + + seekBarY = findViewById(R.id.seekBarY) + seekBarY!!.setOnSeekBarChangeListener(this) + + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) + + chart!!.description.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart.setMaxVisibleValueCount(40); + chart!!.setMaxVisibleValueCount(40) // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); + chart!!.setPinchZoom(false) - chart.setDrawGridBackground(false); - chart.setDrawBarShadow(false); + chart!!.setDrawGridBackground(false) + chart!!.setDrawBarShadow(false) - chart.setDrawValueAboveBar(false); - chart.setHighlightFullBarEnabled(false); + chart!!.setDrawValueAboveBar(false) + chart!!.setHighlightFullBarEnabled(false) // change the position of the y-labels - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setValueFormatter(new MyAxisValueFormatter()); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - chart.getAxisRight().setEnabled(false); + val leftAxis = chart!!.axisLeft + leftAxis.valueFormatter = MyAxisValueFormatter() + leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) + chart!!.axisRight.isEnabled = false - XAxis xLabels = chart.getXAxis(); - xLabels.setPosition(XAxisPosition.TOP); + val xLabels = chart!!.xAxis + xLabels.position = XAxisPosition.TOP // chart.setDrawXLabels(false); // chart.setDrawYLabels(false); // setting data - seekBarX.setProgress(12); - seekBarY.setProgress(100); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - l.setFormSize(8f); - l.setFormToTextSpace(4f); - l.setXEntrySpace(6f); + seekBarX!!.progress = 12 + seekBarY!!.progress = 100 + + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + l.formSize = 8f + l.formToTextSpace = 4f + l.xEntrySpace = 6f // chart.setDrawLegend(false); } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - - tvX.setText(String.valueOf(seekBarX.getProgress())); - tvY.setText(String.valueOf(seekBarY.getProgress())); - - ArrayList values = new ArrayList<>(); - Double[] sampleValues = DataTools.Companion.getValues(100 + 2); - - for (int i = 0; i < seekBarX.getProgress(); i++) { - float mul = (seekBarY.getProgress() + 1); - float val1 = (sampleValues[i].floatValue() * mul) + mul / 3; - float val2 = (sampleValues[i + 1].floatValue() * mul) + mul / 3; - float val3 = (sampleValues[i + 2].floatValue() * mul) + mul / 3; - values.add(new BarEntry( - i, - new float[]{val1, val2, val3}, - getResources().getDrawable(R.drawable.star))); + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + tvX!!.text = seekBarX!!.progress.toString() + tvY!!.text = seekBarY!!.progress.toString() + + val values = ArrayList() + val sampleValues = getValues(100 + 2) + + for (i in 0.. 0) { - set1 = (BarDataSet) chart.getData().getDataSetByIndex(0); - set1.setEntries(values); - chart.getData().notifyDataChanged(); - chart.notifyDataSetChanged(); + if (chart!!.data != null && + chart!!.data!!.dataSetCount > 0 + ) { + set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set1.entries = values + chart!!.data!!.notifyDataChanged() + chart!!.notifyDataSetChanged() } else { - set1 = new BarDataSet(values, "Statistics Vienna 2014"); - set1.setDrawIcons(false); - set1.setColors(getColors()); - set1.setStackLabels(new String[]{"Births", "Divorces", "Marriages"}); + set1 = BarDataSet(values, "Statistics Vienna 2014") + set1.isDrawIconsEnabled = false + set1.setColors(*this.colors) + set1.setStackLabels(arrayOf("Births", "Divorces", "Marriages")) - ArrayList dataSets = new ArrayList<>(); - dataSets.add(set1); + val dataSets = ArrayList() + dataSets.add(set1) - BarData data = new BarData(dataSets); - data.setValueFormatter(new MyValueFormatter()); - data.setValueTextColor(Color.WHITE); + val data = BarData(dataSets) + data.setValueFormatter(MyValueFormatter()) + data.setValueTextColor(Color.WHITE) - chart.setData(data); + chart!!.setData(data) } - chart.setFitBars(true); - chart.invalidate(); + chart!!.setFitBars(true) + chart!!.invalidate() } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bar, menu); - return true; + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.bar, menu) + return true } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java")); - startActivity(i); - break; + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java".toUri()) + startActivity(i) } - case R.id.actionToggleValues: { - List sets = chart.getData() - .getDataSets(); - for (IBarDataSet iSet : sets) { + R.id.actionToggleValues -> { + val sets: MutableList = chart!!.data!! + .dataSets - BarDataSet set = (BarDataSet) iSet; - set.setDrawValues(!set.isDrawValuesEnabled()); + for (iSet in sets) { + val set = iSet as BarDataSet + set.isDrawValuesEnabled = !set.isDrawValuesEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleIcons: { - List sets = chart.getData() - .getDataSets(); - for (IBarDataSet iSet : sets) { + R.id.actionToggleIcons -> { + val sets: MutableList = chart!!.data!! + .dataSets - BarDataSet set = (BarDataSet) iSet; - set.setDrawIcons(!set.isDrawIconsEnabled()); + for (iSet in sets) { + val set = iSet as BarDataSet + set.isDrawIconsEnabled = !set.isDrawIconsEnabled } - chart.invalidate(); - break; + chart!!.invalidate() } - case R.id.actionToggleHighlight: { - if (chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() } - break; } - case R.id.actionTogglePinch: { - chart.setPinchZoom(!chart.isPinchZoomEnabled()); - chart.invalidate(); - break; + R.id.actionTogglePinch -> { + chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + + chart!!.invalidate() } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() } - case R.id.actionToggleBarBorders: { - for (IBarDataSet set : chart.getData().getDataSets()) - ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); - chart.invalidate(); - break; + R.id.actionToggleBarBorders -> { + for (set in chart!!.data!!.dataSets) (set as BarDataSet).setBarBorderWidth(if (set.barBorderWidth == 1f) 0f else 1f) + + chart!!.invalidate() } - case R.id.animateX: { - chart.animateX(2000); - break; + + R.id.animateX -> { + chart!!.animateX(2000) } - case R.id.animateY: { - chart.animateY(2000); - break; + + R.id.animateY -> { + chart!!.animateY(2000) } - case R.id.animateXY: { - chart.animateXY(2000, 2000); - break; + R.id.animateXY -> { + chart!!.animateXY(2000, 2000) } - case R.id.actionSave: { + + R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); + saveToGallery() } else { - requestStoragePermission(chart); + requestStoragePermission(chart) } - break; } } - return true; + return true } - @Override - protected void saveToGallery() { - saveToGallery(chart, "StackedBarActivity"); + override fun saveToGallery() { + saveToGallery(chart, "StackedBarActivity") } - @Override - public void onStartTrackingTouch(SeekBar seekBar) {} + override fun onStartTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onStopTrackingTouch(SeekBar seekBar) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} - @Override - public void onValueSelected(Entry e, Highlight h) { + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null || h == null) return - BarEntry entry = (BarEntry) e; + val entry = e as BarEntry - if (entry.getYVals() != null) - Log.i("VAL SELECTED", "Value: " + entry.getYVals()[h.getStackIndex()]); - else - Log.i("VAL SELECTED", "Value: " + entry.getY()); + if (entry.yVals != null) Log.i("VAL SELECTED", "Value: " + entry.yVals!![h.stackIndex]) + else Log.i("VAL SELECTED", "Value: " + entry.y) } - @Override - public void onNothingSelected() {} + override fun onNothingSelected() {} - private int[] getColors() { + private val colors: IntArray + get() { + // have as many colors as stack-values per entry - // have as many colors as stack-values per entry - int[] colors = new int[3]; + val colors = IntArray(3) - System.arraycopy(ColorTemplate.MATERIAL_COLORS, 0, colors, 0, 3); + System.arraycopy(ColorTemplate.MATERIAL_COLORS, 0, colors, 0, 3) - return colors; - } + return colors + } } diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt index 0b144e5516..4c92622a1b 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt @@ -1,267 +1,247 @@ - -package info.appdev.chartexample; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; - -import androidx.core.content.ContextCompat; - -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.WindowManager; - -import com.github.mikephil.charting.charts.HorizontalBarChart; -import com.github.mikephil.charting.components.AxisBase; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.formatter.IValueFormatter; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.listener.OnChartValueSelectedListener; -import com.github.mikephil.charting.utils.ViewPortHandler; - -import info.appdev.chartexample.notimportant.DemoBase; - -import java.text.DecimalFormat; -import java.util.ArrayList; -import java.util.List; - -public class StackedBarActivityNegative extends DemoBase implements - OnChartValueSelectedListener { - - private HorizontalBarChart chart; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_age_distribution); - - setTitle("StackedBarActivityNegative"); - - chart = findViewById(R.id.chart1); - chart.setOnChartValueSelectedListener(this); - chart.setDrawGridBackground(false); - chart.getDescription().setEnabled(false); - - // scaling can now only be done on x- and y-axis separately - chart.setPinchZoom(false); - - chart.setDrawBarShadow(false); - chart.setDrawValueAboveBar(true); - chart.setHighlightFullBarEnabled(false); - - chart.getAxisLeft().setEnabled(false); - chart.getAxisRight().setAxisMaximum(25f); - chart.getAxisRight().setAxisMinimum(-25f); - chart.getAxisRight().setDrawGridLines(false); - chart.getAxisRight().setDrawZeroLine(true); - chart.getAxisRight().setLabelCount(7, false); - chart.getAxisRight().setValueFormatter(new CustomFormatter()); - chart.getAxisRight().setTextSize(9f); - - XAxis xAxis = chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTH_SIDED); - xAxis.setDrawGridLines(false); - xAxis.setDrawAxisLine(false); - xAxis.setTextSize(9f); - xAxis.setAxisMinimum(0f); - xAxis.setAxisMaximum(110f); - xAxis.setCenterAxisLabels(true); - xAxis.setLabelCount(12); - xAxis.setGranularity(10f); - xAxis.setValueFormatter(new IAxisValueFormatter() { - - private final DecimalFormat format = new DecimalFormat("###"); - - @Override - public String getFormattedValue(float value, AxisBase axis) { - return format.format(value) + "-" + format.format(value + 10); - } - }); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.HORIZONTAL); - l.setDrawInside(false); - l.setFormSize(8f); - l.setFormToTextSpace(4f); - l.setXEntrySpace(6f); - - // IMPORTANT: When using negative values in stacked bars, always make sure the negative values are in the array first - ArrayList values = new ArrayList<>(); - values.add(new BarEntry(5, new float[]{-10, 10})); - values.add(new BarEntry(15, new float[]{-12, 13})); - values.add(new BarEntry(25, new float[]{-15, 15})); - values.add(new BarEntry(35, new float[]{-17, 17})); - values.add(new BarEntry(45, new float[]{-19, 20})); - values.add(new BarEntry(45, new float[]{-19, 20}, getResources().getDrawable(R.drawable.star))); - values.add(new BarEntry(55, new float[]{-19, 19})); - values.add(new BarEntry(65, new float[]{-16, 16})); - values.add(new BarEntry(75, new float[]{-13, 14})); - values.add(new BarEntry(85, new float[]{-10, 11})); - values.add(new BarEntry(95, new float[]{-5, 6})); - values.add(new BarEntry(105, new float[]{-1, 2})); - - BarDataSet set = new BarDataSet(values, "Age Distribution"); - set.setDrawIcons(false); - set.setValueFormatter(new CustomFormatter()); - set.setValueTextSize(7f); - set.setAxisDependency(YAxis.AxisDependency.RIGHT); - set.setColors(Color.rgb(67, 67, 72), Color.rgb(124, 181, 236)); - set.setStackLabels(new String[]{ - "Men", "Women" - }); - - BarData data = new BarData(set); - data.setBarWidth(8.5f); - chart.setData(data); - chart.invalidate(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.bar, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - case R.id.viewGithub: { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java")); - startActivity(i); - break; - } - case R.id.actionToggleValues: { - List sets = chart.getData().getDataSets(); - - for (IBarDataSet iSet : sets) { - BarDataSet set = (BarDataSet) iSet; - set.setDrawValues(!set.isDrawValuesEnabled()); - } - - chart.invalidate(); - break; - } - case R.id.actionToggleIcons: { - List sets = chart.getData().getDataSets(); - - for (IBarDataSet iSet : sets) { - - BarDataSet set = (BarDataSet) iSet; - set.setDrawIcons(!set.isDrawIconsEnabled()); - } - - chart.invalidate(); - break; - } - case R.id.actionToggleHighlight: { - if (chart.getData() != null) { - chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled()); - chart.invalidate(); - } - break; - } - case R.id.actionTogglePinch: { - if (chart.isPinchZoomEnabled()) { - chart.setPinchZoom(false); - } else { - chart.setPinchZoom(true); - } - - chart.invalidate(); - break; - } - case R.id.actionToggleAutoScaleMinMax: { - chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled()); - chart.notifyDataSetChanged(); - break; - } - case R.id.actionToggleBarBorders: { - for (IBarDataSet set : chart.getData().getDataSets()) { - ((BarDataSet) set).setBarBorderWidth(set.barBorderWidth == 1.f ? 0.f : 1.f); - } - - chart.invalidate(); - break; - } - case R.id.animateX: { - chart.animateX(3000); - break; - } - case R.id.animateY: { - chart.animateY(3000); - break; - } - case R.id.animateXY: { - - chart.animateXY(3000, 3000); - break; - } - case R.id.actionSave: { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { - saveToGallery(); - } else { - requestStoragePermission(chart); - } - break; - } - } - return true; - } - - @Override - protected void saveToGallery() { - saveToGallery(chart, "StackedBarActivityNegative"); - } - - @Override - public void onValueSelected(Entry e, Highlight h) { - BarEntry entry = (BarEntry) e; - Log.i("VAL SELECTED", - "Value: " + Math.abs(entry.getYVals()[h.getStackIndex()])); - } - - @Override - public void onNothingSelected() { - Log.i("NOTING SELECTED", ""); - } - - private class CustomFormatter implements IValueFormatter, IAxisValueFormatter { - - private final DecimalFormat mFormat; - - CustomFormatter() { - mFormat = new DecimalFormat("###"); - } - - // data - @Override - public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { - return mFormat.format(Math.abs(value)) + "m"; - } - - // YAxis - @Override - public String getFormattedValue(float value, AxisBase axis) { - return mFormat.format(Math.abs(value)) + "m"; - } - } +package info.appdev.chartexample + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.net.Uri +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.view.WindowManager +import androidx.core.content.ContextCompat +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.components.AxisBase +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.formatter.IValueFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener +import com.github.mikephil.charting.utils.ViewPortHandler +import info.appdev.chartexample.notimportant.DemoBase +import java.text.DecimalFormat +import kotlin.math.abs +import androidx.core.net.toUri + +class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { + private var chart: HorizontalBarChart? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) + setContentView(R.layout.activity_age_distribution) + + setTitle("StackedBarActivityNegative") + + chart = findViewById(R.id.chart1) + chart!!.setOnChartValueSelectedListener(this) + chart!!.setDrawGridBackground(false) + chart!!.description.isEnabled = false + + // scaling can now only be done on x- and y-axis separately + chart!!.setPinchZoom(false) + + chart!!.setDrawBarShadow(false) + chart!!.setDrawValueAboveBar(true) + chart!!.setHighlightFullBarEnabled(false) + + chart!!.axisLeft.isEnabled = false + chart!!.axisRight.axisMaximum = 25f + chart!!.axisRight.axisMinimum = -25f + chart!!.axisRight.setDrawGridLines(false) + chart!!.axisRight.setDrawZeroLine(true) + chart!!.axisRight.setLabelCount(7, false) + chart!!.axisRight.valueFormatter = CustomFormatter() + chart!!.axisRight.textSize = 9f + + val xAxis = chart!!.xAxis + xAxis.position = XAxisPosition.BOTH_SIDED + xAxis.setDrawGridLines(false) + xAxis.setDrawAxisLine(false) + xAxis.textSize = 9f + xAxis.axisMinimum = 0f + xAxis.axisMaximum = 110f + xAxis.setCenterAxisLabels(true) + xAxis.labelCount = 12 + xAxis.granularity = 10f + xAxis.valueFormatter = object : IAxisValueFormatter { + private val format = DecimalFormat("###") + + override fun getFormattedValue(value: Float, axis: AxisBase?): String { + return format.format(value.toDouble()) + "-" + format.format((value + 10).toDouble()) + } + } + + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.HORIZONTAL + l.setDrawInside(false) + l.formSize = 8f + l.formToTextSpace = 4f + l.xEntrySpace = 6f + + // IMPORTANT: When using negative values in stacked bars, always make sure the negative values are in the array first + val values = ArrayList() + values.add(BarEntry(5f, floatArrayOf(-10f, 10f))) + values.add(BarEntry(15f, floatArrayOf(-12f, 13f))) + values.add(BarEntry(25f, floatArrayOf(-15f, 15f))) + values.add(BarEntry(35f, floatArrayOf(-17f, 17f))) + values.add(BarEntry(45f, floatArrayOf(-19f, 20f))) + values.add(BarEntry(45f, floatArrayOf(-19f, 20f), getResources().getDrawable(R.drawable.star))) + values.add(BarEntry(55f, floatArrayOf(-19f, 19f))) + values.add(BarEntry(65f, floatArrayOf(-16f, 16f))) + values.add(BarEntry(75f, floatArrayOf(-13f, 14f))) + values.add(BarEntry(85f, floatArrayOf(-10f, 11f))) + values.add(BarEntry(95f, floatArrayOf(-5f, 6f))) + values.add(BarEntry(105f, floatArrayOf(-1f, 2f))) + + val set = BarDataSet(values, "Age Distribution") + set.isDrawIconsEnabled = false + set.valueFormatter = CustomFormatter() + set.valueTextSize = 7f + set.axisDependency = AxisDependency.RIGHT + set.setColors(Color.rgb(67, 67, 72), Color.rgb(124, 181, 236)) + set.setStackLabels( + arrayOf( + "Men", "Women" + ) + ) + + val data = BarData(set) + data.barWidth = 8.5f + chart!!.setData(data) + chart!!.invalidate() + } + + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.bar, menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.viewGithub -> { + val i = Intent(Intent.ACTION_VIEW) + i.setData("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java".toUri()) + startActivity(i) + } + + R.id.actionToggleValues -> { + val sets: MutableList = chart!!.data!!.dataSets + + for (iSet in sets) { + val set = iSet as BarDataSet + set.isDrawValuesEnabled = !set.isDrawValuesEnabled + } + + chart!!.invalidate() + } + + R.id.actionToggleIcons -> { + val sets: MutableList = chart!!.data!!.dataSets + + for (iSet in sets) { + val set = iSet as BarDataSet + set.isDrawIconsEnabled = !set.isDrawIconsEnabled + } + + chart!!.invalidate() + } + + R.id.actionToggleHighlight -> { + if (chart!!.data != null) { + chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled + chart!!.invalidate() + } + } + + R.id.actionTogglePinch -> { + if (chart!!.isPinchZoomEnabled) { + chart!!.setPinchZoom(false) + } else { + chart!!.setPinchZoom(true) + } + + chart!!.invalidate() + } + + R.id.actionToggleAutoScaleMinMax -> { + chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled + chart!!.notifyDataSetChanged() + } + + R.id.actionToggleBarBorders -> { + for (set in chart!!.data!!.dataSets) { + (set as BarDataSet).setBarBorderWidth(if (set.barBorderWidth == 1f) 0f else 1f) + } + + chart!!.invalidate() + } + + R.id.animateX -> { + chart!!.animateX(3000) + } + + R.id.animateY -> { + chart!!.animateY(3000) + } + + R.id.animateXY -> { + chart!!.animateXY(3000, 3000) + } + + R.id.actionSave -> { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + saveToGallery() + } else { + requestStoragePermission(chart) + } + } + } + return true + } + + override fun saveToGallery() { + saveToGallery(chart, "StackedBarActivityNegative") + } + + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e == null || h == null) return + + val entry = e as BarEntry + Log.i( + "VAL SELECTED", + "Value: " + abs(entry.yVals!![h.stackIndex]) + ) + } + + override fun onNothingSelected() { + Log.i("NOTING SELECTED", "") + } + + private inner class CustomFormatter : IValueFormatter, IAxisValueFormatter { + private val mFormat = DecimalFormat("###") + + // data + override fun getFormattedValue(value: Float, entry: Entry?, dataSetIndex: Int, viewPortHandler: ViewPortHandler?): String { + return mFormat.format(abs(value).toDouble()) + "m" + } + + // YAxis + override fun getFormattedValue(value: Float, axis: AxisBase?): String { + return mFormat.format(abs(value).toDouble()) + "m" + } + } } diff --git a/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt b/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt index 94cf35e259..f4a8dd3415 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt +++ b/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt @@ -1,31 +1,29 @@ -package info.appdev.chartexample.custom; +package info.appdev.chartexample.custom -import android.graphics.Canvas; -import android.graphics.Paint; - -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.renderer.scatter.IShapeRenderer; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; +import android.graphics.Canvas +import android.graphics.Paint +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.renderer.scatter.IShapeRenderer +import com.github.mikephil.charting.utils.Utils.convertDpToPixel +import com.github.mikephil.charting.utils.ViewPortHandler /** * Custom shape renderer that draws a single line. * Created by philipp on 26/06/16. */ -public class CustomScatterShapeRenderer implements IShapeRenderer -{ - - @Override - public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint) { - - final float shapeHalf = Utils.convertDpToPixel(dataSet.scatterShapeSize) / 2f; +class CustomScatterShapeRenderer : IShapeRenderer { + override fun renderShape( + c: Canvas?, dataSet: IScatterDataSet, viewPortHandler: ViewPortHandler, + posX: Float, posY: Float, renderPaint: Paint + ) { + val shapeHalf = convertDpToPixel(dataSet.scatterShapeSize) / 2f - c.drawLine( - posX - shapeHalf, - posY - shapeHalf, - posX + shapeHalf, - posY + shapeHalf, - renderPaint); + c?.drawLine( + posX - shapeHalf, + posY - shapeHalf, + posX + shapeHalf, + posY + shapeHalf, + renderPaint + ) } } diff --git a/app/src/main/java/info/appdev/chartexample/custom/DayAxisValueFormatter.kt b/app/src/main/java/info/appdev/chartexample/custom/DayAxisValueFormatter.kt index 3dd38d72ee..abf3bbefb7 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/DayAxisValueFormatter.kt +++ b/app/src/main/java/info/appdev/chartexample/custom/DayAxisValueFormatter.kt @@ -5,7 +5,7 @@ import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.formatter.IAxisValueFormatter import kotlin.math.max -class DayAxisValueFormatter(private val chart: BarLineChartBase<*>) : IAxisValueFormatter { +class DayAxisValueFormatter(private val chart: BarLineChartBase<*, *, *>) : IAxisValueFormatter { private val months = arrayOf( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ) diff --git a/app/src/main/java/info/appdev/chartexample/custom/MyFillFormatter.kt b/app/src/main/java/info/appdev/chartexample/custom/MyFillFormatter.kt index a63f824cb5..4b6db9f627 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/MyFillFormatter.kt +++ b/app/src/main/java/info/appdev/chartexample/custom/MyFillFormatter.kt @@ -6,7 +6,7 @@ import com.github.mikephil.charting.interfaces.datasets.ILineDataSet @Suppress("unused") class MyFillFormatter(private val fillPos: Float) : IFillFormatter { - override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float { + override fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float { // your logic could be here return fillPos } diff --git a/app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.kt b/app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.kt index 5161c53793..04a2e693cd 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.kt +++ b/app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.kt @@ -1,18 +1,15 @@ - -package info.appdev.chartexample.custom; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.widget.TextView; - -import com.github.mikephil.charting.components.MarkerView; -import com.github.mikephil.charting.data.CandleEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; - -import info.appdev.chartexample.R; +package info.appdev.chartexample.custom + +import android.annotation.SuppressLint +import android.content.Context +import android.widget.TextView +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.CandleEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Utils.formatNumber +import info.appdev.chartexample.R /** * Custom implementation of the MarkerView. @@ -20,33 +17,21 @@ import info.appdev.chartexample.R; * @author Philipp Jahoda */ @SuppressLint("ViewConstructor") -public class MyMarkerView extends MarkerView { - - private final TextView tvContent; - - public MyMarkerView(Context context, int layoutResource) { - super(context, layoutResource); - - tvContent = findViewById(R.id.tvContent); - } +class MyMarkerView(context: Context, layoutResource: Int) : MarkerView(context, layoutResource) { + private val tvContent = findViewById(R.id.tvContent) // runs every time the MarkerView is redrawn, can be used to update the // content (user-interface) - @Override - public void refreshContent(Entry e, Highlight highlight) { - - if (e instanceof CandleEntry ce) { - tvContent.setText(Utils.formatNumber(ce.getHigh(), 0, true)); + override fun refreshContent(e: Entry, highlight: Highlight) { + if (e is CandleEntry) { + tvContent?.text = formatNumber(e.high, 0, true) } else { - - tvContent.setText(Utils.formatNumber(e.getY(), 0, true)); + tvContent?.text = formatNumber(e.y, 0, true) } - super.refreshContent(e, highlight); + super.refreshContent(e, highlight) } - @Override - public MPPointF getOffset() { - return new MPPointF(-(getWidth() / 2), -getHeight()); - } + override val offset: MPPointF + get() = MPPointF(-(width / 2).toFloat(), -height.toFloat()) } diff --git a/app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.kt b/app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.kt index 686634dad5..c14ffd14d0 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.kt +++ b/app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.kt @@ -1,19 +1,15 @@ - -package info.appdev.chartexample.custom; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.graphics.Typeface; -import android.widget.TextView; - -import com.github.mikephil.charting.components.MarkerView; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.MPPointF; - -import java.text.DecimalFormat; - -import info.appdev.chartexample.R; +package info.appdev.chartexample.custom + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Typeface +import android.widget.TextView +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import info.appdev.chartexample.R +import java.text.DecimalFormat /** * Custom implementation of the MarkerView. @@ -21,29 +17,22 @@ import info.appdev.chartexample.R; * @author Philipp Jahoda */ @SuppressLint("ViewConstructor") -public class RadarMarkerView extends MarkerView { - - private final TextView tvContent; - private final DecimalFormat format = new DecimalFormat("##0"); +class RadarMarkerView(context: Context, layoutResource: Int) : MarkerView(context, layoutResource) { + private val tvContent = findViewById(R.id.tvContent) + private val format = DecimalFormat("##0") - public RadarMarkerView(Context context, int layoutResource) { - super(context, layoutResource); - - tvContent = findViewById(R.id.tvContent); - tvContent.setTypeface(Typeface.createFromAsset(context.getAssets(), "OpenSans-Light.ttf")); + init { + tvContent?.setTypeface(Typeface.createFromAsset(context.assets, "OpenSans-Light.ttf")) } // runs every time the MarkerView is redrawn, can be used to update the // content (user-interface) - @Override - public void refreshContent(Entry e, Highlight highlight) { - tvContent.setText(String.format("%s %%", format.format(e.getY()))); + override fun refreshContent(e: Entry, highlight: Highlight) { + tvContent?.text = String.format("%s %%", format.format(e.y.toDouble())) - super.refreshContent(e, highlight); + super.refreshContent(e, highlight) } - @Override - public MPPointF getOffset() { - return new MPPointF(-(getWidth() / 2), -getHeight() - 10); - } + override val offset: MPPointF + get() = MPPointF(-(width / 2).toFloat(), (-height - 10).toFloat()) } diff --git a/app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.kt b/app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.kt index e56a8896a6..b69ca47ebe 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.kt +++ b/app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.kt @@ -1,62 +1,46 @@ - -package info.appdev.chartexample.custom; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.widget.TextView; - -import com.github.mikephil.charting.components.MarkerView; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; - -import info.appdev.chartexample.R; +package info.appdev.chartexample.custom + +import android.annotation.SuppressLint +import android.content.Context +import android.widget.TextView +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Utils.formatNumber +import info.appdev.chartexample.R /** * Custom implementation of the MarkerView. * * @author Philipp Jahoda */ -@SuppressWarnings("unused") +@Suppress("unused") @SuppressLint("ViewConstructor") -public class StackedBarsMarkerView extends MarkerView { - - private TextView tvContent; - - public StackedBarsMarkerView(Context context, int layoutResource) { - super(context, layoutResource); - - tvContent = findViewById(R.id.tvContent); - } +class StackedBarsMarkerView(context: Context, layoutResource: Int) : MarkerView(context, layoutResource) { + private val tvContent = findViewById(R.id.tvContent) // runs every time the MarkerView is redrawn, can be used to update the // content (user-interface) - @Override - public void refreshContent(Entry e, Highlight highlight) { - - if (e instanceof BarEntry) { - - BarEntry be = (BarEntry) e; - - if(be.getYVals() != null) { + override fun refreshContent(e: Entry, highlight: Highlight) { + if (e is BarEntry) { + val be = e + if (be.yVals != null) { // draw the stack value - tvContent.setText(Utils.formatNumber(be.getYVals()[highlight.getStackIndex()], 0, true)); + + tvContent?.text = formatNumber(be.yVals!![highlight.stackIndex], 0, true) } else { - tvContent.setText(Utils.formatNumber(be.getY(), 0, true)); + tvContent?.text = formatNumber(be.y, 0, true) } } else { - - tvContent.setText(Utils.formatNumber(e.getY(), 0, true)); + tvContent?.text = formatNumber(e.y, 0, true) } - super.refreshContent(e, highlight); + super.refreshContent(e, highlight) } - @Override - public MPPointF getOffset() { - return new MPPointF(-(getWidth() / 2), -getHeight()); - } + override val offset: MPPointF + get() = MPPointF(-(width / 2).toFloat(), -height.toFloat()) } diff --git a/app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.kt b/app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.kt index 7bebd17ded..5ecf9c281f 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.kt +++ b/app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.kt @@ -1,19 +1,15 @@ - -package info.appdev.chartexample.custom; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.widget.TextView; - -import com.github.mikephil.charting.components.MarkerView; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.formatter.IAxisValueFormatter; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.MPPointF; - -import java.text.DecimalFormat; - -import info.appdev.chartexample.R; +package info.appdev.chartexample.custom + +import android.annotation.SuppressLint +import android.content.Context +import android.widget.TextView +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.formatter.IAxisValueFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import info.appdev.chartexample.R +import java.text.DecimalFormat /** * Custom implementation of the MarkerView. @@ -21,33 +17,19 @@ import info.appdev.chartexample.R; * @author Philipp Jahoda */ @SuppressLint("ViewConstructor") -public class XYMarkerView extends MarkerView { - - private final TextView tvContent; - private final IAxisValueFormatter xAxisValueFormatter; +class XYMarkerView(context: Context, private val xAxisValueFormatter: IAxisValueFormatter) : MarkerView(context, R.layout.custom_marker_view) { + private val tvContent = findViewById(R.id.tvContent) - private final DecimalFormat format; - - public XYMarkerView(Context context, IAxisValueFormatter xAxisValueFormatter) { - super(context, R.layout.custom_marker_view); - - this.xAxisValueFormatter = xAxisValueFormatter; - tvContent = findViewById(R.id.tvContent); - format = new DecimalFormat("###.0"); - } + private val format = DecimalFormat("###.0") // runs every time the MarkerView is redrawn, can be used to update the // content (user-interface) - @Override - public void refreshContent(Entry e, Highlight highlight) { + override fun refreshContent(e: Entry, highlight: Highlight) { + tvContent?.text = String.format("x: %s, y: %s", xAxisValueFormatter.getFormattedValue(e.x, null), format.format(e.y.toDouble())) - tvContent.setText(String.format("x: %s, y: %s", xAxisValueFormatter.getFormattedValue(e.getX(), null), format.format(e.getY()))); - - super.refreshContent(e, highlight); + super.refreshContent(e, highlight) } - @Override - public MPPointF getOffset() { - return new MPPointF(-(getWidth() / 2), -getHeight()); - } + override val offset: MPPointF + get() = MPPointF(-(width / 2).toFloat(), -height.toFloat()) } diff --git a/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt b/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt index 053211a060..1c23957ab5 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt @@ -1,113 +1,97 @@ -package info.appdev.chartexample.fragments; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.MotionEvent; -import android.view.View; -import android.view.ViewGroup; -import android.widget.FrameLayout; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.listener.ChartTouchListener; -import com.github.mikephil.charting.listener.OnChartGestureListener; - -import info.appdev.chartexample.R; -import info.appdev.chartexample.custom.MyMarkerView; - - -public class BarChartFrag extends SimpleFragment implements OnChartGestureListener { - - @NonNull - public static Fragment newInstance() { - return new BarChartFrag(); - } - - private BarChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_bar, container, false); +package info.appdev.chartexample.fragments + +import android.graphics.Typeface +import android.os.Bundle +import android.util.Log +import android.view.LayoutInflater +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture +import com.github.mikephil.charting.listener.OnChartGestureListener +import info.appdev.chartexample.R +import info.appdev.chartexample.custom.MyMarkerView + +class BarChartFrag : SimpleFragment(), OnChartGestureListener { + private var chart: BarChart? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_bar, container, false) // create a new chart object - chart = new BarChart(getActivity()); - chart.getDescription().setEnabled(false); - chart.setOnChartGestureListener(this); + chart = BarChart(requireActivity()) + chart?.description?.isEnabled = false + chart?.onChartGestureListener = this - MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view); - mv.setChartView(chart); // For bounds control - chart.setMarker(mv); + val mv = MyMarkerView(requireContext(), R.layout.custom_marker_view) + mv.chartView = chart // For bounds control + chart?.setMarker(mv) - chart.setDrawGridBackground(false); - chart.setDrawBarShadow(false); + chart?.setDrawGridBackground(false) + chart?.setDrawBarShadow(false) - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - chart.setData(generateBarData(1, 20000)); + chart?.setData(generateBarData(1, 20000f)) - Legend l = chart.getLegend(); - l.setTypeface(tf); + val l = chart?.legend + l?.typeface = tf - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tf); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) + val leftAxis = chart?.axisLeft + leftAxis?.typeface = tf + leftAxis?.axisMinimum = 0f // this replaces setStartAtZero(true) - chart.getAxisRight().setEnabled(false); + chart?.axisRight?.isEnabled = false - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(false); + val xAxis = chart?.xAxis + xAxis?.isEnabled = false // programmatically add the chart - FrameLayout parent = v.findViewById(R.id.parentLayout); - parent.addView(chart); + val parent = v.findViewById(R.id.parentLayout) + parent?.addView(chart) - return v; + return v } - @Override - public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { - Log.i("Gesture", "START"); + override fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + Log.i("Gesture", "START") } - @Override - public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { - Log.i("Gesture", "END"); - chart.highlightValues(null); + override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + Log.i("Gesture", "END") + chart?.highlightValues(null) } - @Override - public void onChartLongPressed(MotionEvent me) { - Log.i("LongPress", "Chart long pressed."); + override fun onChartLongPressed(me: MotionEvent?) { + Log.i("LongPress", "Chart long pressed.") } - @Override - public void onChartDoubleTapped(MotionEvent me) { - Log.i("DoubleTap", "Chart double-tapped."); + override fun onChartDoubleTapped(me: MotionEvent?) { + Log.i("DoubleTap", "Chart double-tapped.") } - @Override - public void onChartSingleTapped(MotionEvent me) { - Log.i("SingleTap", "Chart single-tapped."); + override fun onChartSingleTapped(me: MotionEvent?) { + Log.i("SingleTap", "Chart single-tapped.") } - @Override - public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { - Log.i("Fling", "Chart fling. VelocityX: " + velocityX + ", VelocityY: " + velocityY); + override fun onChartFling(me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float) { + Log.i("Fling", "Chart fling. VelocityX: $velocityX, VelocityY: $velocityY") } - @Override - public void onChartScale(MotionEvent me, float scaleX, float scaleY) { - Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY); + override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) { + Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY") } - @Override - public void onChartTranslate(MotionEvent me, float dX, float dY) { - Log.i("Translate / Move", "dX: " + dX + ", dY: " + dY); - } + override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) { + Log.i("Translate / Move", "dX: $dX, dY: $dY") + } + companion object { + fun newInstance(): Fragment { + return BarChartFrag() + } + } } diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt index fd6aeb4cc0..0c030c18cf 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt @@ -1,56 +1,48 @@ -package info.appdev.chartexample.fragments; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import info.appdev.chartexample.R; +package info.appdev.chartexample.fragments -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; +import android.graphics.Typeface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.LineChart +import info.appdev.chartexample.R -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; +class ComplexityFragment : SimpleFragment() { + private var chart: LineChart? = null + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_line, container, false) -public class ComplexityFragment extends SimpleFragment { + chart = v.findViewById(R.id.lineChart1) - @NonNull - public static Fragment newInstance() { - return new ComplexityFragment(); - } - - @SuppressWarnings("FieldCanBeLocal") - private LineChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_line, container, false); + chart?.description?.isEnabled = false - chart = v.findViewById(R.id.lineChart1); + chart?.setDrawGridBackground(false) - chart.getDescription().setEnabled(false); + chart?.setData(complexity) + chart?.animateX(3000) - chart.setDrawGridBackground(false); + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - chart.setData(getComplexity()); - chart.animateX(3000); + val l = chart?.legend + l?.typeface = tf - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); + val leftAxis = chart?.axisLeft + leftAxis?.typeface = tf - Legend l = chart.getLegend(); - l.setTypeface(tf); + chart?.axisRight?.isEnabled = false - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tf); + val xAxis = chart?.xAxis + xAxis?.isEnabled = false - chart.getAxisRight().setEnabled(false); - - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(false); + return v + } - return v; + companion object { + fun newInstance(): Fragment { + return ComplexityFragment() + } } } diff --git a/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt b/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt index bd698348c0..e05e464ff7 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt @@ -1,65 +1,60 @@ -package info.appdev.chartexample.fragments; -import android.graphics.Color; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import info.appdev.chartexample.R; +package info.appdev.chartexample.fragments + +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.text.style.RelativeSizeSpan +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend +import info.appdev.chartexample.R + +class PieChartFrag : SimpleFragment() { + private var chart: PieChart? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_pie, container, false) + + chart = v.findViewById(R.id.pieChart1) + chart?.description?.isEnabled = false + + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") + + chart?.setCenterTextTypeface(tf) + chart?.centerText = generateCenterText() + chart?.setCenterTextSize(10f) + chart?.setCenterTextTypeface(tf) -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.RelativeSizeSpan; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.components.Legend; + // radius of the center hole in percent of maximum radius + chart?.holeRadius = 45f + chart?.transparentCircleRadius = 50f + val l = chart?.legend + l?.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l?.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l?.orientation = Legend.LegendOrientation.VERTICAL + l?.setDrawInside(false) -public class PieChartFrag extends SimpleFragment { + chart?.setData(generatePieData()) - @NonNull - public static Fragment newInstance() { - return new PieChartFrag(); + return v } - @SuppressWarnings("FieldCanBeLocal") - private PieChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_pie, container, false); - - chart = v.findViewById(R.id.pieChart1); - chart.getDescription().setEnabled(false); - - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); - - chart.setCenterTextTypeface(tf); - chart.setCenterText(generateCenterText()); - chart.setCenterTextSize(10f); - chart.setCenterTextTypeface(tf); - - // radius of the center hole in percent of maximum radius - chart.setHoleRadius(45f); - chart.setTransparentCircleRadius(50f); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - - chart.setData(generatePieData()); - - return v; + private fun generateCenterText(): SpannableString { + val s = SpannableString("Revenues\nQuarters 2015") + s.setSpan(RelativeSizeSpan(2f), 0, 8, 0) + s.setSpan(ForegroundColorSpan(Color.GRAY), 8, s.length, 0) + return s } - private SpannableString generateCenterText() { - SpannableString s = new SpannableString("Revenues\nQuarters 2015"); - s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0); - s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0); - return s; + companion object { + fun newInstance(): Fragment { + return PieChartFrag() + } } } diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt b/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt index f7c7c3d1f3..f92b8a937d 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt @@ -1,70 +1,61 @@ -package info.appdev.chartexample.fragments; +package info.appdev.chartexample.fragments -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; +import android.graphics.Typeface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.ScatterChart +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import info.appdev.chartexample.R +import info.appdev.chartexample.custom.MyMarkerView -import com.github.mikephil.charting.charts.ScatterChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; +class ScatterChartFrag : SimpleFragment() { + private var chart: ScatterChart? = null -import info.appdev.chartexample.R; -import info.appdev.chartexample.custom.MyMarkerView; + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_scatter, container, false) + chart = v.findViewById(R.id.scatterChart1) + chart?.description?.isEnabled = false -public class ScatterChartFrag extends SimpleFragment { + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - @NonNull - public static Fragment newInstance() { - return new ScatterChartFrag(); - } - - @SuppressWarnings("FieldCanBeLocal") - private ScatterChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_scatter, container, false); - - chart = v.findViewById(R.id.scatterChart1); - chart.getDescription().setEnabled(false); + val mv = MyMarkerView(requireContext(), R.layout.custom_marker_view) + mv.chartView = chart // For bounds control + chart?.setMarker(mv) - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); + chart?.setDrawGridBackground(false) + chart?.setData(generateScatterData(6, 10000f)) - MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view); - mv.setChartView(chart); // For bounds control - chart.setMarker(mv); + val xAxis = chart?.xAxis + xAxis?.isEnabled = true + xAxis?.position = XAxisPosition.BOTTOM - chart.setDrawGridBackground(false); - chart.setData(generateScatterData(6, 10000)); + val leftAxis = chart?.axisLeft + leftAxis?.typeface = tf - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(true); - xAxis.setPosition(XAxisPosition.BOTTOM); + val rightAxis = chart?.axisRight + rightAxis?.typeface = tf + rightAxis?.setDrawGridLines(false) - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tf); - - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setTypeface(tf); - rightAxis.setDrawGridLines(false); - - Legend l = chart.getLegend(); - l.setWordWrapEnabled(true); - l.setTypeface(tf); - l.setFormSize(14f); - l.setTextSize(9f); + val l = chart?.legend + l?.isWordWrapEnabled = true + l?.typeface = tf + l?.formSize = 14f + l?.textSize = 9f // increase the space between legend & bottom and legend & content - l.setYOffset(13f); - chart.setExtraBottomOffset(16f); + l?.yOffset = 13f + chart?.extraBottomOffset = 16f + + return v + } - return v; + companion object { + fun newInstance(): Fragment { + return ScatterChartFrag() + } } } diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt index 249c4f5ba2..4220068db0 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt @@ -1,194 +1,184 @@ -package info.appdev.chartexample.fragments; - -import android.graphics.Color; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.github.mikephil.charting.charts.ScatterChart; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.data.PieDataSet; -import com.github.mikephil.charting.data.PieEntry; -import com.github.mikephil.charting.data.ScatterData; -import com.github.mikephil.charting.data.ScatterDataSet; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.FileUtils; -import info.appdev.chartexample.DataTools; - -import java.util.ArrayList; - -@SuppressWarnings({"SameParameterValue", "WeakerAccess"}) -public abstract class SimpleFragment extends Fragment { - - private Typeface tf; - - public SimpleFragment() { - - } - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Regular.ttf"); - return super.onCreateView(inflater, container, savedInstanceState); +package info.appdev.chartexample.fragments + +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.ScatterChart.ScatterShape +import com.github.mikephil.charting.charts.ScatterChart.ScatterShape.Companion.allDefaultShapes +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.data.PieDataSet +import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.ScatterData +import com.github.mikephil.charting.data.ScatterDataSet +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.FileUtils.loadEntriesFromAssets +import info.appdev.chartexample.DataTools.Companion.getValues + +abstract class SimpleFragment : Fragment() { + private var tf: Typeface? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Regular.ttf") + return super.onCreateView(inflater, container, savedInstanceState) } - protected BarData generateBarData(int dataSets, float range) { - int count = 12; - Double[] values = DataTools.Companion.getValues(count); - ArrayList sets = new ArrayList<>(); + protected fun generateBarData(dataSets: Int, range: Float): BarData { + val count = 12 + val values = getValues(count) + val sets = ArrayList() - for(int i = 0; i < dataSets; i++) { + for (i in 0..() - ArrayList entries = new ArrayList<>(); - - for(int j = 0; j < count; j++) { - entries.add(new BarEntry(j, (float) (values[j].floatValue() * range) + range / 4)); + for (j in 0.. sets = new ArrayList<>(); - - ScatterChart.ScatterShape[] shapes = ScatterChart.ScatterShape.getAllDefaultShapes(); + protected fun generateScatterData(dataSets: Int, range: Float): ScatterData { + val count = 100 + val values = getValues(count) + val sets = ArrayList() - for(int i = 0; i < dataSets; i++) { + val shapes: Array = allDefaultShapes - ArrayList entries = new ArrayList<>(); + for (i in 0..() - for(int j = 0; j < count; j++) { - entries.add(new Entry(j, (float) (values[j].floatValue() * range) + range / 4)); + for (j in 0..() - int count = 4; - Double[] values = DataTools.Companion.getValues(count); - ArrayList entries1 = new ArrayList<>(); - - for(int i = 0; i < count; i++) { - entries1.add(new PieEntry((float) ((values[i].floatValue() * 60) + 40), "Quarter " + (i+1))); + for (i in 0.. sets = new ArrayList<>(); - LineDataSet ds1 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "sine.txt"), "Sine function"); - LineDataSet ds2 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "cosine.txt"), "Cosine function"); + protected fun generateLineData(): LineData { + val sets = ArrayList() + val ds1 = LineDataSet(loadEntriesFromAssets(requireContext().assets, "sine.txt"), "Sine function") + val ds2 = LineDataSet(loadEntriesFromAssets(requireContext().assets, "cosine.txt"), "Cosine function") - ds1.setLineWidth(2f); - ds2.setLineWidth(2f); + ds1.setLineWidth(2f) + ds2.setLineWidth(2f) - ds1.setDrawCircles(false); - ds2.setDrawCircles(false); + ds1.setDrawCircles(false) + ds2.setDrawCircles(false) - ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]); - ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]); + ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]) + ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]) // load DataSets from files in assets folder - sets.add(ds1); - sets.add(ds2); + sets.add(ds1) + sets.add(ds2) - LineData d = new LineData(sets); - d.setValueTypeface(tf); - return d; + val d = LineData(sets) + d.setValueTypeface(tf) + return d } - protected LineData getComplexity() { - - ArrayList sets = new ArrayList<>(); - - LineDataSet ds1 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "n.txt"), "O(n)"); - LineDataSet ds2 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "nlogn.txt"), "O(nlogn)"); - LineDataSet ds3 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "square.txt"), "O(n\u00B2)"); - LineDataSet ds4 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "three.txt"), "O(n\u00B3)"); - - ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]); - ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]); - ds3.setColor(ColorTemplate.VORDIPLOM_COLORS[2]); - ds4.setColor(ColorTemplate.VORDIPLOM_COLORS[3]); - - ds1.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]); - ds2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[1]); - ds3.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[2]); - ds4.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[3]); - - ds1.setLineWidth(2.5f); - ds1.setCircleRadius(3f); - ds2.setLineWidth(2.5f); - ds2.setCircleRadius(3f); - ds3.setLineWidth(2.5f); - ds3.setCircleRadius(3f); - ds4.setLineWidth(2.5f); - ds4.setCircleRadius(3f); - - - // load DataSets from files in assets folder - sets.add(ds1); - sets.add(ds2); - sets.add(ds3); - sets.add(ds4); - - LineData d = new LineData(sets); - d.setValueTypeface(tf); - return d; - } + protected val complexity: LineData + get() { + val sets = ArrayList() + + val ds1 = + LineDataSet(loadEntriesFromAssets(requireContext().assets, "n.txt"), "O(n)") + val ds2 = + LineDataSet(loadEntriesFromAssets(requireContext().assets, "nlogn.txt"), "O(nlogn)") + val ds3 = + LineDataSet(loadEntriesFromAssets(requireContext().assets, "square.txt"), "O(n\u00B2)") + val ds4 = + LineDataSet(loadEntriesFromAssets(requireContext().assets, "three.txt"), "O(n\u00B3)") + + ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]) + ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]) + ds3.setColor(ColorTemplate.VORDIPLOM_COLORS[2]) + ds4.setColor(ColorTemplate.VORDIPLOM_COLORS[3]) + + ds1.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]) + ds2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[1]) + ds3.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[2]) + ds4.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[3]) + + ds1.setLineWidth(2.5f) + ds1.setCircleRadius(3f) + ds2.setLineWidth(2.5f) + ds2.setCircleRadius(3f) + ds3.setLineWidth(2.5f) + ds3.setCircleRadius(3f) + ds4.setLineWidth(2.5f) + ds4.setCircleRadius(3f) + + + // load DataSets from files in assets folder + sets.add(ds1) + sets.add(ds2) + sets.add(ds3) + sets.add(ds4) + + val d = LineData(sets) + d.setValueTypeface(tf) + return d + } - private final String[] mLabels = new String[] { "Company A", "Company B", "Company C", "Company D", "Company E", "Company F" }; + private val mLabels: Array = arrayOf("Company A", "Company B", "Company C", "Company D", "Company E", "Company F") - private String getLabel(int i) { - return mLabels[i]; + private fun getLabel(i: Int): String { + return mLabels[i] } } diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt index 68858b64cb..eba12584b2 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt @@ -1,58 +1,50 @@ -package info.appdev.chartexample.fragments; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import info.appdev.chartexample.R; +package info.appdev.chartexample.fragments -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; +import android.graphics.Typeface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.LineChart +import info.appdev.chartexample.R -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; +class SineCosineFragment : SimpleFragment() { + private var chart: LineChart? = null + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_line, container, false) -public class SineCosineFragment extends SimpleFragment { + chart = v.findViewById(R.id.lineChart1) - @NonNull - public static Fragment newInstance() { - return new SineCosineFragment(); - } - - @SuppressWarnings("FieldCanBeLocal") - private LineChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_line, container, false); + chart?.description?.isEnabled = false - chart = v.findViewById(R.id.lineChart1); + chart?.setDrawGridBackground(false) - chart.getDescription().setEnabled(false); + chart?.setData(generateLineData()) + chart?.animateX(3000) - chart.setDrawGridBackground(false); + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - chart.setData(generateLineData()); - chart.animateX(3000); + val l = chart?.legend + l?.typeface = tf - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); + val leftAxis = chart?.axisLeft + leftAxis?.typeface = tf + leftAxis?.axisMaximum = 1.2f + leftAxis?.axisMinimum = -1.2f - Legend l = chart.getLegend(); - l.setTypeface(tf); + chart?.axisRight?.isEnabled = false - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tf); - leftAxis.setAxisMaximum(1.2f); - leftAxis.setAxisMinimum(-1.2f); + val xAxis = chart?.xAxis + xAxis?.isEnabled = false - chart.getAxisRight().setEnabled(false); - - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(false); + return v + } - return v; + companion object { + fun newInstance(): Fragment { + return SineCosineFragment() + } } } diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ViewPagerSimpleChartDemo.kt b/app/src/main/java/info/appdev/chartexample/fragments/ViewPagerSimpleChartDemo.kt index eb179f3cfc..f229ab4574 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/ViewPagerSimpleChartDemo.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/ViewPagerSimpleChartDemo.kt @@ -1,12 +1,16 @@ package info.appdev.chartexample.fragments import android.content.Intent -import android.net.Uri import android.os.Bundle -import android.view.* +import android.view.Gravity +import android.view.Menu +import android.view.MenuItem +import android.view.View +import android.view.WindowManager import android.widget.FrameLayout import android.widget.Toast import androidx.coordinatorlayout.widget.CoordinatorLayout +import androidx.core.net.toUri import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter @@ -24,23 +28,23 @@ class ViewPagerSimpleChartDemo : DemoBase() { window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) setContentView(R.layout.activity_awesomedesign) val pager = findViewById(R.id.pager) - pager.offscreenPageLimit = 3 - pager.adapter = PageAdapter(supportFragmentManager) + pager?.offscreenPageLimit = 3 + pager?.adapter = PageAdapter(supportFragmentManager) showSnackbar("Swipe left and right for more awesome design examples!") } - private inner class PageAdapter(fm: FragmentManager?) : FragmentPagerAdapter(fm!!) { + private inner class PageAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) { override fun getItem(pos: Int): Fragment { - var f: Fragment? = null - when (pos) { - 0 -> f = SineCosineFragment.newInstance() - 1 -> f = ComplexityFragment.newInstance() - 2 -> f = BarChartFrag.newInstance() - 3 -> f = ScatterChartFrag.newInstance() - 4 -> f = PieChartFrag.newInstance() + val f = when (pos) { + 0 -> SineCosineFragment.newInstance() + 1 -> ComplexityFragment.newInstance() + 2 -> BarChartFrag.newInstance() + 3 -> ScatterChartFrag.newInstance() + 4 -> PieChartFrag.newInstance() + else -> throw ArrayIndexOutOfBoundsException(pos) } - return f!! + return f } override fun getCount(): Int { @@ -57,7 +61,8 @@ class ViewPagerSimpleChartDemo : DemoBase() { when (item.itemId) { R.id.viewGithub -> { val i = Intent(Intent.ACTION_VIEW) - i.data = Uri.parse("https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/fragments/SimpleChartDemo.java") + i.data = + "https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/com/xxmassdeveloper/mpchartexample/fragments/SimpleChartDemo.java".toUri() startActivity(i) } } @@ -68,7 +73,7 @@ class ViewPagerSimpleChartDemo : DemoBase() { public override fun saveToGallery() = Unit private fun showSnackbar(text: String) { - val viewPos : View = findViewById(android.R.id.content) + val viewPos = findViewById(android.R.id.content) ?: return val snackbar = Snackbar.make(viewPos, text, Snackbar.LENGTH_SHORT) val view = snackbar.view when (val params = view.layoutParams) { diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt index 20e34da0f4..0cd27f959b 100644 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt @@ -1,92 +1,77 @@ -package info.appdev.chartexample.listviewitems; +package info.appdev.chartexample.listviewitems -import android.annotation.SuppressLint; -import android.content.Context; -import android.graphics.Typeface; -import android.view.LayoutInflater; -import android.view.View; +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Typeface +import android.view.LayoutInflater +import android.view.View +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.ChartData +import info.appdev.chartexample.R -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.ChartData; +class BarChartItem(cd: ChartData<*, *>, c: Context) : ChartItem(cd) { + private val mTf = Typeface.createFromAsset(c.assets, "OpenSans-Regular.ttf") -import info.appdev.chartexample.R; - -public class BarChartItem extends ChartItem { - - private final Typeface mTf; - - public BarChartItem(ChartData cd, Context c) { - super(cd); - - mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); - } - - @Override - public int getItemType() { - return TYPE_BARCHART; - } + override val itemType: Int + get() = TYPE_BARCHART @SuppressLint("InflateParams") - @Override - public View getView(int position, View convertView, Context c) { - - ViewHolder holder; + override fun getView(position: Int, convertView: View?, c: Context): View { + var convertView = convertView + val holder: ViewHolder if (convertView == null) { - - holder = new ViewHolder(); + holder = ViewHolder() convertView = LayoutInflater.from(c).inflate( - R.layout.list_item_barchart, null); - holder.chart = convertView.findViewById(R.id.chart); - - convertView.setTag(holder); + R.layout.list_item_barchart, null + ) + holder.chart = convertView.findViewById(R.id.chart) + convertView.tag = holder } else { - holder = (ViewHolder) convertView.getTag(); + holder = convertView.tag as ViewHolder } // apply styling - holder.chart.getDescription().setEnabled(false); - holder.chart.setDrawGridBackground(false); - holder.chart.setDrawBarShadow(false); - - XAxis xAxis = holder.chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setTypeface(mTf); - xAxis.setDrawGridLines(false); - xAxis.setDrawAxisLine(true); - - YAxis leftAxis = holder.chart.getAxisLeft(); - leftAxis.setTypeface(mTf); - leftAxis.setLabelCount(5, false); - leftAxis.setSpaceTop(20f); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - YAxis rightAxis = holder.chart.getAxisRight(); - rightAxis.setTypeface(mTf); - rightAxis.setLabelCount(5, false); - rightAxis.setSpaceTop(20f); - rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - mChartData.setValueTypeface(mTf); + holder.chart?.description?.isEnabled = false + holder.chart?.setDrawGridBackground(false) + holder.chart?.setDrawBarShadow(false) + + val xAxis = holder.chart?.xAxis + xAxis?.position = XAxisPosition.BOTTOM + xAxis?.typeface = mTf + xAxis?.setDrawGridLines(false) + xAxis?.setDrawAxisLine(true) + + val leftAxis = holder.chart?.axisLeft + leftAxis?.typeface = mTf + leftAxis?.setLabelCount(5, false) + leftAxis?.spaceTop = 20f + leftAxis?.axisMinimum = 0f // this replaces setStartAtZero(true) + + val rightAxis = holder.chart?.axisRight + rightAxis?.typeface = mTf + rightAxis?.setLabelCount(5, false) + rightAxis?.spaceTop = 20f + rightAxis?.axisMinimum = 0f // this replaces setStartAtZero(true) + + mChartData.setValueTypeface(mTf) // set data - holder.chart.setData((BarData) mChartData); - holder.chart.setFitBars(true); + holder.chart?.setData(mChartData as BarData?) + holder.chart?.setFitBars(true) // do not forget to refresh the chart // holder.chart.invalidate(); - holder.chart.animateY(700); + holder.chart?.animateY(700) - return convertView; + return convertView } - private static class ViewHolder { - BarChart chart; + private class ViewHolder { + var chart: BarChart? = null } } diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt index eb5300c2ae..ce08c1c55b 100644 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt @@ -1,29 +1,24 @@ -package info.appdev.chartexample.listviewitems; +package info.appdev.chartexample.listviewitems -import android.content.Context; -import android.view.View; - -import com.github.mikephil.charting.data.ChartData; +import android.content.Context +import android.view.View +import com.github.mikephil.charting.data.ChartData /** * Base class of the Chart ListView items * @author philipp - * */ -@SuppressWarnings("unused") -public abstract class ChartItem { +@Suppress("unused") +abstract class ChartItem internal constructor(cd: ChartData<*, *>) { + var mChartData: ChartData<*, *> = cd - static final int TYPE_BARCHART = 0; - static final int TYPE_LINECHART = 1; - static final int TYPE_PIECHART = 2; + abstract val itemType: Int - ChartData mChartData; + abstract fun getView(position: Int, convertView: View?, c: Context): View - ChartItem(ChartData cd) { - this.mChartData = cd; + companion object { + const val TYPE_BARCHART: Int = 0 + const val TYPE_LINECHART: Int = 1 + const val TYPE_PIECHART: Int = 2 } - - public abstract int getItemType(); - - public abstract View getView(int position, View convertView, Context c); } diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt index 7d95061314..0700fbcd26 100644 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt @@ -1,89 +1,73 @@ +package info.appdev.chartexample.listviewitems -package info.appdev.chartexample.listviewitems; +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Typeface +import android.view.LayoutInflater +import android.view.View +import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.data.LineData +import info.appdev.chartexample.R -import android.annotation.SuppressLint; -import android.content.Context; -import android.graphics.Typeface; -import android.view.LayoutInflater; -import android.view.View; +class LineChartItem(cd: ChartData<*, *>, c: Context) : ChartItem(cd) { + private val mTf = Typeface.createFromAsset(c.assets, "OpenSans-Regular.ttf") -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.data.LineData; - -import info.appdev.chartexample.R; - -public class LineChartItem extends ChartItem { - - private final Typeface mTf; - - public LineChartItem(ChartData cd, Context c) { - super(cd); - - mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); - } - - @Override - public int getItemType() { - return TYPE_LINECHART; - } + override val itemType: Int + get() = TYPE_LINECHART @SuppressLint("InflateParams") - @Override - public View getView(int position, View convertView, Context c) { - - ViewHolder holder; + override fun getView(position: Int, convertView: View?, c: Context): View { + var convertView = convertView + val holder: ViewHolder if (convertView == null) { - - holder = new ViewHolder(); + holder = ViewHolder() convertView = LayoutInflater.from(c).inflate( - R.layout.list_item_linechart, null); - holder.chart = convertView.findViewById(R.id.chart); - - convertView.setTag(holder); + R.layout.list_item_linechart, null + ) + holder.chart = convertView.findViewById(R.id.chart) + convertView.tag = holder } else { - holder = (ViewHolder) convertView.getTag(); + holder = convertView.tag as ViewHolder } // apply styling // holder.chart.setValueTypeface(mTf); - holder.chart.getDescription().setEnabled(false); - holder.chart.setDrawGridBackground(false); - - XAxis xAxis = holder.chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setTypeface(mTf); - xAxis.setDrawGridLines(false); - xAxis.setDrawAxisLine(true); - - YAxis leftAxis = holder.chart.getAxisLeft(); - leftAxis.setTypeface(mTf); - leftAxis.setLabelCount(5, false); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - YAxis rightAxis = holder.chart.getAxisRight(); - rightAxis.setTypeface(mTf); - rightAxis.setLabelCount(5, false); - rightAxis.setDrawGridLines(false); - rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) + holder.chart!!.description.isEnabled = false + holder.chart!!.setDrawGridBackground(false) + + val xAxis = holder.chart!!.xAxis + xAxis.position = XAxisPosition.BOTTOM + xAxis.typeface = mTf + xAxis.setDrawGridLines(false) + xAxis.setDrawAxisLine(true) + + val leftAxis = holder.chart!!.axisLeft + leftAxis.typeface = mTf + leftAxis.setLabelCount(5, false) + leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) + + val rightAxis = holder.chart!!.axisRight + rightAxis.typeface = mTf + rightAxis.setLabelCount(5, false) + rightAxis.setDrawGridLines(false) + rightAxis.axisMinimum = 0f // this replaces setStartAtZero(true) // set data - holder.chart.setData((LineData) mChartData); + holder.chart!!.setData(mChartData as LineData?) // do not forget to refresh the chart // holder.chart.invalidate(); - holder.chart.animateX(750); + holder.chart!!.animateX(750) - return convertView; + return convertView } - private static class ViewHolder { - LineChart chart; + private class ViewHolder { + var chart: LineChart? = null } } diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt index c46916f7f5..a0794f7d20 100644 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt @@ -1,106 +1,96 @@ - -package info.appdev.chartexample.listviewitems; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.graphics.Color; -import android.graphics.Typeface; -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.RelativeSizeSpan; -import android.view.LayoutInflater; -import android.view.View; - -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.formatter.PercentFormatter; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.R; - -public class PieChartItem extends ChartItem { - - private final Typeface mTf; - private final SpannableString mCenterText; - - public PieChartItem(ChartData cd, Context c) { - super(cd); - - mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); - mCenterText = generateCenterText(); +package info.appdev.chartexample.listviewitems + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.graphics.Typeface +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.text.style.RelativeSizeSpan +import android.view.LayoutInflater +import android.view.View +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.formatter.PercentFormatter +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.ColorTemplate.holoBlue +import info.appdev.chartexample.R + +class PieChartItem(cd: ChartData<*, *>, c: Context) : ChartItem(cd) { + private val mTf = Typeface.createFromAsset(c.assets, "OpenSans-Regular.ttf") + private val mCenterText: SpannableString + + init { + mCenterText = generateCenterText() } - @Override - public int getItemType() { - return TYPE_PIECHART; - } + override val itemType: Int + get() = TYPE_PIECHART @SuppressLint("InflateParams") - @Override - public View getView(int position, View convertView, Context c) { - - ViewHolder holder; + override fun getView(position: Int, convertView: View?, c: Context): View { + var convertView = convertView + val holder: ViewHolder if (convertView == null) { - - holder = new ViewHolder(); + holder = ViewHolder() convertView = LayoutInflater.from(c).inflate( - R.layout.list_item_piechart, null); - holder.chart = convertView.findViewById(R.id.chart); - - convertView.setTag(holder); + R.layout.list_item_piechart, null + ) + holder.chart = convertView.findViewById(R.id.chart) + convertView.tag = holder } else { - holder = (ViewHolder) convertView.getTag(); + holder = convertView.tag as ViewHolder } // apply styling - holder.chart.getDescription().setEnabled(false); - holder.chart.setHoleRadius(52f); - holder.chart.setTransparentCircleRadius(57f); - holder.chart.setCenterText(mCenterText); - holder.chart.setCenterTextTypeface(mTf); - holder.chart.setCenterTextSize(9f); - holder.chart.setUsePercentValues(true); - holder.chart.setExtraOffsets(5, 10, 50, 10); - - mChartData.setValueFormatter(new PercentFormatter()); - mChartData.setValueTypeface(mTf); - mChartData.setValueTextSize(11f); - mChartData.setValueTextColor(Color.WHITE); + holder.chart!!.description.isEnabled = false + holder.chart!!.holeRadius = 52f + holder.chart!!.transparentCircleRadius = 57f + holder.chart!!.centerText = mCenterText + holder.chart!!.setCenterTextTypeface(mTf) + holder.chart!!.setCenterTextSize(9f) + holder.chart!!.setUsePercentValues(true) + holder.chart!!.setExtraOffsets(5f, 10f, 50f, 10f) + + mChartData.setValueFormatter(PercentFormatter()) + mChartData.setValueTypeface(mTf) + mChartData.setValueTextSize(11f) + mChartData.setValueTextColor(Color.WHITE) // set data - holder.chart.setData((PieData) mChartData); + holder.chart!!.setData(mChartData as PieData?) - Legend l = holder.chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - l.setYEntrySpace(0f); - l.setYOffset(0f); + val l = holder.chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) + l.yEntrySpace = 0f + l.yOffset = 0f // do not forget to refresh the chart // holder.chart.invalidate(); - holder.chart.animateY(900); + holder.chart!!.animateY(900) - return convertView; + return convertView } - private SpannableString generateCenterText() { - SpannableString s = new SpannableString("MPAndroidChart\ncreated by\nPhilipp Jahoda"); - s.setSpan(new RelativeSizeSpan(1.6f), 0, 14, 0); - s.setSpan(new ForegroundColorSpan(ColorTemplate.VORDIPLOM_COLORS[0]), 0, 14, 0); - s.setSpan(new RelativeSizeSpan(.9f), 14, 25, 0); - s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, 25, 0); - s.setSpan(new RelativeSizeSpan(1.4f), 25, s.length(), 0); - s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), 25, s.length(), 0); - return s; + private fun generateCenterText(): SpannableString { + val s = SpannableString("MPAndroidChart\ncreated by\nPhilipp Jahoda") + s.setSpan(RelativeSizeSpan(1.6f), 0, 14, 0) + s.setSpan(ForegroundColorSpan(ColorTemplate.VORDIPLOM_COLORS[0]), 0, 14, 0) + s.setSpan(RelativeSizeSpan(.9f), 14, 25, 0) + s.setSpan(ForegroundColorSpan(Color.GRAY), 14, 25, 0) + s.setSpan(RelativeSizeSpan(1.4f), 25, s.length, 0) + s.setSpan(ForegroundColorSpan(holoBlue), 25, s.length, 0) + return s } - private static class ViewHolder { - PieChart chart; + private class ViewHolder { + var chart: PieChart? = null } } diff --git a/app/src/main/java/info/appdev/chartexample/notimportant/DemoBase.kt b/app/src/main/java/info/appdev/chartexample/notimportant/DemoBase.kt index d9a372fe4e..7058d2e131 100644 --- a/app/src/main/java/info/appdev/chartexample/notimportant/DemoBase.kt +++ b/app/src/main/java/info/appdev/chartexample/notimportant/DemoBase.kt @@ -14,6 +14,8 @@ import com.github.mikephil.charting.charts.Chart import com.google.android.material.snackbar.Snackbar import info.appdev.chartexample.R import java.text.DateFormatSymbols +import androidx.core.view.size +import androidx.core.view.get abstract class DemoBase : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback { @@ -41,14 +43,15 @@ abstract class DemoBase : AppCompatActivity(), ActivityCompat.OnRequestPermissio override fun onPrepareOptionsMenu(menu: Menu?): Boolean { menu?.let { - for (i in 0 until menu.size()) { - val menuItem: MenuItem = menu.getItem(i) + for (i in 0 until menu.size) { + val menuItem: MenuItem = menu[i] optionMenus.add(menuItem.title.toString()) } } return super.onPrepareOptionsMenu(menu) } + @Deprecated("This method has been deprecated in favor of using the\n {@link OnBackPressedDispatcher} via {@link #getOnBackPressedDispatcher()}.\n The OnBackPressedDispatcher controls how back button events are dispatched\n to one or more {@link OnBackPressedCallback} objects.") override fun onBackPressed() { super.onBackPressed() overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity) @@ -82,7 +85,7 @@ abstract class DemoBase : AppCompatActivity(), ActivityCompat.OnRequestPermissio } } - protected fun saveToGallery(chart: Chart<*>?, name: String) { + protected fun saveToGallery(chart: Chart<*, *, *>?, name: String) { chart?.let { if (chart.saveToGallery(name + "_" + System.currentTimeMillis(), 70)) Toast.makeText(applicationContext, "Saving SUCCESSFUL!", Toast.LENGTH_SHORT).show() diff --git a/app/src/main/java/info/appdev/chartexample/notimportant/MainActivity.kt b/app/src/main/java/info/appdev/chartexample/notimportant/MainActivity.kt index fd4405ffec..78eddcdcbb 100644 --- a/app/src/main/java/info/appdev/chartexample/notimportant/MainActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/notimportant/MainActivity.kt @@ -46,6 +46,7 @@ import info.appdev.chartexample.ScrollViewActivity import info.appdev.chartexample.SpecificPositionsLineChartActivity import info.appdev.chartexample.StackedBarActivity import info.appdev.chartexample.StackedBarActivityNegative +import androidx.core.net.toUri class MainActivity : AppCompatActivity(), OnItemClickListener { @@ -58,8 +59,8 @@ class MainActivity : AppCompatActivity(), OnItemClickListener { Utils.init(this) val adapter = MenuAdapter(this, menuItems) val lv = findViewById(R.id.listViewMain) - lv.adapter = adapter - lv.onItemClickListener = this + lv?.adapter = adapter + lv?.onItemClickListener = this } override fun onItemClick(av: AdapterView<*>?, v: View, pos: Int, arg3: Long) { @@ -78,7 +79,7 @@ class MainActivity : AppCompatActivity(), OnItemClickListener { when (item.itemId) { R.id.viewGithub -> { i = Intent(Intent.ACTION_VIEW) - i.data = Uri.parse("https://github.com/AppDevNext/AndroidChart") + i.data = "https://github.com/AppDevNext/AndroidChart".toUri() startActivity(i) } R.id.report -> { @@ -90,7 +91,7 @@ class MainActivity : AppCompatActivity(), OnItemClickListener { } R.id.website -> { i = Intent(Intent.ACTION_VIEW) - i.data = Uri.parse("http://at.linkedin.com/in/philippjahoda") + i.data = "http://at.linkedin.com/in/philippjahoda".toUri() startActivity(i) } } diff --git a/app/src/main/java/info/appdev/chartexample/notimportant/MenuAdapter.kt b/app/src/main/java/info/appdev/chartexample/notimportant/MenuAdapter.kt index 256da36206..f9cea8eaba 100644 --- a/app/src/main/java/info/appdev/chartexample/notimportant/MenuAdapter.kt +++ b/app/src/main/java/info/appdev/chartexample/notimportant/MenuAdapter.kt @@ -10,7 +10,7 @@ import android.widget.ArrayAdapter import android.widget.TextView import info.appdev.chartexample.R -internal class MenuAdapter(context: Context, objects: List?>?) : ArrayAdapter?>(context, 0, objects!!) { +internal class MenuAdapter(context: Context, objects: List>) : ArrayAdapter?>(context, 0, objects) { private val mTypeFaceLight: Typeface = Typeface.createFromAsset(context.assets, "OpenSans-Light.ttf") private val mTypeFaceRegular: Typeface = Typeface.createFromAsset(context.assets, "OpenSans-Regular.ttf") From 0a164053577d6190192b10f7459d3defc9d47e87 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sun, 14 Sep 2025 09:47:34 -0400 Subject: [PATCH 6/9] Clean up setters --- .../mikephil/charting/charts/BarChart.kt | 48 ++---- .../charting/charts/BarLineChartBase.kt | 47 +----- .../mikephil/charting/charts/BubbleChart.kt | 5 +- .../charting/charts/CandleStickChart.kt | 5 +- .../mikephil/charting/charts/CombinedChart.kt | 67 ++++----- .../mikephil/charting/charts/LineChart.kt | 9 +- .../mikephil/charting/charts/ScatterChart.kt | 5 +- .../mikephil/charting/data/LineDataSet.kt | 142 ++++++------------ .../charting/data/LineRadarDataSet.kt | 78 +++------- .../dataprovider/BarDataProvider.kt | 8 +- .../dataprovider/BubbleDataProvider.kt | 2 +- .../dataprovider/CandleDataProvider.kt | 2 +- .../dataprovider/LineDataProvider.kt | 2 +- .../dataprovider/ScatterDataProvider.kt | 2 +- .../interfaces/datasets/ILineDataSet.kt | 18 +-- .../interfaces/datasets/ILineRadarDataSet.kt | 21 +-- .../appdev/chartexample/AnotherBarActivity.kt | 7 +- .../appdev/chartexample/BarChartActivity.kt | 6 +- .../BarChartActivityMultiDataset.kt | 4 +- .../chartexample/BarChartActivitySinus.kt | 6 +- .../chartexample/BarChartPositiveNegative.kt | 6 +- .../chartexample/BubbleChartActivity.kt | 4 +- .../chartexample/CandleStickChartActivity.kt | 5 +- .../chartexample/CombinedChartActivity.kt | 19 ++- .../chartexample/CubicLineChartActivity.kt | 34 ++--- .../info/appdev/chartexample/DataTools.kt | 22 ++- .../appdev/chartexample/DrawChartActivity.kt | 6 +- .../chartexample/DynamicalAddingActivity.kt | 10 +- .../appdev/chartexample/FilledLineActivity.kt | 40 ++--- .../HorizontalBarChartActivity.kt | 9 +- .../HorizontalBarNegativeChartActivity.kt | 6 +- .../chartexample/InvertedLineChartActivity.kt | 10 +- .../appdev/chartexample/LineChartActivity.kt | 12 +- .../chartexample/LineChartActivityColored.kt | 10 +- .../chartexample/LineChartDualAxisActivity.kt | 42 +++--- .../info/appdev/chartexample/LineChartTime.kt | 26 ++-- .../chartexample/ListViewBarChartActivity.kt | 2 +- .../ListViewMultiChartActivity.kt | 8 +- .../chartexample/MultiLineChartActivity.kt | 20 ++- .../chartexample/PerformanceLineChart.kt | 10 +- .../appdev/chartexample/RadarChartActivity.kt | 18 +-- .../chartexample/RealtimeLineChartActivity.kt | 10 +- .../chartexample/ScatterChartActivity.kt | 2 +- .../appdev/chartexample/ScrollViewActivity.kt | 4 +- .../SpecificPositionsLineChartActivity.kt | 24 +-- .../appdev/chartexample/StackedBarActivity.kt | 8 +- .../StackedBarActivityNegative.kt | 11 +- .../chartexample/fragments/BarChartFrag.kt | 4 +- .../fragments/ComplexityFragment.kt | 2 +- .../fragments/ScatterChartFrag.kt | 2 +- .../chartexample/fragments/SimpleFragment.kt | 24 +-- .../fragments/SineCosineFragment.kt | 2 +- .../listviewitems/BarChartItem.kt | 4 +- .../listviewitems/LineChartItem.kt | 2 +- 54 files changed, 376 insertions(+), 526 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt index b967d839a0..26086364b1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.kt @@ -143,53 +143,34 @@ open class BarChart : BarLineChartBase, BarDataP getTransformer(set.axisDependency).rectValueToPixel(outputRect) } - /** - * If set to true, all values are drawn above their bars, instead of below their top. - * - */ - fun setDrawValueAboveBar(enabled: Boolean) { - mDrawValueAboveBar = enabled - } - /** * returns true if drawing values above bars is enabled, false if not * */ - override val isDrawValueAboveBarEnabled: Boolean + override var isDrawValueAboveBarEnabled: Boolean get() = mDrawValueAboveBar - - /** - * If set to true, a grey area is drawn behind each bar that indicates the maximum value. Enabling his will reduce - * performance by about 50%. - * - */ - fun setDrawBarShadow(enabled: Boolean) { - mDrawBarShadow = enabled - } + set(value) { + mDrawValueAboveBar = value + } /** * returns true if drawing shadows (maxvalue) for each bar is enabled, false if not * */ - override val isDrawBarShadowEnabled: Boolean + override var isDrawBarShadowEnabled: Boolean get() = mDrawBarShadow - - /** - * Set this to true to make the highlight operation full-bar oriented, false to make it highlight single values (relevant - * only for stacked). If enabled, highlighting operations will highlight the whole bar, even if only a single stack entry - * was tapped. - * Default: false - * - */ - fun setHighlightFullBarEnabled(enabled: Boolean) { - mHighlightFullBarEnabled = enabled - } + set(value) { + mDrawBarShadow = value + } /** * @return true the highlight operation is be full-bar oriented, false if single-value */ - override val isHighlightFullBarEnabled: Boolean + override var isHighlightFullBarEnabled: Boolean get() = mHighlightFullBarEnabled + set(value) { + mHighlightFullBarEnabled = value + } /** * Highlights the value at the given x-value in the given DataSet. Provide @@ -203,8 +184,11 @@ open class BarChart : BarLineChartBase, BarDataP super.highlightValue(Highlight(x, dataSetIndex, dataIndex), false) } - override val barData: BarData? + override var barData: BarData? get() = mData + set(value) { + mData = value + } /** * Adds half of the bar width to each side of the x-axis range in order to allow the bars of the barchart to be diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt index 469d9691c9..378790ce0b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.kt @@ -163,7 +163,7 @@ abstract class BarLineChartBase, B mRenderer = BubbleChartRenderer(this, mAnimator, viewPortHandler) } - override val bubbleData: BubbleData? + override var bubbleData: BubbleData? get() = mData + set(value) { + mData = value + } override val accessibilityDescription: String? get() = "This is bubble chart" diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt index 72eb645fe1..d9ffdb81e1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CandleStickChart.kt @@ -27,8 +27,11 @@ class CandleStickChart : BarLineChartBase /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt index d440db12e2..7a6bd8d6f6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/LineChart.kt @@ -18,8 +18,11 @@ open class LineChart : BarLineChartBase, LineData mRenderer = LineChartRenderer(this, mAnimator, viewPortHandler) } - override val lineData: LineData - get() = mData ?: LineData() + override var lineData: LineData? + get() = mData + set(value) { + mData = value + } public override fun onDetachedFromWindow() { // releases the bitmap in the renderer to avoid oom error @@ -32,7 +35,7 @@ open class LineChart : BarLineChartBase, LineData override val accessibilityDescription: String? get() { val lineData = lineData - val numberOfPoints = lineData.entryCount + val numberOfPoints = lineData?.entryCount ?: return null // Min and max values... val yAxisValueFormatter = axisLeft.valueFormatter diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt index cf69e9f440..29f74b9673 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/ScatterChart.kt @@ -30,8 +30,11 @@ class ScatterChart : BarLineChartBase, Scat xAxis.spaceMax = 0.5f } - override val scatterData: ScatterData? + override var scatterData: ScatterData? get() = mData + set(value) { + mData = scatterData + } /** * Predefined ScatterShapes that allow the specification of a shape a ScatterDataSet should be drawn with. diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt index e8db4f34b1..3a00d8e995 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt @@ -113,68 +113,41 @@ open class LineDataSet(yVals: MutableList, label: String) : LineRadarData * * @return */ - override val mode: Mode? + override var mode: Mode? get() = mMode + set(value) { + mMode = value + } - /** - * Returns the drawing mode for this LineDataSet - * - * @return - */ - fun setMode(mode: Mode?) { - mMode = mode - } - - /** - * Sets the intensity for cubic lines (if enabled). Max = 1f = very cubic, - * Min = 0.05f = low cubic effect, Default: 0.2f - * - * @param intensity - */ - fun setCubicIntensity(intensity: Float) { - var intensity = intensity - if (intensity > 1f) intensity = 1f - if (intensity < 0.05f) intensity = 0.05f - - mCubicIntensity = intensity - } - - override val cubicIntensity: Float + override var cubicIntensity: Float get() = mCubicIntensity + set(value) { + var intensity = value + if (intensity > 1f) intensity = 1f + if (intensity < 0.05f) intensity = 0.05f - /** - * Sets the radius of the drawn circles. - * Default radius = 4f, Min = 1f - * - * @param radius - */ - fun setCircleRadius(radius: Float) { - if (radius >= 1f) { - mCircleRadius = Utils.convertDpToPixel(radius) - } else { - Log.e("LineDataSet", "Circle radius cannot be < 1") + mCubicIntensity = intensity } - } - override val circleRadius: Float + override var circleRadius: Float get() = mCircleRadius - - /** - * Sets the hole radius of the drawn circles. - * Default radius = 2f, Min = 0.5f - * - * @param holeRadius - */ - fun setCircleHoleRadius(holeRadius: Float) { - if (holeRadius >= 0.5f) { - mCircleHoleRadius = Utils.convertDpToPixel(holeRadius) - } else { - Log.e("LineDataSet", "Circle radius cannot be < 0.5") + set(value) { + if (value >= 1f) { + mCircleRadius = Utils.convertDpToPixel(value) + } else { + Log.e("LineDataSet", "Circle radius cannot be < 1") + } } - } - override val circleHoleRadius: Float + override var circleHoleRadius: Float get() = mCircleHoleRadius + set(holeRadius) { + if (holeRadius >= 0.5f) { + mCircleHoleRadius = Utils.convertDpToPixel(holeRadius) + } else { + Log.e("LineDataSet", "Circle radius cannot be < 0.5") + } + } @get:Deprecated("") @set:Deprecated("") @@ -193,7 +166,7 @@ open class LineDataSet(yVals: MutableList, label: String) : LineRadarData * @param size */ set(size) { - setCircleRadius(size) + circleRadius = size } /** @@ -223,21 +196,17 @@ open class LineDataSet(yVals: MutableList, label: String) : LineRadarData override val isDashedLineEnabled: Boolean get() = mDashPathEffect != null - override val dashPathEffect: DashPathEffect? + override var dashPathEffect: DashPathEffect? get() = mDashPathEffect + set(value) { + mDashPathEffect = value + } - /** - * set this to true to enable the drawing of circle indicators for this - * DataSet, default true - * - * @param enabled - */ - fun setDrawCircles(enabled: Boolean) { - this.mDrawCircles = enabled - } - - override val isDrawCirclesEnabled: Boolean + override var isDrawCirclesEnabled: Boolean get() = mDrawCircles + set(value) { + mDrawCircles = value + } @Deprecated("") override val isDrawCubicEnabled: Boolean @@ -308,42 +277,23 @@ open class LineDataSet(yVals: MutableList, label: String) : LineRadarData circleColors.clear() } - /** - * Sets the color of the inner circle of the line-circles. - * - * @param color - */ - fun setCircleHoleColor(color: Int) { - mCircleHoleColor = color - } - - override val circleHoleColor: Int + override var circleHoleColor: Int get() = mCircleHoleColor + set(value) { + mCircleHoleColor = value + } - /** - * Set this to true to allow drawing a hole in each data circle. - * - * @param enabled - */ - fun setDrawCircleHole(enabled: Boolean) { - mDrawCircleHole = enabled - } - - override val isDrawCircleHoleEnabled: Boolean + override var isDrawCircleHoleEnabled: Boolean get() = mDrawCircleHole + set(value) { + mDrawCircleHole = value + } - /** - * Sets a custom IFillFormatter to the chart that handles the position of the - * filled-line for each DataSet. Set this to null to use the default logic. - * - * @param formatter - */ - fun setFillFormatter(formatter: IFillFormatter?) { - mFillFormatter = formatter ?: DefaultFillFormatter() - } - - override val fillFormatter: IFillFormatter + override var fillFormatter: IFillFormatter get() = mFillFormatter + set(value) { + mFillFormatter = value + } enum class Mode { LINEAR, diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt index f637cef287..5143318177 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineRadarDataSet.kt @@ -37,67 +37,39 @@ abstract class LineRadarDataSet(yVals: MutableList, label: String) */ private var mDrawFilled = false - override val fillColor: Int + override var fillColor: Int get() = mFillColor + set(value) { + mFillColor = value + mFillDrawable = null + } - /** - * Sets the color that is used for filling the area below the line. - * Resets an eventually set "fillDrawable". - * - * @param color - */ - fun setFillColor(color: Int) { - mFillColor = color - mFillDrawable = null - } - - override val fillDrawable: Drawable? + override var fillDrawable: Drawable? get() = mFillDrawable + set(value) { + mFillDrawable = value + } - /** - * Sets the drawable to be used to fill the area below the line. - * - * @param drawable - */ - fun setFillDrawable(drawable: Drawable?) { - this.mFillDrawable = drawable - } - - override val fillAlpha: Int + override var fillAlpha: Int get() = mFillAlpha + set(value) { + mFillAlpha = value + } - /** - * sets the alpha value (transparency) that is used for filling the line - * surface (0-255), default: 85 - * - * @param alpha - */ - fun setFillAlpha(alpha: Int) { - mFillAlpha = alpha - } - - /** - * set the line width of the chart (min = 0.2f, max = 10f); default 1f NOTE: - * thinner line == better performance, thicker line == worse performance - * - * @param width - */ - fun setLineWidth(width: Float) { - var width = width - if (width < 0.0f) width = 0.0f - if (width > 10.0f) width = 10.0f - mLineWidth = Utils.convertDpToPixel(width) - } - - override val lineWidth: Float + override var lineWidth: Float get() = mLineWidth - - override fun setDrawFilled(enabled: Boolean) { - mDrawFilled = enabled - } - - override val isDrawFilledEnabled: Boolean + set(value) { + var width = value + if (width < 0.0f) width = 0.0f + if (width > 10.0f) width = 10.0f + mLineWidth = Utils.convertDpToPixel(width) + } + + override var isDrawFilledEnabled: Boolean get() = mDrawFilled + set(value) { + mDrawFilled = value + } protected fun copy(lineRadarDataSet: LineRadarDataSet<*>) { super.copy(lineRadarDataSet) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt index 3f857bb55d..499a2a53e3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BarDataProvider.kt @@ -3,8 +3,8 @@ package com.github.mikephil.charting.interfaces.dataprovider import com.github.mikephil.charting.data.BarData interface BarDataProvider : BarLineScatterCandleBubbleDataProvider { - val barData: BarData? - val isDrawBarShadowEnabled: Boolean - val isDrawValueAboveBarEnabled: Boolean - val isHighlightFullBarEnabled: Boolean + var barData: BarData? + var isDrawBarShadowEnabled: Boolean + var isDrawValueAboveBarEnabled: Boolean + var isHighlightFullBarEnabled: Boolean } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt index 10cecf0a8d..f0a7bb4af3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/BubbleDataProvider.kt @@ -3,5 +3,5 @@ package com.github.mikephil.charting.interfaces.dataprovider import com.github.mikephil.charting.data.BubbleData interface BubbleDataProvider : BarLineScatterCandleBubbleDataProvider { - val bubbleData: BubbleData? + var bubbleData: BubbleData? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt index e5a5347a03..842f059e12 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/CandleDataProvider.kt @@ -3,5 +3,5 @@ package com.github.mikephil.charting.interfaces.dataprovider import com.github.mikephil.charting.data.CandleData interface CandleDataProvider : BarLineScatterCandleBubbleDataProvider { - val candleData: CandleData? + var candleData: CandleData? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/LineDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/LineDataProvider.kt index 48077c81df..8185c61553 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/LineDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/LineDataProvider.kt @@ -5,6 +5,6 @@ import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.data.LineData interface LineDataProvider : BarLineScatterCandleBubbleDataProvider { - val lineData: LineData? + var lineData: LineData? fun getAxis(axis: AxisDependency?): YAxis? } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt index 43684658d0..80a2e57671 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/dataprovider/ScatterDataProvider.kt @@ -3,5 +3,5 @@ package com.github.mikephil.charting.interfaces.dataprovider import com.github.mikephil.charting.data.ScatterData interface ScatterDataProvider : BarLineScatterCandleBubbleDataProvider { - val scatterData: ScatterData? + var scatterData: ScatterData? } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt index 3c57afcf22..3dd3e0ebd0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.kt @@ -14,7 +14,7 @@ interface ILineDataSet : ILineRadarDataSet { * * @return */ - val mode: LineDataSet.Mode? + var mode: LineDataSet.Mode? /** * Returns the intensity of the cubic lines (the effect intensity). @@ -22,7 +22,7 @@ interface ILineDataSet : ILineRadarDataSet { * * @return */ - val cubicIntensity: Float + var cubicIntensity: Float @get:Deprecated("") val isDrawCubicEnabled: Boolean @@ -33,12 +33,12 @@ interface ILineDataSet : ILineRadarDataSet { /** * Returns the size of the drawn circles. */ - val circleRadius: Float + var circleRadius: Float /** * Returns the hole radius of the drawn circles. */ - val circleHoleRadius: Float + var circleHoleRadius: Float /** * Returns the color at the given index of the DataSet's circle-color array. @@ -61,28 +61,28 @@ interface ILineDataSet : ILineRadarDataSet { * * @return */ - val isDrawCirclesEnabled: Boolean + var isDrawCirclesEnabled: Boolean /** * Returns the color of the inner circle (the circle-hole). * * @return */ - val circleHoleColor: Int + var circleHoleColor: Int /** * Returns true if drawing the circle-holes is enabled, false if not. * * @return */ - val isDrawCircleHoleEnabled: Boolean + var isDrawCircleHoleEnabled: Boolean /** * Returns the DashPathEffect that is used for drawing the lines. * * @return */ - val dashPathEffect: DashPathEffect? + var dashPathEffect: DashPathEffect? /** * Returns true if the dashed-line effect is enabled, false if not. @@ -97,5 +97,5 @@ interface ILineDataSet : ILineRadarDataSet { * * @return */ - val fillFormatter: IFillFormatter + var fillFormatter: IFillFormatter } \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt index 9876a279bc..2ddb46e0b0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/ILineRadarDataSet.kt @@ -12,14 +12,14 @@ interface ILineRadarDataSet : ILineScatterCandleRadarDataSet { * * @return */ - val fillColor: Int + var fillColor: Int /** * Returns the drawable used for filling the area below the line. * * @return */ - val fillDrawable: Drawable? + var fillDrawable: Drawable? /** * Returns the alpha value that is used for filling the line surface, @@ -27,30 +27,19 @@ interface ILineRadarDataSet : ILineScatterCandleRadarDataSet { * * @return */ - val fillAlpha: Int + var fillAlpha: Int /** * Returns the stroke-width of the drawn line * * @return */ - val lineWidth: Float + var lineWidth: Float /** * Returns true if filled drawing is enabled, false if not * * @return */ - val isDrawFilledEnabled: Boolean - - /** - * Set to true if the DataSet should be drawn filled (surface), and not just - * as a line, disabling this will give great performance boost. Please note that this method - * uses the canvas.clipPath(...) method for drawing the filled area. - * For devices with API level < 18 (Android 4.3), hardware acceleration of the chart should - * be turned off. Default: false - * - * @param enabled - */ - fun setDrawFilled(enabled: Boolean) + var isDrawFilledEnabled: Boolean } diff --git a/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt b/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt index 54294e2e53..fe553c0c8b 100644 --- a/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/AnotherBarActivity.kt @@ -3,7 +3,6 @@ package info.appdev.chartexample import android.Manifest import android.content.Intent import android.content.pm.PackageManager -import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -12,6 +11,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.data.BarData @@ -21,7 +21,6 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.notimportant.DemoBase -import androidx.core.net.toUri class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { private var chart: BarChart? = null @@ -60,8 +59,8 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { // scaling can now only be done on x- and y-axis separately chart?.setPinchZoom(false) - chart?.setDrawBarShadow(false) - chart?.setDrawGridBackground(false) + chart?.isDrawBarShadowEnabled = true + chart?.drawGridBackground = false val xAxis = chart?.xAxis xAxis?.position = XAxisPosition.BOTTOM diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt index e0a5341a33..fde06d66e1 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt @@ -66,8 +66,8 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect chart?.setOnChartValueSelectedListener(this) chart?.setRoundedBarRadius(50f) - chart?.setDrawBarShadow(false) - chart?.setDrawValueAboveBar(true) + chart?.isDrawBarShadowEnabled = false + chart?.isDrawValueAboveBarEnabled = true chart?.description?.isEnabled = false @@ -78,7 +78,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect // scaling can now only be done on x- and y-axis separately chart?.setPinchZoom(false) - chart?.setDrawGridBackground(false) + chart?.drawGridBackground = false // chart.setDrawYLabels(false); val xAxisFormatter: IAxisValueFormatter = DayAxisValueFormatter(chart!!) diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt b/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt index 286471121c..ba791d0240 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivityMultiDataset.kt @@ -67,9 +67,9 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar // scaling can now only be done on x- and y-axis separately chart?.setPinchZoom(false) - chart?.setDrawBarShadow(false) + chart?.isDrawBarShadowEnabled = false - chart?.setDrawGridBackground(false) + chart?.drawGridBackground = false // create a custom MarkerView (extend MarkerView) and specify the layout // to use for it diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt b/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt index d1f66338d1..4f7ac5ee36 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivitySinus.kt @@ -47,8 +47,8 @@ class BarChartActivitySinus : DemoBase(), OnSeekBarChangeListener { chart = findViewById(R.id.chart1) - chart!!.setDrawBarShadow(false) - chart!!.setDrawValueAboveBar(true) + chart!!.isDrawBarShadowEnabled = false + chart!!.isDrawValueAboveBarEnabled = true chart!!.description.isEnabled = false @@ -63,7 +63,7 @@ class BarChartActivitySinus : DemoBase(), OnSeekBarChangeListener { // chart.setDrawBarShadow(true); // chart.setDrawXLabels(false); - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false // chart.setDrawYLabels(false); val xAxis = chart!!.xAxis diff --git a/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt b/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt index 0924d6e7b0..f13792c93f 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartPositiveNegative.kt @@ -42,15 +42,15 @@ class BarChartPositiveNegative : DemoBase() { chart!!.extraLeftOffset = 70f chart!!.extraRightOffset = 70f - chart!!.setDrawBarShadow(false) - chart!!.setDrawValueAboveBar(true) + chart!!.isDrawBarShadowEnabled = false + chart!!.isDrawValueAboveBarEnabled = true chart!!.description.isEnabled = false // scaling can now only be done on x- and y-axis separately chart!!.setPinchZoom(false) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false val xAxis = chart!!.xAxis xAxis.position = XAxisPosition.BOTTOM diff --git a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt index 975e2594ed..3bf5bd780f 100644 --- a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt @@ -60,7 +60,7 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false chart!!.setTouchEnabled(true) @@ -110,7 +110,7 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel values1.add( BubbleEntry( i.toFloat(), - (sampleValues[i + 1]!! * range).toFloat(), + (sampleValues[i + 1] * range).toFloat(), (sampleValues[i].toFloat() * range), getResources().getDrawable(R.drawable.star) ) diff --git a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt index a0675d977d..296dc3102e 100644 --- a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt @@ -5,7 +5,6 @@ import android.content.Intent import android.content.pm.PackageManager import android.graphics.Color import android.graphics.Paint -import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -14,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.github.mikephil.charting.charts.CandleStickChart import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.components.YAxis.AxisDependency @@ -22,7 +22,6 @@ import com.github.mikephil.charting.data.CandleDataSet import com.github.mikephil.charting.data.CandleEntry import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.notimportant.DemoBase -import androidx.core.net.toUri class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { private var chart: CandleStickChart? = null @@ -62,7 +61,7 @@ class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { // scaling can now only be done on x- and y-axis separately chart!!.setPinchZoom(false) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false val xAxis = chart!!.xAxis xAxis.position = XAxisPosition.BOTTOM diff --git a/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt b/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt index 54c1168e0e..a5f5b49a5e 100644 --- a/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/CombinedChartActivity.kt @@ -2,11 +2,11 @@ package info.appdev.chartexample import android.content.Intent import android.graphics.Color -import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.WindowManager +import androidx.core.net.toUri import com.github.mikephil.charting.charts.CombinedChart import com.github.mikephil.charting.charts.CombinedChart.DrawOrder import com.github.mikephil.charting.components.AxisBase @@ -33,7 +33,6 @@ import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.notimportant.DemoBase import kotlin.math.roundToInt -import androidx.core.net.toUri class CombinedChartActivity : DemoBase() { private var chart: CombinedChart? = null @@ -53,9 +52,9 @@ class CombinedChartActivity : DemoBase() { chart = findViewById(R.id.chart1) chart!!.description.isEnabled = false chart!!.setBackgroundColor(Color.WHITE) - chart!!.setDrawGridBackground(false) - chart!!.setDrawBarShadow(false) - chart!!.setHighlightFullBarEnabled(false) + chart!!.drawGridBackground = false + chart!!.isDrawBarShadowEnabled = false + chart!!.isHighlightFullBarEnabled = false // draw bars behind lines chart!!.drawOrder = arrayOf( @@ -107,15 +106,15 @@ class CombinedChartActivity : DemoBase() { val entries = ArrayList() - for (index in 0..= 18) { // drawables only supported on api level 18 and above val drawable = ContextCompat.getDrawable(context, R.drawable.fade_blue) - lineDataSet01.setFillDrawable(drawable) + lineDataSet01.fillDrawable = drawable } else { - lineDataSet01.setFillColor(Color.BLACK) + lineDataSet01.fillColor = Color.BLACK } val dataSets = ArrayList() dataSets.add(lineDataSet01) // add the data sets diff --git a/app/src/main/java/info/appdev/chartexample/DrawChartActivity.kt b/app/src/main/java/info/appdev/chartexample/DrawChartActivity.kt index a5cf98a906..b4b6884e08 100644 --- a/app/src/main/java/info/appdev/chartexample/DrawChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/DrawChartActivity.kt @@ -46,7 +46,7 @@ class DrawChartActivity : DemoBase(), OnChartValueSelectedListener, OnDrawListen // if disabled, drawn data sets with the finger will not be automatically // finished // chart.setAutoFinish(true); - binding.chart1.setDrawGridBackground(false) + binding.chart1.drawGridBackground = false // add dummy-data to the chart initWithDummyData() @@ -70,8 +70,8 @@ class DrawChartActivity : DemoBase(), OnChartValueSelectedListener, OnDrawListen // create a dataset and give it a type (0) val set1 = LineDataSet(values, "DataSet") - set1.setLineWidth(3f) - set1.setCircleRadius(5f) + set1.lineWidth = 3f + set1.circleRadius = 5f // create a data object with the data sets val data = LineData(set1) diff --git a/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt b/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt index 0a7ea0ea57..0942e292ef 100644 --- a/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/DynamicalAddingActivity.kt @@ -38,7 +38,7 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { chart = findViewById(R.id.chart1) chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false chart!!.description.isEnabled = false chart!!.setNoDataText("No chart data available. Use the menu to add entries and data sets!") @@ -120,8 +120,8 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { } val set = LineDataSet(values, "DataSet $count") - set.setLineWidth(2.5f) - set.setCircleRadius(4.5f) + set.lineWidth = 2.5f + set.circleRadius = 4.5f val color = colors[count % colors.size] @@ -151,8 +151,8 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { private fun createSet(): LineDataSet { val set = LineDataSet(mutableListOf(), "DataSet 1") - set.setLineWidth(2.5f) - set.setCircleRadius(4.5f) + set.lineWidth = 2.5f + set.circleRadius = 4.5f set.setColor(Color.rgb(240, 99, 99)) set.setCircleColor(Color.rgb(240, 99, 99)) set.highLightColor = Color.rgb(190, 190, 190) diff --git a/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt b/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt index 00fb2e117f..f32f1ac309 100644 --- a/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt @@ -42,9 +42,9 @@ class FilledLineActivity : DemoBase() { chart = findViewById(R.id.chart1) chart!!.setBackgroundColor(Color.WHITE) chart!!.setGridBackgroundColor(fillColor) - chart!!.setDrawGridBackground(true) + chart!!.drawGridBackground = true - chart!!.setDrawBorders(true) + chart!!.isDrawBordersEnabled = true // no description text chart!!.description.isEnabled = false @@ -108,41 +108,41 @@ class FilledLineActivity : DemoBase() { set1.axisDependency = AxisDependency.LEFT set1.setColor(Color.rgb(255, 241, 46)) - set1.setDrawCircles(false) - set1.setLineWidth(2f) - set1.setCircleRadius(3f) - set1.setFillAlpha(255) - set1.setDrawFilled(true) - set1.setFillColor(Color.WHITE) + set1.isDrawCirclesEnabled = false + set1.lineWidth = 2f + set1.circleRadius = 3f + set1.fillAlpha = 255 + set1.isDrawFilledEnabled = true + set1.fillColor = Color.WHITE set1.highLightColor = Color.rgb(244, 117, 117) - set1.setDrawCircleHole(false) - set1.setFillFormatter(object : IFillFormatter { + set1.isDrawCircleHoleEnabled = false + set1.fillFormatter = object : IFillFormatter { override fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float { // change the return value here to better understand the effect // return 0; return chart!!.axisLeft.axisMinimum } - }) + } // create a dataset and give it a type set2 = LineDataSet(values2, "DataSet 2") set2.axisDependency = AxisDependency.LEFT set2.setColor(Color.rgb(255, 241, 46)) - set2.setDrawCircles(false) - set2.setLineWidth(2f) - set2.setCircleRadius(3f) - set2.setFillAlpha(255) - set2.setDrawFilled(true) - set2.setFillColor(Color.WHITE) - set2.setDrawCircleHole(false) + set2.isDrawCirclesEnabled = false + set2.lineWidth = 2f + set2.circleRadius = 3f + set2.fillAlpha = 255 + set2.isDrawFilledEnabled = true + set2.fillColor = Color.WHITE + set2.isDrawCircleHoleEnabled = false set2.highLightColor = Color.rgb(244, 117, 117) - set2.setFillFormatter(object : IFillFormatter { + set2.fillFormatter = object : IFillFormatter { override fun getFillLinePosition(dataSet: ILineDataSet, dataProvider: LineDataProvider): Float { // change the return value here to better understand the effect // return 600; return chart!!.axisLeft.axisMaximum } - }) + } val dataSets = ArrayList() dataSets.add(set1) // add the data sets diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt index 304a5a902a..4c7c6d2fc9 100644 --- a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt @@ -4,7 +4,6 @@ import android.Manifest import android.content.Intent import android.content.pm.PackageManager import android.graphics.RectF -import android.net.Uri import android.os.Bundle import android.util.Log import android.view.Menu @@ -14,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.XAxis.XAxisPosition @@ -27,7 +27,6 @@ import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.MPPointF.Companion.recycleInstance import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.notimportant.DemoBase -import androidx.core.net.toUri class HorizontalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { private var chart: HorizontalBarChart? = null @@ -59,9 +58,9 @@ class HorizontalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartV chart!!.setOnChartValueSelectedListener(this) // chart.setHighlightEnabled(false); - chart!!.setDrawBarShadow(false) + chart!!.isDrawBarShadowEnabled = false - chart!!.setDrawValueAboveBar(true) + chart!!.isDrawValueAboveBarEnabled = true chart!!.description.isEnabled = false @@ -74,7 +73,7 @@ class HorizontalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartV // draw shadows for each bar that show the maximum value // chart.setDrawBarShadow(true); - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false val xl = chart!!.xAxis xl.position = XAxisPosition.BOTTOM diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt index 8322975f97..17b62343c1 100644 --- a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt @@ -58,9 +58,9 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, chart!!.setOnChartValueSelectedListener(this) // chart.setHighlightEnabled(false); - chart!!.setDrawBarShadow(false) + chart!!.isDrawBarShadowEnabled = false - chart!!.setDrawValueAboveBar(true) + chart!!.isDrawValueAboveBarEnabled = true chart!!.description.isEnabled = false @@ -73,7 +73,7 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, // draw shadows for each bar that show the maximum value // chart.setDrawBarShadow(true); - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false val xl = chart!!.xAxis xl.position = XAxisPosition.BOTTOM diff --git a/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt index b23221f146..8dbe246fa0 100644 --- a/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt @@ -55,7 +55,7 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa chart = findViewById(R.id.chart1) chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false // no description text chart!!.description.isEnabled = false @@ -126,8 +126,8 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa // create a dataset and give it a type val set1 = LineDataSet(entries, "DataSet 1") - set1.setLineWidth(1.5f) - set1.setCircleRadius(4f) + set1.lineWidth = 1.5f + set1.circleRadius = 4f // create a data object with the data sets val data = LineData(set1) @@ -174,7 +174,7 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa for (iSet in sets) { val set = iSet as LineDataSet - set.setDrawFilled(!set.isDrawFilledEnabled) + set.isDrawFilledEnabled = !set.isDrawFilledEnabled } chart!!.invalidate() } @@ -185,7 +185,7 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa for (iSet in sets) { val set = iSet as LineDataSet - set.setDrawCircles(!set.isDrawCirclesEnabled) + set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } chart!!.invalidate() } diff --git a/app/src/main/java/info/appdev/chartexample/LineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/LineChartActivity.kt index 72a3b19ca1..de1c835ffd 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/LineChartActivity.kt @@ -55,7 +55,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec // set listeners binding.chart1.setOnChartValueSelectedListener(this) - binding.chart1.setDrawGridBackground(false) + binding.chart1.drawGridBackground = false // create marker to display box when values are selected val mv = MyMarkerView(this, R.layout.custom_marker_view) @@ -189,7 +189,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleFilled -> { binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet - set.setDrawFilled(!set.isDrawFilledEnabled) + set.isDrawFilledEnabled = !set.isDrawFilledEnabled binding.chart1.invalidate() } } @@ -197,7 +197,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleCircles -> { binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet - set.setDrawCircles(!set.isDrawCirclesEnabled) + set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } binding.chart1.invalidate() } @@ -205,7 +205,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleCubic -> { binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet - set.setMode(if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER) + set.mode = if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER } binding.chart1.invalidate() } @@ -213,7 +213,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleStepped -> { binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet - set.setMode(if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED) + set.mode = if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED } binding.chart1.invalidate() } @@ -221,7 +221,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec R.id.actionToggleHorizontalCubic -> { binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet - set.setMode(if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER) + set.mode = if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER } binding.chart1.invalidate() } diff --git a/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt b/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt index 4ad74640e4..cf8d2997be 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt +++ b/app/src/main/java/info/appdev/chartexample/LineChartActivityColored.kt @@ -52,7 +52,7 @@ class LineChartActivityColored : DemoBase() { ) private fun setupChart(chart: LineChart, data: LineData, color: Int) { - (data.getDataSetByIndex(0) as LineDataSet).setCircleHoleColor(color) + (data.getDataSetByIndex(0) as LineDataSet).circleHoleColor = color // no description text chart.description.isEnabled = false @@ -60,7 +60,7 @@ class LineChartActivityColored : DemoBase() { // chart.setDrawHorizontalGrid(false); // // enable / disable grid background - chart.setDrawGridBackground(false) + chart.drawGridBackground = false // chart.getRenderer().getGridPaint().setGridColor(Color.WHITE & 0x70FFFFFF); @@ -112,9 +112,9 @@ class LineChartActivityColored : DemoBase() { // set1.setFillAlpha(110); // set1.setFillColor(Color.RED); - set1.setLineWidth(1.75f) - set1.setCircleRadius(5f) - set1.setCircleHoleRadius(2.5f) + set1.lineWidth = 1.75f + set1.circleRadius = 5f + set1.circleHoleRadius = 2.5f set1.setColor(Color.WHITE) set1.setCircleColor(Color.WHITE) set1.highLightColor = Color.WHITE diff --git a/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt b/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt index 35476b9aa1..216c78aab4 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/LineChartDualAxisActivity.kt @@ -75,7 +75,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa // enable scaling and dragging chart!!.isDragEnabled = true chart!!.setScaleEnabled(true) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false chart!!.isHighlightPerDragEnabled = true // if disabled, scaling can be done on x- and y-axis separately @@ -174,12 +174,12 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa set1.axisDependency = AxisDependency.LEFT set1.setColor(holoBlue) set1.setCircleColor(Color.WHITE) - set1.setLineWidth(2f) - set1.setCircleRadius(3f) - set1.setFillAlpha(65) - set1.setFillColor(holoBlue) + set1.lineWidth = 2f + set1.circleRadius = 3f + set1.fillAlpha = 65 + set1.fillColor = holoBlue set1.highLightColor = Color.rgb(244, 117, 117) - set1.setDrawCircleHole(false) + set1.isDrawCircleHoleEnabled = false //set1.setFillFormatter(new MyFillFormatter(0f)); //set1.setDrawHorizontalHighlightIndicator(false); @@ -191,11 +191,11 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa set2.axisDependency = AxisDependency.RIGHT set2.setColor(Color.MAGENTA) set2.setCircleColor(Color.WHITE) - set2.setLineWidth(2f) - set2.setCircleRadius(3f) - set2.setFillAlpha(65) - set2.setFillColor(Color.BLUE) - set2.setDrawCircleHole(false) + set2.lineWidth = 2f + set2.circleRadius = 3f + set2.fillAlpha = 65 + set2.fillColor = Color.BLUE + set2.isDrawCircleHoleEnabled = false set2.highLightColor = Color.rgb(244, 117, 117) //set2.setFillFormatter(new MyFillFormatter(900f)); @@ -203,11 +203,11 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa set3.axisDependency = AxisDependency.RIGHT set3.setColor(Color.YELLOW) set3.setCircleColor(Color.WHITE) - set3.setLineWidth(2f) - set3.setCircleRadius(3f) - set3.setFillAlpha(65) - set3.setFillColor(colorWithAlpha(Color.YELLOW, 200)) - set3.setDrawCircleHole(false) + set3.lineWidth = 2f + set3.circleRadius = 3f + set3.fillAlpha = 65 + set3.fillColor = colorWithAlpha(Color.YELLOW, 200) + set3.isDrawCircleHoleEnabled = false set3.highLightColor = Color.rgb(244, 117, 117) // create a data object with the data sets @@ -258,7 +258,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa for (iSet in sets) { val set = iSet as LineDataSet - set.setDrawFilled(!set.isDrawFilledEnabled) + set.isDrawFilledEnabled = !set.isDrawFilledEnabled } chart!!.invalidate() } @@ -269,7 +269,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa for (iSet in sets) { val set = iSet as LineDataSet - set.setDrawCircles(!set.isDrawCirclesEnabled) + set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } chart!!.invalidate() } @@ -280,7 +280,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa for (iSet in sets) { val set = iSet as LineDataSet - set.setMode( + set.mode = ( if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else @@ -296,7 +296,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa for (iSet in sets) { val set = iSet as LineDataSet - set.setMode( + set.mode = ( if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else @@ -312,7 +312,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa for (iSet in sets) { val set = iSet as LineDataSet - set.setMode( + set.mode = ( if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else diff --git a/app/src/main/java/info/appdev/chartexample/LineChartTime.kt b/app/src/main/java/info/appdev/chartexample/LineChartTime.kt index 4f807fc867..08d38a0742 100644 --- a/app/src/main/java/info/appdev/chartexample/LineChartTime.kt +++ b/app/src/main/java/info/appdev/chartexample/LineChartTime.kt @@ -64,7 +64,7 @@ class LineChartTime : DemoBase(), OnSeekBarChangeListener { // enable scaling and dragging chart!!.isDragEnabled = true chart!!.setScaleEnabled(true) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false chart!!.isHighlightPerDragEnabled = true // set an alternative background color @@ -139,13 +139,13 @@ class LineChartTime : DemoBase(), OnSeekBarChangeListener { set1.axisDependency = AxisDependency.LEFT set1.setColor(holoBlue) set1.valueTextColor = holoBlue - set1.setLineWidth(1.5f) - set1.setDrawCircles(false) + set1.lineWidth = 1.5f + set1.isDrawCirclesEnabled = false set1.isDrawValuesEnabled = false - set1.setFillAlpha(65) - set1.setFillColor(holoBlue) + set1.fillAlpha = 65 + set1.fillColor = holoBlue set1.highLightColor = Color.rgb(244, 117, 117) - set1.setDrawCircleHole(false) + set1.isDrawCircleHoleEnabled = false // create a data object with the data sets val data = LineData(set1) @@ -194,8 +194,7 @@ class LineChartTime : DemoBase(), OnSeekBarChangeListener { for (iSet in sets) { val set = iSet as LineDataSet - if (set.isDrawFilledEnabled) set.setDrawFilled(false) - else set.setDrawFilled(true) + set.isDrawFilledEnabled = !set.isDrawFilledEnabled } chart!!.invalidate() } @@ -206,8 +205,7 @@ class LineChartTime : DemoBase(), OnSeekBarChangeListener { for (iSet in sets) { val set = iSet as LineDataSet - if (set.isDrawCirclesEnabled) set.setDrawCircles(false) - else set.setDrawCircles(true) + set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } chart!!.invalidate() } @@ -218,8 +216,8 @@ class LineChartTime : DemoBase(), OnSeekBarChangeListener { for (iSet in sets) { val set = iSet as LineDataSet - if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) set.setMode(LineDataSet.Mode.LINEAR) - else set.setMode(LineDataSet.Mode.CUBIC_BEZIER) + if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) set.mode = LineDataSet.Mode.LINEAR + else set.mode = LineDataSet.Mode.CUBIC_BEZIER } chart!!.invalidate() } @@ -230,8 +228,8 @@ class LineChartTime : DemoBase(), OnSeekBarChangeListener { for (iSet in sets) { val set = iSet as LineDataSet - if (set.mode == LineDataSet.Mode.STEPPED) set.setMode(LineDataSet.Mode.LINEAR) - else set.setMode(LineDataSet.Mode.STEPPED) + if (set.mode == LineDataSet.Mode.STEPPED) set.mode = LineDataSet.Mode.LINEAR + else set.mode = LineDataSet.Mode.STEPPED } chart!!.invalidate() } diff --git a/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt index 2de9774f99..962946c856 100644 --- a/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt @@ -81,7 +81,7 @@ class ListViewBarChartActivity : DemoBase() { data.setValueTextColor(Color.BLACK) } holder.chart!!.description.isEnabled = false - holder.chart!!.setDrawGridBackground(false) + holder.chart!!.drawGridBackground = false val xAxis = holder.chart!!.xAxis xAxis.position = XAxisPosition.BOTTOM diff --git a/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt b/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt index 2bf1bde94c..15064d3301 100644 --- a/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt @@ -98,8 +98,8 @@ class ListViewMultiChartActivity : DemoBase() { } val d1 = LineDataSet(values1, "New DataSet $cnt, (1)") - d1.setLineWidth(2.5f) - d1.setCircleRadius(4.5f) + d1.lineWidth = 2.5f + d1.circleRadius = 4.5f d1.highLightColor = Color.rgb(244, 117, 117) d1.isDrawValuesEnabled = false @@ -110,8 +110,8 @@ class ListViewMultiChartActivity : DemoBase() { } val d2 = LineDataSet(values2, "New DataSet $cnt, (2)") - d2.setLineWidth(2.5f) - d2.setCircleRadius(4.5f) + d2.lineWidth = 2.5f + d2.circleRadius = 4.5f d2.highLightColor = Color.rgb(244, 117, 117) d2.setColor(ColorTemplate.VORDIPLOM_COLORS[0]) d2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]) diff --git a/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt index 2128af91f8..7e72350469 100644 --- a/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/MultiLineChartActivity.kt @@ -57,9 +57,9 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu chart = findViewById(R.id.chart1) chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false chart!!.description.isEnabled = false - chart!!.setDrawBorders(false) + chart!!.isDrawBordersEnabled = false chart!!.axisLeft.isEnabled = false chart!!.axisRight.setDrawAxisLine(false) @@ -113,8 +113,8 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu } val d = LineDataSet(values, "DataSet " + (z + 1)) - d.setLineWidth(2.5f) - d.setCircleRadius(4f) + d.lineWidth = 2.5f + d.circleRadius = 4f val color = colors[z % colors.size] d.setColor(color) @@ -183,8 +183,7 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu for (iSet in sets) { val set = iSet as LineDataSet - if (set.isDrawFilledEnabled) set.setDrawFilled(false) - else set.setDrawFilled(true) + set.isDrawFilledEnabled = !set.isDrawFilledEnabled } chart!!.invalidate() } @@ -195,8 +194,7 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu for (iSet in sets) { val set = iSet as LineDataSet - if (set.isDrawCirclesEnabled) set.setDrawCircles(false) - else set.setDrawCircles(true) + set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } chart!!.invalidate() } @@ -207,7 +205,7 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu for (iSet in sets) { val set = iSet as LineDataSet - set.setMode( + set.mode = ( if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else @@ -223,7 +221,7 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu for (iSet in sets) { val set = iSet as LineDataSet - set.setMode( + set.mode = ( if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else @@ -239,7 +237,7 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu for (iSet in sets) { val set = iSet as LineDataSet - set.setMode( + set.mode = ( if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else diff --git a/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt b/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt index c3dae688eb..0081b550da 100644 --- a/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt +++ b/app/src/main/java/info/appdev/chartexample/PerformanceLineChart.kt @@ -37,7 +37,7 @@ class PerformanceLineChart : DemoBase(), OnSeekBarChangeListener { seekBarValues!!.setOnSeekBarChangeListener(this) chart = findViewById(R.id.chart1) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false // no description text chart!!.description.isEnabled = false @@ -76,11 +76,11 @@ class PerformanceLineChart : DemoBase(), OnSeekBarChangeListener { val set1 = LineDataSet(values, "DataSet 1") set1.setColor(Color.BLACK) - set1.setLineWidth(0.5f) + set1.lineWidth = 0.5f set1.isDrawValuesEnabled = false - set1.setDrawCircles(false) - set1.setMode(LineDataSet.Mode.LINEAR) - set1.setDrawFilled(false) + set1.isDrawCirclesEnabled = false + set1.mode = LineDataSet.Mode.LINEAR + set1.isDrawFilledEnabled = false // create a data object with the data sets val data = LineData(set1) diff --git a/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt index afaebbd7f1..ae8fcb477c 100644 --- a/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/RadarChartActivity.kt @@ -112,19 +112,19 @@ class RadarChartActivity : DemoBase() { val set1 = RadarDataSet(entries1, "Last Week") set1.setColor(Color.rgb(103, 110, 129)) - set1.setFillColor(Color.rgb(103, 110, 129)) - set1.setDrawFilled(true) - set1.setFillAlpha(180) - set1.setLineWidth(2f) + set1.fillColor = (Color.rgb(103, 110, 129)) + set1.isDrawFilledEnabled = true + set1.fillAlpha = 180 + set1.lineWidth = 2f set1.isDrawHighlightCircleEnabled = true set1.setDrawHighlightIndicators(false) val set2 = RadarDataSet(entries2, "This Week") set2.setColor(Color.rgb(121, 162, 175)) - set2.setFillColor(Color.rgb(121, 162, 175)) - set2.setDrawFilled(true) - set2.setFillAlpha(180) - set2.setLineWidth(2f) + set2.fillColor = (Color.rgb(121, 162, 175)) + set2.isDrawFilledEnabled = true + set2.fillAlpha = 180 + set2.lineWidth = 2f set2.isDrawHighlightCircleEnabled = true set2.setDrawHighlightIndicators(false) @@ -185,7 +185,7 @@ class RadarChartActivity : DemoBase() { .dataSets as ArrayList for (set in sets) { - set.setDrawFilled(!set.isDrawFilledEnabled) + set.isDrawFilledEnabled = !set.isDrawFilledEnabled } chart!!.invalidate() } diff --git a/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt index 8fa09c18b1..fa8e829f03 100644 --- a/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/RealtimeLineChartActivity.kt @@ -51,7 +51,7 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { // enable scaling and dragging chart!!.isDragEnabled = true chart!!.setScaleEnabled(true) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false // if disabled, scaling can be done on x- and y-axis separately chart!!.setPinchZoom(true) @@ -131,10 +131,10 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { set.axisDependency = AxisDependency.LEFT set.setColor(holoBlue) set.setCircleColor(Color.WHITE) - set.setLineWidth(2f) - set.setCircleRadius(4f) - set.setFillAlpha(65) - set.setFillColor(holoBlue) + set.lineWidth = 2f + set.circleRadius = 4f + set.fillAlpha = 65 + set.fillColor = holoBlue set.highLightColor = Color.rgb(244, 117, 117) set.valueTextColor = Color.WHITE set.valueTextSize = 9f diff --git a/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt b/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt index 1659d7bd83..a687640958 100644 --- a/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ScatterChartActivity.kt @@ -57,7 +57,7 @@ class ScatterChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSe chart!!.description.isEnabled = false chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false chart!!.setTouchEnabled(true) chart!!.setMaxHighlightDistance(50f) diff --git a/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt b/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt index 6b2880aa30..18fb809e6d 100644 --- a/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ScrollViewActivity.kt @@ -35,8 +35,8 @@ class ScrollViewActivity : DemoBase() { // scaling can now only be done on x- and y-axis separately chart!!.setPinchZoom(false) - chart!!.setDrawBarShadow(false) - chart!!.setDrawGridBackground(false) + chart!!.isDrawBarShadowEnabled = false + chart!!.drawGridBackground = false val xAxis = chart!!.xAxis xAxis.position = XAxisPosition.BOTTOM diff --git a/app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt b/app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt index ba3d4fa564..055f0812f3 100644 --- a/app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt @@ -57,7 +57,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, mChart = findViewById(R.id.chart1) as LineChart mChart!!.onChartGestureListener = this mChart!!.setOnChartValueSelectedListener(this) - mChart!!.setDrawGridBackground(false) + mChart!!.drawGridBackground = false // no description text mChart!!.description.isEnabled = false @@ -157,7 +157,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleFilled -> { mChart!!.data?.dataSets?.forEach { val set = it as LineDataSet - set.setDrawFilled(!set.isDrawFilledEnabled) + set.isDrawFilledEnabled = !set.isDrawFilledEnabled } mChart!!.invalidate() } @@ -165,7 +165,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleCircles -> { mChart!!.data?.dataSets?.forEach { val set = it as LineDataSet - set.setDrawCircles(!set.isDrawCirclesEnabled) + set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } mChart!!.invalidate() } @@ -173,7 +173,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleCubic -> { mChart!!.data?.dataSets?.forEach { val set = it as LineDataSet - set.setMode(if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER) + set.mode = (if (set.mode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER) } mChart!!.invalidate() } @@ -181,7 +181,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleStepped -> { mChart!!.data?.dataSets?.forEach { val set = it as LineDataSet - set.setMode(if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED) + set.mode = (if (set.mode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED) } mChart!!.invalidate() } @@ -189,7 +189,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, R.id.actionToggleHorizontalCubic -> { mChart!!.data?.dataSets?.forEach { val set = it as LineDataSet - set.setMode(if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER) + set.mode = (if (set.mode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER) } mChart!!.invalidate() } @@ -267,20 +267,20 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, set11.enableDashedHighlightLine(10f, 5f, 0f) set11.setColor(Color.BLACK) set11.setCircleColor(Color.BLACK) - set11.setLineWidth(1f) - set11.setCircleRadius(3f) - set11.setDrawCircleHole(false) + set11.lineWidth = 1f + set11.circleRadius = 3f + set11.isDrawCircleHoleEnabled = false set11.valueTextSize = 9f - set11.setDrawFilled(true) + set11.isDrawFilledEnabled = true set11.formLineWidth = 1f set11.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f) set11.formSize = 15f if (Utils.sDKInt >= 18) { // fill drawable only supported on api level 18 and above val drawable = ContextCompat.getDrawable(this, R.drawable.fade_blue) - set11.setFillDrawable(drawable) + set11.fillDrawable = drawable } else { - set11.setFillColor(Color.BLACK) + set11.fillColor = Color.BLACK } val dataSets = ArrayList() dataSets.add(set11) // add the datasets diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt index 0d30faee8e..3959f2b23e 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt @@ -65,11 +65,11 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele // scaling can now only be done on x- and y-axis separately chart!!.setPinchZoom(false) - chart!!.setDrawGridBackground(false) - chart!!.setDrawBarShadow(false) + chart!!.drawGridBackground = false + chart!!.isDrawBarShadowEnabled = false - chart!!.setDrawValueAboveBar(false) - chart!!.setHighlightFullBarEnabled(false) + chart!!.isDrawValueAboveBarEnabled = false + chart!!.isHighlightFullBarEnabled = false // change the position of the y-labels val leftAxis = chart!!.axisLeft diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt index 4c92622a1b..ea8ec01fbc 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt @@ -4,13 +4,13 @@ import android.Manifest import android.content.Intent import android.content.pm.PackageManager import android.graphics.Color -import android.net.Uri import android.os.Bundle import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.WindowManager import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.components.Legend @@ -29,7 +29,6 @@ import com.github.mikephil.charting.utils.ViewPortHandler import info.appdev.chartexample.notimportant.DemoBase import java.text.DecimalFormat import kotlin.math.abs -import androidx.core.net.toUri class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { private var chart: HorizontalBarChart? = null @@ -46,15 +45,15 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { chart = findViewById(R.id.chart1) chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) + chart!!.drawGridBackground = false chart!!.description.isEnabled = false // scaling can now only be done on x- and y-axis separately chart!!.setPinchZoom(false) - chart!!.setDrawBarShadow(false) - chart!!.setDrawValueAboveBar(true) - chart!!.setHighlightFullBarEnabled(false) + chart!!.isDrawBarShadowEnabled = false + chart!!.isDrawValueAboveBarEnabled = true + chart!!.isHighlightFullBarEnabled = false chart!!.axisLeft.isEnabled = false chart!!.axisRight.axisMaximum = 25f diff --git a/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt b/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt index 1c23957ab5..baefbdbc50 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt @@ -30,8 +30,8 @@ class BarChartFrag : SimpleFragment(), OnChartGestureListener { mv.chartView = chart // For bounds control chart?.setMarker(mv) - chart?.setDrawGridBackground(false) - chart?.setDrawBarShadow(false) + chart?.drawGridBackground = false + chart?.isDrawBarShadowEnabled = false val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt index 0c030c18cf..645365b2fc 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt @@ -19,7 +19,7 @@ class ComplexityFragment : SimpleFragment() { chart?.description?.isEnabled = false - chart?.setDrawGridBackground(false) + chart?.drawGridBackground = false chart?.setData(complexity) chart?.animateX(3000) diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt b/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt index f92b8a937d..6fe8651fa8 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt @@ -26,7 +26,7 @@ class ScatterChartFrag : SimpleFragment() { mv.chartView = chart // For bounds control chart?.setMarker(mv) - chart?.setDrawGridBackground(false) + chart?.drawGridBackground = false chart?.setData(generateScatterData(6, 10000f)) val xAxis = chart?.xAxis diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt index 4220068db0..a411cb7732 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt @@ -114,11 +114,11 @@ abstract class SimpleFragment : Fragment() { val ds1 = LineDataSet(loadEntriesFromAssets(requireContext().assets, "sine.txt"), "Sine function") val ds2 = LineDataSet(loadEntriesFromAssets(requireContext().assets, "cosine.txt"), "Cosine function") - ds1.setLineWidth(2f) - ds2.setLineWidth(2f) + ds1.lineWidth = 2f + ds2.lineWidth = 2f - ds1.setDrawCircles(false) - ds2.setDrawCircles(false) + ds1.isDrawCirclesEnabled = false + ds2.isDrawCirclesEnabled = false ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]) ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]) @@ -155,14 +155,14 @@ abstract class SimpleFragment : Fragment() { ds3.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[2]) ds4.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[3]) - ds1.setLineWidth(2.5f) - ds1.setCircleRadius(3f) - ds2.setLineWidth(2.5f) - ds2.setCircleRadius(3f) - ds3.setLineWidth(2.5f) - ds3.setCircleRadius(3f) - ds4.setLineWidth(2.5f) - ds4.setCircleRadius(3f) + ds1.lineWidth = 2.5f + ds1.circleRadius = 3f + ds2.lineWidth = 2.5f + ds2.circleRadius = 3f + ds3.lineWidth = 2.5f + ds3.circleRadius = 3f + ds4.lineWidth = 2.5f + ds4.circleRadius = 3f // load DataSets from files in assets folder diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt index eba12584b2..d7c4fb1a4e 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt @@ -19,7 +19,7 @@ class SineCosineFragment : SimpleFragment() { chart?.description?.isEnabled = false - chart?.setDrawGridBackground(false) + chart?.drawGridBackground = false chart?.setData(generateLineData()) chart?.animateX(3000) diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt index 0cd27f959b..546b86c603 100644 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt @@ -37,8 +37,8 @@ class BarChartItem(cd: ChartData<*, *>, c: Context) : ChartItem(cd) { // apply styling holder.chart?.description?.isEnabled = false - holder.chart?.setDrawGridBackground(false) - holder.chart?.setDrawBarShadow(false) + holder.chart?.drawGridBackground = false + holder.chart?.isDrawBarShadowEnabled = false val xAxis = holder.chart?.xAxis xAxis?.position = XAxisPosition.BOTTOM diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt index 0700fbcd26..c89572e659 100644 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt @@ -38,7 +38,7 @@ class LineChartItem(cd: ChartData<*, *>, c: Context) : ChartItem(cd) { // apply styling // holder.chart.setValueTypeface(mTf); holder.chart!!.description.isEnabled = false - holder.chart!!.setDrawGridBackground(false) + holder.chart!!.drawGridBackground = false val xAxis = holder.chart!!.xAxis xAxis.position = XAxisPosition.BOTTOM From 80aee16e3cefdb00e2b6603f0add5fdd572aea28 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sun, 14 Sep 2025 09:53:23 -0400 Subject: [PATCH 7/9] Replace deprecated resource getters --- .../github/mikephil/charting/components/MarkerImage.kt | 3 ++- .../java/com/github/mikephil/charting/data/LineDataSet.kt | 3 ++- .../com/github/mikephil/charting/utils/ColorTemplate.kt | 3 ++- .../java/info/appdev/chartexample/BarChartActivity.kt | 3 ++- .../java/info/appdev/chartexample/BubbleChartActivity.kt | 8 ++++---- .../info/appdev/chartexample/CandleStickChartActivity.kt | 3 ++- .../appdev/chartexample/HorizontalBarChartActivity.kt | 3 ++- .../chartexample/HorizontalBarNegativeChartActivity.kt | 3 ++- .../java/info/appdev/chartexample/PieChartActivity.kt | 3 ++- .../info/appdev/chartexample/PieChartRoundedActivity.kt | 3 ++- .../java/info/appdev/chartexample/StackedBarActivity.kt | 3 ++- .../appdev/chartexample/StackedBarActivityNegative.kt | 3 ++- 12 files changed, 26 insertions(+), 15 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt index 285f9f4fd2..cc93738068 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/MarkerImage.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Rect import android.graphics.drawable.Drawable +import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.withTranslation import com.github.mikephil.charting.charts.Chart import com.github.mikephil.charting.data.Entry @@ -20,7 +21,7 @@ import java.lang.ref.WeakReference */ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMarker { private var mDrawable: Drawable = - mContext.resources.getDrawable(drawableResourceId, null) + ResourcesCompat.getDrawable(mContext.resources, drawableResourceId, mContext.theme)!! private var mOffset: MPPointF = MPPointF() private val mOffset2 = MPPointF() diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt index 3a00d8e995..a514d24fbb 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/LineDataSet.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Color import android.graphics.DashPathEffect import android.util.Log +import androidx.core.content.res.ResourcesCompat import com.github.mikephil.charting.formatter.DefaultFillFormatter import com.github.mikephil.charting.formatter.IFillFormatter import com.github.mikephil.charting.interfaces.datasets.ILineDataSet @@ -253,7 +254,7 @@ open class LineDataSet(yVals: MutableList, label: String) : LineRadarData clrs.clear() for (color in colors) { - clrs.add(c.resources.getColor(color)) + clrs.add(ResourcesCompat.getColor(c.resources, color, c.theme)) } this.circleColors = clrs diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt index 26ea663b95..bf14a16514 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.kt @@ -2,6 +2,7 @@ package com.github.mikephil.charting.utils import android.content.res.Resources import android.graphics.Color +import androidx.core.content.res.ResourcesCompat /** * Class that holds predefined color integer arrays (e.g. @@ -103,7 +104,7 @@ object ColorTemplate { val result: MutableList = ArrayList() for (i in colors) { - result.add(r.getColor(i)) + result.add(ResourcesCompat.getColor(r, i, r.newTheme())) } return result diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt index fde06d66e1..ca02ab84a1 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt @@ -13,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.Legend @@ -141,7 +142,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect val `val` = (sampleValues[i].toFloat() * (range + 1)) if (`val` * 100 < 25) { - values.add(BarEntry(i.toFloat(), `val`, getResources().getDrawable(R.drawable.star))) + values.add(BarEntry(i.toFloat(), `val`, ResourcesCompat.getDrawable(resources, R.drawable.star, theme))) } else { values.add(BarEntry(i.toFloat(), `val`)) } diff --git a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt index 3bf5bd780f..7469edf7dc 100644 --- a/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/BubbleChartActivity.kt @@ -13,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.charts.BubbleChart import com.github.mikephil.charting.components.Legend @@ -112,14 +113,13 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel i.toFloat(), (sampleValues[i + 1] * range).toFloat(), (sampleValues[i].toFloat() * range), - getResources().getDrawable(R.drawable.star) + ResourcesCompat.getDrawable(resources, R.drawable.star, theme) ) ) values2.add( BubbleEntry( - i.toFloat(), (sampleValues[i + 2] * range).toFloat(), (sampleValues[i + 1].toFloat() * range), getResources().getDrawable( - R.drawable.star - ) + i.toFloat(), (sampleValues[i + 2] * range).toFloat(), (sampleValues[i + 1].toFloat() * range), + ResourcesCompat.getDrawable(resources, R.drawable.star, theme) ) ) values3.add(BubbleEntry(i.toFloat(), (sampleValues[i] * range).toFloat(), (sampleValues[i + 2].toFloat() * range))) diff --git a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt index 296dc3102e..c76fd4c77e 100644 --- a/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/CandleStickChartActivity.kt @@ -13,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.charts.CandleStickChart import com.github.mikephil.charting.components.XAxis.XAxisPosition @@ -114,7 +115,7 @@ class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { `val` - low, if (even) `val` + open else `val` - open, if (even) `val` - close else `val` + close, - getResources().getDrawable(R.drawable.star) + ResourcesCompat.getDrawable(resources, R.drawable.star, theme) ) ) } diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt index 4c7c6d2fc9..53c6395572 100644 --- a/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/HorizontalBarChartActivity.kt @@ -13,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.components.Legend @@ -123,7 +124,7 @@ class HorizontalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartV values.add( BarEntry( i * spaceForBar, `val`, - getResources().getDrawable(R.drawable.star) + ResourcesCompat.getDrawable(resources, R.drawable.star, theme) ) ) } diff --git a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt index 17b62343c1..a9112050b3 100644 --- a/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt @@ -13,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.components.Legend @@ -121,7 +122,7 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, values.add( BarEntry( i * spaceForBar, `val`, - getResources().getDrawable(R.drawable.star) + ResourcesCompat.getDrawable(resources, R.drawable.star, theme) ) ) } diff --git a/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt b/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt index 8e73817014..c519e8392d 100644 --- a/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/PieChartActivity.kt @@ -18,6 +18,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing import com.github.mikephil.charting.charts.PieChart @@ -125,7 +126,7 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect PieEntry( (sampleValues[i].toFloat() * range) + range / 5, parties[i % parties.size], - getResources().getDrawable(R.drawable.star) + ResourcesCompat.getDrawable(resources, R.drawable.star, theme) ) ) } diff --git a/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt b/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt index 0c3abcc7d1..0790a05e54 100644 --- a/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/PieChartRoundedActivity.kt @@ -18,6 +18,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing import com.github.mikephil.charting.charts.PieChart @@ -127,7 +128,7 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu PieEntry( (sampleValues[i].toFloat() * range) + range / 5, parties[i % parties.size], - getResources().getDrawable(R.drawable.star) + ResourcesCompat.getDrawable(resources, R.drawable.star, theme) ) ) } diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt index 3959f2b23e..c671602a90 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt @@ -13,6 +13,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.XAxis.XAxisPosition @@ -115,7 +116,7 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele BarEntry( i.toFloat(), floatArrayOf(val1, val2, val3), - getResources().getDrawable(R.drawable.star) + ResourcesCompat.getDrawable(resources, R.drawable.star, theme) ) ) } diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt index ea8ec01fbc..2516b44616 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivityNegative.kt @@ -10,6 +10,7 @@ import android.view.Menu import android.view.MenuItem import android.view.WindowManager import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.components.AxisBase @@ -98,7 +99,7 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { values.add(BarEntry(25f, floatArrayOf(-15f, 15f))) values.add(BarEntry(35f, floatArrayOf(-17f, 17f))) values.add(BarEntry(45f, floatArrayOf(-19f, 20f))) - values.add(BarEntry(45f, floatArrayOf(-19f, 20f), getResources().getDrawable(R.drawable.star))) + values.add(BarEntry(45f, floatArrayOf(-19f, 20f), ResourcesCompat.getDrawable(resources, R.drawable.star, theme))) values.add(BarEntry(55f, floatArrayOf(-19f, 19f))) values.add(BarEntry(65f, floatArrayOf(-16f, 16f))) values.add(BarEntry(75f, floatArrayOf(-13f, 14f))) From 7c37518b2286b6b7942e7c5e32b79749842a4177 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sun, 14 Sep 2025 19:39:09 -0400 Subject: [PATCH 8/9] Fix unit tests --- .../charting/jobs/AnimatedMoveViewJob.kt | 2 +- .../mikephil/charting/jobs/MoveViewJob.kt | 2 +- .../github/mikephil/charting/jobs/ZoomJob.kt | 2 +- .../github/mikephil/charting/utils/FSize.kt | 2 +- .../mikephil/charting/utils/MPPointD.kt | 2 +- .../mikephil/charting/utils/MPPointF.kt | 2 +- .../mikephil/charting/utils/ObjectPool.kt | 36 +++++++------------ .../github/mikephil/charting/utils/Utils.kt | 2 +- .../mikephil/charting/test/ChartDataTest.kt | 4 +-- .../mikephil/charting/test/DataSetTest.kt | 26 +++++++------- .../mikephil/charting/test/ObjectPoolTest.kt | 8 ++--- 11 files changed, 38 insertions(+), 50 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt index 07767cbeac..74cdca8a0e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt @@ -41,7 +41,7 @@ class AnimatedMoveViewJob( private val pool = ObjectPool.Companion.create(4, AnimatedMoveViewJob(ViewPortHandler(), 0f, 0f, null, null, 0f, 0f, 0)) init { - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } fun getInstance( diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt index 8e5fe58cb7..8bbcdf75fa 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt @@ -28,7 +28,7 @@ open class MoveViewJob(viewPortHandler: ViewPortHandler, xValue: Float, yValue: private val pool = ObjectPool.Companion.create(2, MoveViewJob(ViewPortHandler(), 0f, 0f, null, null)) init { - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } fun getInstance(viewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, v: View?): MoveViewJob { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt index dc3aef6119..75bc0126be 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt @@ -49,7 +49,7 @@ open class ZoomJob( private val pool = ObjectPool.Companion.create(1, ZoomJob(ViewPortHandler(), 0f, 0f, 0f, 0f, null, null, null)) init { - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } fun getInstance( diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt index a137b53bbf..1cfb52c626 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt @@ -54,7 +54,7 @@ open class FSize : Poolable { private val pool: ObjectPool = ObjectPool.create(256, FSize(0f, 0f)) init { - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt index f4ce07e3b4..cf4095ce9d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt @@ -23,7 +23,7 @@ class MPPointD private constructor(var x: Double, var y: Double) : Poolable = ObjectPool.Companion.create(64, MPPointD(0.0, 0.0)) init { - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } fun getInstance(x: Double, y: Double): MPPointD { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt index 461af3d202..84868f2d5b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt @@ -38,7 +38,7 @@ class MPPointF : Poolable { private var pool: ObjectPool = ObjectPool.Companion.create(32, MPPointF(0f, 0f)) init { - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } fun getInstance(x: Float, y: Float): MPPointF { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt index 3ee2f5d4f5..cbe6955ea0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt @@ -26,37 +26,25 @@ class ObjectPool> private constructor(withCapacity: Int, `object private var desiredCapacity: Int private val objects = ArrayList(withCapacity) private val modelObject: T? - private var replenishPercentage: Float + var replenishPercentage: Float = 1.0f + set(value) { + var p = value + if (p > 1) { + p = 1f + } else if (p < 0f) { + p = 0f + } + field = p + } init { require(withCapacity > 0) { "Object Pool must be instantiated with a capacity greater than 0!" } this.desiredCapacity = withCapacity this.modelObject = `object` - this.replenishPercentage = 1.0f this.refillPool() } - /** - * Set the percentage of the pool to replenish on empty. Valid values are between - * 0.00f and 1.00f - * - * @param percentage a value between 0 and 1, representing the percentage of the pool to replenish. - */ - fun setReplenishPercentage(percentage: Float) { - var p = percentage - if (p > 1) { - p = 1f - } else if (p < 0f) { - p = 0f - } - this.replenishPercentage = p - } - - fun getReplenishPercentage(): Float { - return replenishPercentage - } - private fun refillPool(percentage: Float = this.replenishPercentage) { var portionOfCapacity = (desiredCapacity * percentage).toInt() @@ -68,7 +56,7 @@ class ObjectPool> private constructor(withCapacity: Int, `object this.objects.clear() - repeat(portionOfCapacity + 1) { + repeat(portionOfCapacity) { this.objects.add(modelObject!!.instantiate()) } } @@ -122,7 +110,7 @@ class ObjectPool> private constructor(withCapacity: Int, `object * @param objects A list of objects of type T to recycle */ @Synchronized - fun recycle(objects: MutableList) { + fun recycle(objects: List) { val objectsListSize = objects.size while (objectsListSize + this.objects.size > this.desiredCapacity) { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt index 1533e9c9f7..5ca117bad7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt @@ -152,7 +152,7 @@ object Utils { return paint.measureText(demoText).toInt() } - private val mCalcTextHeightRect = Rect() + private val mCalcTextHeightRect by lazy { Rect() } /** * calculates the approximate height of a text, depending on a demo text diff --git a/MPChartLib/src/test/java/com/github/mikephil/charting/test/ChartDataTest.kt b/MPChartLib/src/test/java/com/github/mikephil/charting/test/ChartDataTest.kt index 2658ee6a14..2686be28c2 100644 --- a/MPChartLib/src/test/java/com/github/mikephil/charting/test/ChartDataTest.kt +++ b/MPChartLib/src/test/java/com/github/mikephil/charting/test/ChartDataTest.kt @@ -39,13 +39,13 @@ class ChartDataTest { Assert.assertEquals(-2f, data.yMin, 0.01f) Assert.assertEquals(50f, data.yMax, 0.01f) - Assert.assertEquals(3, data.maxEntryCountSet.entryCount) + Assert.assertEquals(3, data.maxEntryCountSet?.entryCount) // now add and remove values data.addEntry(Entry(-10f, -10f), 0) Assert.assertEquals(set1, data.maxEntryCountSet) - Assert.assertEquals(4, data.maxEntryCountSet.entryCount) + Assert.assertEquals(4, data.maxEntryCountSet?.entryCount) Assert.assertEquals(-10f, data.getYMin(YAxis.AxisDependency.LEFT), 0.01f) Assert.assertEquals(50f, data.getYMax(YAxis.AxisDependency.LEFT), 0.01f) diff --git a/MPChartLib/src/test/java/com/github/mikephil/charting/test/DataSetTest.kt b/MPChartLib/src/test/java/com/github/mikephil/charting/test/DataSetTest.kt index b5db37fc29..b8d3645cd5 100644 --- a/MPChartLib/src/test/java/com/github/mikephil/charting/test/DataSetTest.kt +++ b/MPChartLib/src/test/java/com/github/mikephil/charting/test/DataSetTest.kt @@ -137,31 +137,31 @@ class DataSetTest { val set = ScatterDataSet(entries, "") var closest = set.getEntryForXValue(17f, Float.NaN, DataSet.Rounding.CLOSEST) - Assert.assertEquals(15f, closest.x, 0.01f) + Assert.assertEquals(15f, closest!!.x, 0.01f) Assert.assertEquals(5f, closest.y, 0.01f) closest = set.getEntryForXValue(17f, Float.NaN, DataSet.Rounding.DOWN) - Assert.assertEquals(15f, closest.x, 0.01f) + Assert.assertEquals(15f, closest!!.x, 0.01f) Assert.assertEquals(5f, closest.y, 0.01f) closest = set.getEntryForXValue(15f, Float.NaN, DataSet.Rounding.DOWN) - Assert.assertEquals(15f, closest.x, 0.01f) + Assert.assertEquals(15f, closest!!.x, 0.01f) Assert.assertEquals(5f, closest.y, 0.01f) closest = set.getEntryForXValue(14f, Float.NaN, DataSet.Rounding.DOWN) - Assert.assertEquals(10f, closest.x, 0.01f) + Assert.assertEquals(10f, closest!!.x, 0.01f) Assert.assertEquals(10f, closest.y, 0.01f) closest = set.getEntryForXValue(17f, Float.NaN, DataSet.Rounding.UP) - Assert.assertEquals(21f, closest.x, 0.01f) + Assert.assertEquals(21f, closest!!.x, 0.01f) Assert.assertEquals(5f, closest.y, 0.01f) closest = set.getEntryForXValue(21f, Float.NaN, DataSet.Rounding.UP) - Assert.assertEquals(21f, closest.x, 0.01f) + Assert.assertEquals(21f, closest!!.x, 0.01f) Assert.assertEquals(5f, closest.y, 0.01f) closest = set.getEntryForXValue(21f, Float.NaN, DataSet.Rounding.CLOSEST) - Assert.assertEquals(21f, closest.x, 0.01f) + Assert.assertEquals(21f, closest!!.x, 0.01f) Assert.assertEquals(5f, closest.y, 0.01f) } @@ -186,27 +186,27 @@ class DataSetTest { val set = ScatterDataSet(values, "") var closest = set.getEntryForXValue(0f, Float.NaN, DataSet.Rounding.CLOSEST) - Assert.assertEquals(0f, closest.x, 0.01f) + Assert.assertEquals(0f, closest!!.x, 0.01f) Assert.assertEquals(10f, closest.y, 0.01f) closest = set.getEntryForXValue(5f, Float.NaN, DataSet.Rounding.CLOSEST) - Assert.assertEquals(5f, closest.x, 0.01f) + Assert.assertEquals(5f, closest!!.x, 0.01f) Assert.assertEquals(80f, closest.y, 0.01f) closest = set.getEntryForXValue(5.4f, Float.NaN, DataSet.Rounding.CLOSEST) - Assert.assertEquals(5f, closest.x, 0.01f) + Assert.assertEquals(5f, closest!!.x, 0.01f) Assert.assertEquals(80f, closest.y, 0.01f) closest = set.getEntryForXValue(4.6f, Float.NaN, DataSet.Rounding.CLOSEST) - Assert.assertEquals(5f, closest.x, 0.01f) + Assert.assertEquals(5f, closest!!.x, 0.01f) Assert.assertEquals(80f, closest.y, 0.01f) closest = set.getEntryForXValue(7f, Float.NaN, DataSet.Rounding.CLOSEST) - Assert.assertEquals(7f, closest.x, 0.01f) + Assert.assertEquals(7f, closest!!.x, 0.01f) Assert.assertEquals(100f, closest.y, 0.01f) closest = set.getEntryForXValue(4f, Float.NaN, DataSet.Rounding.CLOSEST) - Assert.assertEquals(4f, closest.x, 0.01f) + Assert.assertEquals(4f, closest!!.x, 0.01f) Assert.assertEquals(60f, closest.y, 0.01f) var entries = set.getEntriesForXValue(4f) diff --git a/MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.kt b/MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.kt index bb390d30ef..26e8a2243b 100644 --- a/MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.kt +++ b/MPChartLib/src/test/java/com/github/mikephil/charting/test/ObjectPoolTest.kt @@ -6,13 +6,13 @@ import org.junit.Assert import org.junit.Test class ObjectPoolTest { - internal class TestPoolable private constructor(var foo: Int, var bar: Int) : Poolable() { - override fun instantiate(): Poolable { + internal class TestPoolable private constructor(var foo: Int, var bar: Int) : Poolable() { + override fun instantiate(): TestPoolable { return TestPoolable(0, 0) } companion object { - private val pool: ObjectPool = ObjectPool.create(4, TestPoolable(0, 0)) as ObjectPool + private val pool: ObjectPool = ObjectPool.create(4, TestPoolable(0, 0)) fun getInstance(foo: Int, bar: Int): TestPoolable { val result = pool.get() @@ -25,7 +25,7 @@ class ObjectPoolTest { pool.recycle(instance) } - fun recycleInstances(instances: List?) { + fun recycleInstances(instances: List) { pool.recycle(instances) } From d70e06572171babbe716b37f98aba105d833c9e8 Mon Sep 17 00:00:00 2001 From: Zachary Wander Date: Sun, 14 Sep 2025 19:44:11 -0400 Subject: [PATCH 9/9] Port upstream fix --- .../github/mikephil/charting/charts/CombinedChart.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt index e19fd98f77..4684ea7d5b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Canvas import android.util.AttributeSet import android.util.Log +import com.github.mikephil.charting.components.IMarker import com.github.mikephil.charting.data.BarData import com.github.mikephil.charting.data.BubbleData import com.github.mikephil.charting.data.CandleData @@ -17,6 +18,7 @@ import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet import com.github.mikephil.charting.renderer.CombinedChartRenderer + /** * This chart class allows the combination of lines, bars, scatter and candle * data all displayed in one chart area. @@ -209,11 +211,13 @@ open class CombinedChart : BarLineChartBase