浏览代码

charts demo

/main
kg 6 年前
当前提交
7d24f31e
共有 12 个文件被更改,包括 355 次插入2 次删除
  1. 2
      Runtime/widgets/framework.cs
  2. 8
      Tests/Editor/Widgets.cs
  3. 8
      Tests/Editor/demo_charts.meta
  4. 149
      Tests/Editor/demo_charts/bar.cs
  5. 11
      Tests/Editor/demo_charts/bar.cs.meta
  6. 38
      Tests/Editor/demo_charts/color_palette.cs
  7. 11
      Tests/Editor/demo_charts/color_palette.cs.meta
  8. 67
      Tests/Editor/demo_charts/main.cs
  9. 11
      Tests/Editor/demo_charts/main.cs.meta
  10. 41
      Tests/Editor/demo_charts/tween.cs
  11. 11
      Tests/Editor/demo_charts/tween.cs.meta

2
Runtime/widgets/framework.cs


}
public abstract class StatefulWidget : Widget {
protected StatefulWidget(Key key) : base(key: key) {
protected StatefulWidget(Key key = null) : base(key: key) {
}
public override Element createElement() {

8
Tests/Editor/Widgets.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UIWidgets.Tests.demo_charts;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.editor;
using Unity.UIWidgets.foundation;

this.eventsPage,
this.asPage,
this.stack,
this.mouseHover
this.mouseHover,
this.charts
};
this._optionStrings = this._options.Select(x => x.Method.Name).ToArray();
this._selected = 0;

Widget mouseHover() {
return new MouseHoverWidget();
}
Widget charts() {
return new ChartPage();
}
}

8
Tests/Editor/demo_charts.meta


fileFormatVersion: 2
guid: 16325b3157a5144c789848b8ecc53311
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

149
Tests/Editor/demo_charts/bar.cs


using System.Collections.Generic;
using System.Linq;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using UnityEngine;
using Canvas = Unity.UIWidgets.ui.Canvas;
using Color = Unity.UIWidgets.ui.Color;
using Rect = Unity.UIWidgets.ui.Rect;
namespace UIWidgets.Tests.demo_charts {
public class BarChart {
public BarChart(List<Bar> bars) {
this.bars = bars;
}
public static BarChart empty() {
return new BarChart(new List<Bar>());
}
public static BarChart random(Size size) {
var barWidthFraction = 0.75f;
var ranks = selectRanks(ColorPalette.primary.length);
var barCount = ranks.Count;
var barDistance = size.width / (1 + barCount);
var barWidth = barDistance * barWidthFraction;
var startX = barDistance - barWidth / 2;
var bars = Enumerable.Range(0, barCount).Select(i => new Bar(
ranks[i],
startX + i * barDistance,
barWidth,
Random.value * size.height,
ColorPalette.primary[ranks[i]]
)).ToList();
return new BarChart(bars);
}
static List<int> selectRanks(int cap) {
var ranks = new List<int>();
var rank = 0;
while (true) {
if (Random.value < 0.2f) {
rank++;
}
if (cap <= rank) {
break;
}
ranks.Add(rank);
rank++;
}
return ranks;
}
public readonly List<Bar> bars;
}
public class BarChartTween : Tween<BarChart> {
readonly MergeTween<Bar> _barsTween;
public BarChartTween(BarChart begin, BarChart end) : base(begin: begin, end: end) {
this._barsTween = new MergeTween<Bar>(begin.bars, end.bars);
}
public override BarChart lerp(float t) {
return new BarChart(this._barsTween.lerp(t));
}
}
public class Bar : MergeTweenable<Bar> {
public Bar(int rank, float x, float width, float height, Color color) {
this.rank = rank;
this.x = x;
this.width = width;
this.height = height;
this.color = color;
}
public readonly int rank;
public readonly float x;
public readonly float width;
public readonly float height;
public readonly Color color;
public Bar empty {
get { return new Bar(this.rank, this.x, 0.0f, 0.0f, this.color); }
}
public bool less(Bar other) {
return this.rank < other.rank;
}
public Tween<Bar> tweenTo(Bar other) {
return new BarTween(this, other);
}
public static Bar lerp(Bar begin, Bar end, float t) {
D.assert(begin.rank == end.rank);
return new Bar(
begin.rank,
MathUtils.lerpFloat(begin.x, end.x, t),
MathUtils.lerpFloat(begin.width, end.width, t),
MathUtils.lerpFloat(begin.height, end.height, t),
Color.lerp(begin.color, end.color, t)
);
}
}
public class BarTween : Tween<Bar> {
public BarTween(Bar begin, Bar end) : base(begin: begin, end: end) {
D.assert(begin.rank == end.rank);
}
public override Bar lerp(float t) {
return Bar.lerp(this.begin, this.end, t);
}
}
public class BarChartPainter : AbstractCustomPainter {
public BarChartPainter(Animation<BarChart> animation)
: base(repaint: animation) {
this.animation = animation;
}
public readonly Animation<BarChart> animation;
public override void paint(Canvas canvas, Size size) {
var paint = new Paint();
paint.style = PaintingStyle.fill;
var chart = this.animation.value;
foreach (var bar in chart.bars) {
paint.color = bar.color;
canvas.drawRect(
Rect.fromLTWH(
bar.x,
size.height - bar.height,
bar.width,
bar.height
),
paint
);
}
}
public override bool shouldRepaint(CustomPainter old) {
return false;
}
}
}

11
Tests/Editor/demo_charts/bar.cs.meta


fileFormatVersion: 2
guid: 20c43f616dd7640279364f93d965f4c0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

38
Tests/Editor/demo_charts/color_palette.cs


using System.Collections.Generic;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
namespace UIWidgets.Tests.demo_charts {
public class ColorPalette {
public static readonly ColorPalette primary = new ColorPalette(new List<Color> {
Colors.blue[400],
Colors.red[400],
Colors.green[400],
Colors.yellow[400],
Colors.purple[400],
Colors.orange[400],
Colors.teal[400]
});
public ColorPalette(List<Color> colors) {
D.assert(colors.isNotEmpty);
this._colors = colors;
}
readonly List<Color> _colors;
public Color this[int index] {
get { return this._colors[index % this.length]; }
}
public int length {
get { return this._colors.Count; }
}
public Color random() {
return this[Random.Range(0, this.length - 1)];
}
}
}

11
Tests/Editor/demo_charts/color_palette.cs.meta


fileFormatVersion: 2
guid: f9e9e9f9c5ecd474b8ef6a194f2d9fc7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

67
Tests/Editor/demo_charts/main.cs


using System;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
namespace UIWidgets.Tests.demo_charts {
/***
* from https://github.com/mravn/charts
*/
public class ChartPage : StatefulWidget {
public override State createState() {
return new ChartPageState();
}
}
public class ChartPageState : TickerProviderStateMixin<ChartPage> {
public static readonly Size size = new Size(200.0f, 100.0f);
AnimationController _animation;
BarChartTween _tween;
public override
void initState() {
base.initState();
this._animation = new AnimationController(
duration: new TimeSpan(0, 0, 0, 0, 300),
vsync: this
);
this._tween = new BarChartTween(
BarChart.empty(),
BarChart.random(size)
);
this._animation.forward();
}
public override void dispose() {
this._animation.dispose();
base.dispose();
}
void changeData() {
this.setState(() => {
this._tween = new BarChartTween(
this._tween.evaluate(this._animation),
BarChart.random(size)
);
this._animation.forward(from: 0.0f);
});
}
public override Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new CustomPaint(
size: size,
painter: new BarChartPainter(this._tween.animate(this._animation))
)
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Unity.UIWidgets.material.Icons.refresh),
onPressed: this.changeData
)
);
}
}
}

