前面的兩篇文章【動(dòng)畫效果(八) 、動(dòng)畫效果(九) 】中,我們只需要統(tǒng)計(jì)產(chǎn)品和地區(qū),如果現(xiàn)在增加一個(gè)統(tǒng)計(jì)項(xiàng)目——銷售渠道,那么使用之前的堆疊條形圖和分組條形圖都不適合。我們可以將兩者結(jié)合,使用分組+堆疊條形圖,實(shí)際效果如下圖所示:
在金平等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷,外貿(mào)網(wǎng)站建設(shè),金平網(wǎng)站建設(shè)費(fèi)用合理。
如上圖,我們使用同一種顏色的不同透明度表示不同的銷售渠道,為了實(shí)現(xiàn)不同的透明度,我們需要先更新一下color_palette.dart文件的代碼:
import 'package:flutter/material.dart'; import 'dart:math'; class ColorPalette { static final ColorPalette primary = new ColorPalette([ Colors.blue[400], Colors.red[400], Colors.green[400], Colors.yellow[400], Colors.purple[400], Colors.orange[400], Colors.teal[400], ]); ColorPalette(List colors) : _colors = colors { assert(colors.isNotEmpty); } factory ColorPalette.monochrome(Color base, int length) { return new ColorPalette( new List.generate( length, (i) => _brighterColor(base, i, length) ) ); } static Color _brighterColor(Color base, int i, int n) { /* const Color.fromARGB( int a, int r, int g, int b ) 從四個(gè)整數(shù)的低8位構(gòu)造一個(gè)顏色 a:a是alpha值,0是透明的,255是完全不透明的 r:r為紅色,從0到255 g:g為紅色,從0到255 b:b為紅色,從0到255 */ return new Color.fromARGB( /* int alpha 這個(gè)顏色的alpha通道是8位值 值為0表示此顏色完全透明。值為255表示此顏色完全不透明 */ base.alpha, _brighterComponent(base.red, i, n), _brighterComponent(base.green, i, n), _brighterComponent(base.blue, i, n) ); } static int _brighterComponent(int base, int i, int n) { return (base + i * (255 - base) / n).floor(); } final List _colors; Color operator [](int index) => _colors[index % length]; int get length => _colors.length; Color random(Random random) => this[random.nextInt(length)]; }
最后我們?cè)俑乱幌耣ar.dart文件的代碼:
import 'package:flutter/material.dart'; import 'package:flutter/animation.dart'; import 'dart:ui' show lerpDouble; import 'dart:math'; import 'color_palette.dart'; import 'tween.dart'; class BarChart { BarChart(this.groups); final Listgroups; factory BarChart.empty(Size size) { return new BarChart( []); } factory BarChart.random(Size size, Random random) { const groupWidthFraction = 0.75; const stackWidthFraction = 0.9; final groupRanks = _selectRanks(random, 5); final groupCount = groupRanks.length; final groupDistance = size.width / (1+groupCount); final groupWidth = groupDistance * groupWidthFraction; final startX = groupDistance - groupWidth/2; final stackRanks = _selectRanks(random, ColorPalette.primary.length ~/ 2); final stackCount = stackRanks.length; final stackDistance = groupWidth / stackCount; final stackWidth = stackDistance * stackWidthFraction; final groups = new List.generate( groupCount, (i) { final stacks = new List.generate( stackCount, (j) { final baseColor = ColorPalette.primary[stackRanks[j]]; final barRanks = _selectRanks(random, 4); final monochrome = new ColorPalette.monochrome(baseColor, 4); final bars = new List.generate( barRanks.length, (k) => new Bar( barRanks[k], random.nextDouble() * size.height / 2, monochrome[barRanks[k]], ) ); return new BarStack( stackRanks[j], baseColor, startX + i * groupDistance + j * stackDistance, stackWidth, bars, ); } ); return new BarGroup( groupRanks[i], stacks ); } ); return new BarChart(groups); } static List _selectRanks(Random random, int cap) { final ranks = []; var rank = 0; while(true) { rank += random.nextInt(2); if(cap <= rank) break; ranks.add(rank); rank++; } return ranks; } } class BarChartTween extends Tween { BarChartTween(BarChart begin, BarChart end) : _groupsTween = new MergeTween (begin.groups, end.groups), super(begin: begin, end: end); final MergeTween _groupsTween; @override BarChart lerp(double t) => new BarChart(_groupsTween.lerp(t)); } class BarGroup implements MergeTweenable { BarGroup(this.rank, this.stacks); final int rank; final List stacks; @override BarGroup get empty => new BarGroup(rank, []); @override bool operator <(BarGroup other) => rank < other.rank; @override Tween tweenTo(BarGroup other) => new BarGroupTween(this, other); } class BarGroupTween extends Tween { BarGroupTween(BarGroup begin, BarGroup end) : _stacksTween = new MergeTween (begin.stacks, end.stacks), super(begin: begin, end: end) { assert(begin.rank == end.rank); } final MergeTween _stacksTween; @override BarGroup lerp(double t) => new BarGroup( begin.rank, _stacksTween.lerp(t) ); } class BarStack implements MergeTweenable { BarStack(this.rank, this.baseColor, this.x, this.width, this.bars); final int rank; final Color baseColor; final double x; final double width; final List bars; @override BarStack get empty => new BarStack(rank, baseColor, x, 0.0, []); @override bool operator <(BarStack other) => rank < other.rank; @override Tween tweenTo(BarStack other) => new BarStackTween(this, other); } class BarStackTween extends Tween { BarStackTween(BarStack begin, BarStack end) : _barsTween = new MergeTween (begin.bars, end.bars), super(begin: begin, end: end) { assert(begin.rank == end.rank); } final MergeTween _barsTween; @override BarStack lerp(double t) => new BarStack( begin.rank, Color.lerp(begin.baseColor, end.baseColor, t), lerpDouble(begin.x, end.x, t), lerpDouble(begin.width, end.width, t), _barsTween.lerp(t) ); } class Bar extends MergeTweenable { Bar(this.rank, this.height, this.color); final int rank; final double height; final Color color; @override Bar get empty => new Bar(rank, 0.0, color); @override bool operator <(Bar other) => rank < other.rank; @override Tween tweenTo(Bar other) => new BarTween(this, other); static Bar lerp(Bar begin, Bar end, double t) { assert(begin.rank == end.rank); return new Bar( begin.rank, lerpDouble(begin.height, end.height, t), Color.lerp(begin.color, end.color, t) ); } } class BarTween extends Tween { BarTween(Bar begin, Bar end) : super(begin: begin, end: end) { assert(begin.rank == end.rank); } @override Bar lerp(double t) => Bar.lerp(begin, end, t); } class BarChartPainter extends CustomPainter { BarChartPainter(Animation animation) : animation = animation, super(repaint: animation); final Animation animation; @override void paint(Canvas canvas, Size size) { final barPaint = new Paint()..style = PaintingStyle.fill; final linePaint = new Paint() ..style = PaintingStyle.stroke ..color = Colors.white ..strokeWidth = 1.0; final linePath = new Path(); final chart = animation.value; for(final group in chart.groups) { for(final stack in group.stacks) { var y = size.height; for(final bar in stack.bars) { barPaint.color = bar.color; canvas.drawRect( new Rect.fromLTWH( stack.x, y - bar.height, stack.width, bar.height ), barPaint ); if(y < size.height) { linePath.moveTo(stack.x, y); linePath.lineTo(stack.x + stack.width, y); } y -= bar.height; } canvas.drawPath( linePath, linePaint ); linePath.reset(); } } } @override bool shouldRepaint(BarChartPainter old) => false; }
關(guān)于如何實(shí)現(xiàn)動(dòng)畫效果的教程到這里就暫時(shí)告一段落了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。