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

Custom label feature added #45

Merged
merged 5 commits into from
Mar 6, 2023
Merged
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
Prev Previous commit
Next Next commit
custom label started
SahilSharma2710 committed Mar 3, 2023
commit ea188a72a4ab9a978bfeb165d4e0fa9eca80e4fe
34 changes: 14 additions & 20 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -23,26 +23,20 @@ class _MyGaugeExampleState extends State<MyGaugeExample> {
children: [
SizedBox(
child: LinearGauge(
value: 25,
indicator: LinearGaugeIndicator(
value: 20, shape: PointerShape.circle),
// customLabels: CustomLinearGaugeLabel(labelSymbol: "%"),

// rulers: ,
rangeLinearGauge: [
// RangeLinearGauge(
// color: Colors.blue[400]!, start: 0, end: 10),
// RangeLinearGauge(
// color: Colors.green[400]!, start: 10, end: 25),
// RangeLinearGauge(
// color: Colors.orange[400]!, start: 25, end: 35),
// RangeLinearGauge(
// color: Colors.indigo[400]!, start: 35, end: 50),
// RangeLinearGauge(
// color: Colors.purple[400]!, start: 50, end: 75),
// RangeLinearGauge(
// color: Colors.red[400]!, start: 75, end: 100)
]),
value: 30,
start: 10,
end: 30,
indicator: LinearGaugeIndicator(
value: 27.5, shape: PointerShape.circle),
// customLabels: [
// CustomLinearGaugeLabel(text: "10%", value: 10),
// CustomLinearGaugeLabel(text: "15\$", value: 15),
// CustomLinearGaugeLabel(text: "20@", value: 20),
// CustomLinearGaugeLabel(text: "25@", value: 25),
// CustomLinearGaugeLabel(text: "27.5#", value: 27.5),
// CustomLinearGaugeLabel(text: "30*", value: 30)
// ],
),
),
],
),
7 changes: 2 additions & 5 deletions lib/linear_gauge/custom_label/custom_linear_gauge_label.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
class CustomLinearGaugeLabel {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a small change here
can we change the class name to RulerCustomLabel? I think that make more sense .
What do you think ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes agreed that will make more sense . Custom labels are for all gauges not only for linear. Will change that.

Copy link
Contributor

@hasnentai hasnentai Mar 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or how about maybe CustomRulerLabel? Lets try to keep RulerLabel together

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per terminology we used , labels are part of rulers only. So CustomRulerBar or CustomRuler can also work but for user CustomRulerLabel make more sence . So what should be the final call

const CustomLinearGaugeLabel({
// this.labelSymbol = "",
this.text = "",
this.value = 0,
required this.text,
required this.value,
});

// /// `labelSymbol` Sets the symbol for the label
// final String? labelSymbol;
final String? text;
final double? value;
}
10 changes: 5 additions & 5 deletions lib/linear_gauge/linear_gauge.dart
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ class LinearGauge extends LeafRenderObjectWidget {
this.indicator = const LinearGaugeIndicator(),
this.rulers = const RulerStyle(),
this.rangeLinearGauge = const [],
this.customLabels = const CustomLinearGaugeLabel(),
this.customLabels = const [],
}) : super(key: key);

///
@@ -182,7 +182,7 @@ class LinearGauge extends LeafRenderObjectWidget {
///
/// `customLabels` customizes the label [CustomLinearGaugeLabel] properties
///
final CustomLinearGaugeLabel? customLabels;
final List<CustomLinearGaugeLabel>? customLabels;

///
///
@@ -216,15 +216,15 @@ class LinearGauge extends LeafRenderObjectWidget {
indicator: indicator!,
value: value!,
rangeLinearGauge: rangeLinearGauge!,
// labelSymbol: customLabels!.labelSymbol!
customLabels: customLabels!,
);
}

