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 rotate angle to chart #124

Merged
merged 8 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 2 additions & 0 deletions example/lib/line_chart/samples/line_chart_sample2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class _LineChartSample2State extends State<LineChartSample2> {
bottomTitles: SideTitles(
showTitles: true,
reservedSize: 22,
rotateAngle: 45,
arefhosseini marked this conversation as resolved.
Show resolved Hide resolved
textStyle: TextStyle(
color: const Color(0xff68737d),
fontWeight: FontWeight.bold,
Expand All @@ -99,6 +100,7 @@ class _LineChartSample2State extends State<LineChartSample2> {
),
leftTitles: SideTitles(
showTitles: true,
rotateAngle: 45,
textStyle: TextStyle(
color: const Color(0xff67727d),
fontWeight: FontWeight.bold,
Expand Down
23 changes: 20 additions & 3 deletions lib/src/chart/bar_chart/bar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import '../../utils/utils.dart';

class BarChartPainter extends AxisChartPainter<BarChartData> with TouchHandler<BarTouchResponse> {
Paint barPaint, bgTouchTooltipPaint;
Paint clearPaint;
Expand Down Expand Up @@ -288,7 +290,12 @@ class BarChartPainter extends AxisChartPainter<BarChartData> with TouchHandler<B
tp.layout(maxWidth: getExtraNeededHorizontalSpace());
x -= tp.width + leftTitles.margin;
y -= tp.height / 2;
canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(leftTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));
canvas.restore();

verticalSeek += leftTitles.interval;
}
Expand All @@ -310,7 +317,12 @@ class BarChartPainter extends AxisChartPainter<BarChartData> with TouchHandler<B
tp.layout(maxWidth: getExtraNeededHorizontalSpace());
x += rightTitles.margin;
y -= tp.height / 2;
canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(rightTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));
canvas.restore();

verticalSeek += rightTitles.interval;
}
Expand All @@ -329,10 +341,15 @@ class BarChartPainter extends AxisChartPainter<BarChartData> with TouchHandler<B
TextPainter(text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr);
tp.layout();

final double textX = groupBarPos.groupX - (tp.width / 2);
final double textY = drawSize.height + getTopOffsetDrawSize() + bottomTitles.margin;
final double x = groupBarPos.groupX - (tp.width / 2);
final double y = drawSize.height + getTopOffsetDrawSize() + bottomTitles.margin;

tp.paint(canvas, Offset(textX, textY));
canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(bottomTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));
canvas.restore();
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/chart/base/base_chart/base_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class SideTitles {
final TextStyle textStyle;
final double margin;
final double interval;
final double rotateAngle;

const SideTitles({
this.showTitles = false,
Expand All @@ -130,6 +131,7 @@ class SideTitles {
),
this.margin = 6,
this.interval = 1.0,
this.rotateAngle = 0.0,
arefhosseini marked this conversation as resolved.
Show resolved Hide resolved
});

static SideTitles lerp(SideTitles a, SideTitles b, double t) {
Expand Down
29 changes: 21 additions & 8 deletions lib/src/chart/line_chart/line_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:fl_chart/src/chart/base/base_chart/touch_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import '../../utils/utils.dart';
import 'line_chart_data.dart';

class LineChartPainter extends AxisChartPainter<LineChartData> with TouchHandler<LineTouchResponse> {
Expand Down Expand Up @@ -579,8 +580,12 @@ class LineChartPainter extends AxisChartPainter<LineChartData> with TouchHandler
tp.layout(maxWidth: getExtraNeededHorizontalSpace());
x -= tp.width + leftTitles.margin;
y -= tp.height / 2;
canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(leftTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));

canvas.restore();
verticalSeek += leftTitles.interval;
}
}
Expand All @@ -603,8 +608,12 @@ class LineChartPainter extends AxisChartPainter<LineChartData> with TouchHandler
x -= tp.width / 2;
y -= topTitles.margin + tp.height;

canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(topTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));

canvas.restore();
horizontalSeek += topTitles.interval;
}
}
Expand All @@ -625,8 +634,12 @@ class LineChartPainter extends AxisChartPainter<LineChartData> with TouchHandler
tp.layout(maxWidth: getExtraNeededHorizontalSpace());
x += rightTitles.margin;
y -= tp.height / 2;
canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(rightTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));

canvas.restore();
verticalSeek += rightTitles.interval;
}
}
Expand All @@ -638,19 +651,19 @@ class LineChartPainter extends AxisChartPainter<LineChartData> with TouchHandler
while (horizontalSeek <= data.maxX) {
double x = getPixelX(horizontalSeek, viewSize);
double y = viewSize.height + getTopOffsetDrawSize();

final String text = bottomTitles.getTitles(horizontalSeek);

final TextSpan span = TextSpan(style: bottomTitles.textStyle, text: text);
final TextPainter tp =
TextPainter(text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr);
tp.layout();

x -= tp.width / 2;
y += bottomTitles.margin;

canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(bottomTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));

canvas.restore();
horizontalSeek += bottomTitles.interval;
}
}
Expand Down
21 changes: 21 additions & 0 deletions lib/src/chart/scatter_chart/scatter_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:fl_chart/src/chart/base/base_chart/touch_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import '../../utils/utils.dart';
import 'scatter_chart_data.dart';

class ScatterChartPainter extends AxisChartPainter<ScatterChartData> with TouchHandler<ScatterTouchResponse> {
Expand Down Expand Up @@ -66,7 +67,12 @@ class ScatterChartPainter extends AxisChartPainter<ScatterChartData> with TouchH
tp.layout(maxWidth: getExtraNeededHorizontalSpace());
x -= tp.width + leftTitles.margin;
y -= tp.height / 2;
canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(leftTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));
canvas.restore();

verticalSeek += leftTitles.interval;
}
Expand All @@ -90,7 +96,12 @@ class ScatterChartPainter extends AxisChartPainter<ScatterChartData> with TouchH
x -= tp.width / 2;
y -= topTitles.margin + tp.height;

canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(topTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));
canvas.restore();

horizontalSeek += topTitles.interval;
}
Expand All @@ -112,7 +123,12 @@ class ScatterChartPainter extends AxisChartPainter<ScatterChartData> with TouchH
tp.layout(maxWidth: getExtraNeededHorizontalSpace());
x += rightTitles.margin;
y -= tp.height / 2;
canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(rightTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));
canvas.restore();

verticalSeek += rightTitles.interval;
}
Expand All @@ -136,7 +152,12 @@ class ScatterChartPainter extends AxisChartPainter<ScatterChartData> with TouchH
x -= tp.width / 2;
y += bottomTitles.margin;

canvas.save();
canvas.translate(x, y);
canvas.rotate(radians(bottomTitles.rotateAngle));
canvas.translate(-x, -y);
tp.paint(canvas, Offset(x, y));
canvas.restore();

horizontalSeek += bottomTitles.interval;
}
Expand Down