@@ -9,6 +9,263 @@ documentation: ug
99
1010# Methods in Flutter Cartesian Charts (SfCartesianChart)
1111
12+ ## Methods in Plot band
13+
14+ ### drawRect method
15+
16+ The ` drawRect ` method is used to customize the appearance of the plot band.
17+
18+ This following code sample shows how to customize the appearance of the plot band by using drawRect method.
19+
20+ {% tabs %}
21+ {% highlight dart %}
22+
23+ late List<_SalesData> _data;
24+
25+ @override
26+ void initState() {
27+ _data = <_SalesData>[
28+ _SalesData('Jan', 35),
29+ _SalesData('Feb', 28),
30+ _SalesData('Mar', 34),
31+ _SalesData('Apr', 32),
32+ _SalesData('May', 40),
33+ ];
34+ super.initState();
35+ }
36+
37+ @override
38+ Widget build(BuildContext context) {
39+ final int startIndex = 1; // Feb
40+ final int endIndex = 3; // Apr
41+
42+ return Scaffold(
43+ body: SfCartesianChart(
44+ primaryXAxis: CategoryAxis(
45+ plotBands: <PlotBand>[
46+ CustomStripedPlotBand(
47+ start: startIndex.toDouble(),
48+ end: endIndex.toDouble(),
49+ color: const Color(0xFFFFF3E0),
50+ opacity: 0.9,
51+ borderColor: Colors.deepOrange,
52+ borderWidth: 1.2,
53+ patternColor: Colors.deepOrange,
54+ stripeOpacity: 0.45,
55+ stripeWidth: 4.0,
56+ stripeSpacing: 10.0,
57+ shouldRenderAboveSeries: true,
58+ ),
59+ ],
60+ ),
61+ series: <CartesianSeries<_SalesData, String>>[
62+ LineSeries<_SalesData, String>(),
63+ ],
64+ ),
65+ );
66+ }
67+
68+ class _SalesData {
69+ _SalesData(this.year, this.sales);
70+ final String year;
71+ final double sales;
72+ }
73+
74+ /// Custom striped plot band that draws vertical dashed stripes inside the band.
75+ class CustomStripedPlotBand extends PlotBand {
76+ const CustomStripedPlotBand({
77+ super.isVisible,
78+ super.start,
79+ super.end,
80+ super.color = const Color(0xFFE0E0E0),
81+ super.opacity,
82+ super.borderColor,
83+ super.borderWidth = 0,
84+ super.shouldRenderAboveSeries,
85+
86+ // Custom stripe properties
87+ this.patternColor = const Color(0xFF000000),
88+ this.stripeOpacity = 0.25,
89+ this.stripeWidth = 4.0,
90+ this.stripeSpacing = 6.0,
91+ });
92+
93+ final Color patternColor;
94+ final double stripeOpacity;
95+ final double stripeWidth;
96+ final double stripeSpacing;
97+
98+ @override
99+ void drawRect(
100+ Canvas canvas,
101+ Rect rect,
102+ Paint fillPaint, [
103+ Paint? strokePaint,
104+ ]) {
105+ // Early return if bounds width is invalid
106+ if (rect.width <= 0) {
107+ return;
108+ }
109+
110+ final paint = Paint()
111+ ..color = patternColor.withOpacity(stripeOpacity)
112+ ..style = PaintingStyle.fill;
113+
114+ canvas.save();
115+ canvas.clipRect(rect);
116+
117+ // Fixed dash pattern based on stripe width
118+ final double dashLength = stripeWidth * 2;
119+ final double gapLength = stripeWidth;
120+ // Calculate number of stripes based on width
121+ final double totalWidth = rect.width;
122+ final double stripeSpacing = (stripeWidth + this.stripeSpacing);
123+ final int numberOfStripes = math.max(
124+ 1,
125+ (totalWidth / stripeSpacing).floor(),
126+ );
127+
128+ // Draw stripes
129+ for (int i = 0; i < numberOfStripes; i++) {
130+ final double x = rect.left + (i * stripeSpacing) + (stripeSpacing / 2);
131+ if (x + stripeWidth > rect.right) break;
132+ // Use absolute positioning for Y coordinates
133+ final double yStart = math.min(rect.top, rect.bottom);
134+ final double yEnd = math.max(rect.top, rect.bottom);
135+ // Draw dashed pattern
136+ double currentY = yStart;
137+ bool drawDash = true;
138+ while (currentY < yEnd) {
139+ if (drawDash) {
140+ final double segmentEnd = math.min(currentY + dashLength, yEnd);
141+ canvas.drawRect(
142+ Rect.fromLTWH(x, currentY, stripeWidth, segmentEnd - currentY),
143+ paint,
144+ );
145+ currentY = segmentEnd;
146+ } else {
147+ currentY = math.min(currentY + gapLength, yEnd);
148+ }
149+ drawDash = !drawDash;
150+ }
151+ }
152+ canvas.restore();
153+ }
154+ }
155+
156+ {% endhighlight %}
157+ {% endtabs %}
158+
159+ ![ custom plotband appearance] ( images\axis-customization\plotband_custom_rect.png )
160+
161+ ### drawText method
162+
163+ The ` drawText ` method is used to customize the text and text style of the plot band.
164+
165+ The following code sample shows how to customize the plot band text and text style using the drawText method.
166+
167+ {% tabs %}
168+ {% highlight dart %}
169+
170+ late List<_SalesData> _data;
171+
172+ @override
173+ void initState() {
174+ _data = <_SalesData>[
175+ _SalesData('Jan', 35),
176+ _SalesData('Feb', 28),
177+ _SalesData('Mar', 34),
178+ _SalesData('Apr', 32),
179+ _SalesData('May', 40),
180+ ];
181+ super.initState();
182+ }
183+
184+ @override
185+ Widget build(BuildContext context) {
186+ final int startIndex = 1; // Feb
187+ final int endIndex = 3; // Apr
188+
189+ return Scaffold(
190+ body: SfCartesianChart(
191+ primaryXAxis: CategoryAxis(
192+ plotBands: <PlotBand>[
193+ CustomStripedPlotBand(
194+ start: startIndex.toDouble(),
195+ end: endIndex.toDouble(),
196+ color: Colors.orange,
197+ opacity: 0.8,
198+ text: "Custom plotband text",
199+ textStyle: TextStyle(
200+ color: Colors.black,
201+ fontSize: 14,
202+ fontWeight: FontWeight.bold,
203+ ),
204+ shouldRenderAboveSeries: true,
205+ ),
206+ ],
207+ ),
208+ series: <CartesianSeries<_SalesData, String>>[
209+ LineSeries<_SalesData, String>(),
210+ ],
211+ ),
212+ );
213+ }
214+
215+ class _SalesData {
216+ _SalesData(this.year, this.sales);
217+ final String year;
218+ final double sales;
219+ }
220+
221+ /// Custom striped plot band that draws vertical dashed stripes inside the band.
222+ class CustomStripedPlotBand extends PlotBand {
223+ const CustomStripedPlotBand({
224+ super.isVisible,
225+ super.start,
226+ super.end,
227+ super.color,
228+ super.opacity,
229+ super.textAngle,
230+ super.text,
231+ super.textStyle,
232+ super.shouldRenderAboveSeries,
233+ });
234+
235+ @override
236+ void drawText(Canvas canvas, Offset position, TextStyle style, int angle) {
237+ if (text == null || text!.isEmpty) {
238+ return;
239+ }
240+ final TextSpan span = TextSpan(text: text, style: style);
241+ final TextPainter textPainter = TextPainter(
242+ text: span,
243+ textAlign: TextAlign.center,
244+ textDirection: TextDirection.ltr,
245+ );
246+ textPainter.layout();
247+ if (angle == 0) {
248+ textPainter.paint(canvas, position);
249+ } else {
250+ final double halfWidth = textPainter.width / 2;
251+ final double halfHeight = textPainter.height / 2;
252+ canvas
253+ ..save()
254+ ..translate(position.dx + halfWidth, position.dy + halfHeight)
255+ ..rotate(degreesToRadians(angle.toDouble()));
256+ textPainter.paint(canvas, Offset(-halfWidth, -halfHeight));
257+ canvas.restore();
258+ }
259+ }
260+
261+ double degreesToRadians(double deg) => deg * (pi / 180);
262+ }
263+
264+ {% endhighlight %}
265+ {% endtabs %}
266+
267+ ![ Custom plotband text] ( images\axis-customization\plotband_custom_text.png )
268+
12269## Methods in tooltipBehavior
13270
14271### Show method in tooltipBehavior
0 commit comments