在我的应用程序中,我有四个要绘制为折线图的系列。每个系列的大小相同,其中三个为double
,最后一个为DateTime
列表。这三个双重系列出现在GraphData
类型的类对象列表中,如下所示:
public class GraphData
{
public string Name { get; set; }
public List<double> Data { get; set; }
}
作为一项附加要求,我希望每个都有自己的Y轴。
到目前为止,这是我的整个程序,它可以毫无问题地在自己的轴上绘制三个图形。
public partial class MainWindow : Window
{
public SeriesCollection SeriesCollection { get; set; }
public AxesCollection YAxesCollection { get; set; }
public List<GraphData> GraphDatas { get; set; }
public List<DateTime> TimeStamps { get; set; }
public MainWindow()
{
InitializeComponent();
GraphDatas = GetGraphData();
TimeStamps = GetTimeStamps(GraphDatas[0].Data.Count);
Plot();
}
private void Plot()
{
SeriesCollection = new SeriesCollection();
YAxesCollection = new AxesCollection();
var count = 0;
foreach (var data in GraphDatas)
{
var gLineSeries = new GLineSeries
{
Title = data.Name,
Values = data.Data.AsGearedValues().WithQuality(Quality.Low),
PointGeometry = null,
Fill = Brushes.Transparent,
ScalesYAt = count
};
SeriesCollection.Add(gLineSeries);
YAxesCollection.Add(new Axis() { Title = data.Name });
count++;
}
DataContext = this;
}
private List<GraphData> GetGraphData()
{
var dataList = new List<GraphData>
{
new GraphData() { Name = "DataA", Data = new List<double>() { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 11.0, 11.0, 9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, } },
new GraphData() { Name = "DataB", Data = new List<double>() { 26, 33, 65, 28, 34, 55, 25, 44, 50, 36, 26, 37, 43, 62, 35, 38, 45, 32, 28, 34 } },
new GraphData() { Name = "DataC", Data = new List<double>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 } }
};
return dataList;
}
private List<DateTime> GetTimeStamps(int limit)
{
var timeStamps = new List<DateTime>();
var now = DateTime.Now;
for (int i = 0; i < limit; i++)
{
if (i == 0)
timeStamps.Add(now);
else
{
now = now.AddDays(1);
timeStamps.Add(now);
}
}
return timeStamps;
}
}
我的XAML
看起来很简单:
<Grid>
<lvc:CartesianChart Series="{Binding SeriesCollection}"
AxisY="{Binding YAxesCollection}"
DisableAnimations="True"
LegendLocation="Right">
</lvc:CartesianChart>
</Grid>
GetGraphData()
和GetTimeStamps()
是模拟我原始功能的伪函数。
现在这很好用,除了X轴不是DateTime
之外,因为显然我没有这样绘制。但是我该怎么做呢?
The official documentation以及this SO Post仅展示了如何仅使用一个Y-Axis
来做到这一点。
答案 0 :(得分:1)
我将首先对模型进行一些更改,以使其显示完整图片。时间戳是数据点的一部分,您需要将它们包装在一起以使实时图表HUX <-
可以绘制数据。
mapper
如果要保留当前的提取(CSV)流程,可以简单地将数据LINQ public class DataPoint
{
public DataPoint(DateTime timeStamp, double value)
{
TimeStamp = timeStamp;
Value = value;
}
public double Value { get; }
public DateTime TimeStamp { get; }
}
public class GraphData
{
public string Name { get; set; }
public List<DataPoint> Data { get; set; }
}
转换成其绘图表形式。
Zip
XAML
public partial class MainWindow : Window
{
public SeriesCollection SeriesCollection { get; set; }
public Func<double, string> Formatter { get; set; }
public AxesCollection YAxesCollection { get; set; }
public List<GraphData> GraphDatas { get; set; }
public MainWindow()
{
InitializeComponent();
var timeStamps = GetTimeStamps(20);
GraphDatas = GetGraphData(timeStamps);
Plot();
}
private List<GraphData> GetGraphData(List<DateTime> timeStamps)
{
var valuesA = new List<double>() { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 11.0, 11.0, 9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, };
var valuesB = new List<double>() { 26, 33, 65, 28, 34, 55, 25, 44, 50, 36, 26, 37, 43, 62, 35, 38, 45, 32, 28, 34 };
var valuesC = new List<double>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
List<DataPoint> MergeData(List<double> values) => timeStamps.Zip(values, (x, y) => new DataPoint(x, y)).ToList();
var dataList = new List<GraphData>
{
new GraphData() { Name = "DataA", Data = MergeData(valuesA) },
new GraphData() { Name = "DataB", Data = MergeData(valuesB) },
new GraphData() { Name = "DataC", Data = MergeData(valuesC) },
};
return dataList;
}
private void Plot()
{
var mapper = Mappers.Xy<DataPoint>()
.X(dp => (double)dp.TimeStamp.Ticks)
.Y(dp => dp.Value);
SeriesCollection = new SeriesCollection(mapper);
YAxesCollection = new AxesCollection();
var count = 0;
foreach (var data in GraphDatas)
{
var gLineSeries = new GLineSeries
{
Title = data.Name,
Values = data.Data.AsGearedValues().WithQuality(Quality.Low),
PointGeometry = null,
Fill = Brushes.Transparent,
ScalesYAt = count
};
SeriesCollection.Add(gLineSeries);
YAxesCollection.Add(new Axis() { Title = data.Name });
count++;
}
Formatter = value => new DateTime((long)value).ToString("yyyy-MM:dd HH:mm:ss");
DataContext = this;
}
private List<DateTime> GetTimeStamps(int limit)
{
var timeStamps = new List<DateTime>();
var now = DateTime.Now;
for (int i = 0; i < limit; i++)
{
if (i == 0)
timeStamps.Add(now);
else
{
now = now.AddDays(1);
timeStamps.Add(now);
}
}
return timeStamps;
}
}