graphview icon indicating copy to clipboard operation
graphview copied to clipboard

How to scale and center a graph so that the whole graph is visible

Open CaptainDario opened this issue 3 years ago • 4 comments

Thank you for the great package!

I am trying to show a graph and I want that it is centered and scaled to a size so that the whole graph is visible. Is this possible?

This is what I have: drawing

This is what I would like to have: drawing

CaptainDario avatar Sep 19 '22 19:09 CaptainDario

have you found a solution by any chance?

marcusrohden avatar Feb 06 '23 10:02 marcusrohden

is this answered?

shaheer-ahmed-dev avatar Sep 15 '23 06:09 shaheer-ahmed-dev

I did it like this

https://github.com/CaptainDario/DaKanji/blob/main/lib/widgets/dictionary/kanji_group_widget.dart#L78

Which results in smth like this

Screenshot_20230916_002317.jpg

CaptainDario avatar Sep 15 '23 22:09 CaptainDario

I stumbled accross the same issue and here's my solution that is more dynamic (works well when the child graph is smaller than the viewport as well as bigger)

    @override
    Widget build(BuildContext c) {
      return InteractiveViewer(
        constrained: false,
        boundaryMargin: const EdgeInsets.all(100),
        minScale: 0.01,
        maxScale: 5.6,
        child: ConstrainedBox(
          constraints: BoxConstraints(
            minWidth: MediaQuery.of(context).size.width,
            minHeight: MediaQuery.of(context).size.height,
          ),
          child: Center(
            child: GraphView(
              graph: graph,
              algorithm: BuchheimWalkerAlgorithm(builder, TreeEdgeRenderer(builder)),
              paint: Paint()
                ..color = Constant.primaryColor
                ..strokeWidth = 1
                ..style = PaintingStyle.stroke,
              builder: (Node node) => (node as MNode).builder(context, draggedTarget),
            ),
          ),
        ),
      );
    }

Masadow avatar Feb 24 '24 10:02 Masadow