banner



How To Draw Charts On Notability

Sheet tutorial 04 | How to draw a pie chart and with a round bending in the flutter?

When nosotros draw an arc, which is close with a straight line, not with a round angle, so I will tell you lot how to draw an arc with a round angle.

We ofttimes have many to data to prove in the flutter, some oft choose to evidence as a pie nautical chart. In this tutorial, I will tell you how to do it. But before it, you should know how to employ the canvas, if you lot exercise not know you tin look Canvass tutorial 01.

Before nosotros start our work, permit's see the product equally below.

our products

In the flutter, we tin only draw an arc with right angle directly, but how to create an arc with a round angle? We have an thought show below.

If we draw an arc, it will be similar theorignal movie, sometimes we do not like it, we just desire a 1 with a round angle above, so we can add each circle for both of them. The film is very clear for you lot to sympathise. And then when we want to describe an arc with ii round angle end. We demand to draw a mutual arc first, then ii circles.

Information technology is piece of cake, nosotros only use the API in the flutter. We create a function to use, if we input right param, we will get an arc.

                      void            _drawArcWithCenter(
Canvas sheet,
Paint paint, {
Starting time centre,
double radius,
startRadian = 0.0,
sweepRadian = pi,
}) {
canvas.drawArc(
Rect.fromCircle(center: center, radius: radius),
startRadian,
sweepRadian,
fake,
paint,
);
}

We will draw 2 circles for the arc, only sometimes we practise not demand the round angle withal. And so we need the params to choose whether or not we need them. The code is below, the hasStartArc,hasEndArc params mean that we whether or not need the offset circle or the stop circle. the other params are same every bit the arc we draw in a higher place.

                      void            _drawArcTwoPoint(Sail canvas, Pigment pigment,
{Start center,
double radius,
startRadian = 0.0,
sweepRadian = pi,
hasStartArc = imitation,
hasEndArc = false}) {
var smallR = paint.strokeWidth / 2;
paint.strokeWidth = smallR;
if (hasStartArc) {
var startCenter = LineCircle.radianPoint(
Point(centre.dx, centre.dy), radius, startRadian);
paint.style = PaintingStyle.fill;
canvas.drawCircle(Beginning(startCenter.x, startCenter.y), smallR, pigment);
}
if (hasEndArc) {
var endCenter = LineCircle.radianPoint(
Point(middle.dx, center.dy), radius, startRadian + sweepRadian);
paint.style = PaintingStyle.fill up;
canvas.drawCircle(Outset(endCenter.ten, endCenter.y), smallR, paint);
}
}

Sometimes, we have some set represent all of our information, we want to show them in i pie nautical chart, so start, nosotros need to calculate the total count of the data. Then calculate the proportion of the radian of the circle, every bit we know, the radian of the circumvolve is 2*pi, and so we tin can get the radian, in our fore tutorial, we tin like shooting fish in a barrel to get the points of the small circles.

Calculate radian with each part.

          List<double> radians = List<double>();
for (double d in sources) {
double radian = d * ii * pi / total;
radians.add together(radian);
}

Draw all arc with radians.

                      for            (int i = 0; i < radians.length; i++) {
var rd = radians[i];
paint.colour = colors[i % colors.length];
paint.strokeWidth = paintWidth;
_drawArcWithCenter(canvas, paint,
middle: heart, radius: radius, startRadian: startA, sweepRadian: rd);
startA += rd;
}

Draw all round angle with end circles

                      for            (int i = 0; i < radians.length; i++) {
var rd = radians[i];
paint.color = colors[i % colors.length];
paint.strokeWidth = paintWidth;
_drawArcTwoPoint(canvas, paint,
center: eye,
radius: radius,
startRadian: startA,
sweepRadian: rd,
hasEndArc: true);
startA += rd;
}

