首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >LiveCharts的基础知识--我该如何画一条线?

LiveCharts的基础知识--我该如何画一条线?
EN

Stack Overflow用户
提问于 2017-03-03 22:50:22
回答 1查看 3K关注 0票数 8

我很难理解LiveCharts到底应该发生什么。我这里有一块XAML:

代码语言:javascript
运行
复制
<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="/CPF;component/Images/background-top-cropped2.png" Stretch="None"></ImageBrush>
    </Grid.Background>
    <lvc:CartesianChart Series="{Binding myData}" LegendLocation="Right" x:Name="myChart">
        <lvc:CartesianChart.AxisY>
            <lvc:Axis Title="Sales" LabelFormatter="{Binding YFormatter}"></lvc:Axis>
        </lvc:CartesianChart.AxisY>
        <lvc:CartesianChart.AxisX>
            <lvc:Axis Title="Month" Labels="{Binding Labels}"></lvc:Axis>
        </lvc:CartesianChart.AxisX>
    </lvc:CartesianChart>
</Grid>

下面的代码是:

代码语言:javascript
运行
复制
public MainWindow()
{
    InitializeComponent();
    DrawGraphs();
}
public void DrawGraphs()
{
    LineSeries mySeries = new LineSeries
    {
        Values = new ChartValues<int> { 12, 23, 55, 1 }
    };

    myChart.Series.Add(mySeries);
}

在运行时,“myChart.Series.Add(MySeries)”抛出一个空引用异常错误。我不知道怎么解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-04 01:00:17

因为您直接将系列添加到图表中,而不使用MVVM,所以不需要XAML中的所有绑定。您可以简单地这样做:

XAML:

代码语言:javascript
运行
复制
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <lvc:CartesianChart x:Name="myChart" />
</Grid>

CS:

代码语言:javascript
运行
复制
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DrawGraphs();
    }
    public void DrawGraphs()
    {
        LineSeries mySeries = new LineSeries
        {
            Values = new ChartValues<int> { 12, 23, 55, 1 }
        };

        myChart.Series.Add(mySeries);
    }
}

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42589775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档