怎么更改坐标轴标题access_更改LiveCharts中坐标轴标签的格式

本文介绍如何在 LiveCharts 中修改散点图的 y 轴标签格式,将显示的数值从两位小数改为三位,通过自定义 LabelFormatter 实现精确控制。代码示例展示了如何设置格式函数以确保始终显示三位小数。
摘要由CSDN通过智能技术生成

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 xvals = new List();

List yvals = new List();

private int _amountValues;

private int _startValue;

private List _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;

}

}

}

解决方案

The answer is:

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

cartesianChart2.AxisY.Add(new Axis

{

LabelFormatter = formatFunc,

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值