初始化图表,主要设置图表的线型,坐标轴等:
Func<double,string> formatFunc=(x)=>string.Format("0:0.00",x);//设置标签格式
var fillLine=new SolidColorBrush();//设置线图格式刷
fillLine.Opacity=1;//设置不透明度
var fillPoint=new SolidColorBrush();
fillPoint.Opacity=1;
fillPoint.Color=System.Windows.Media.Color.FromRgb(230,148,26);
//初始化表系列
Chart.Series=new SeriesCollection
{
new LineSeries//线图
{
Title="Fitted Line",//设置系列名称;
PointGeometry=null,
Fill=fillLine,
PointForeground=System.Windows.Media.Brushes.Blue,
},
new ScatterSeries
{
Title="Original Value",//设置系列名称;
PointGeometry=DefaultGeometries.Circle,
Fill=fillPoint,
},
//此处还可以添加其他的线性图,比如柱状图等;或者加上for循环添加其他线型
};
//设置坐标轴
Chart.AxisX=new AxesCollection
{
new Axis
{
Title="T(K)",
Foreground=System.Windows.Media.Brushes.Black,
FontSize=12,
};
}
Chart.AxisY.Add(new Axis
{
Title="Heat Capacity(W)",
FontSize=12.
LabelFormatter=formatFunc,
Foreground=System.Windows.Media.Brushes.Black,
});
Chart.LegendLocation=LengendLocation.Right;//设置图例的位置
给图表添加数据:
Chart.Series[0].Values=new ChartValues<ObservablePoint>();
Chart.Series[1].Values=new ChartValues<ObservablePoint>();
double[] XL= new double[6];
double[] YL= new double[6];//线图源数据
double[] XP=new dobule[6];
double[] YP=new double[6];//散点图源数据
XL={1,2,3,4,5,6};
YL={2,4,6,8,10,12};
XP={1,2,3,4,5,6};
YP={2.01,4.02,6.01,8.02,10.01,12.02};
for(int i=0;i<6;i++)
{
var pointLine=new ObservablePoint()
{
X=XL[i],
Y=YL[i]
}
var pointPoint=new ObservablePoint()
{
X=XP[i],
Y=XP[i]
}
Chart.Series[0].Values.Add(pointLine);
Chart.Series[1].Values.Add(pointPoint);
}
Chart.AxisX[0].Separator.IsEnable=false;
以上就是对LiveChart的操作。使用LiveChart的前提是在NuGet程序包中添加LiveChart