11
Tests/Editor/demo_charts/main.cs.meta


fileFormatVersion: 2
guid: 905d6da1ea7b947be937556edda2b664
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

41
Tests/Editor/demo_charts/tween.cs


using System.Collections.Generic;
using System.Linq;
using Unity.UIWidgets.animation;
namespace UIWidgets.Tests.demo_charts {
public interface MergeTweenable<T> {
T empty { get; }
Tween<T> tweenTo(T other);
bool less(T other);
}
public class MergeTween<T> : Tween<List<T>> where T : MergeTweenable<T> {
public MergeTween(List<T> begin, List<T> end) : base(begin: begin, end: end) {
int bMax = begin.Count;
int eMax = end.Count;
var b = 0;
var e = 0;
while (b + e < bMax + eMax) {
if (b < bMax && (e == eMax || begin[b].less(end[e]))) {
this._tweens.Add(begin[b].tweenTo(begin[b].empty));
b++;
} else if (e < eMax && (b == bMax || end[e].less(begin[b]))) {
this._tweens.Add(end[e].empty.tweenTo(end[e]));
e++;
} else {
this._tweens.Add(begin[b].tweenTo(end[e]));
b++;
e++;
}
}
}
readonly List<Tween<T>> _tweens = new List<Tween<T>>();
public override List<T> lerp(float t) {
return Enumerable.Range(0, this._tweens.Count).Select(i => this._tweens[i].lerp(t)).ToList();
}
}
}

11
Tests/Editor/demo_charts/tween.cs.meta


fileFormatVersion: 2
guid: ae95c92eb6d8346f3bbc89a91d9e191d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存