Skip to content

Commit

Permalink
Add isMinValueAtCenter property to RadarChartData
Browse files Browse the repository at this point in the history
  • Loading branch information
soraef committed Jun 14, 2024
1 parent 514bd9e commit 7735ead
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/src/chart/radar_chart/radar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class RadarChartData extends BaseChartData with EquatableMixin {
BorderSide? tickBorderData,
BorderSide? gridBorderData,
RadarTouchData? radarTouchData,
this.isMinValueAtCenter = false,
super.borderData,
}) : assert(dataSets != null && dataSets.hasEqualDataEntriesLength),
assert(
Expand Down Expand Up @@ -154,6 +155,9 @@ class RadarChartData extends BaseChartData with EquatableMixin {
/// Handles touch behaviors and responses.
final RadarTouchData radarTouchData;

/// If [isMinValueAtCenter] is true, the minimum value of the [RadarChart] will be at the center of the chart.
final bool isMinValueAtCenter;

/// [titleCount] we use this value to determine number of [RadarChart] grid or lines.
int get titleCount => dataSets[0].dataEntries.length;

Expand Down
23 changes: 20 additions & 3 deletions lib/src/chart/radar_chart/radar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class RadarChartPainter extends BaseChartPainter<RadarChartData> {
double getChartCenterValue(RadarChartData data) {
final dataSetMaxValue = data.maxEntry.value;
final dataSetMinValue = data.minEntry.value;

if (data.isMinValueAtCenter) {
return dataSetMinValue;
}

final tickSpace = getSpaceBetweenTicks(data);
final centerValue = dataSetMinValue - tickSpace;

Expand All @@ -100,6 +105,10 @@ class RadarChartPainter extends BaseChartPainter<RadarChartData> {
@visibleForTesting
double getFirstTickValue(RadarChartData data) {
final defaultCenterValue = getDefaultChartCenterValue();
if (data.isMinValueAtCenter) {
return defaultCenterValue;
}

final dataSetMaxValue = data.maxEntry.value;
final dataSetMinValue = data.minEntry.value;

Expand All @@ -111,9 +120,14 @@ class RadarChartPainter extends BaseChartPainter<RadarChartData> {

@visibleForTesting
double getSpaceBetweenTicks(RadarChartData data) {
final defaultCenterValue = getDefaultChartCenterValue();
final dataSetMaxValue = data.maxEntry.value;
final dataSetMinValue = data.minEntry.value;

if (data.isMinValueAtCenter) {
return (dataSetMaxValue - dataSetMinValue) / (data.tickCount);
}

final defaultCenterValue = getDefaultChartCenterValue();
final tickSpace = (dataSetMaxValue - dataSetMinValue) / data.tickCount;
final defaultTickSpace =
(dataSetMaxValue - defaultCenterValue) / (data.tickCount + 1);
Expand Down Expand Up @@ -171,7 +185,9 @@ class RadarChartPainter extends BaseChartPainter<RadarChartData> {
tickValue += tickSpace;
}

final tickDistance = radius / (ticks.length);
final tickDistance = data.isMinValueAtCenter
? radius / (ticks.length - 1)
: radius / ticks.length;

_tickPaint
..color = data.tickBorderData.color
Expand All @@ -180,7 +196,8 @@ class RadarChartPainter extends BaseChartPainter<RadarChartData> {
/// draw radar ticks
ticks.sublist(0, ticks.length - 1).asMap().forEach(
(index, tick) {
final tickRadius = tickDistance * (index + 1);
final tickRadius =
tickDistance * (index + (data.isMinValueAtCenter ? 0 : 1));
if (data.radarShape == RadarShape.circle) {
canvasWrapper.drawCircle(centerOffset, tickRadius, _tickPaint);
} else {
Expand Down
1 change: 1 addition & 0 deletions repo_files/documentations/radar_chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ When you change the chart's state, it animates to the new state internally (usin
|tickBorderData|Style of the tick borders|BorderSide(color: Colors.black, width: 2)|
|gridBorderData|Style of the grid borders|BorderSide(color: Colors.black, width: 2)|
|radarTouchData|[RadarTouchData](#radartouchdata-read-about-touch-handling) handles the touch behaviors and responses.|RadarTouchData()|
|isMinValueAtCenter|If true, the minimum value of the [RadarChart] will be at the center of the chart.|false|

### RadarDataSet
|PropName |Description |default value|
Expand Down

0 comments on commit 7735ead

Please sign in to comment.