But sometimes we need to show one role is very special, so I ascertain a current role that can show 2 round angles in the pie chart. Also, yous tin use the param of curPaintWidthto get in different from other parts. Let's see the whole code fragment of drawing piechart.

                      void            _drawArcGroup(Sheet canvas,
{Showtime center,
double radius,
List<double> sources,
Listing<Colour> colors,
double startAngle = 0.0,
double paintWidth = 10.0,
bool hasEnd = false,
hasCurrent = false,
int curIndex = 0,
curPaintWidth = 12.0}) {
affirm(sources != nix && sources.length > 0);
assert(colors != null && colors.length > 0);
var pigment = Paint()
..style = PaintingStyle.make full
..color = BLUE_NORMAL
..strokeWidth = paintWidth
..isAntiAlias = true;
double total = 0;
for (double d in sources) {
total += d;
}
assert(total > 0.0);
List<double> radians = List<double>();
for (double d in sources) {
double radian = d * 2 * pi / total;
radians.add(radian);
}
var startA = startAngle;
pigment.style = PaintingStyle.stroke;
var curStartAngle = 0.0;
for (int i = 0; i < radians.length; i++) {
var rd = radians[i];
if (hasCurrent && curIndex == i) {
curStartAngle = startA;
startA += rd;
keep;
}
pigment.color = colors[i % colors.length];
paint.strokeWidth = paintWidth;
_drawArcWithCenter(canvas, pigment,
center: center, radius: radius, startRadian: startA, sweepRadian: rd);
startA += rd;
}
if (hasEnd) {
startA = startAngle;
paint.strokeWidth = paintWidth;
for (int i = 0; i < radians.length; i++) {
var rd = radians[i];
if (hasCurrent && curIndex == i) {
startA += rd;
continue;
}
pigment.color = colors[i % colors.length];
paint.strokeWidth = paintWidth;
_drawArcTwoPoint(canvas, pigment,
center: middle,
radius: radius,
startRadian: startA,
sweepRadian: rd,
hasEndArc: true);
startA += rd;
}
}

if (hasCurrent) {
paint.color = colors[curIndex % colors.length];
paint.strokeWidth = curPaintWidth;
paint.manner = PaintingStyle.stroke;
_drawArcWithCenter(canvas, pigment,
center: center,
radius: radius,
startRadian: curStartAngle,
sweepRadian: radians[curIndex]);
}
if (hasCurrent && hasEnd) {
var rd = radians[curIndex % radians.length];
paint.color = colors[curIndex % colors.length];
paint.strokeWidth = curPaintWidth;
pigment.style = PaintingStyle.fill up;
_drawArcTwoPoint(sheet, paint,
center: eye,
radius: radius,
startRadian: curStartAngle,
sweepRadian: rd,
hasEndArc: true,
hasStartArc: true);
}
}

Just in our void paint(sheet,size) function, utilise our office in a higher place. Just like follow.

                      void            paint(Canvas canvas, Size size) {
//580*648
if (size.width > 1.0 && size.height > 1.0) {
print(">1.9");
_sizeUtil.logicSize = size;
}
var paint = Paint()
..mode = PaintingStyle.fill up
..color = BLUE_NORMAL
..strokeWidth = 2.0
..isAntiAlias = truthful;
paint.color = Colors.grey[900];
// canvas.drawCircle(
// Offset(_sizeUtil.getAxisX(250), _sizeUtil.getAxisY(250.0)),
// _sizeUtil.getAxisBoth(200.0),
// paint);
paint.color = RED_DARK1;
paint.strokeWidth = 20;
pigment.mode = PaintingStyle.stroke;
var center = Starting time(
_sizeUtil.getAxisX(250.0),
_sizeUtil.getAxisY(250.0),
);
var radius = _sizeUtil.getAxisBoth(200);
_drawArcGroup(
sheet,
eye: center,
radius: radius,
sources: [1, 2, ane, two, ane, 2, 1, ii, 1, two],
colors: [BLUE_DARK1, RED_DARK1, BLUE_DARK2, GREEN_NORMAL, YELLOW_NORMAL],
paintWidth: 50.0,
startAngle: 0.0,
hasEnd: true,
hasCurrent: false,
curPaintWidth: thirty.0,
curIndex: one,
);
canvas.salvage();
canvas.restore();
}

If you practise not want the round angle, but change the hasEnd=imitation ,if you lot want show the someone to large, you should set harCurrent=ture and assign curPaintWidth a value of yous like. The curIndex is for which i should be gear up. For example, nosotros change value every bit below.

          _drawArcGroup(
canvas,
center: center,
radius: radius,
sources: [one, 2, 1, 2, 1, 2, ],
colors: [BLUE_DARK1, RED_DARK1, BLUE_DARK2, GREEN_NORMAL, YELLOW_NORMAL],
paintWidth: 50.0,
startAngle: 0.0,
hasEnd: false,
hasCurrent: true,
curPaintWidth: threescore.0,
curIndex: one,
);

The result.

The other param is for your research. Such as modifying its calibration.

In this tutorial, we can draw the pie chart in the palpitate.

  1. The principle of the arc with a circular angle.
  2. Describe the arc and two circles.
  3. Define a function to describe a pie chart.
  4. Apply the pie nautical chart with the dissimilar values of the params.

The whole project, if you similar, thanks for a star in the GitHub: https://github.com/FlutterOpen/flutter-canvas

The End

Developer: https://github.com/nb312

facebook page: https://world wide web.facebook.com/flutteropen

Thank you for your reading, you tin follow the Palpitate Open up.

Source: https://medium.com/flutteropen/canvas-tutorial-04-how-to-draw-a-pie-chart-and-with-a-round-angle-in-the-flutter-8a22126704f4

Posted by: francisviode1952.blogspot.com

0 Response to "How To Draw Charts On Notability"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel