Skip to content

Commit

Permalink
fixed minX, minY functionality bug, related to issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Jun 4, 2019
1 parent a01c354 commit 58947ec
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
1 change: 1 addition & 0 deletions example/lib/line_chart/samples/line_chart_sample3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class LineChartSample3 extends StatelessWidget {
}),
),
],
minY: 0,
gridData: FlGridData(
show: true,
drawHorizontalGrid: true,
Expand Down
1 change: 1 addition & 0 deletions example/lib/line_chart/samples/line_chart_sample4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class LineChartSample4 extends StatelessWidget {
),
),
],
minY: 0,
titlesData: FlTitlesData(
horizontalTitlesTextStyle:
TextStyle(fontSize: 10, color: Colors.purple, fontWeight: FontWeight.bold),
Expand Down
1 change: 1 addition & 0 deletions example/lib/line_chart/samples/line_chart_sample5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class LineChartSample5 extends StatelessWidget {
dotData: FlDotData(show: false),
),
],
minY: 0,
titlesData: FlTitlesData(
getVerticalTitles: (val) {
return "";
Expand Down
48 changes: 24 additions & 24 deletions lib/src/chart/base/fl_axis_chart/fl_axis_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'fl_axis_chart_data.dart';
/// in child classes -> [BarChartPainter], [LineChartPainter]
abstract class FlAxisChartPainter<D extends FlAxisChartData> extends FlChartPainter<D> {
final D data;

Paint gridPaint;

FlAxisChartPainter(this.data) : super(data) {
Expand All @@ -25,28 +26,27 @@ abstract class FlAxisChartPainter<D extends FlAxisChartData> extends FlChartPain
if (!data.gridData.show || data.gridData == null) {
return;
}
var usableViewSize = getChartUsableDrawSize(viewSize);
final Size usableViewSize = getChartUsableDrawSize(viewSize);
// Show Vertical Grid
if (data.gridData.drawVerticalGrid) {
gridPaint.color = data.gridData.verticalGridColor;
gridPaint.strokeWidth = data.gridData.verticalGridLineWidth;

int verticalCounter = 1;
while (data.gridData.verticalInterval * verticalCounter < data.maxY) {
var currentIntervalSeek = data.gridData.verticalInterval * verticalCounter;
if (data.gridData.checkToShowVerticalGrid(currentIntervalSeek)) {
double bothY = getPixelY(currentIntervalSeek, usableViewSize);
double x1 = 0 + getLeftOffsetDrawSize();
double y1 = bothY + getTopOffsetDrawSize();
double x2 = usableViewSize.width + getLeftOffsetDrawSize();
double y2 = bothY + getTopOffsetDrawSize();
double verticalSeek = data.minY;
while (verticalSeek < data.maxY) {
if (data.gridData.checkToShowVerticalGrid(verticalSeek)) {
final double bothY = getPixelY(verticalSeek, usableViewSize);
final double x1 = 0 + getLeftOffsetDrawSize();
final double y1 = bothY + getTopOffsetDrawSize();
final double x2 = usableViewSize.width + getLeftOffsetDrawSize();
final double y2 = bothY + getTopOffsetDrawSize();
canvas.drawLine(
Offset(x1, y1),
Offset(x2, y2),
gridPaint,
);
}
verticalCounter++;
verticalSeek += data.gridData.verticalInterval;
}
}

Expand All @@ -55,22 +55,22 @@ abstract class FlAxisChartPainter<D extends FlAxisChartData> extends FlChartPain
gridPaint.color = data.gridData.horizontalGridColor;
gridPaint.strokeWidth = data.gridData.horizontalGridLineWidth;

int horizontalCounter = 1;
while (data.gridData.horizontalInterval * horizontalCounter < data.maxX) {
var currentIntervalSeek = data.gridData.horizontalInterval * horizontalCounter;
if (data.gridData.checkToShowHorizontalGrid(currentIntervalSeek)) {
double bothX = getPixelX(currentIntervalSeek, usableViewSize);
double x1 = bothX;
double y1 = 0 + getTopOffsetDrawSize();
double x2 = bothX;
double y2 = usableViewSize.height + getTopOffsetDrawSize();
double horizontalSeek = data.minX;
while (horizontalSeek < data.maxX) {
if (data.gridData.checkToShowHorizontalGrid(horizontalSeek)) {
final double bothX = getPixelX(horizontalSeek, usableViewSize);
final double x1 = bothX;
final double y1 = 0 + getTopOffsetDrawSize();
final double x2 = bothX;
final double y2 = usableViewSize.height + getTopOffsetDrawSize();
canvas.drawLine(
Offset(x1, y1),
Offset(x2, y2),
gridPaint,
);
}
horizontalCounter++;

horizontalSeek += data.gridData.horizontalInterval;
}
}
}
Expand All @@ -79,7 +79,7 @@ abstract class FlAxisChartPainter<D extends FlAxisChartData> extends FlChartPain
/// to the view base axis x .
/// the view 0, 0 is on the top/left, but the spots is bottom/left
double getPixelX(double spotX, Size chartUsableSize) {
return ((spotX / data.maxX) * chartUsableSize.width) + getLeftOffsetDrawSize();
return (((spotX - data.minX) / (data.maxX - data.minX)) * chartUsableSize.width) + getLeftOffsetDrawSize();
}

/// With this function we can convert our [FlSpot] y
Expand All @@ -88,7 +88,7 @@ abstract class FlAxisChartPainter<D extends FlAxisChartData> extends FlChartPain
double spotY,
Size chartUsableSize,
) {
double y = data.maxY - spotY;
return ((y / data.maxY) * chartUsableSize.height) + getTopOffsetDrawSize();
final double y = (data.maxY - data.minY) - spotY + data.minY;
return ((y / (data.maxY - data.minY)) * chartUsableSize.height) + getTopOffsetDrawSize();
}
}
2 changes: 1 addition & 1 deletion lib/src/chart/base/fl_chart/fl_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FlBorderData {
typedef GetTitleFunction = String Function(double value);

String defaultGetTitle(double value) {
return '${value.toInt()}';
return '${value}';
}

/// This class is responsible to hold data about showing titles.
Expand Down
30 changes: 15 additions & 15 deletions lib/src/chart/line_chart/line_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,48 +236,48 @@ class LineChartPainter extends FlAxisChartPainter {

// Vertical Titles
if (data.titlesData.showVerticalTitles) {
int verticalCounter = 0;
while (data.gridData.verticalInterval * verticalCounter <= data.maxY) {
double verticalSeek = data.minY;
while (verticalSeek <= data.maxY) {
double x = 0 + getLeftOffsetDrawSize();
double y = getPixelY(data.gridData.verticalInterval * verticalCounter, viewSize) +
double y = getPixelY(verticalSeek, viewSize) +
getTopOffsetDrawSize();

String text =
data.titlesData.getVerticalTitles(data.gridData.verticalInterval * verticalCounter);
final String text =
data.titlesData.getVerticalTitles(verticalSeek);

TextSpan span = TextSpan(style: data.titlesData.verticalTitlesTextStyle, text: text);
TextPainter tp = TextPainter(
final TextSpan span = TextSpan(style: data.titlesData.verticalTitlesTextStyle, text: text);
final TextPainter tp = TextPainter(
text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr);
tp.layout(maxWidth: getExtraNeededHorizontalSpace());
x -= tp.width + data.titlesData.verticalTitleMargin;
y -= (tp.height / 2);
y -= tp.height / 2;
tp.paint(canvas, Offset(x, y));

verticalCounter++;
verticalSeek += data.gridData.verticalInterval;
}
}

// Horizontal titles
if (data.titlesData.showHorizontalTitles) {
int horizontalCounter = 0;
while (data.gridData.horizontalInterval * horizontalCounter <= data.maxX) {
double x = getPixelX(data.gridData.horizontalInterval * horizontalCounter, viewSize);
double horizontalSeek = data.minX;
while (horizontalSeek <= data.maxX) {
double x = getPixelX(horizontalSeek, viewSize);
double y = viewSize.height + getTopOffsetDrawSize();

String text = data.titlesData
.getHorizontalTitles(data.gridData.horizontalInterval * horizontalCounter);
.getHorizontalTitles(horizontalSeek);

TextSpan span = TextSpan(style: data.titlesData.horizontalTitlesTextStyle, text: text);
TextPainter tp = TextPainter(
text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr);
tp.layout();

x -= (tp.width / 2);
x -= tp.width / 2;
y += data.titlesData.horizontalTitleMargin;

tp.paint(canvas, Offset(x, y));

horizontalCounter++;
horizontalSeek += data.gridData.horizontalInterval;
}
}
}
Expand Down

0 comments on commit 58947ec

Please sign in to comment.