折线图在绑定列表<键值对>时只有一个轴

本文关键字:有一个 键值对 绑定 列表 折线图 | 更新日期: 2023-09-27 18:30:28

好吧,我有下一个问题 - 在页面上我有WPF工具包图表,折线图。

<chartingToolkit:Chart Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0" Name="lineChart" Visibility="Hidden" VerticalAlignment="Top" Height="150" BorderThickness="3" BorderBrush="#FF423852">
                        <chartingToolkit:Chart.Axes>
                            <chartingToolkit:LinearAxis Orientation="Y" Minimum="0">
                                <chartingToolkit:LinearAxis.MajorTickMarkStyle>
                                    <Style TargetType="Line">
                                        <Setter Property="Stroke" Value="#bdb3ce" />
                                        <Setter Property="StrokeThickness"  Value="1" />
                                        <Setter Property="Y1"   Value="-4" />
                                        <Setter Property="Y2"  Value="4" />
                                    </Style>
                                </chartingToolkit:LinearAxis.MajorTickMarkStyle>
                            </chartingToolkit:LinearAxis>
                        </chartingToolkit:Chart.Axes>
                        <chartingToolkit:Chart.Style>
                            <Style TargetType="Control">
                                <Setter Property="Foreground" Value="#bdb3ce" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="chartingToolkit:Chart">
                                            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Name="BorderParent">
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="Auto" />
                                                        <RowDefinition Height="*" />
                                                    </Grid.RowDefinitions>
                                                    <!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
                                                    <Grid Grid.Row="1" Margin="5 15">
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="*" />
                                                            <ColumnDefinition Width="Auto" />
                                                        </Grid.ColumnDefinitions>
                                                        <primitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                                            <Grid x:Name="PlotArea" Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}"/>
                                                            <Border Canvas.ZIndex="10" BorderBrush="#61596f" BorderThickness="1 0 0 1" Margin="5 2"/>
                                                            <Canvas  Background="Transparent" x:Name="activePointGridLines" Margin="5 0 0 0" />
                                                        </primitives:EdgePanel>
                                                    </Grid>
                                                </Grid>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </chartingToolkit:Chart.Style>
                        <chartingToolkit:Chart.LegendStyle>
                            <Style TargetType="Control">
                                <Setter Property="Width" Value="0" />
                            </Style>
                        </chartingToolkit:Chart.LegendStyle>
                        <chartingToolkit:Chart.PlotAreaStyle>
                            <Style TargetType="Grid">
                                <Setter Property="Background" Value="Transparent" />
                                <Setter Property="SnapsToDevicePixels" Value="True" />
                            </Style>
                        </chartingToolkit:Chart.PlotAreaStyle>
                        <chartingToolkit:LineSeries IndependentValuePath="Key" DependentValuePath="Value" ItemsSource="{Binding}" DataContext="{Binding}" Margin="5.5,0,0,3" DataPointStyle="{StaticResource SeriesDataPointStyle}" Name="nLineSeries">
                            <chartingToolkit:LineSeries.IndependentAxis>
                                <chartingToolkit:CategoryAxis Orientation="X" MajorTickMarkStyle="{StaticResource ChartMajorTickMarkStyle}"/>
                            </chartingToolkit:LineSeries.IndependentAxis>
                        </chartingToolkit:LineSeries>
                    </chartingToolkit:Chart>

当我转到此页面时,即使数据绑定到图表DataContext,lineChart也可以为空。我注意到,当图表为空时,它只有一个轴(Y 轴)。当图表不为空时,绑定后图表有 2 个轴。

private void InitLineChart()
    {
        var valueList = new List<KeyValuePair<string, int>>();
        //fill List with data
        _lineChart.DataContext = valueList;
    }

什么会导致这种行为以及如何解决它?

折线图在绑定列表<键值对>时只有一个轴

问题

已解决。

我已经删除了IndependentAxis

<chartingToolkit:LineSeries IndependentValuePath="Key" DependentValuePath="Value" ItemsSource="{Binding}" DataContext="{Binding}" Margin="5.5,0,0,3" DataPointStyle="{StaticResource SeriesDataPointStyle}" Name="nLineSeries">
       <chartingToolkit:LineSeries.IndependentAxis>
              <chartingToolkit:CategoryAxis Orientation="X" MajorTickMarkStyle="{StaticResource ChartMajorTickMarkStyle}"/>
       </chartingToolkit:LineSeries.IndependentAxis>
</chartingToolkit:LineSeries>

<chartingToolkit:LineSeries IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}" Margin="5.5,0,0,3" DataPointStyle="{StaticResource SeriesDataPointStyle}" />

现在,将数据绑定到图表DataContext,而是绑定到存在的LineSeries ItemsSource

_lineChart.DataContext = valueList;

var lineChartSerie = (LineSeries)_lineChart.Series.FirstOrDefault();
if (lineChartSerie != null) lineChartSerie.ItemsSource = valueList;