c# - 使用实时图表创建 WPF C# 图表

标签 c# wpf livecharts

我使用实时图表。 SeriesCollection 传递一个 Line Series,其中 Values = Chart Values。在 Binding 的 xaml 中,我传递 SeriesCollection。据我了解,仅传输 y 点(默认情况下我不更改任何内容)。如何传输x点?通过索引在 ChartValue 中尝试,发誓索引为空。即使您最初设置了图表值的大小,图表上未填充的单元格也会填充为 Y = 0。请帮忙,已经坐了三天了。

GenerateChart.cs:

public ChartValues<double> Points { get; set; }
        double[] arraySeries = new double[30];
        double[] array = new double[20];
        public LineSeries GenerateSeries(string axis)
        {
            Random randomSeries = new Random();
            ChartValues<double> series = new ChartValues<double>(new double[20]);

            if (axis == "Y")
            {
                for (int i = 0; i < 5; i++)
                {
                    double randomValue = randomSeries.Next(1, 20);
                    if (!array.Contains(randomValue))
                    {
                        array[i] = randomValue;
                    }
                    else
                    {
                        i--;
                    }
                }

                for (int i = 0; i < 5; i++)
                {
                    double randomValue = randomSeries.Next(1, 20);
                    if (!arraySeries.Contains(randomValue))
                    {
                        int index = Convert.ToInt32(array[i]);
                        arraySeries[index] = randomValue;
                    }
                    else
                    {
                        i--;
                    }
                }

                for (int i = 0; i < 20; i++)
                {
                    if (arraySeries[i] != 0)
                    {
                        series.Insert(i, arraySeries[i]);
                    }
                }
                //series.AddRange(arraySeries);

            }
            Points = series;
            var testSeries = new LineSeries
            {
                Title = "Test",
                Values = series
            };

            return testSeries;
        }

RandomSeries.cs:

public SeriesCollection Series { get; private set; }

        public SeriesCollection SeriesX { get; private set; }

        public ChartValues<double> Points { get; private set; }
        double[] arraySeries = new double[30];
        double[] array = new double[20];
        public SeriesCollection BuidChart()
        {
            Random randomSeries = new Random();
            var generateChart = new GenerateChart();

            Series = new SeriesCollection 
            {
                generateChart.GenerateSeries("Y")
            };
            Points = generateChart.Points;
            return Series;
        } 

ModelView.cs:

public SeriesCollection SeriesCollection { get; set; }
        public ChartValues<double> Points { get; set; }
        public RandomSeries randomSeries;
        public Func<double, string> YFormatter { get; set; }
        public string[] Labels { get; set; }
        public SeriesCollection SeriesCollectionX { get; set; }
        public void BuildFunction()
        {
            //Points.Clear();
            //SeriesCollection.Clear();
            randomSeries = new RandomSeries();
            SeriesCollection = new SeriesCollection();
            Points = new ChartValues<double>();
            SeriesCollection.AddRange(randomSeries.BuidChart());
            //Points.AddRange(randomSeries.Points);
            //SeriesCollection.AddRange(randomSeries.BuidChart());
            //Points.AddRange(randomSeries.Points);
            Labels = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jan1", "Feb1", "Mar1", "Apr1", "May1" };
        }

MainWindow.xaml:

<Window.DataContext>
        <local:ModelView/>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="257*"/>
            <ColumnDefinition Width="536*"/>
        </Grid.ColumnDefinitions>
        <lvc:CartesianChart Series="{Binding SeriesCollection}" LegendLocation="Right" Grid.ColumnSpan="2" Margin="0,0,-0.4,0" >
            <!--<lvc:CartesianChart.AxisY>
                <lvc:Axis Title="Y" LabelFormatter="{Binding YFormatter}"></lvc:Axis>
            </lvc:CartesianChart.AxisY>-->
            <lvc:CartesianChart.AxisX>
                <lvc:Axis Title="X" Labels="{Binding Labels}"></lvc:Axis>
            </lvc:CartesianChart.AxisX>
        </lvc:CartesianChart>
    </Grid>

Result program: enter image description here

最佳答案

在 LiveChart 中,可以使用 ObservablePoint 来表示图表中的 X、Y 位置。

您需要添加适当的命名空间才能使用它们

using LiveCharts.Defaults;

我在你的函数中修改了两件事,首先我将 ChartValues 的定义和初始化更改为空和 ObservablePoint 的 typeof,所以稍后我们将动态填充这些。您还需要将 Points 对象更改为 ObservablePoint 类型

ChartValues<ObservablePoint> series = new ChartValues<ObservablePoint>();

并且还修改了这些系列对象的填充以仅添加非零:

for (int i = 0; i < 20; i++)
{
    if (arraySeries[i] != 0)
    {
        series.Add(new ObservablePoint(i, arraySeries[i]));
    }
 }

它不会在非零值之间绘制零 y 点。

关于c# - 使用实时图表创建 WPF C# 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54587638/

相关文章:

c# - 使用多个分组依据字段时按日期分组的 Linq

.net - 如何在 LiveCharts 中添加每点工具提示?如何让同一条线上的点有不同的颜色?

c# - 绑定(bind)MVVM模式中自定义元素的自定义事件

c# - WPF Datepicker 中的日历周

WPF:按钮未占据列表框中的完整空间

wpf - 在资源中创建一个控件,然后在XAML WPF中重用它

c# - 将 LiveCharts 饼图 PieSeries 值绑定(bind)到后面代码中的属性

c# - 在不加载的情况下按条件更新数据库表中的字段

c# - 使用 linq 更改数组中的一个字段

c# - 对应用于 MongoDB 中数组元素中每个项目的函数进行排序