nabil6391 / graphview

Flutter GraphView is used to display data in graph structures. It can display Tree layout, Directed and Layered graph. Useful for Family Tree, Hierarchy View.
MIT License
420 stars 114 forks source link

Graph Connected Line #53

Closed aKaushik941 closed 1 year ago

aKaushik941 commented 2 years ago

How I can change the graph connected line to a dotted line?

nabil6391 commented 2 years ago
class DashedLinePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    double dashWidth = 9, dashSpace = 5, startX = 0;
    final paint = Paint()
      ..color = Colors.grey
      ..strokeWidth = 1;
    while (startX < size.width) {
      canvas.drawLine(Offset(startX, 0), Offset(startX + dashWidth, 0), paint);
      startX += dashWidth + dashSpace;
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}
a1573595 commented 2 years ago

In some cases, we want to be able to support both solid and dashed lines.

a1573595 commented 1 year ago

thanks for help.

截圖 2022-09-02 上午11 06 25
kadirfayzi commented 1 year ago

thanks for help.

截圖 2022-09-02 上午11 06 25

Hello, how did you implement that on graphview ? can you share an example code please. Thanks

a1573595 commented 1 year ago

thanks for help.

截圖 2022-09-02 上午11 06 25

Hello, how did you implement that on graphview ? can you share an example code please. Thanks

Just extends EdgeRenderer class and use canvas.drawLine like nabil6391.

kadirfayzi commented 1 year ago

thanks for help.

截圖 2022-09-02 上午11 06 25

Hello, how did you implement that on graphview ? can you share an example code please. Thanks

Just extends EdgeRenderer class and use canvas.drawLine like nabil6391.

Can you share your code please ?

nabil6391 commented 1 year ago

Then, create a helper function to draw the dotted line:

dart Copy code void _drawDottedLine(Canvas canvas, Offset start, Offset end, Paint paint) { Paint dottedPaint = Paint() ..color = paint.color ..strokeWidth = paint.strokeWidth ..style = PaintingStyle.stroke;

double distance = (end - start).distance; double dashLength = 4; double gapLength = 4; double totalLength = dashLength + gapLength;

final Path path = ui.Path()..moveTo(start.dx, start.dy); for (double i = 0; i < distance; i += totalLength) { path.lineTo(start.dx + (i + dashLength) (end.dx - start.dx) / distance, start.dy + (i + dashLength) (end.dy - start.dy) / distance); path.moveTo(start.dx + (i + totalLength) (end.dx - start.dx) / distance, start.dy + (i + totalLength) (end.dy - start.dy) / distance); }

canvas.drawPath(path, dottedPaint); } Finally, replace the canvas.drawLine call in the render method with the _drawDottedLine helper function:

dart Copy code _drawDottedLine(canvas, Offset(clippedLine[0], clippedLine[1]), Offset(triangleCentroid[0], triangleCentroid[1]), edge.paint ?? paint); Now, the arrow lines will be drawn as dotted lines. You can customize the dashLength and gapLength variables in the _drawDottedLine function to achieve different patterns of dashes and gaps.