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

Overlapping labels, label alignment - add workaround to documentation #1663

Open
atreeon opened this issue May 14, 2024 · 0 comments
Open

Overlapping labels, label alignment - add workaround to documentation #1663

atreeon opened this issue May 14, 2024 · 0 comments

Comments

@atreeon
Copy link

atreeon commented May 14, 2024

There are quite a few open issues (or closed but regressed issues) that have problems with labels.
Screenshot 2024-05-14 at 11 45 07

I found the best way to paint a label and change its rotation was with a custom painter using code similar to this.

Thought I'd add the workaround here and link to the other issues and perhaps ask for an update to the documentation. Note, I couldn't seem to get the text to overlay and have a higher 'z index' to appear over the top of the bar charts so I'm just putting the description next to it.

    return SideTitleWidget(
      axisSide: AxisSide.left,
      child: Container(
        width: 10,
        child: RotatedBox(
          quarterTurns: 3,
          child: CustomPaint(
            painter: MyCustomPainter(text: text, textStyle: style),
          ),
        ),
      ),
    );
class MyCustomPainter extends CustomPainter {
  final String text;
  final TextStyle textStyle;

  MyCustomPainter({super.repaint, required this.textStyle, required this.text});

  @override
  void paint(Canvas canvas, Size size) {
    const textStyle = TextStyle(
      color: Colors.black,
      fontSize: 20,
    );

    var textSpan = TextSpan(
      text: text,
      style: textStyle,
    );
    final textPainter = TextPainter(
      text: textSpan,
      textDirection: TextDirection.ltr,
    );
    textPainter.layout(
      minWidth: 0,
    );

    final offset = Offset(15, 30);
    textPainter.paint(canvas, offset);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

#1132 #1434 #922 #911

Note: it isn't 'perfect' as the gridlines and anything else is painted on top of the labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant