C# 更改LiveCharts中轴刻度标签的格式

C# 更改LiveCharts中轴刻度标签的格式,c#,winforms,format,axis-labels,livecharts,C#,Winforms,Format,Axis Labels,Livecharts,我就是找不到改变y轴刻度标签格式的解决方案。 现在我得到了像0.03和0.035这样的标签。但我总是需要小数点后三位数字。 最大的问题是,如何访问标签格式 这是我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Sy

我就是找不到改变y轴刻度标签格式的解决方案。 现在我得到了像
0.03
0.035
这样的标签。但我总是需要小数点后三位数字。 最大的问题是,如何访问标签格式

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using LiveCharts;
using LiveCharts.Helpers;
using LiveCharts.Wpf;
using LiveCharts.Wpf.Charts.Base;
using Brushes = System.Windows.Media.Brushes;
using Color = System.Windows.Media.Color;
using MessageBox = System.Windows.Forms.MessageBox;

namespace LiveAnalysis
{
    public partial class Form1 : Form
    {
        List<DateTime> xvals = new List<DateTime>();
        List<double> yvals = new List<double>();
        private int _amountValues;
        private int _startValue;
        private List<double> _yvalsRange;

        public Form1()
        {
            InitializeComponent();

            // event
            cartesianChart2.DataClick += CartesianChart1OnDataClick;
            hScrollBar1.ValueChanged += (sender, e) => hScrollBar1_ValueChange(sender, e, hScrollBar1.Value);

            // get original data
            using (var db = new Analysis.DataClasses1DataContext())
            {
                var lttResults = db.LttResults;
                var par1 = 0.0;
                foreach (var data in lttResults)
                {
                    if (data.GatewayEventType == 41)
                        par1 = data.FloatValue;
                    if (data.GatewayEventType != 42) continue;

                    var par2 = data.FloatValue;
                    var diff = Math.Round(par1 - par2, 3);
                    yvals.Add(diff);
                    xvals.Add(data.DateTime);
                }
            }

            // chart settings
            ChartSettings();
        }

        private void ChartSettings()
        {
            // performance
            cartesianChart2.DisableAnimations = true;
            cartesianChart2.DataTooltip = null;
            cartesianChart2.Hoverable = false;

            _startValue = 0;
            _amountValues = 400;
            _yvalsRange = yvals.GetRange(_startValue, _startValue + _amountValues);

            // series setting
            ScatterSeries scatterSeries1 = new ScatterSeries("Series1");
            cartesianChart2.Series.Add(scatterSeries1);
            scatterSeries1.Values = _yvalsRange.AsChartValues();
            scatterSeries1.MaxPointShapeDiameter = 10;
            scatterSeries1.Title = "Series1";

            cartesianChart2.AxisX.Add(new Axis
            {
                Name = "xAxis",
                Title = "DateTime",
                FontSize = 22,
                Foreground = System.Windows.Media.Brushes.Black,
                MinValue = 0,
                MaxValue = _amountValues,
            });
            cartesianChart2.AxisY.Add(new Axis
            {
                Name = "yAxis",
                Title = "Time difference",
                FontSize = 22,
                Foreground = System.Windows.Media.Brushes.Black,
                MinValue = -0.025,
                MaxValue = 0.04,
            });
        }

        private void CartesianChart1OnDataClick(object sender, ChartPoint chartPoint)
        {
            label1.Text = $@"You clicked: {chartPoint.X},{chartPoint.Y}";
        }

        private void button2_Click_1(object sender, EventArgs e)
        {

        }

        private void hScrollBar1_ValueChange(object sender, EventArgs e, int value)
        {
            RedrawGraph(value);
        }

        private void RedrawGraph(int value)
        {
            _startValue = value * 10;
            _yvalsRange = yvals.GetRange(_startValue, _startValue + _amountValues);
            cartesianChart2.Series[0].Values = _yvalsRange.AsChartValues();
            cartesianChart2.AxisX[0].MinValue = _startValue;
            cartesianChart2.AxisX[0].MaxValue = _startValue + _amountValues;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Forms;
使用System.Windows.Media;
使用动态图表;
使用LiveCharts.Helpers;
使用LiveCharts.Wpf;
使用LiveCharts.Wpf.Charts.Base;
使用画笔=System.Windows.Media.Brush;
使用Color=System.Windows.Media.Color;
使用MessageBox=System.Windows.Forms.MessageBox;
命名空间活动分析
{
公共部分类Form1:Form
{
List XVAL=新列表();
List yvals=新列表();
私有整数值;
私人国际标准价值;
私有列表yvalsRange;
公共表格1()
{
初始化组件();
//事件
cartesianChart2.DataClick+=CartesianChart1OnDataClick;
hScrollBar1.ValueChanged+=(发送方,e)=>hScrollBar1\u值更改(发送方,e,hScrollBar1.Value);
//获取原始数据
使用(var db=new Analysis.DataClasses1DataContext())
{
var lttResults=db.lttResults;
var par1=0.0;
foreach(lttResults中的var数据)
{
if(data.GatewayEventType==41)
par1=data.FloatValue;
如果(data.GatewayEventType!=42)继续;
var par2=data.FloatValue;
var diff=数学圆(第1-2、3段);
yvals.Add(diff);
xvals.Add(data.DateTime);
}
}
//图表设置
图表设置();
}
私有空图表设置()
{
//演出
cartesianChart2.DisableAnimations=true;
cartesianChart2.DataTooltip=null;
cartesianChart2.Hoverable=false;
_起始值=0;
_数量值=400;
_yvalsRange=yvals.GetRange(_startValue,_startValue+_amountValue);
//系列设置
散射系列散射系列1=新的散射系列(“系列1”);
cartesianChart2.系列。添加(分散系列1);
scatterSeries1.Values=_yvalsRange.AsChartValues();
散射系列1.MaxPointShapeDiameter=10;
scatterSeries1.Title=“Series1”;
cartesianChart2.axix.Add(新轴
{
Name=“xAxis”,
Title=“DateTime”,
FontSize=22,
前台=System.Windows.Media.brusks.Black,
最小值=0,
MaxValue=\u amountValue,
});
cartesianChart2.AxisY.Add(新轴
{
Name=“yAxis”,
Title=“时差”,
FontSize=22,
前台=System.Windows.Media.brusks.Black,
最小值=-0.025,
最大值=0.04,
});
}
私有无效CartesianChart1OnDataClick(对象发送者,ChartPoint ChartPoint)
{
label1.Text=$@“您单击:{chartPoint.X},{chartPoint.Y}”;
}
私有无效按钮2\u单击\u 1(对象发送者,事件参数e)
{
}
私有void hScrollBar1_值更改(对象发送方、事件参数e、int值)
{
重绘图形(值);
}
私有void重绘图(int值)
{
_起始值=值*10;
_yvalsRange=yvals.GetRange(_startValue,_startValue+_amountValue);
cartesianChart2.Series[0]。值=_yvalsRange.AsChartValues();
cartesianChart2.AxisX[0].MinValue=\u startValue;
cartesianChart2.AxisX[0]。MaxValue=\u startValue+\u AmountValue;
}
}
}
答案是:

Func<double, string> formatFunc = (x) => string.Format("{0:0.000}", x);

cartesianChart2.AxisY.Add(new Axis
{
    LabelFormatter = formatFunc,
});
Func formatFunc=(x)=>string.Format(“{0:0.000}”,x);
cartesianChart2.AxisY.Add(新轴
{
LabelFormatter=formatFunc,
});

Nice,我正要链接livecharts的文档,很高兴您找到了它!