Teafiles和茶馆图表库背后的架构
本文关键字:背后 茶馆 Teafiles | 更新日期: 2023-09-27 18:18:21
我偶然发现了一个叫做Teafiles.net的开源。net库,它可以处理时间序列的存储和检索。专有产品"茶馆"可以绘制这样的时间序列。我想知道茶馆产品是否也提供源代码,是开源还是付费许可。我对能够只加载当前图表视图上可见的数据点的技术以及如何实现类似的解决方案感兴趣。
我正在寻找实现类似的东西,并想知道是否有人遇到过类似的技术或知道是否付费茶馆许可证也可与源代码。
我目前正在开发一个基于ZedGraph库的趋势解决方案,我正在使用TeaFiles来缓存来自数据库的大量数据。
我不知道茶馆解决方案背后到底是什么样的技术。但我也使用了一种方法来显示来自TeaFile的大量数据中两个日期之间的一组点。
ZedGraph库有一个FilteredPointList
对象,它执行自动数据点抽取。它包括一个SetBounds
方法,允许您选择要显示的日期范围和要显示的最大点数。通常,它对应于视图的实际宽度。
FilteredPointList
(原始源代码)使用两个double数组来包含XY数据。考虑到T是一个包含DateTime和double属性的结构,通过用TeaFile对象替换数组可以很容易地使这个类适应TeaFilePointList
。
实现不是最优的,但我开始这样做。稍后我可能会更新这段代码,以包含TeaFile的MemoryMappedFile特性。这样会快得多。
public class TeaFilePointList : IPointList
{
TeaFile<point> tf;
private int _maxPts = -1;
private int _minBoundIndex = -1;
private int _maxBoundIndex = -1;
struct point
{
public TeaTime.Time x;
public double y;
}
public TeaFilePointList(DateTime[] x, double[] y)
{
tf = TeaFile<point>.Create(Path.GetRandomFileName() + ".tea");
for (var i = 0; i < x.Length; i++)
tf.Write(new point() { x = x[i], y = y[i] });
}
public void SetBounds(double min, double max, int maxPts)
{
_maxPts = maxPts;
// find the index of the start and end of the bounded range
var xmin = (DateTime)new XDate(min);
var xmax = (DateTime)new XDate(max);
int first = tf.BinarySearch(xmin, item => (DateTime)item.x);
int last = tf.BinarySearch(xmax, item => (DateTime)item.x);
// Make sure the bounded indices are legitimate
// if BinarySearch() doesn't find the value, it returns the bitwise
// complement of the index of the 1st element larger than the sought value
if (first < 0)
{
if (first == -1)
first = 0;
else
first = ~(first + 1);
}
if (last < 0)
last = ~last;
_minBoundIndex = first;
_maxBoundIndex = last;
}
public int Count
{
get
{
int arraySize = (int)tf.Count;
// Is the filter active?
if (_minBoundIndex >= 0 && _maxBoundIndex >= 0 && _maxPts > 0)
{
// get the number of points within the filter bounds
int boundSize = _maxBoundIndex - _minBoundIndex + 1;
// limit the point count to the filter bounds
if (boundSize < arraySize)
arraySize = boundSize;
// limit the point count to the declared max points
if (arraySize > _maxPts)
arraySize = _maxPts;
}
return arraySize;
}
}
public PointPair this[int index]
{
get
{
if (_minBoundIndex >= 0 && _maxBoundIndex >= 0 && _maxPts >= 0)
{
// get number of points in bounded range
int nPts = _maxBoundIndex - _minBoundIndex + 1;
if (nPts > _maxPts)
{
// if we're skipping points, then calculate the new index
index = _minBoundIndex + (int)((double)index * (double)nPts / (double)_maxPts);
}
else
{
// otherwise, index is just offset by the start of the bounded range
index += _minBoundIndex;
}
}
double xVal, yVal;
if (index >= 0 && index < tf.Count)
xVal = new XDate(tf.Items[index].x);
else
xVal = PointPair.Missing;
if (index >= 0 && index < tf.Count)
yVal = tf.Items[index].y;
else
yVal = PointPair.Missing;
return new PointPair(xVal, yVal, PointPair.Missing, null);
}
}
public object Clone()
{
throw new NotImplementedException(); // I'm lazy...
}
public void Close()
{
tf.Close();
tf.Dispose();
File.Delete(tf.Name);
}
}
最难的部分是实现TeaFile的BinarySearch,以便使用DateTime快速搜索记录。我看了看阵列。通过使用反编译器实现BinarySearch,并且我在下面编写了扩展:
public static int BinarySearch<T, U>(this TeaFile<T> tf, U target, Func<T, U> indexer) where T : struct
{
var lo = 0;
var hi = (int)tf.Count - 1;
var comp = Comparer<U>.Default;
while(lo <= hi)
{
var median = lo + (hi - lo >> 1);
var num = comp.Compare(indexer(tf.Items[median]), target);
if (num == 0)
return median;
if (num < 0)
lo = median + 1;
else
hi = median - 1;
}
return ~lo;
}
如果ZedGraph不适合你的需要,至少你得到了这个想法。FilteredPointList类中使用的抽取算法非常好,可以以另一种方式进行调整以满足您的需要。