我找不到改变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;
}
}
}
答案 0 :(得分:4)
答案是:
Func<double, string> formatFunc = (x) => string.Format("{0:0.000}", x);
cartesianChart2.AxisY.Add(new Axis
{
LabelFormatter = formatFunc,
});