问题
I just can't find a solution for changing the format of the y-axis tick labels.
Now I get labels like 0.03
and 0.035
. But I always need three digits behind the decimal point.
The big question is, how to access the label format?
This is my code:
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;
}
}
}
回答1:
The answer is:
Func<double, string> formatFunc = (x) => string.Format("{0:0.000}", x);
cartesianChart2.AxisY.Add(new Axis
{
LabelFormatter = formatFunc,
});
来源:https://stackoverflow.com/questions/42976646/change-the-format-of-the-axis-tick-labels-in-livecharts