Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isMinValueAtCenter property to RadarChartData #1682

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
soraef marked this conversation as resolved.
Show resolved Hide resolved

/// [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