@override
void updateRenderObject(
BuildContext context, RenderLinearGauge renderObject) {
renderObject
..setEnd = end!
..setCustomLabels = customLabels!
..setGaugeOrientation = gaugeOrientation!
..setLabelTopMargin = labelTopMargin!
..setPrimaryRulerColor = rulers!.primaryRulerColor!
@@ -235,6 +235,7 @@ class LinearGauge extends LeafRenderObjectWidget {
..setSecondaryRulersWidth = rulers!.secondaryRulersWidth!
..setShowLinearGaugeContainer = showLinearGaugeContainer!
..setStart = start!
..setEnd = end!
..setSteps = steps!
..setTextStyle = rulers!.textStyle!
..setSecondaryRulerPerInterval = rulers!.secondaryRulerPerInterval!
@@ -250,6 +251,5 @@ class LinearGauge extends LeafRenderObjectWidget {
..setValue = value!
..setLinearGaugeIndicator = indicator
..setRangeLinearGauge = rangeLinearGauge;
// ..setLabelSymbol = customLabels!.labelSymbol;
}
}
94 changes: 47 additions & 47 deletions lib/linear_gauge/linear_gauge_label.dart
Original file line number Diff line number Diff line change
@@ -49,62 +49,64 @@ class LinearGaugeLabel {
/// The formula is from the below source
/// (!)[https://stackoverflow.com/a/3542512/4565953]
void generateOffSetsForLabel(
Size startLabel,
Size endLabel,
Size size,
double end,
double primaryRulersHeight,
double linearGaugeBoxContainerHeight,
double labelTopMargin,
LinearGaugeIndicator indicator,
Size labelSymbolSize,
) {
Size startLabel,
Size endLabel,
Size size,
double end,
double primaryRulersHeight,
double linearGaugeBoxContainerHeight,
double labelTopMargin,
LinearGaugeIndicator indicator,
bool isCustomLabelsGiven) {
primaryRulers.clear();

Offset a = Offset((startLabel.width / 2) + (indicator.width! / 2),
linearGaugeBoxContainerHeight);
Offset b = Offset(
size.width -
((endLabel.width + labelSymbolSize.width) / 2) -
(indicator.width! / 2),
size.width - (endLabel.width / 2) - (indicator.width! / 2),
linearGaugeBoxContainerHeight);

for (int i = 0; i < _linearGaugeLabel.length; i++) {
double n1 = ((_linearGaugeLabel[i].value! - _linearGaugeLabel[0].value!) /
(_linearGaugeLabel[_linearGaugeLabel.length - 1].value! -
_linearGaugeLabel[0].value!)) *
100;

n1 = 100 / n1;
double n = (_linearGaugeLabel[0].value! +
_linearGaugeLabel[_linearGaugeLabel.length - 1].value!) /
_linearGaugeLabel[i].value!;

if (i == 0) {
primaryRulers[_linearGaugeLabel[i].text!] = [
a,
Offset(a.dx, primaryRulersHeight)
];
} else if (i == _linearGaugeLabel.length - 1) {
primaryRulers[_linearGaugeLabel[i].text!] = [
b,
Offset(b.dx, primaryRulersHeight)
];
} else {
double x = ((n1 - 1) / n1) * a.dx + (1 / n1) * b.dx;
double y = ((n1 - 1) / n1) * a.dy + (1 / n1) * b.dy;

primaryRulers[_linearGaugeLabel[i].text!] = [
if (isCustomLabelsGiven) {
for (int i = 0; i < _linearGaugeLabel.length; i++) {
// n is the nth point of the line
double n = 100 /
(((_linearGaugeLabel[i].value! - _linearGaugeLabel[0].value!) /
(_linearGaugeLabel[_linearGaugeLabel.length - 1].value! -
_linearGaugeLabel[0].value!)) *
100);

if (i == 0) {
primaryRulers[_linearGaugeLabel[i].value!.toString()] = [
a,
Offset(a.dx, primaryRulersHeight)
];
} else if (i == _linearGaugeLabel.length - 1) {
primaryRulers[_linearGaugeLabel[i].value!.toString()] = [
b,
Offset(b.dx, primaryRulersHeight)
];
} else {
double x = ((n - 1) / n) * a.dx + (1 / n) * b.dx;
double y = ((n - 1) / n) * a.dy + (1 / n) * b.dy;

primaryRulers[_linearGaugeLabel[i].value!.toString()] = [
Offset(x, y),
Offset(x, primaryRulersHeight)
];
}
}
} else {
for (int i = 0; i < _linearGaugeLabel.length; i++) {
double x = a.dx * (1 - ((i) / (_linearGaugeLabel.length - 1))) +
b.dx * (i / (_linearGaugeLabel.length - 1));
double y = a.dy * (1 - ((i) / (_linearGaugeLabel.length - 1))) +
b.dy * (i / (_linearGaugeLabel.length - 1));

primaryRulers[_linearGaugeLabel[i].value!.toString()] = [
Offset(x, y),
Offset(x, primaryRulersHeight)
];
}

// double x = a.dx * (1 - ((i) / (_linearGaugeLabel.length - 1))) +
// b.dx * (i / (_linearGaugeLabel.length - 1));
// double y = a.dy * (1 - ((i) / (_linearGaugeLabel.length - 1))) +
// b.dy * (i / (_linearGaugeLabel.length - 1));

}
}

@@ -137,8 +139,6 @@ class LinearGaugeLabel {
double y = a.dy * (1 - ((i) / (totalRulers + 1))) +
b.dy * (i / (totalRulers + 1));

// print(x);

if (Offset(x, y) != a) {
Offset secondaryRulerStartPoint;
Offset secondaryRulerEndPoint;
74 changes: 33 additions & 41 deletions lib/linear_gauge/linear_gauge_painter.dart
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ class RenderLinearGauge extends RenderBox {
required bool showPrimaryRulers,
required double value,
required List<RangeLinearGauge> rangeLinearGauge,
// required String labelSymbol,
required List<CustomLinearGaugeLabel> customLabels,
}) : assert(start < end, "Start should be grater then end"),
_start = start,
_end = end,
@@ -57,8 +57,8 @@ class RenderLinearGauge extends RenderBox {
_showPrimaryRulers = showPrimaryRulers,
_value = value,
_indicator = indicator,
_rangeLinearGauge = rangeLinearGauge;
// _labelSymbol = labelSymbol;
_rangeLinearGauge = rangeLinearGauge,
_customLabels = customLabels;

///
double _valueInPixel = 0;
@@ -347,11 +347,11 @@ class RenderLinearGauge extends RenderBox {
markNeedsPaint();
}

String? get getLabelSymbol => _labelSymbol;
String? _labelSymbol;
set setLabelSymbol(String? val) {
if (_labelSymbol == val) return;
_labelSymbol = val;
List<CustomLinearGaugeLabel>? get getCustomLabels => _customLabels;
List<CustomLinearGaugeLabel>? _customLabels = <CustomLinearGaugeLabel>[];
set setCustomLabels(List<CustomLinearGaugeLabel>? val) {
if (_customLabels == val) return;
_customLabels = val;
markNeedsPaint();
}

@@ -365,10 +365,10 @@ class RenderLinearGauge extends RenderBox {
final Paint _linearGaugeContainerValuePaint = Paint();
final LinearGaugeLabel _linearGaugeLabel = LinearGaugeLabel();

late Size _startLabelSize, _endLabelSize, _labelSymbolSize;
late Size _startLabelSize, _endLabelSize;

void _calculateRulerPoints() {
if (false) {
if (getCustomLabels!.isEmpty) {
double screenSize = 3 * size.width;
final double count = math.max(screenSize / 100, 1.0);
double interval = (getEnd - getStart) / (screenSize / 100);
@@ -390,26 +390,16 @@ class RenderLinearGauge extends RenderBox {
start: getStart,
end: getEnd,
);
} else {
_linearGaugeLabel.addCustomLabels(labelList: getCustomLabels!);
}

_linearGaugeLabel.addCustomLabels(labelList: [
CustomLinearGaugeLabel(text: "10", value: 10),
CustomLinearGaugeLabel(text: "15", value: 15),
CustomLinearGaugeLabel(text: "20", value: 20),
CustomLinearGaugeLabel(text: "25", value: 25),
CustomLinearGaugeLabel(text: "27.5", value: 27.5),
CustomLinearGaugeLabel(text: "30", value: 30)
]);

_startLabelSize = _linearGaugeLabel.getLabelSize(
textStyle: getTextStyle, value: getStart.toInt().toString());

_endLabelSize = _linearGaugeLabel.getLabelSize(
textStyle: getTextStyle, value: getEnd.toInt().toString());

_labelSymbolSize = _linearGaugeLabel.getLabelSize(
textStyle: getTextStyle, value: getLabelSymbol ?? "");

_linearGaugeLabel.generateOffSetsForLabel(
_startLabelSize,
_endLabelSize,
@@ -419,20 +409,18 @@ class RenderLinearGauge extends RenderBox {
getLinearGaugeBoxDecoration.height,
getLabelTopMargin,
_indicator,
_labelSymbolSize);
getCustomLabels!.isNotEmpty);
}

void _drawLabels(
Canvas canvas,
String text,
double? value,
List<Offset> list,
) {
final ui.ParagraphStyle paragraphStyle = ui.ParagraphStyle(
textDirection: TextDirection.ltr,
);
final String labelText = text + (getLabelSymbol ?? "");

final double? value = double.tryParse(text);

// calculator method to get the text style based on the range
Color getRangeColor(String text) {
@@ -474,16 +462,12 @@ class RenderLinearGauge extends RenderBox {
final ui.ParagraphBuilder paragraphBuilder =
ui.ParagraphBuilder(paragraphStyle)
..pushStyle(labelTextStyle)
..addText(labelText);
..addText(text);
final ui.Paragraph paragraph = paragraphBuilder.build();
final Size labelSize = _linearGaugeLabel.getLabelSize(
textStyle: getTextStyle, value: value!.toInt().toString());
final Size labelSymbolSize = _linearGaugeLabel.getLabelSize(
textStyle: getTextStyle, value: getLabelSymbol ?? "");
final Size finalLabelSize =
Size(labelSymbolSize.width + labelSize.width, labelSize.height);
final Size labelSize =
_linearGaugeLabel.getLabelSize(textStyle: getTextStyle, value: text);

paragraph.layout(ui.ParagraphConstraints(width: finalLabelSize.width));
paragraph.layout(ui.ParagraphConstraints(width: labelSize.width));

// offset for drawing the label on the canvas
Offset labelPosition;
@@ -525,7 +509,7 @@ class RenderLinearGauge extends RenderBox {

if (showLabel) {
end = size.width -
(((_endLabelSize.width + _labelSymbolSize.width) / 2) +
((_endLabelSize.width / 2) +
(_startLabelSize.width / 2) +
(_indicator.width!));

@@ -636,6 +620,7 @@ class RenderLinearGauge extends RenderBox {

void _drawPrimaryRulers(Canvas canvas) {
_setPrimaryRulersPaint();
int count = 0;

_linearGaugeLabel.getPrimaryRulersOffset.forEach((key, value) {
double y;
@@ -645,7 +630,6 @@ class RenderLinearGauge extends RenderBox {

for (int i = 0; i < rangeLinearGauge!.length; i++) {
var range = rangeLinearGauge![i].end;

var offset = double.parse(key);
if (offset >= rangeLinearGauge![i].start && offset <= range) {
primaryRulerColor = rangeLinearGauge![i].color;
@@ -687,8 +671,10 @@ class RenderLinearGauge extends RenderBox {

canvas.drawLine(primaryRulerStartPoint, a, _primaryRulersPaint);
if (showLabel) {
_drawLabels(canvas, key, value);
_drawLabels(canvas, _linearGaugeLabel.getListOfLabel[count].text!,
_linearGaugeLabel.getListOfLabel[count].value!, value);
}
count++;
});
}

@@ -771,10 +757,16 @@ class RenderLinearGauge extends RenderBox {
if (_indicator.getPointerValue == null) {
_indicator.setPointerValue = value;
}

var firstOff = _linearGaugeLabel
.getPrimaryRulersOffset[getStart.toInt().toString()]![0] +
firstOffset;
var firstOff;
if (getCustomLabels!.isEmpty) {
firstOff =
_linearGaugeLabel.getPrimaryRulersOffset[getStart.toString()]![0] +
firstOffset;
} else {
firstOff = _linearGaugeLabel.getPrimaryRulersOffset[
getCustomLabels![0].value!.toDouble().toString()]![0] +
firstOffset;
}

getLinearGaugeIndicator.drawPointer(
_indicator.shape!, canvas, firstOff, this);